github.com/mailgun/holster/v4@v4.20.0/discovery/memberlist_test.go (about) 1 package discovery_test 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/mailgun/holster/v4/discovery" 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestMemberListMultiplePeers(t *testing.T) { 13 p0 := discovery.Peer{ID: "id-0", Metadata: []byte("address-0"), IsSelf: true} 14 p1 := discovery.Peer{ID: "id-1", Metadata: []byte("address-1")} 15 16 onUpdateCh := make(chan []discovery.Peer, 2) 17 ml0, err := discovery.NewMemberList(context.Background(), discovery.MemberListConfig{ 18 BindAddress: "localhost:8519", 19 Peer: p0, 20 OnUpdate: func(peers []discovery.Peer) { 21 onUpdateCh <- peers 22 }, 23 }) 24 require.NoError(t, err) 25 defer ml0.Close(context.Background()) 26 27 e := <-onUpdateCh 28 assert.Equal(t, e[0], p0) 29 30 ml1, err := discovery.NewMemberList(context.Background(), discovery.MemberListConfig{ 31 KnownPeers: []string{"localhost:8519"}, 32 BindAddress: "localhost:8518", 33 Peer: p1, 34 }) 35 require.NoError(t, err) 36 defer ml1.Close(context.Background()) 37 38 e = <-onUpdateCh 39 assert.Equal(t, []discovery.Peer{p0, p1}, e) 40 }