TestThrottleContextTimeout tests whether a Throttle will return an error when its context is canceled.
(t *testing.T)
| 149 | // TestThrottleContextTimeout tests whether a Throttle will return an error |
| 150 | // when its context is canceled. |
| 151 | func TestThrottleContextTimeout(t *testing.T) { |
| 152 | callsCounter := 0 |
| 153 | effector := callsCountFunction(&callsCounter) |
| 154 | |
| 155 | ctx, cancel := context.WithTimeout(context.Background(), 250*time.Millisecond) |
| 156 | defer cancel() |
| 157 | |
| 158 | throttle := Throttle(effector, 1, 1, time.Second) |
| 159 | |
| 160 | s, e := throttle(ctx) |
| 161 | if e != nil { |
| 162 | t.Error("unexpected error:", e) |
| 163 | } else { |
| 164 | t.Log("output:", s) |
| 165 | } |
| 166 | |
| 167 | // Wait for timeout |
| 168 | time.Sleep(300 * time.Millisecond) |
| 169 | |
| 170 | _, e = throttle(ctx) |
| 171 | if e != nil { |
| 172 | t.Log("got expected error:", e) |
| 173 | } else { |
| 174 | t.Error("didn't get expected error") |
| 175 | } |
| 176 | } |
nothing calls this directly
no test coverage detected