From 01d69fc960a15b4f21f7e5270e7f353a202cef85 Mon Sep 17 00:00:00 2001 From: Tomasz Slabon Date: Tue, 14 May 2024 19:56:01 +0200 Subject: [PATCH] Added unit tests for inactivity submission states --- pkg/protocol/inactivity/states_test.go | 70 ++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 pkg/protocol/inactivity/states_test.go diff --git a/pkg/protocol/inactivity/states_test.go b/pkg/protocol/inactivity/states_test.go new file mode 100644 index 0000000000..61d2d5c205 --- /dev/null +++ b/pkg/protocol/inactivity/states_test.go @@ -0,0 +1,70 @@ +package inactivity + +import ( + "reflect" + "testing" + + "github.com/keep-network/keep-core/pkg/net" + "github.com/keep-network/keep-core/pkg/protocol/state" +) + +func TestReceivedMessages(t *testing.T) { + state := state.NewBaseAsyncState() + + message1 := &claimSignatureMessage{senderID: 1} + message2 := &claimSignatureMessage{senderID: 2} + message3 := &claimSignatureMessage{senderID: 1} + message4 := &claimSignatureMessage{senderID: 3} + message5 := &claimSignatureMessage{senderID: 3} + + state.ReceiveToHistory(newMockNetMessage(message1)) + state.ReceiveToHistory(newMockNetMessage(message2)) + state.ReceiveToHistory(newMockNetMessage(message3)) + state.ReceiveToHistory(newMockNetMessage(message4)) + state.ReceiveToHistory(newMockNetMessage(message5)) + + expectedMessages := []*claimSignatureMessage{message1, message2, message4} + actualType1Messages := receivedMessages[*claimSignatureMessage](state) + if !reflect.DeepEqual(expectedMessages, actualType1Messages) { + t.Errorf( + "unexpected messages\n"+ + "expected: [%v]\n"+ + "actual: [%v]", + expectedMessages, + actualType1Messages, + ) + } +} + +type mockNetMessage struct { + payload interface{} +} + +func newMockNetMessage(payload interface{}) *mockNetMessage { + return &mockNetMessage{payload} +} + +func (mnm *mockNetMessage) TransportSenderID() net.TransportIdentifier { + panic("not implemented") +} + +func (mnm *mockNetMessage) SenderPublicKey() []byte { + panic("not implemented") +} + +func (mnm *mockNetMessage) Payload() interface{} { + return mnm.payload +} + +func (mnm *mockNetMessage) Type() string { + payload, ok := mnm.payload.(message) + if !ok { + panic("wrong payload type") + } + + return payload.Type() +} + +func (mnm *mockNetMessage) Seqno() uint64 { + panic("not implemented") +}