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

Function TestFutureConcurrent

ch04/future_test.go:86–114  ·  view source on GitHub ↗

TestFutureConcurrent tests that the Future is thread-safe.

(t *testing.T)

Source from the content-addressed store, hash-verified

84
85// TestFutureConcurrent tests that the Future is thread-safe.
86func TestFutureConcurrent(t *testing.T) {
87 start := time.Now()
88
89 ctx := context.Background()
90 future := SlowFunction(ctx)
91
92 var wg sync.WaitGroup
93
94 for i := 0; i < 1000; i++ {
95 wg.Add(1)
96
97 go func() {
98 defer wg.Done()
99 res, err := future.Result()
100 if err != nil {
101 t.Error(err)
102 return
103 }
104
105 if !strings.HasPrefix(res, "I slept for") {
106 t.Error("unexpected output:", res)
107 }
108
109 elapsedCheck(t, start, 2)
110 }()
111 }
112
113 wg.Wait()
114}
115
116// TestFutureTimeout makes sure that the future will time out with an error
117// if its context is canceled with a timeout

Callers

nothing calls this directly

Calls 4

SlowFunctionFunction · 0.85
elapsedCheckFunction · 0.85
ResultMethod · 0.65
WaitMethod · 0.65

Tested by

no test coverage detected