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

Function TestFutureTimeout

ch04/future_test.go:118–145  ·  view source on GitHub ↗

TestFutureTimeout makes sure that the future will time out with an error if its context is canceled with a timeout

(t *testing.T)

Source from the content-addressed store, hash-verified

116// TestFutureTimeout makes sure that the future will time out with an error
117// if its context is canceled with a timeout
118func TestFutureTimeout(t *testing.T) {
119 start := time.Now()
120
121 // Get a context decorated with a 1-second timeout
122 ctx, cancel := context.WithTimeout(context.Background(), time.Second*1)
123
124 // The cancel function returned by context.WithTimeout should be called,
125 // not discarded, to avoid a context leak
126 defer cancel()
127
128 future := SlowFunction(ctx)
129
130 // We should time out with a "context deadline exceeded" error
131 res, err := future.Result()
132 if err != nil {
133 if !strings.Contains(err.Error(), "deadline") {
134 t.Error("received unexpected error maybe: ", err)
135 }
136 }
137
138 // Result should be empty
139 if res != "" {
140 t.Error("should have an empty result")
141 }
142
143 // Timeout should be after 1 second
144 elapsedCheck(t, start, 1)
145}
146
147// TestFutureCancel
148func TestFutureCancel(t *testing.T) {

Callers

nothing calls this directly

Calls 3

SlowFunctionFunction · 0.85
elapsedCheckFunction · 0.85
ResultMethod · 0.65

Tested by

no test coverage detected