Skip to content

Commit

Permalink
Merge pull request #1003 from apernet/fix-test
Browse files Browse the repository at this point in the history
fix: flaky tests caused by occasionally closing channel multiple times
  • Loading branch information
tobyxdd authored Mar 23, 2024
2 parents a037880 + 89a99a0 commit bbf4231
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
7 changes: 5 additions & 2 deletions core/internal/integration_tests/close_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package integration_tests

import (
"io"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -48,13 +49,14 @@ func TestClientServerTCPClose(t *testing.T) {
// Server outbound connection should write the same thing, then close.
sobConn := mocks.NewMockConn(t)
sobConnCh := make(chan struct{}) // For close signal only
sobConnChCloseFunc := sync.OnceFunc(func() { close(sobConnCh) })
sobConn.EXPECT().Read(mock.Anything).RunAndReturn(func(bs []byte) (int, error) {
<-sobConnCh
return 0, io.EOF
})
sobConn.EXPECT().Write([]byte("happy")).Return(5, nil)
sobConn.EXPECT().Close().RunAndReturn(func() error {
close(sobConnCh)
sobConnChCloseFunc()
return nil
})
serverOb.EXPECT().TCP(addr).Return(sobConn, nil).Once()
Expand Down Expand Up @@ -133,6 +135,7 @@ func TestClientServerUDPIdleTimeout(t *testing.T) {
// to trigger the server's UDP idle timeout.
sobConn := mocks.NewMockUDPConn(t)
sobConnCh := make(chan []byte, 1)
sobConnChCloseFunc := sync.OnceFunc(func() { close(sobConnCh) })
sobConn.EXPECT().ReadFrom(mock.Anything).RunAndReturn(func(bs []byte) (int, string, error) {
d := <-sobConnCh
if d == nil {
Expand Down Expand Up @@ -167,7 +170,7 @@ func TestClientServerUDPIdleTimeout(t *testing.T) {
}
// Now we wait for 3 seconds, the server should close the UDP session.
sobConn.EXPECT().Close().RunAndReturn(func() error {
close(sobConnCh)
sobConnChCloseFunc()
return nil
})
eventLogger.EXPECT().UDPError(mock.Anything, mock.Anything, uint32(1), nil).Once()
Expand Down
11 changes: 7 additions & 4 deletions core/internal/integration_tests/trafficlogger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package integration_tests

import (
"io"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -46,6 +47,7 @@ func TestClientServerTrafficLoggerTCP(t *testing.T) {

sobConn := mocks.NewMockConn(t)
sobConnCh := make(chan []byte, 1)
sobConnChCloseFunc := sync.OnceFunc(func() { close(sobConnCh) })
sobConn.EXPECT().Read(mock.Anything).RunAndReturn(func(bs []byte) (int, error) {
b := <-sobConnCh
if b == nil {
Expand All @@ -55,9 +57,9 @@ func TestClientServerTrafficLoggerTCP(t *testing.T) {
}
})
sobConn.EXPECT().Close().RunAndReturn(func() error {
close(sobConnCh)
sobConnChCloseFunc()
return nil
}).Once()
})
serverOb.EXPECT().TCP(addr).Return(sobConn, nil).Once()

conn, err := c.TCP(addr)
Expand Down Expand Up @@ -125,6 +127,7 @@ func TestClientServerTrafficLoggerUDP(t *testing.T) {

sobConn := mocks.NewMockUDPConn(t)
sobConnCh := make(chan []byte, 1)
sobConnChCloseFunc := sync.OnceFunc(func() { close(sobConnCh) })
sobConn.EXPECT().ReadFrom(mock.Anything).RunAndReturn(func(bs []byte) (int, string, error) {
b := <-sobConnCh
if b == nil {
Expand All @@ -134,9 +137,9 @@ func TestClientServerTrafficLoggerUDP(t *testing.T) {
}
})
sobConn.EXPECT().Close().RunAndReturn(func() error {
close(sobConnCh)
sobConnChCloseFunc()
return nil
}).Once()
})
serverOb.EXPECT().UDP(addr).Return(sobConn, nil).Once()

conn, err := c.UDP()
Expand Down

0 comments on commit bbf4231

Please sign in to comment.