From 5501e2460c3d866d490c157d96dafe2bbed47e11 Mon Sep 17 00:00:00 2001 From: Michael Boeckli Date: Thu, 21 Mar 2024 17:51:27 +0100 Subject: [PATCH] Integration test for peer --- peer_integration_test.go | 101 ++++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 48 deletions(-) diff --git a/peer_integration_test.go b/peer_integration_test.go index 45363bb..a11f957 100644 --- a/peer_integration_test.go +++ b/peer_integration_test.go @@ -3,11 +3,19 @@ package p2p import ( "fmt" "log" + "log/slog" "os" "testing" + "time" + "github.com/libsv/go-p2p/wire" "github.com/ory/dockertest/v3" "github.com/ory/dockertest/v3/docker" + "github.com/stretchr/testify/require" +) + +const ( + p2pPortBinding = "18335" ) var ( @@ -34,10 +42,10 @@ func TestMain(m *testing.M) { Repository: "bitcoinsv/bitcoin-sv", Tag: "1.1.0", Env: []string{}, - ExposedPorts: []string{p2pPort}, + ExposedPorts: []string{p2pPortBinding, p2pPort}, PortBindings: map[docker.Port][]docker.PortBinding{ - "18335": { - {HostIP: "0.0.0.0", HostPort: p2pPort}, + p2pPort: { + {HostIP: "0.0.0.0", HostPort: p2pPortBinding}, }, }, Cmd: []string{"/entrypoint.sh", "bitcoind"}, @@ -70,48 +78,45 @@ func TestMain(m *testing.M) { os.Exit(code) } -// -//// -////func TestNewPeer(t *testing.T) { -//// t.Helper() -//// if testing.Short() { -//// t.Skip("skipping integration test") -//// } -//// -//// t.Run("break and re-establish peer connection", func(t *testing.T) { -//// logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo})) -//// -//// pm := NewPeerManager(logger, wire.TestNet) -//// require.NotNil(t, pm) -//// -//// peerHandler := NewMockPeerHandler() -//// -//// time.Sleep(5 * time.Second) -//// -//// peer, err := NewPeer(logger, "localhost:18333", peerHandler, wire.TestNet) -//// require.NoError(t, err) -//// -//// time.Sleep(5 * time.Second) -//// -//// require.True(t, peer.Connected()) -//// -//// dockerClient := pool.Client -//// -//// // restart container and break connection -//// err = dockerClient.RestartContainer(resource.Container.ID, 10) -//// require.NoError(t, err) -//// -//// time.Sleep(6 * time.Second) -//// -//// // expect that peer has disconnected -//// require.False(t, peer.Connected()) -//// -//// // wait longer than the reconnect interval and expect that peer has re-established connection -//// time.Sleep(reconnectInterval + 2*time.Second) -//// require.True(t, peer.Connected()) -//// -//// //err = dockerClient.StopContainer(resource.Container.ID, 10) -//// require.NoError(t, err) -//// peer.Shutdown() -//// }) -////} +func TestNewPeer(t *testing.T) { + t.Helper() + if testing.Short() { + t.Skip("skipping integration test") + } + + t.Run("break and re-establish peer connection", func(t *testing.T) { + logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo})) + + pm := NewPeerManager(logger, wire.TestNet) + require.NotNil(t, pm) + + peerHandler := NewMockPeerHandler() + + time.Sleep(5 * time.Second) + + peer, err := NewPeer(logger, "localhost:"+p2pPortBinding, peerHandler, wire.TestNet) + require.NoError(t, err) + + time.Sleep(5 * time.Second) + + require.True(t, peer.Connected()) + + dockerClient := pool.Client + + // restart container and break connection + err = dockerClient.RestartContainer(resource.Container.ID, 10) + require.NoError(t, err) + + time.Sleep(6 * time.Second) + + // expect that peer has disconnected + require.False(t, peer.Connected()) + + // wait longer than the reconnect interval and expect that peer has re-established connection + time.Sleep(reconnectInterval + 2*time.Second) + require.True(t, peer.Connected()) + + require.NoError(t, err) + peer.Shutdown() + }) +}