Skip to content

Commit 37ba290

Browse files
authored
test: add goleak (#536)
Fixes #528
1 parent 3a50e03 commit 37ba290

File tree

7 files changed

+80
-6
lines changed

7 files changed

+80
-6
lines changed

connection_manager_test.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
ouroboros "github.com/blinklabs-io/gouroboros"
2323
"github.com/blinklabs-io/gouroboros/internal/test/ouroboros_mock"
2424
"github.com/blinklabs-io/gouroboros/protocol/keepalive"
25+
"go.uber.org/goleak"
2526
)
2627

2728
func TestConnectionManagerTagString(t *testing.T) {
@@ -46,19 +47,22 @@ func TestConnectionManagerTagString(t *testing.T) {
4647
}
4748

4849
func TestConnectionManagerConnError(t *testing.T) {
50+
defer goleak.VerifyNone(t)
4951
expectedConnId := 2
5052
expectedErr := io.EOF
5153
doneChan := make(chan any)
5254
connManager := ouroboros.NewConnectionManager(
5355
ouroboros.ConnectionManagerConfig{
5456
ConnClosedFunc: func(connId int, err error) {
55-
if connId != expectedConnId {
56-
t.Fatalf("did not receive error from expected connection: got %d, wanted %d", connId, expectedConnId)
57-
}
58-
if err != expectedErr {
59-
t.Fatalf("did not receive expected error: got: %s, expected: %s", err, expectedErr)
57+
if err != nil {
58+
if connId != expectedConnId {
59+
t.Fatalf("did not receive error from expected connection: got %d, wanted %d", connId, expectedConnId)
60+
}
61+
if err != expectedErr {
62+
t.Fatalf("did not receive expected error: got: %s, expected: %s", err, expectedErr)
63+
}
64+
close(doneChan)
6065
}
61-
close(doneChan)
6266
},
6367
},
6468
)
@@ -91,13 +95,22 @@ func TestConnectionManagerConnError(t *testing.T) {
9195
}
9296
select {
9397
case <-doneChan:
98+
// Shutdown other connections
99+
for _, tmpConn := range connManager.GetConnectionsByTags() {
100+
if tmpConn.Id != expectedConnId {
101+
tmpConn.Conn.Close()
102+
}
103+
}
104+
// TODO: actually wait for shutdown
105+
time.Sleep(5 * time.Second)
94106
return
95107
case <-time.After(10 * time.Second):
96108
t.Fatalf("did not receive error within timeout")
97109
}
98110
}
99111

100112
func TestConnectionManagerConnClosed(t *testing.T) {
113+
defer goleak.VerifyNone(t)
101114
expectedConnId := 42
102115
doneChan := make(chan any)
103116
connManager := ouroboros.NewConnectionManager(
@@ -138,6 +151,8 @@ func TestConnectionManagerConnClosed(t *testing.T) {
138151
)
139152
select {
140153
case <-doneChan:
154+
// TODO: actually wait for shutdown
155+
time.Sleep(5 * time.Second)
141156
return
142157
case <-time.After(10 * time.Second):
143158
t.Fatalf("did not receive error within timeout")

connection_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ package ouroboros_test
1717
import (
1818
"fmt"
1919
"testing"
20+
"time"
2021

2122
ouroboros "github.com/blinklabs-io/gouroboros"
2223
"github.com/blinklabs-io/gouroboros/internal/test/ouroboros_mock"
24+
"go.uber.org/goleak"
2325
)
2426

2527
// Ensure that we don't panic when closing the Connection object after a failed Dial() call
2628
func TestDialFailClose(t *testing.T) {
29+
defer goleak.VerifyNone(t)
2730
oConn, err := ouroboros.New()
2831
if err != nil {
2932
t.Fatalf("unexpected error when creating Connection object: %s", err)
@@ -37,6 +40,7 @@ func TestDialFailClose(t *testing.T) {
3740
}
3841

3942
func TestDoubleClose(t *testing.T) {
43+
defer goleak.VerifyNone(t)
4044
mockConn := ouroboros_mock.NewConnection(
4145
ouroboros_mock.ProtocolRoleClient,
4246
[]ouroboros_mock.ConversationEntry{
@@ -71,4 +75,10 @@ func TestDoubleClose(t *testing.T) {
7175
err,
7276
)
7377
}
78+
// Wait for connection shutdown
79+
select {
80+
case <-oConn.ErrorChan():
81+
case <-time.After(10 * time.Second):
82+
t.Errorf("did not shutdown within timeout")
83+
}
7484
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/fxamacker/cbor/v2 v2.6.0
77
github.com/jinzhu/copier v0.4.0
88
github.com/utxorpc/go-codegen v0.4.0
9+
go.uber.org/goleak v1.3.0
910
golang.org/x/crypto v0.20.0
1011
)
1112

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
12
github.com/fxamacker/cbor/v2 v2.6.0 h1:sU6J2usfADwWlYDAFhZBQ6TnLFBHxgesMrQfQgk1tWA=
23
github.com/fxamacker/cbor/v2 v2.6.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ=
34
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
45
github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8=
56
github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
7+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
8+
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
69
github.com/utxorpc/go-codegen v0.4.0 h1:9YzCURt5Jrnwii8VmQYeKH6K/wYXMQx+DmdjzieSZpM=
710
github.com/utxorpc/go-codegen v0.4.0/go.mod h1:rFk8LI55uFqnwftPYTTklTKB8Lhwh4K+ltb2BtrFe3g=
811
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
912
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
13+
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
14+
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
1015
golang.org/x/crypto v0.20.0 h1:jmAMJJZXr5KiCw05dfYK9QnqaqKLYXijU23lsEdcQqg=
1116
golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ=
1217
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
1318
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
1419
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
1520
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
1621
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
22+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

internal/test/ouroboros_mock/mock_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ package ouroboros_mock
1616

1717
import (
1818
"testing"
19+
"time"
1920

2021
ouroboros "github.com/blinklabs-io/gouroboros"
22+
"go.uber.org/goleak"
2123
)
2224

2325
// Basic test of conversation mock functionality
2426
func TestBasic(t *testing.T) {
27+
defer goleak.VerifyNone(t)
2528
mockConn := NewConnection(
2629
ProtocolRoleClient,
2730
[]ConversationEntry{
@@ -40,4 +43,10 @@ func TestBasic(t *testing.T) {
4043
if err := oConn.Close(); err != nil {
4144
t.Fatalf("unexpected error when closing Ouroboros object: %s", err)
4245
}
46+
// Wait for connection shutdown
47+
select {
48+
case <-oConn.ErrorChan():
49+
case <-time.After(10 * time.Second):
50+
t.Errorf("did not shutdown within timeout")
51+
}
4352
}

protocol/handshake/client_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ package handshake_test
1717
import (
1818
"fmt"
1919
"testing"
20+
"time"
2021

2122
ouroboros "github.com/blinklabs-io/gouroboros"
2223
"github.com/blinklabs-io/gouroboros/internal/test/ouroboros_mock"
24+
"go.uber.org/goleak"
2325
)
2426

2527
func TestClientBasicHandshake(t *testing.T) {
28+
defer goleak.VerifyNone(t)
2629
mockConn := ouroboros_mock.NewConnection(
2730
ouroboros_mock.ProtocolRoleClient,
2831
[]ouroboros_mock.ConversationEntry{
@@ -50,9 +53,16 @@ func TestClientBasicHandshake(t *testing.T) {
5053
if err := oConn.Close(); err != nil {
5154
t.Fatalf("unexpected error when closing Ouroboros object: %s", err)
5255
}
56+
// Wait for connection shutdown
57+
select {
58+
case <-oConn.ErrorChan():
59+
case <-time.After(10 * time.Second):
60+
t.Errorf("did not shutdown within timeout")
61+
}
5362
}
5463

5564
func TestClientDoubleStart(t *testing.T) {
65+
defer goleak.VerifyNone(t)
5666
mockConn := ouroboros_mock.NewConnection(
5767
ouroboros_mock.ProtocolRoleClient,
5868
[]ouroboros_mock.ConversationEntry{
@@ -82,4 +92,10 @@ func TestClientDoubleStart(t *testing.T) {
8292
if err := oConn.Close(); err != nil {
8393
t.Fatalf("unexpected error when closing Ouroboros object: %s", err)
8494
}
95+
// Wait for connection shutdown
96+
select {
97+
case <-oConn.ErrorChan():
98+
case <-time.After(10 * time.Second):
99+
t.Errorf("did not shutdown within timeout")
100+
}
85101
}

protocol/handshake/server_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@ package handshake_test
1717
import (
1818
"fmt"
1919
"testing"
20+
"time"
2021

2122
ouroboros "github.com/blinklabs-io/gouroboros"
2223
"github.com/blinklabs-io/gouroboros/internal/test/ouroboros_mock"
2324
"github.com/blinklabs-io/gouroboros/protocol"
2425
"github.com/blinklabs-io/gouroboros/protocol/handshake"
26+
"go.uber.org/goleak"
2527
)
2628

2729
func TestServerBasicHandshake(t *testing.T) {
30+
defer goleak.VerifyNone(t)
2831
mockConn := ouroboros_mock.NewConnection(
2932
ouroboros_mock.ProtocolRoleServer,
3033
[]ouroboros_mock.ConversationEntry{
@@ -76,9 +79,17 @@ func TestServerBasicHandshake(t *testing.T) {
7679
if err := oConn.Close(); err != nil {
7780
t.Fatalf("unexpected error when closing Ouroboros object: %s", err)
7881
}
82+
// Wait for connection shutdown
83+
select {
84+
case <-oConn.ErrorChan():
85+
case <-time.After(10 * time.Second):
86+
t.Errorf("did not shutdown within timeout")
87+
}
7988
}
8089

8190
func TestServerHandshakeRefuseVersionMismatch(t *testing.T) {
91+
// TODO: fix leaking goroutines
92+
//defer goleak.VerifyNone(t)
8293
expectedErr := fmt.Errorf("handshake failed: refused due to version mismatch")
8394
mockConn := ouroboros_mock.NewConnection(
8495
ouroboros_mock.ProtocolRoleServer,
@@ -131,6 +142,12 @@ func TestServerHandshakeRefuseVersionMismatch(t *testing.T) {
131142
}
132143
} else {
133144
oConn.Close()
145+
// Wait for connection shutdown
146+
select {
147+
case <-oConn.ErrorChan():
148+
case <-time.After(10 * time.Second):
149+
t.Errorf("did not shutdown within timeout")
150+
}
134151
t.Fatalf("did not receive expected error")
135152
}
136153
}

0 commit comments

Comments
 (0)