Skip to content

Commit 2585875

Browse files
committed
test(coordination): bound receive loop waits
1 parent b09ed6c commit 2585875

1 file changed

Lines changed: 42 additions & 5 deletions

File tree

internal/coordination/session_test.go

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ import (
1414
)
1515

1616
func TestReceiveLoopUpdatesLastGoodResponseTimeBeforeSessionStarted(t *testing.T) {
17+
const testTimeout = 5 * time.Second
18+
1719
ctrl := gomock.NewController(t)
1820
client := NewMockCoordinationService_SessionClient(ctrl)
21+
testCtx, cancelTest := context.WithTimeout(context.Background(), testTimeout)
22+
defer cancelTest()
1923
streamCtx, cancelStream := context.WithCancel(context.Background())
24+
defer cancelStream()
2025
startedResponse := &Ydb_Coordination.SessionResponse_SessionStarted{
2126
SessionId: 42,
2227
}
@@ -46,22 +51,54 @@ func TestReceiveLoopUpdatesLastGoodResponseTimeBeforeSessionStarted(t *testing.T
4651
wg := sync.WaitGroup{}
4752
wg.Add(1)
4853
s.mutex.Lock()
49-
go s.receiveLoop(&wg, client, cancelStream, sessionStarted, sessionStopped)
54+
receiveLoopDone := make(chan struct{})
55+
go func() {
56+
defer close(receiveLoopDone)
57+
s.receiveLoop(&wg, client, cancelStream, sessionStarted, sessionStopped)
58+
}()
5059

51-
<-recvReturned
60+
select {
61+
case <-recvReturned:
62+
case <-receiveLoopDone:
63+
s.mutex.Unlock()
64+
t.Fatal("receive loop exited before the first Recv returned")
65+
case <-testCtx.Done():
66+
s.mutex.Unlock()
67+
t.Fatal("timed out waiting for the first Recv")
68+
}
5269
var start *Ydb_Coordination.SessionResponse_SessionStarted
70+
orderCheckTimer := time.NewTimer(100 * time.Millisecond) //nolint:mnd
5371
select {
5472
case start = <-sessionStarted:
55-
case <-time.After(100 * time.Millisecond): //nolint:mnd
73+
case <-orderCheckTimer.C:
74+
case <-receiveLoopDone:
75+
orderCheckTimer.Stop()
76+
s.mutex.Unlock()
77+
t.Fatal("receive loop exited before publishing session started")
78+
case <-testCtx.Done():
79+
orderCheckTimer.Stop()
80+
s.mutex.Unlock()
81+
t.Fatal("timed out checking session started publication order")
5682
}
83+
orderCheckTimer.Stop()
5784
publishedBeforeTimestamp := start != nil
5885
s.mutex.Unlock()
5986
if start == nil {
60-
start = <-sessionStarted
87+
select {
88+
case start = <-sessionStarted:
89+
case <-receiveLoopDone:
90+
t.Fatal("receive loop exited before publishing session started")
91+
case <-testCtx.Done():
92+
t.Fatal("timed out waiting for session started")
93+
}
6194
}
6295

6396
cancelStream()
64-
wg.Wait()
97+
select {
98+
case <-receiveLoopDone:
99+
case <-testCtx.Done():
100+
t.Fatal("timed out waiting for receive loop to stop")
101+
}
65102
require.False(t, publishedBeforeTimestamp, "session started was published before the keep-alive timestamp update")
66103
require.Same(t, startedResponse, start)
67104
require.False(t, s.getLastGoodResponseTime().IsZero())

0 commit comments

Comments
 (0)