This test checks that requests made through CallContext can be canceled by canceling the context.
(transport string, t *testing.T)
| 106 | // This test checks that requests made through CallContext can be canceled by canceling |
| 107 | // the context. |
| 108 | func testClientCancel(transport string, t *testing.T) { |
| 109 | server := newTestServer("service", new(Service)) |
| 110 | defer server.Stop() |
| 111 | |
| 112 | // What we want to achieve is that the context gets canceled |
| 113 | // at various stages of request processing. The interesting cases |
| 114 | // are: |
| 115 | // - cancel during dial |
| 116 | // - cancel while performing a HTTP request |
| 117 | // - cancel while waiting for a response |
| 118 | // |
| 119 | // To trigger those, the times are chosen such that connections |
| 120 | // are killed within the deadline for every other call (maxKillTimeout |
| 121 | // is 2x maxCancelTimeout). |
| 122 | // |
| 123 | // Once a connection is dead, there is a fair chance it won't connect |
| 124 | // successfully because the accept is delayed by 1s. |
| 125 | maxContextCancelTimeout := 300 * time.Millisecond |
| 126 | fl := &flakeyListener{ |
| 127 | maxAcceptDelay: 1 * time.Second, |
| 128 | maxKillTimeout: 600 * time.Millisecond, |
| 129 | } |
| 130 | |
| 131 | var client *Client |
| 132 | switch transport { |
| 133 | case "ws", "http": |
| 134 | c, hs := httpTestClient(server, transport, fl) |
| 135 | defer hs.Close() |
| 136 | client = c |
| 137 | case "ipc": |
| 138 | c, l := ipcTestClient(server, fl) |
| 139 | defer l.Close() |
| 140 | client = c |
| 141 | default: |
| 142 | panic("unknown transport: " + transport) |
| 143 | } |
| 144 | |
| 145 | // These tests take a lot of time, run them all at once. |
| 146 | // You probably want to run with -parallel 1 or comment out |
| 147 | // the call to t.Parallel if you enable the logging. |
| 148 | t.Parallel() |
| 149 | |
| 150 | // The actual test starts here. |
| 151 | var ( |
| 152 | wg sync.WaitGroup |
| 153 | nreqs = 10 |
| 154 | ncallers = 6 |
| 155 | ) |
| 156 | caller := func(index int) { |
| 157 | defer wg.Done() |
| 158 | for i := 0; i < nreqs; i++ { |
| 159 | var ( |
| 160 | ctx context.Context |
| 161 | cancel func() |
| 162 | timeout = time.Duration(rand.Int63n(int64(maxContextCancelTimeout))) |
| 163 | ) |
| 164 | if index < ncallers/2 { |
| 165 | // For half of the callers, create a context without deadline |
no test coverage detected