(t *testing.T, nc *nats.Conn)
| 138 | } |
| 139 | |
| 140 | func testContextRequestWithTimeoutCanceled(t *testing.T, nc *nats.Conn) { |
| 141 | ctx, cancelCB := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 142 | defer cancelCB() |
| 143 | |
| 144 | nc.Subscribe("fast", func(m *nats.Msg) { |
| 145 | nc.Publish(m.Reply, []byte("OK")) |
| 146 | }) |
| 147 | |
| 148 | // Fast request should not fail |
| 149 | resp, err := nc.RequestWithContext(ctx, "fast", []byte("")) |
| 150 | if err != nil { |
| 151 | t.Fatalf("Expected request with context to not fail on fast response: %s", err) |
| 152 | } |
| 153 | got := string(resp.Data) |
| 154 | expected := "OK" |
| 155 | if got != expected { |
| 156 | t.Errorf("Expected to receive %s, got: %s", expected, got) |
| 157 | } |
| 158 | |
| 159 | // Cancel the context already so that rest of requests fail. |
| 160 | cancelCB() |
| 161 | |
| 162 | // Context is already canceled so requests should immediately fail. |
| 163 | _, err = nc.RequestWithContext(ctx, "fast", []byte("world")) |
| 164 | if err == nil { |
| 165 | t.Fatal("Expected request with timeout context to fail") |
| 166 | } |
| 167 | |
| 168 | // Reported error is "context canceled" from Context package, |
| 169 | // which is not a timeout error. |
| 170 | type timeoutError interface { |
| 171 | Timeout() bool |
| 172 | } |
| 173 | if _, ok := err.(timeoutError); ok { |
| 174 | t.Errorf("Expected to not have a timeout error") |
| 175 | } |
| 176 | expected = `context canceled` |
| 177 | if !strings.Contains(err.Error(), expected) { |
| 178 | t.Errorf("Expected %q error, got: %q", expected, err.Error()) |
| 179 | } |
| 180 | |
| 181 | // 2nd request should fail again even if fast because context has already been canceled |
| 182 | _, err = nc.RequestWithContext(ctx, "fast", []byte("world")) |
| 183 | if err == nil { |
| 184 | t.Fatal("Expected request with context to fail") |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | func TestContextRequestWithTimeoutCanceled(t *testing.T) { |
| 189 | s := RunDefaultServer() |
no test coverage detected