chore: pass ctx in dial functions (#11)

This commit is contained in:
gabrielmer 2025-01-03 09:48:44 +01:00 committed by GitHub
parent 7eb7647599
commit 769594edec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 12 deletions

View File

@ -417,10 +417,8 @@ func (w *Waku) ListenAddresses() ([]multiaddr.Multiaddr, error) {
return w.node.ListenAddresses() return w.node.ListenAddresses()
} }
func (w *Waku) DialPeer(address multiaddr.Multiaddr) error { func (w *Waku) DialPeer(ctx context.Context, address multiaddr.Multiaddr) error {
// Using WakuConnect so it matches the go-waku's behavior and terminology // Using WakuConnect so it matches the go-waku's behavior and terminology
ctx, cancel := context.WithTimeout(w.ctx, requestTimeout)
defer cancel()
return w.node.Connect(ctx, address) return w.node.Connect(ctx, address)
} }
@ -429,9 +427,7 @@ func (w *Waku) RelayPublish(ctx context.Context, message *pb.WakuMessage, pubsub
return w.node.RelayPublish(ctx, message, pubsubTopic) return w.node.RelayPublish(ctx, message, pubsubTopic)
} }
func (w *Waku) DialPeerByID(peerID peer.ID, protocol libp2pproto.ID) error { func (w *Waku) DialPeerByID(ctx context.Context, peerID peer.ID, protocol libp2pproto.ID) error {
ctx, cancel := context.WithTimeout(w.ctx, requestTimeout)
defer cancel()
return w.node.DialPeerByID(ctx, peerID, protocol) return w.node.DialPeerByID(ctx, peerID, protocol)
} }

View File

@ -103,7 +103,9 @@ func TestBasicWaku(t *testing.T) {
require.True(t, isDisconnected, "nwaku should be disconnected from the store node") require.True(t, isDisconnected, "nwaku should be disconnected from the store node")
// Re-connect // Re-connect
err = w.DialPeer(storeNodeMa) ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()
err = w.DialPeer(ctx, storeNodeMa)
require.NoError(t, err) require.NoError(t, err)
// Check that we are connected again // Check that we are connected again
@ -384,7 +386,9 @@ func TestDial(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.True(t, receiverPeerCount == 0, "Receiver node should have no connected peers") require.True(t, receiverPeerCount == 0, "Receiver node should have no connected peers")
// Dial // Dial
err = dialerNode.DialPeer(receiverMultiaddr[0]) ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()
err = dialerNode.DialPeer(ctx, receiverMultiaddr[0])
require.NoError(t, err) require.NoError(t, err)
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
// Check that both nodes now have one connected peer // Check that both nodes now have one connected peer
@ -438,7 +442,9 @@ func TestRelay(t *testing.T) {
require.NotNil(t, receiverMultiaddr) require.NotNil(t, receiverMultiaddr)
// Dial so they become peers // Dial so they become peers
err = senderNode.DialPeer(receiverMultiaddr[0]) ctx1, cancel1 := context.WithTimeout(context.Background(), requestTimeout)
defer cancel1()
err = senderNode.DialPeer(ctx1, receiverMultiaddr[0])
require.NoError(t, err) require.NoError(t, err)
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
// Check that both nodes now have one connected peer // Check that both nodes now have one connected peer
@ -457,9 +463,9 @@ func TestRelay(t *testing.T) {
} }
// send message // send message
pubsubTopic := FormatWakuRelayTopic(senderNodeWakuConfig.ClusterID, senderNodeWakuConfig.Shards[0]) pubsubTopic := FormatWakuRelayTopic(senderNodeWakuConfig.ClusterID, senderNodeWakuConfig.Shards[0])
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout) ctx2, cancel2 := context.WithTimeout(context.Background(), requestTimeout)
defer cancel() defer cancel2()
senderNode.RelayPublish(ctx, message, pubsubTopic) senderNode.RelayPublish(ctx2, message, pubsubTopic)
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
wg.Add(1) wg.Add(1)