MCPcopy Create free account
hub / github.com/cloud-native-go/examples / TestThrottleContextTimeout

Function TestThrottleContextTimeout

ch04/throttle_test.go:151–176  ·  view source on GitHub ↗

TestThrottleContextTimeout tests whether a Throttle will return an error when its context is canceled.

(t *testing.T)

Source from the content-addressed store, hash-verified

149// TestThrottleContextTimeout tests whether a Throttle will return an error
150// when its context is canceled.
151func 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}

Callers

nothing calls this directly

Calls 2

callsCountFunctionFunction · 0.85
ThrottleFunction · 0.70

Tested by

no test coverage detected