Skip to content

Commit

Permalink
fix linter
Browse files Browse the repository at this point in the history
  • Loading branch information
algorandskiy committed Nov 20, 2024
1 parent e696946 commit 3991111
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions util/rateLimit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func TestNewElasticRateLimiter(t *testing.T) {
func TestElasticRateLimiterCongestionControlled(t *testing.T) {
partitiontest.PartitionTest(t)
client := mockClient("client")
clientAddr := string(client.RoutingAddr())
erl := NewElasticRateLimiter(3, 2, time.Second, nil)
// give the ERL a congestion controler with well defined behavior for testing
erl.cm = mockCongestionControl{}
Expand All @@ -62,24 +61,24 @@ func TestElasticRateLimiterCongestionControlled(t *testing.T) {
// because the ERL gives capacity to a reservation, and then asynchronously drains capacity from the share,
// wait a moment before testing the size of the sharedCapacity
time.Sleep(100 * time.Millisecond)
assert.Equal(t, 1, len(erl.capacityByClient[clientAddr]))
assert.Equal(t, 1, len(erl.capacityByClient[string(client.RoutingAddr())]))
assert.Equal(t, 1, len(erl.sharedCapacity))
assert.NoError(t, err)

erl.EnableCongestionControl()
_, _, err = erl.ConsumeCapacity(client)
assert.Equal(t, 0, len(erl.capacityByClient[clientAddr]))
assert.Equal(t, 0, len(erl.capacityByClient[string(client.RoutingAddr())]))
assert.Equal(t, 1, len(erl.sharedCapacity))
assert.NoError(t, err)

_, _, err = erl.ConsumeCapacity(client)
assert.Equal(t, 0, len(erl.capacityByClient[clientAddr]))
assert.Equal(t, 0, len(erl.capacityByClient[string(client.RoutingAddr())]))
assert.Equal(t, 1, len(erl.sharedCapacity))
assert.Error(t, err)

erl.DisableCongestionControl()
_, _, err = erl.ConsumeCapacity(client)
assert.Equal(t, 0, len(erl.capacityByClient[clientAddr]))
assert.Equal(t, 0, len(erl.capacityByClient[string(client.RoutingAddr())]))
assert.Equal(t, 0, len(erl.sharedCapacity))
assert.NoError(t, err)
}
Expand Down Expand Up @@ -137,47 +136,46 @@ func TestZeroSizeReservations(t *testing.T) {
func TestConsumeReleaseCapacity(t *testing.T) {
partitiontest.PartitionTest(t)
client := mockClient("client")
clientAddr := string(client.RoutingAddr())
erl := NewElasticRateLimiter(4, 3, time.Second, nil)

c1, _, err := erl.ConsumeCapacity(client)
// because the ERL gives capacity to a reservation, and then asynchronously drains capacity from the share,
// wait a moment before testing the size of the sharedCapacity
time.Sleep(100 * time.Millisecond)
assert.Equal(t, 2, len(erl.capacityByClient[clientAddr]))
assert.Equal(t, 2, len(erl.capacityByClient[string(client.RoutingAddr())]))
assert.Equal(t, 1, len(erl.sharedCapacity))
assert.NoError(t, err)

_, _, err = erl.ConsumeCapacity(client)
assert.Equal(t, 1, len(erl.capacityByClient[clientAddr]))
assert.Equal(t, 1, len(erl.capacityByClient[string(client.RoutingAddr())]))
assert.Equal(t, 1, len(erl.sharedCapacity))
assert.NoError(t, err)

_, _, err = erl.ConsumeCapacity(client)
assert.Equal(t, 0, len(erl.capacityByClient[clientAddr]))
assert.Equal(t, 0, len(erl.capacityByClient[string(client.RoutingAddr())]))
assert.Equal(t, 1, len(erl.sharedCapacity))
assert.NoError(t, err)

// remember this capacity, as it is a shared capacity
c4, _, err := erl.ConsumeCapacity(client)
assert.Equal(t, 0, len(erl.capacityByClient[clientAddr]))
assert.Equal(t, 0, len(erl.capacityByClient[string(client.RoutingAddr())]))
assert.Equal(t, 0, len(erl.sharedCapacity))
assert.NoError(t, err)

_, _, err = erl.ConsumeCapacity(client)
assert.Equal(t, 0, len(erl.capacityByClient[clientAddr]))
assert.Equal(t, 0, len(erl.capacityByClient[string(client.RoutingAddr())]))
assert.Equal(t, 0, len(erl.sharedCapacity))
assert.Error(t, err)

// now release the capacity and observe the items return to the correct places
err = c1.Release()
assert.Equal(t, 1, len(erl.capacityByClient[clientAddr]))
assert.Equal(t, 1, len(erl.capacityByClient[string(client.RoutingAddr())]))
assert.Equal(t, 0, len(erl.sharedCapacity))
assert.NoError(t, err)

// now release the capacity and observe the items return to the correct places
err = c4.Release()
assert.Equal(t, 1, len(erl.capacityByClient[clientAddr]))
assert.Equal(t, 1, len(erl.capacityByClient[string(client.RoutingAddr())]))
assert.Equal(t, 1, len(erl.sharedCapacity))
assert.NoError(t, err)

Expand Down

0 comments on commit 3991111

Please sign in to comment.