Skip to content

fix: Deadlock and race in peer, test improvements #3086

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 21, 2022
Prev Previous commit
Next Next commit
fix: Improve teardown and timeout of peer tests
  • Loading branch information
mafredri committed Jul 21, 2022
commit 0474aa934e1fae2604c37c37f998927dfa356f81
53 changes: 42 additions & 11 deletions peer/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ func TestConn(t *testing.T) {
// Create a channel that closes on disconnect.
channel, err := server.CreateChannel(context.Background(), "wow", nil)
assert.NoError(t, err)
defer channel.Close()

err = wan.Stop()
require.NoError(t, err)
// Once the connection is marked as disconnected, this
Expand All @@ -107,10 +109,13 @@ func TestConn(t *testing.T) {
t.Parallel()
client, server, _ := createPair(t)
exchange(t, client, server)
cch, err := client.CreateChannel(context.Background(), "hello", &peer.ChannelOptions{})
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
cch, err := client.CreateChannel(ctx, "hello", &peer.ChannelOptions{})
require.NoError(t, err)
defer cch.Close()

sch, err := server.Accept(context.Background())
sch, err := server.Accept(ctx)
require.NoError(t, err)
defer sch.Close()

Expand All @@ -123,9 +128,12 @@ func TestConn(t *testing.T) {
t.Parallel()
client, server, wan := createPair(t)
exchange(t, client, server)
cch, err := client.CreateChannel(context.Background(), "hello", &peer.ChannelOptions{})
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
cch, err := client.CreateChannel(ctx, "hello", &peer.ChannelOptions{})
require.NoError(t, err)
sch, err := server.Accept(context.Background())
defer cch.Close()
sch, err := server.Accept(ctx)
require.NoError(t, err)
defer sch.Close()

Expand Down Expand Up @@ -170,13 +178,29 @@ func TestConn(t *testing.T) {
srv, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
defer srv.Close()
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
go func() {
sch, err := server.Accept(context.Background())
assert.NoError(t, err)
sch, err := server.Accept(ctx)
if err != nil {
assert.NoError(t, err)
return
}
defer sch.Close()

nc2 := sch.NetConn()
defer nc2.Close()

nc1, err := net.Dial("tcp", srv.Addr().String())
assert.NoError(t, err)
if err != nil {
assert.NoError(t, err)
return
}
defer nc1.Close()

go func() {
defer nc1.Close()
defer nc2.Close()
_, _ = io.Copy(nc1, nc2)
}()
_, _ = io.Copy(nc2, nc1)
Expand Down Expand Up @@ -204,7 +228,7 @@ func TestConn(t *testing.T) {
c := http.Client{
Transport: defaultTransport,
}
req, err := http.NewRequestWithContext(context.Background(), "GET", "http://localhost/", nil)
req, err := http.NewRequestWithContext(ctx, "GET", "http://localhost/", nil)
require.NoError(t, err)
resp, err := c.Do(req)
require.NoError(t, err)
Expand Down Expand Up @@ -272,14 +296,21 @@ func TestConn(t *testing.T) {
t.Parallel()
client, server, _ := createPair(t)
exchange(t, client, server)
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
go func() {
channel, err := client.CreateChannel(context.Background(), "test", nil)
assert.NoError(t, err)
channel, err := client.CreateChannel(ctx, "test", nil)
if err != nil {
assert.NoError(t, err)
return
}
defer channel.Close()
_, err = channel.Write([]byte{1, 2})
assert.NoError(t, err)
}()
channel, err := server.Accept(context.Background())
channel, err := server.Accept(ctx)
require.NoError(t, err)
defer channel.Close()
data := make([]byte, 1)
_, err = channel.Read(data)
require.NoError(t, err)
Expand Down