Skip to content

Commit

Permalink
feat: add keepalive test
Browse files Browse the repository at this point in the history
Signed-off-by: Ales Verbic <[email protected]>
  • Loading branch information
verbotenj committed Mar 25, 2024
1 parent e0f46d7 commit 2e2981e
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 1 deletion.
2 changes: 1 addition & 1 deletion connection_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"time"

ouroboros "github.com/blinklabs-io/gouroboros"
"github.com/blinklabs-io/ouroboros-mock"
"github.com/blinklabs-io/gouroboros/protocol/keepalive"
ouroboros_mock "github.com/blinklabs-io/ouroboros-mock"
"go.uber.org/goleak"
)

Expand Down
149 changes: 149 additions & 0 deletions protocol/keepalive/serverclient_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package keepalive_test

import (
"fmt"
"testing"
"time"

ouroboros "github.com/blinklabs-io/gouroboros"
"github.com/blinklabs-io/gouroboros/protocol"
"github.com/blinklabs-io/gouroboros/protocol/keepalive"
ouroboros_mock "github.com/blinklabs-io/ouroboros-mock"
)

const (
// MockKeepAliveWrongCookie is the wrong cookie for a keep-alive response
MockKeepAliveWrongCookie uint16 = 1000
)

// ConversationEntryKeepAliveResponse is a pre-defined conversation entry for a keep-alive response
var ConversationEntryKeepAliveResponseWrongResponse = ouroboros_mock.ConversationEntryOutput{
ProtocolId: keepalive.ProtocolId,
IsResponse: true,
Messages: []protocol.Message{
keepalive.NewMsgKeepAliveResponse(MockKeepAliveWrongCookie),
},
}

// ConversationKeepAlive is a pre-defined conversation with a NtN handshake and repeated keep-alive requests
// and responses
var ConversationKeepAliveWrongResponse = []ouroboros_mock.ConversationEntry{
ouroboros_mock.ConversationEntryHandshakeRequestGeneric,
ouroboros_mock.ConversationEntryHandshakeNtNResponse,
ouroboros_mock.ConversationEntryKeepAliveRequest,
ConversationEntryKeepAliveResponseWrongResponse,
}

func TestServerKeepaliveHandling(t *testing.T) {
mockConn := ouroboros_mock.NewConnection(
ouroboros_mock.ProtocolRoleClient,
ouroboros_mock.ConversationKeepAlive,
).(*ouroboros_mock.Connection)

// Async mock connection error handler
asyncErrChan := make(chan error, 1)
go func() {
err := <-mockConn.ErrorChan()
if err != nil {
asyncErrChan <- fmt.Errorf("received unexpected error\n got: %v\n wanted: no error", err)
}
close(asyncErrChan)
}()

oConn, err := ouroboros.New(
ouroboros.WithConnection(mockConn),
ouroboros.WithNetworkMagic(ouroboros_mock.MockNetworkMagic),
ouroboros.WithNodeToNode(true),
ouroboros.WithKeepAlive(true),
ouroboros.WithKeepAliveConfig(keepalive.NewConfig(
keepalive.WithKeepAliveFunc(func(protocolID uint16) error {
t.Log("keep alive beat")
return nil
}),
keepalive.WithPeriod(time.Millisecond*100),
// Set correct cookie
keepalive.WithCookie(ouroboros_mock.MockKeepAliveCookie),
keepalive.WithDoneFunc(func() error {
t.Log("keep alive done")
return nil
}),
)),
)
if err != nil {
t.Fatalf("unexpected error when creating Connection object: %s", err)
}

// Wait for mock connection shutdown
select {
case err, ok := <-asyncErrChan:
if ok {
t.Fatal(err.Error())
}
case <-time.After(2 * time.Second):
t.Fatalf("did not complete within timeout")
}

// Close connection
if err := oConn.Close(); err != nil {
t.Fatalf("unexpected error when closing Connection object: %s", err)
}
}

func TestServerKeepaliveHandlingWithWrongResponse(t *testing.T) {
mockConn := ouroboros_mock.NewConnection(
ouroboros_mock.ProtocolRoleClient,
ConversationKeepAliveWrongResponse,
).(*ouroboros_mock.Connection)

// Expected cookie is 0x3e7 instead of 0x3e8
expectedErr := "input error: parsed message does not match expected value: got &keepalive.MsgKeepAlive{MessageBase:protocol.MessageBase{_:struct {}{}, rawCbor:[]uint8(nil), MessageType:0x0}, Cookie:0x3e8}, expected &keepalive.MsgKeepAlive{MessageBase:protocol.MessageBase{_:struct {}{}, rawCbor:[]uint8(nil), MessageType:0x0}, Cookie:0x3e7}"
// Async mock connection error handler
asyncErrChan := make(chan error, 1)
go func() {
err := <-mockConn.ErrorChan()
if err == nil {
asyncErrChan <- fmt.Errorf("did not receive expected error")
} else {
if err.Error() != expectedErr {
asyncErrChan <- fmt.Errorf("did not receive expected error\n got: %s\n wanted: %s", err, expectedErr)
}
}
close(asyncErrChan)
}()

oConn, err := ouroboros.New(
ouroboros.WithConnection(mockConn),
ouroboros.WithNetworkMagic(ouroboros_mock.MockNetworkMagic),
ouroboros.WithNodeToNode(true),
ouroboros.WithKeepAlive(true),
ouroboros.WithKeepAliveConfig(keepalive.NewConfig(
keepalive.WithKeepAliveFunc(func(protocolID uint16) error {
t.Log("keep alive beat")
return nil
}),
keepalive.WithPeriod(time.Millisecond*10),
keepalive.WithCookie(MockKeepAliveWrongCookie),
keepalive.WithDoneFunc(func() error {
t.Log("keep alive done")
return nil
}),
)),
)
if err != nil {
t.Fatalf("unexpected error when creating Connection object: %s", err)
}

// Wait for mock connection shutdown
select {
case err, ok := <-asyncErrChan:
if ok {
t.Fatal(err.Error())
}
case <-time.After(2 * time.Second):
t.Fatalf("did not complete within timeout")
}
// Close connection
if err := oConn.Close(); err != nil {
t.Fatalf("unexpected error when closing Connection object: %s", err)
}
}

0 comments on commit 2e2981e

Please sign in to comment.