TestFutureConcurrent tests that the Future is thread-safe.
(t *testing.T)
| 84 | |
| 85 | // TestFutureConcurrent tests that the Future is thread-safe. |
| 86 | func 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 |
nothing calls this directly
no test coverage detected