Skip to content

Commit

Permalink
fix: closing the mDNS connection upon stopping the network
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f committed Jun 14, 2023
1 parent 1787d9f commit 0ccc651
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 26 deletions.
7 changes: 5 additions & 2 deletions network/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func newMdnsService(ctx context.Context, host lp2phost.Host, logger *logger.Logg
logger: logger,
}
// setup mDNS discovery to find local peers
mdns.service = lp2pmdns.NewMdnsService(host, "", mdns)
mdns.service = lp2pmdns.NewMdnsService(host, "pactus-mdns", mdns)

return mdns
}
Expand Down Expand Up @@ -57,5 +57,8 @@ func (mdns *mdnsService) Start() error {
}

func (mdns *mdnsService) Stop() {

err := mdns.service.Close()
if err != nil {
mdns.logger.Error("unable to close the network", "err", err)

Check warning on line 62 in network/mdns.go

View check run for this annotation

Codecov / codecov/patch

network/mdns.go#L62

Added line #L62 was not covered by tests
}
}
65 changes: 45 additions & 20 deletions network/mdns_test.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,55 @@
package network

import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMDNS(t *testing.T) {
conf1 := testConfig()
conf1.EnableMdns = true
net1, err := NewNetwork(conf1)
assert.NoError(t, err)

conf2 := testConfig()
conf2.EnableMdns = true
net2, err := NewNetwork(conf2)
assert.NoError(t, err)

assert.NoError(t, net1.Start())
assert.NoError(t, net2.Start())

assert.NoError(t, net1.JoinGeneralTopic())
assert.NoError(t, net2.JoinGeneralTopic())

time.Sleep(200 * time.Millisecond)

assert.NoError(t, net1.SendTo([]byte("test"), net2.SelfID()))
func TestMdns(t *testing.T) {
for n := 0; n < 60; n++ {
nets := [2]*network{}
fmt.Printf("=========== starting %v", n)
for i := 0; i < 2; i++ {
conf := testConfig()
conf.Listens = []string{"/ip4/127.0.0.1/tcp/0"}
conf.EnableMdns = true
nets[i] = makeTestNetwork(t, conf, nil)

require.NoError(t, nets[i].Start())
time.Sleep(100 * time.Millisecond)
}

// Wait until both nodes discover each other.
m := 0

msg := []byte("test-mdns")

for {
m++
fmt.Printf("~~~ attempt %v", m)
tagInfo := nets[0].host.ConnManager().GetTagInfo(nets[1].SelfID())
if tagInfo == nil || len(tagInfo.Conns) == 0 {
fmt.Print("~~~ attempt continue", m)
time.Sleep(100 * time.Millisecond)
continue
}
break
}

time.Sleep(100 * time.Millisecond)

err := nets[0].SendTo(msg, nets[1].SelfID())
assert.NoError(t, err)

e := shouldReceiveEvent(t, nets[1]).(*StreamMessage)
assert.Equal(t, e.Source, nets[0].SelfID())
assert.Equal(t, readData(t, e.Reader, len(msg)), msg)

nets[0].Stop()
nets[1].Stop()
}
}
5 changes: 2 additions & 3 deletions network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,8 @@ func (n *network) Stop() {
if n.mdns != nil {
n.mdns.Stop()
}
if n.dht != nil {
n.dht.Stop()
}

n.dht.Stop()
n.gossip.Stop()
n.stream.Stop()

Expand Down
8 changes: 7 additions & 1 deletion network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func readData(t *testing.T, r io.ReadCloser, len int) []byte {
buf := make([]byte, len)
_, err := r.Read(buf)
assert.NoError(t, err)
assert.NoError(t, r.Close())

return buf
}
Expand Down Expand Up @@ -143,6 +144,7 @@ func TestNetwork(t *testing.T) {
fmt.Sprintf("/ip4/0.0.0.0/tcp/%v", bootstrapPort),
fmt.Sprintf("/ip6/::/tcp/%v", bootstrapPort),
}
fmt.Printf("Starting Bootstrap node")
networkB := makeTestNetwork(t, confB, []lp2p.Option{})
bootstrapAddresses := []string{
fmt.Sprintf("/ip4/127.0.0.1/tcp/%v/p2p/%v", bootstrapPort, networkB.SelfID().String()),
Expand All @@ -158,6 +160,7 @@ func TestNetwork(t *testing.T) {
"/ip4/0.0.0.0/tcp/0",
"/ip6/::/tcp/0",
}
fmt.Printf("Starting Public node")
networkP := makeTestNetwork(t, confP, []lp2p.Option{
lp2p.ForceReachabilityPublic(),
})
Expand All @@ -172,6 +175,7 @@ func TestNetwork(t *testing.T) {
"/ip4/0.0.0.0/tcp/0",
"/ip6/::/tcp/0",
}
fmt.Printf("Starting Private node M")
networkM := makeTestNetwork(t, confM, []lp2p.Option{
lp2p.ForceReachabilityPrivate(),
})
Expand All @@ -186,6 +190,7 @@ func TestNetwork(t *testing.T) {
"/ip4/0.0.0.0/tcp/0",
"/ip6/::/tcp/0",
}
fmt.Printf("Starting Private node N")
networkN := makeTestNetwork(t, confN, []lp2p.Option{
lp2p.ForceReachabilityPrivate(),
})
Expand All @@ -199,10 +204,11 @@ func TestNetwork(t *testing.T) {
"/ip4/0.0.0.0/tcp/0",
"/ip6/::/tcp/0",
}
fmt.Printf("Starting Private node X")
networkX := makeTestNetwork(t, confX, []lp2p.Option{
lp2p.ForceReachabilityPrivate(),
})
time.Sleep(1 * time.Second)
time.Sleep(2 * time.Second)

t.Run("all nodes have at least one connection to the bootstrap node B", func(t *testing.T) {
assert.GreaterOrEqual(t, networkP.NumConnectedPeers(), 1)
Expand Down

0 comments on commit 0ccc651

Please sign in to comment.