Change TestRateLimitedDelivery to avoid flakes due to randomness

This commit is contained in:
Dmitry 2018-10-10 14:15:14 +03:00
parent 51c7237cc4
commit 80b800906e

View File

@ -58,28 +58,25 @@ func TestRatePeerDropsConnection(t *testing.T) {
} }
func TestRateLimitedDelivery(t *testing.T) { func TestRateLimitedDelivery(t *testing.T) {
cfg := &ratelimiter.Config{Interval: uint64(time.Hour), Capacity: 10 << 10, Quantum: 1 << 10} cfg := &ratelimiter.Config{Interval: uint64(time.Hour), Capacity: 3 << 10, Quantum: 1 << 10}
type testCase struct { type testCase struct {
description string description string
cfg *ratelimiter.Config cfg *ratelimiter.Config
received []uint64 received int
notReceived []uint64
} }
for _, tc := range []testCase{ for _, tc := range []testCase{
{ {
description: "NoEgress", description: "NoEgress",
received: []uint64{1, 2, 3}, received: 3,
}, },
{ {
description: "EgressSmallerThanIngress", description: "EgressSmallerThanIngress",
received: []uint64{1}, received: 1,
notReceived: []uint64{2, 3},
cfg: &ratelimiter.Config{Interval: uint64(time.Hour), Capacity: 2 << 10, Quantum: 1 << 10}, cfg: &ratelimiter.Config{Interval: uint64(time.Hour), Capacity: 2 << 10, Quantum: 1 << 10},
}, },
{ {
description: "EgressSameAsIngress", description: "EgressSameAsIngress",
received: []uint64{1, 2}, received: 2,
notReceived: []uint64{3},
cfg: cfg, cfg: cfg,
}, },
} { } {
@ -95,16 +92,14 @@ func TestRateLimitedDelivery(t *testing.T) {
} }
small2 := small1 small2 := small1
small2.Nonce = 2 small2.Nonce = 2
small2.Data = make([]byte, 3<<10) small3 := small1
big := small1 small3.Nonce = 3
big.Nonce = 3
big.Data = make([]byte, 11<<10)
w, rw1, _ := setupOneConnection(t, cfg, tc.cfg) w, rw1, _ := setupOneConnection(t, cfg, tc.cfg)
require.NoError(t, w.Send(&small1)) require.NoError(t, w.Send(&small1))
require.NoError(t, w.Send(&big))
require.NoError(t, w.Send(&small2)) require.NoError(t, w.Send(&small2))
require.NoError(t, w.Send(&small3))
received := map[uint64]struct{}{} received := map[uint64]struct{}{}
// we can not guarantee that all expected envelopes will be delivered in a one batch // we can not guarantee that all expected envelopes will be delivered in a one batch
@ -116,12 +111,7 @@ func TestRateLimitedDelivery(t *testing.T) {
for { for {
msg, err := rw1.ReadMsg() msg, err := rw1.ReadMsg()
if err == p2p.ErrPipeClosed { if err == p2p.ErrPipeClosed {
for _, n := range tc.received { require.Len(t, received, tc.received)
require.Contains(t, received, n)
}
for _, n := range tc.notReceived {
require.NotContains(t, received, n)
}
break break
} }
require.NoError(t, err) require.NoError(t, err)