Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
irees committed Nov 7, 2023
1 parent 83ca6d1 commit 538fa80
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion internal/meters/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (m *DefaultMeterProvider) getValue(u MeterUser, meterName string, startTime
// fmt.Println("not matched on start time", userEvent.time, startTime)
match = false
}
if !dimsContainedIn(userEvent.dims, checkDims) {
if !dimsContainedIn(checkDims, userEvent.dims) {
// fmt.Println("not matched on dims")
match = false
}
Expand Down
13 changes: 7 additions & 6 deletions internal/meters/limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ type LimitMeter struct {
}

func (c *LimitMeter) GetLimits(meterName string, checkDims Dimensions) []UserMeterLimit {
// The limit matches the event dimensions if all of the LIMIT dimensions are contained in event
var lims []UserMeterLimit
for _, checkLim := range parseGkUserLimits(c.userData) {
if checkLim.MeterName == meterName && dimsContainedIn(checkLim.Dims, checkDims) {
lims = append(lims, checkLim)
for _, userLimit := range parseGkUserLimits(c.userData) {
if userLimit.MeterName == meterName && dimsContainedIn(userLimit.Dims, checkDims) {
lims = append(lims, userLimit)
}
}
for _, checkLim := range c.provider.DefaultLimits {
if checkLim.MeterName == meterName && dimsContainedIn(checkLim.Dims, checkDims) {
lims = append(lims, checkLim)
for _, defaultLimit := range c.provider.DefaultLimits {
if defaultLimit.MeterName == meterName && dimsContainedIn(defaultLimit.Dims, checkDims) {
lims = append(lims, defaultLimit)
}
}
return lims
Expand Down
12 changes: 6 additions & 6 deletions internal/meters/meters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ func testMeter(t *testing.T, mp MeterProvider, cfg testMeterConfig) {
})

t.Run("GetValue match dims", func(t *testing.T) {
addDims1 := []Dimension{{Key: "test", Value: "ok1"}}
addDims2 := []Dimension{{Key: "test", Value: "not ok1"}}
checkDims1 := addDims1
checkDims2 := addDims2
addDims1 := []Dimension{{Key: "test", Value: "a"}, {Key: "other", Value: "boo"}}
addDims2 := []Dimension{{Key: "test", Value: "b"}}
checkDims1 := []Dimension{{Key: "test", Value: "a"}}
checkDims2 := []Dimension{{Key: "test", Value: "b"}}

m1 := mp.NewMeter(cfg.user1)
m2 := mp.NewMeter(cfg.user2)
Expand All @@ -103,8 +103,9 @@ func testMeter(t *testing.T, mp MeterProvider, cfg testMeterConfig) {
v2, _ := m2.GetValue(cfg.testMeter1, d1, d2, checkDims2)
v3, _ := m3.GetValue(cfg.testMeter1, d1, d2, checkDims1)

// m2 uses different dimension
// m1 meter
m1.Meter(cfg.testMeter1, 1, addDims1)
// m2 uses different dimension
m2.Meter(cfg.testMeter1, 2.0, addDims2)
mp.Flush()

Expand All @@ -123,5 +124,4 @@ func testMeter(t *testing.T, mp MeterProvider, cfg testMeterConfig) {
a, _ = m3.GetValue(cfg.testMeter1, d1, d2, checkDims1)
assert.Equal(t, 0.0, a-v3)
})

}

0 comments on commit 538fa80

Please sign in to comment.