From 5337b42cddd14d3b8d22dfd6fdb54cb52496a2cb Mon Sep 17 00:00:00 2001 From: Michael Boeckli Date: Fri, 19 Jul 2024 18:06:16 +0200 Subject: [PATCH] Integration test --- test/peer_integration_test.go | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/test/peer_integration_test.go b/test/peer_integration_test.go index c65bc59..2db87c8 100644 --- a/test/peer_integration_test.go +++ b/test/peer_integration_test.go @@ -148,4 +148,47 @@ func TestNewPeer(t *testing.T) { t.Log("shutdown finished") }) + t.Run("restart", func(t *testing.T) { + logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})) + + peerHandler := p2p.NewMockPeerHandler() + + peer, err := p2p.NewPeer(logger, "localhost:"+p2pPortBinding, peerHandler, wire.TestNet, p2p.WithUserAgent("agent", "0.0.1")) + require.NoError(t, err) + + t.Log("expect that peer has connected") + connectLoop: + for { + select { + case <-time.NewTicker(200 * time.Millisecond).C: + if peer.Connected() { + break connectLoop + } + case <-time.NewTimer(5 * time.Second).C: + t.Fatal("peer did not connect") + } + } + + t.Log("restart peer") + peer.Restart() + + t.Log("expect that peer has re-established connection") + reconnectLoop: + for { + select { + case <-time.NewTicker(200 * time.Millisecond).C: + if peer.Connected() { + break reconnectLoop + } + case <-time.NewTimer(2 * time.Second).C: + t.Fatal("peer did not reconnect") + } + } + + require.NoError(t, err) + + t.Log("shutdown") + peer.Shutdown() + t.Log("shutdown finished") + }) }