(t *testing.T)
| 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 | } |
nothing calls this directly
no test coverage detected