|
| 1 | +package flow_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | + |
| 8 | + "github.com/cloudflare/cloudflared/flow" |
| 9 | +) |
| 10 | + |
| 11 | +func TestFlowLimiter_Unlimited(t *testing.T) { |
| 12 | + unlimitedLimiter := flow.NewLimiter(0) |
| 13 | + |
| 14 | + for i := 0; i < 1000; i++ { |
| 15 | + err := unlimitedLimiter.Acquire("test") |
| 16 | + require.NoError(t, err) |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +func TestFlowLimiter_Limited(t *testing.T) { |
| 21 | + maxFlows := uint64(5) |
| 22 | + limiter := flow.NewLimiter(maxFlows) |
| 23 | + |
| 24 | + for i := uint64(0); i < maxFlows; i++ { |
| 25 | + err := limiter.Acquire("test") |
| 26 | + require.NoError(t, err) |
| 27 | + } |
| 28 | + |
| 29 | + err := limiter.Acquire("should fail") |
| 30 | + require.ErrorIs(t, err, flow.ErrTooManyActiveFlows) |
| 31 | +} |
| 32 | + |
| 33 | +func TestFlowLimiter_AcquireAndReleaseFlow(t *testing.T) { |
| 34 | + maxFlows := uint64(5) |
| 35 | + limiter := flow.NewLimiter(maxFlows) |
| 36 | + |
| 37 | + // Acquire the maximum number of flows |
| 38 | + for i := uint64(0); i < maxFlows; i++ { |
| 39 | + err := limiter.Acquire("test") |
| 40 | + require.NoError(t, err) |
| 41 | + } |
| 42 | + |
| 43 | + // Validate acquire 1 more flows fails |
| 44 | + err := limiter.Acquire("should fail") |
| 45 | + require.ErrorIs(t, err, flow.ErrTooManyActiveFlows) |
| 46 | + |
| 47 | + // Release the maximum number of flows |
| 48 | + for i := uint64(0); i < maxFlows; i++ { |
| 49 | + limiter.Release() |
| 50 | + } |
| 51 | + |
| 52 | + // Validate acquire 1 more flows works |
| 53 | + err = limiter.Acquire("shouldn't fail") |
| 54 | + require.NoError(t, err) |
| 55 | + |
| 56 | + // Release a 10x the number of max flows |
| 57 | + for i := uint64(0); i < 10*maxFlows; i++ { |
| 58 | + limiter.Release() |
| 59 | + } |
| 60 | + |
| 61 | + // Validate it still can only acquire a value = number max flows. |
| 62 | + for i := uint64(0); i < maxFlows; i++ { |
| 63 | + err := limiter.Acquire("test") |
| 64 | + require.NoError(t, err) |
| 65 | + } |
| 66 | + err = limiter.Acquire("should fail") |
| 67 | + require.ErrorIs(t, err, flow.ErrTooManyActiveFlows) |
| 68 | +} |
| 69 | + |
| 70 | +func TestFlowLimiter_SetLimit(t *testing.T) { |
| 71 | + maxFlows := uint64(5) |
| 72 | + limiter := flow.NewLimiter(maxFlows) |
| 73 | + |
| 74 | + // Acquire the maximum number of flows |
| 75 | + for i := uint64(0); i < maxFlows; i++ { |
| 76 | + err := limiter.Acquire("test") |
| 77 | + require.NoError(t, err) |
| 78 | + } |
| 79 | + |
| 80 | + // Validate acquire 1 more flows fails |
| 81 | + err := limiter.Acquire("should fail") |
| 82 | + require.ErrorIs(t, err, flow.ErrTooManyActiveFlows) |
| 83 | + |
| 84 | + // Set the flow limiter to support one more request |
| 85 | + limiter.SetLimit(maxFlows + 1) |
| 86 | + |
| 87 | + // Validate acquire 1 more flows now works |
| 88 | + err = limiter.Acquire("shouldn't fail") |
| 89 | + require.NoError(t, err) |
| 90 | + |
| 91 | + // Validate acquire 1 more flows doesn't work because we already reached the limit |
| 92 | + err = limiter.Acquire("should fail") |
| 93 | + require.ErrorIs(t, err, flow.ErrTooManyActiveFlows) |
| 94 | + |
| 95 | + // Release all flows |
| 96 | + for i := uint64(0); i < maxFlows+1; i++ { |
| 97 | + limiter.Release() |
| 98 | + } |
| 99 | + |
| 100 | + // Validate 1 flow works again |
| 101 | + err = limiter.Acquire("shouldn't fail") |
| 102 | + require.NoError(t, err) |
| 103 | + |
| 104 | + // Set the flow limit to 1 |
| 105 | + limiter.SetLimit(1) |
| 106 | + |
| 107 | + // Validate acquire 1 more flows doesn't work |
| 108 | + err = limiter.Acquire("should fail") |
| 109 | + require.ErrorIs(t, err, flow.ErrTooManyActiveFlows) |
| 110 | + |
| 111 | + // Set the flow limit to unlimited |
| 112 | + limiter.SetLimit(0) |
| 113 | + |
| 114 | + // Validate it can acquire a lot of flows because it is now unlimited. |
| 115 | + for i := uint64(0); i < 10*maxFlows; i++ { |
| 116 | + err := limiter.Acquire("shouldn't fail") |
| 117 | + require.NoError(t, err) |
| 118 | + } |
| 119 | +} |
0 commit comments