Skip to content

Commit

Permalink
Fixed blinking test TestCloseParentContext. Wait group added to wait …
Browse files Browse the repository at this point in the history
…for client to finish sending handshake.

Better wait groups naming.
  • Loading branch information
alexeykiselev committed Dec 19, 2024
1 parent dc78383 commit 7b3fffb
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions pkg/networking/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ func TestCloseParentContext(t *testing.T) {
serverHandler := netmocks.NewMockHandler(t)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

clientConn, serverConn := testConnPipe()
net := networking.NewNetwork()
Expand All @@ -340,44 +339,51 @@ func TestCloseParentContext(t *testing.T) {
serverSession, err := net.NewSession(ctx, serverConn, testConfig(t, mockProtocol, serverHandler, "server"))
require.NoError(t, err)

var closeWG sync.WaitGroup
closeWG.Add(1)
clientWG := new(sync.WaitGroup)
clientWG.Add(1) // Wait for client to send Handshake to server.

var wg sync.WaitGroup
wg.Add(2)
serverWG := new(sync.WaitGroup)
serverWG.Add(1) // Wait for server to send Handshake to client, after that we will close the parent context.

testWG := new(sync.WaitGroup)
testWG.Add(2) // Wait for both client and server to finish.

serverHandler.On("OnClose", serverSession).Return()
sc1 := serverHandler.On("OnHandshake", serverSession, &textHandshake{v: "hello"}).Once().Return()
sc1.Run(func(_ mock.Arguments) {
clientWG.Wait() // Wait for client to send handshake, start replying with Handshake only after that.
n, wErr := serverSession.Write([]byte("hello"))
assert.NoError(t, wErr)
assert.Equal(t, 5, n)
go func() {
closeWG.Wait() // Wait for client to receive server handshake.
cancel() // Close parent context.
wg.Done()
serverWG.Wait() // Wait for client to receive server handshake.
cancel() // Close parent context.
testWG.Done()
}()
})

clientHandler.On("OnClose", clientSession).Return()

// Send handshake to server.
n, err := clientSession.Write([]byte("hello"))
require.NoError(t, err)
assert.Equal(t, 5, n)

cs1 := clientHandler.On("OnHandshake", clientSession, &textHandshake{v: "hello"}).Once().Return()
cs1.Run(func(_ mock.Arguments) {
// On receiving handshake from server, signal to close the server.
closeWG.Done()
// Try to send message to server, but it will fail because server is already closed.
time.Sleep(10 * time.Millisecond) // Wait for server to close.
_, msgErr := clientSession.Write(encodeMessage("Hello session"))
require.ErrorIs(t, msgErr, networking.ErrSessionShutdown)
wg.Done()
serverWG.Done()
go func() {
// Try to send message to server, but it will fail because server is already closed.
time.Sleep(10 * time.Millisecond) // Wait for server to close.
_, msgErr := clientSession.Write(encodeMessage("Hello session"))
require.ErrorIs(t, msgErr, networking.ErrSessionShutdown)
testWG.Done()
}()
})

wg.Wait() // Wait for client to finish.
// Send handshake to server.
n, err := clientSession.Write([]byte("hello"))
require.NoError(t, err)
assert.Equal(t, 5, n)
clientWG.Done() // Signal that handshake was sent to server.

testWG.Wait() // Wait for all interactions to finish.

err = clientSession.Close()
assert.NoError(t, err)
Expand Down

0 comments on commit 7b3fffb

Please sign in to comment.