Skip to content

Commit

Permalink
Integration test for peer
Browse files Browse the repository at this point in the history
  • Loading branch information
boecklim committed Mar 21, 2024
1 parent d2979b6 commit 5501e24
Showing 1 changed file with 53 additions and 48 deletions.
101 changes: 53 additions & 48 deletions peer_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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"},
Expand Down Expand Up @@ -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()
})
}

0 comments on commit 5501e24

Please sign in to comment.