Skip to content

Commit 0e79038

Browse files
authored
Merge pull request #272 from blinklabs-io/test/ouroboros-double-close
test: Ouroboros double close test
2 parents 3713aa0 + fb074e9 commit 0e79038

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

ouroboros_test.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
package ouroboros_test
1616

1717
import (
18-
ouroboros "github.com/blinklabs-io/gouroboros"
18+
"fmt"
1919
"testing"
20+
21+
ouroboros "github.com/blinklabs-io/gouroboros"
22+
"github.com/blinklabs-io/gouroboros/internal/test/ouroboros_mock"
2023
)
2124

2225
// Ensure that we don't panic when closing the Ouroboros object after a failed Dial() call
@@ -32,3 +35,36 @@ func TestDialFailClose(t *testing.T) {
3235
// Close Ouroboros connection
3336
oConn.Close()
3437
}
38+
39+
func TestDoubleClose(t *testing.T) {
40+
mockConn := ouroboros_mock.NewConnection(
41+
[]ouroboros_mock.ConversationEntry{
42+
ouroboros_mock.ConversationEntryHandshakeRequestGeneric,
43+
ouroboros_mock.ConversationEntryHandshakeResponse,
44+
},
45+
)
46+
oConn, err := ouroboros.New(
47+
ouroboros.WithConnection(mockConn),
48+
ouroboros.WithNetworkMagic(ouroboros_mock.MockNetworkMagic),
49+
)
50+
if err != nil {
51+
t.Fatalf("unexpected error when creating Ouroboros object: %s", err)
52+
}
53+
// Async error handler
54+
go func() {
55+
err, ok := <-oConn.ErrorChan()
56+
if !ok {
57+
return
58+
}
59+
// We can't call t.Fatalf() from a different Goroutine, so we panic instead
60+
panic(fmt.Sprintf("unexpected Ouroboros connection error: %s", err))
61+
}()
62+
// Close Ouroboros connection
63+
if err := oConn.Close(); err != nil {
64+
t.Fatalf("unexpected error when closing Ouroboros object: %s", err)
65+
}
66+
// Close Ouroboros connection again
67+
if err := oConn.Close(); err != nil {
68+
t.Fatalf("unexpected error when closing Ouroboros object again: %s", err)
69+
}
70+
}

0 commit comments

Comments
 (0)