Skip to content

Commit

Permalink
Better test workflow. Better wait group naming.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeykiselev committed Dec 19, 2024
1 parent 7b3fffb commit d2e2646
Showing 1 changed file with 34 additions and 25 deletions.
59 changes: 34 additions & 25 deletions pkg/networking/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,42 +170,51 @@ func TestSessionTimeoutOnMessage(t *testing.T) {

serverHandler.On("OnClose", serverSession).Return()

var serverWG sync.WaitGroup
var clientWG sync.WaitGroup
serverWG.Add(1)
clientWG.Add(1)
go func() {
sc1 := serverHandler.On("OnHandshake", serverSession, &textHandshake{v: "hello"}).Once().Return()
sc1.Run(func(_ mock.Arguments) {
n, wErr := serverSession.Write([]byte("hello"))
require.NoError(t, wErr)
assert.Equal(t, 5, n)
serverWG.Done()
})
serverWG.Wait() // Wait for finishing handshake before closing the pipe.
clientWG := new(sync.WaitGroup)
clientWG.Add(1) // Wait for client to send Handshake to server.

// Lock pipe after replying with the handshake from server.
pc.writeBlocker.Lock()
clientWG.Done() // Signal that pipe is locked.
}()
serverWG := new(sync.WaitGroup)
serverWG.Add(1) // Wait for server to reply with Handshake to client.

serverHandler.On("OnClose", serverSession).Return()
clientHandler.On("OnClose", clientSession).Return()
pipeWG := new(sync.WaitGroup)
pipeWG.Add(1) // Wait for pipe to be locked.

// Send handshake to server.
n, err := clientSession.Write([]byte("hello"))
require.NoError(t, err)
assert.Equal(t, 5, n)
testWG := new(sync.WaitGroup)
testWG.Add(1) // Wait for client fail by timeout.

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"))
require.NoError(t, wErr)
assert.Equal(t, 5, n)
serverWG.Done()
})

clientHandler.On("OnClose", clientSession).Return()
cs1 := clientHandler.On("OnHandshake", clientSession, &textHandshake{v: "hello"}).Once().Return()
cs1.Run(func(_ mock.Arguments) {
clientWG.Wait() // Wait for pipe to be locked.
pipeWG.Wait() // Wait for pipe to be locked.
// On receiving handshake from server, send the message back to server.
_, msgErr := clientSession.Write(encodeMessage("Hello session"))
require.ErrorIs(t, msgErr, networking.ErrConnectionWriteTimeout)
testWG.Done()
})

time.Sleep(1 * time.Second) // Let timeout occur.
go func() {
serverWG.Wait() // Wait for finishing handshake before closing the pipe.
pc.writeBlocker.Lock() // Lock pipe after replying with the handshake from server.
pipeWG.Done() // Signal that pipe is locked.
}()

// 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()

err = serverSession.Close()
assert.NoError(t, err) // Expect no error on the server side.
Expand Down

0 comments on commit d2e2646

Please sign in to comment.