Skip to content

Commit

Permalink
Fix blinking test.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeykiselev committed Dec 16, 2024
1 parent 52a893e commit 77633f3
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions pkg/networking/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"io"
"log/slog"
"runtime"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -98,6 +97,7 @@ func TestSessionTimeoutOnHandshake(t *testing.T) {
defer goleak.VerifyNone(t)

mockProtocol := netmocks.NewMockProtocol(t)
mockProtocol.On("EmptyHandshake").Return(&textHandshake{}, nil)

clientHandler := netmocks.NewMockHandler(t)
serverHandler := netmocks.NewMockHandler(t)
Expand All @@ -110,33 +110,36 @@ func TestSessionTimeoutOnHandshake(t *testing.T) {

clientSession, err := net.NewSession(ctx, clientConn, testConfig(t, mockProtocol, clientHandler, "client"))
require.NoError(t, err)
clientHandler.On("OnClose", clientSession).Return()

serverSession, err := net.NewSession(ctx, serverConn, testConfig(t, mockProtocol, serverHandler, "server"))
require.NoError(t, err)

mockProtocol.On("EmptyHandshake").Return(&textHandshake{}, nil)
serverHandler.On("OnClose", serverSession).Return()
clientHandler.On("OnClose", clientSession).Return()

// Lock
pc, ok := clientConn.(*pipeConn)
require.True(t, ok)
pc.writeBlocker.Lock()
runtime.Gosched()

// Send handshake to server, but writing will block because the clientConn is locked.
n, err := clientSession.Write([]byte("hello"))
require.ErrorIs(t, err, networking.ErrConnectionWriteTimeout)
assert.Equal(t, 0, n)

runtime.Gosched()

err = serverSession.Close()
assert.NoError(t, err)

wg := new(sync.WaitGroup)
wg.Add(1)
go func() {
err = clientSession.Close()
assert.ErrorIs(t, err, io.ErrClosedPipe)
wg.Done()
}()

// Unlock "timeout" and close client.
pc.writeBlocker.Unlock()
err = clientSession.Close()
assert.ErrorIs(t, err, io.ErrClosedPipe)
wg.Wait()
}

func TestSessionTimeoutOnMessage(t *testing.T) {
Expand Down

0 comments on commit 77633f3

Please sign in to comment.