-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ales Verbic <[email protected]>
- Loading branch information
Showing
2 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |