Skip to content

Commit 49e76c8

Browse files
committed
Remove the need to Destroy
1 parent a68ec08 commit 49e76c8

2 files changed

Lines changed: 49 additions & 9 deletions

File tree

helpers/waiter.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ func (w *Waiter) Waitf(t ct.TestLike, timeout time.Duration, errFormat string, a
4747
}
4848
}
4949

50+
// Done returns a channel which is closed when Finish is called. It can be used
51+
// in `select` statements where failing the test on a timeout (as Wait does) is
52+
// not appropriate. Prefer `Wait`/`Waitf` otherwise.
53+
func (w *Waiter) Done() <-chan bool {
54+
return w.ch
55+
}
56+
5057
// Finish will cause all goroutines waiting via Wait to stop waiting and return.
5158
// Once this function has been called, subsequent calls to Wait will return immediately.
5259
// To begin waiting again, make a new Waiter.

tests/msc3902/federation_room_join_partial_state_test.go

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4374,13 +4374,7 @@ func beginPartialStateJoin(t *testing.T, server *server, serverRoom *federation.
43744374
// Destroy cleans up the resources associated with the join attempt.
43754375
// It is idempotent and must be called once the test is finished.
43764376
func (psj *partialStateJoinResult) Destroy(t *testing.T) {
4377-
if psj.fedStateIdsSendResponseWaiter != nil {
4378-
psj.fedStateIdsSendResponseWaiter.Finish()
4379-
}
4380-
4381-
if psj.fedStateIdsRequestReceivedWaiter != nil {
4382-
psj.fedStateIdsRequestReceivedWaiter.Finish()
4383-
}
4377+
// FIXME: This does nothing now. Remove.
43844378
}
43854379

43864380
// send a message into the room without letting the homeserver under test know about it.
@@ -4436,7 +4430,26 @@ func handleStateIdsRequests(
44364430
requestReceivedWaiter.Finish()
44374431
}
44384432
if sendResponseWaiter != nil {
4439-
sendResponseWaiter.Waitf(t, 60*time.Second, "Waiting for /state_ids request")
4433+
select {
4434+
case <-sendResponseWaiter.Done():
4435+
// Happy-path now that we're done waiting, continue serving the request now
4436+
case <-req.Context().Done():
4437+
// The request was cancelled (the Complement server is probably shutting down)
4438+
// which means nobody wants this response any more (just bail out without
4439+
// doing any more work).
4440+
//
4441+
// Also as a note: although the cancellation itself happens while the test is
4442+
// still running, `srv.Close()` cancels any if-flight requests but does not
4443+
// wait for this goroutine, so by the time we wake up here the test may have
4444+
// already completed and touching `t` after that panics.
4445+
return
4446+
case <-time.After(60 * time.Second):
4447+
// Sanity check so a wedged test fails loudly instead of blocking forever.
4448+
t.Fatalf(
4449+
"Timed out waiting for the test to finish the `sendResponseWaiter` while trying"+
4450+
"to serve /state_ids response for event %s", queryParams["event_id"],
4451+
)
4452+
}
44404453
}
44414454
t.Logf("Replying to /state_ids request for event %s", queryParams["event_id"])
44424455

@@ -4476,7 +4489,27 @@ func handleStateRequests(
44764489
requestReceivedWaiter.Finish()
44774490
}
44784491
if sendResponseWaiter != nil {
4479-
sendResponseWaiter.Waitf(t, 60*time.Second, "Waiting for /state request")
4492+
4493+
select {
4494+
case <-sendResponseWaiter.Done():
4495+
// Happy-path now that we're done waiting, continue serving the request now
4496+
case <-req.Context().Done():
4497+
// The request was cancelled (the Complement server is probably shutting down)
4498+
// which means nobody wants this response any more (just bail out without
4499+
// doing any more work).
4500+
//
4501+
// Also as a note: although the cancellation itself happens while the test is
4502+
// still running, `srv.Close()` cancels any if-flight requests but does not
4503+
// wait for this goroutine, so by the time we wake up here the test may have
4504+
// already completed and touching `t` after that panics.
4505+
return
4506+
case <-time.After(60 * time.Second):
4507+
// Sanity check so a wedged test fails loudly instead of blocking forever.
4508+
t.Fatalf(
4509+
"Timed out waiting for the test to finish the `sendResponseWaiter` while trying"+
4510+
"to serve /state response for event %s", queryParams["event_id"],
4511+
)
4512+
}
44804513
}
44814514

44824515
t.Logf("Replying to /state request for event %s", queryParams["event_id"])

0 commit comments

Comments
 (0)