diff options
author | Santo Cariotti <santo@dcariotti.me> | 2025-04-08 14:37:33 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2025-04-08 14:39:13 +0200 |
commit | 1f0d9ec8452f15c27cd33c4e3874454c35993743 (patch) | |
tree | c453a31ae5eb823aaf48868eea9fc4daf65f108b /network/network_test.go | |
parent | c5b10e28b358308d8349b940af09f64368172f2e (diff) |
Use internal/pkg structure
Diffstat (limited to 'network/network_test.go')
-rw-r--r-- | network/network_test.go | 52 |
1 files changed, 0 insertions, 52 deletions
diff --git a/network/network_test.go b/network/network_test.go deleted file mode 100644 index 9dbc416..0000000 --- a/network/network_test.go +++ /dev/null @@ -1,52 +0,0 @@ -package network - -import ( - "testing" - "time" - - "github.com/stretchr/testify/assert" -) - -// TestPeerToPeerCommunication tests if two peers can communicate. -func TestPeerToPeerCommunication(t *testing.T) { - // Create a mock of the first peer (peer-1) - peer1IP := "127.0.0.1" - peer1Port := 9001 - peer1 := NewTCPNetwork("peer-1", peer1IP, peer1Port) - - // Create a mock of the second peer (peer-2) - peer2IP := "127.0.0.1" - peer2Port := 9002 - peer2 := NewTCPNetwork("peer-2", peer2IP, peer2Port) - - // Register a message handler on peer-2 to receive the message from peer-1 - peer2.RegisterHandler("chat", func(msg Message) { - assert.Equal(t, "peer-1", msg.Source.ID) - assert.Equal(t, "Hey from peer-1!", string(msg.Payload)) - }) - - // Start the first peer and add the second peer - go peer1.AddPeer("peer-2", peer2IP, peer2Port) - go peer2.AddPeer("peer-1", peer1IP, peer1Port) - - // Wait for the connections to be established - // You might need a little more time based on network delay and retry logic - time.Sleep(5 * time.Second) - - // Send a message from peer-1 to peer-2 - err := peer1.Send("peer-2", "chat", []byte("Hey from peer-1!")) - assert.NoError(t, err) - - // Allow some time for the message to be received and handled - time.Sleep(2 * time.Second) -} - -// TestSendFailure tests if sending a message fails when no connection exists. -func TestSendFailure(t *testing.T) { - peer1 := NewTCPNetwork("peer-1", "127.0.0.1", 9001) - _ = NewTCPNetwork("peer-2", "127.0.0.1", 9002) - - // Attempt to send a message without establishing a connection first - err := peer1.Send("peer-2", "chat", []byte("Message without connection")) - assert.Error(t, err, "Expected error when sending to a non-connected peer") -} |