TestFutureTimeout makes sure that the future will time out with an error if its context is canceled with a timeout
(t *testing.T)
| 116 | // TestFutureTimeout makes sure that the future will time out with an error |
| 117 | // if its context is canceled with a timeout |
| 118 | func 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 |
| 148 | func TestFutureCancel(t *testing.T) { |
nothing calls this directly
no test coverage detected