StartWithAssert starts the given invocation and calls assertCallback with the resulting error when the invocation completes. If assertCallback is nil, expected shutdown errors are silently tolerated.
(t *testing.T, inv *serpent.Invocation, assertCallback func(t *testing.T, err error))
| 177 | // with the resulting error when the invocation completes. If assertCallback |
| 178 | // is nil, expected shutdown errors are silently tolerated. |
| 179 | func StartWithAssert(t *testing.T, inv *serpent.Invocation, assertCallback func(t *testing.T, err error)) { |
| 180 | t.Helper() |
| 181 | |
| 182 | closeCh := make(chan struct{}) |
| 183 | // StartWithWaiter adds its own `t.Cleanup`, so we need to be sure it's added |
| 184 | // before ours. |
| 185 | waiter := StartWithWaiter(t, inv) |
| 186 | t.Cleanup(func() { |
| 187 | waiter.Cancel() |
| 188 | <-closeCh |
| 189 | }) |
| 190 | |
| 191 | go func() { |
| 192 | defer close(closeCh) |
| 193 | err := waiter.Wait() |
| 194 | |
| 195 | if assertCallback != nil { |
| 196 | assertCallback(t, err) |
| 197 | return |
| 198 | } |
| 199 | |
| 200 | switch { |
| 201 | case errors.Is(err, context.Canceled): |
| 202 | return |
| 203 | case err != nil && strings.Contains(err.Error(), "driver: bad connection"): |
| 204 | // When we cancel the context on a query that's being executed within |
| 205 | // a transaction, sometimes, instead of a context.Canceled error we get |
| 206 | // a "driver: bad connection" error. |
| 207 | // https://github.com/lib/pq/issues/1137 |
| 208 | return |
| 209 | default: |
| 210 | assert.NoError(t, err) |
| 211 | } |
| 212 | }() |
| 213 | } |
| 214 | |
| 215 | // Run runs the command and asserts that there is no error. |
| 216 | func Run(t *testing.T, inv *serpent.Invocation) { |