github.com/mailgun/holster/v4@v4.20.0/discovery/consul_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 TestConsulSinglePeer(t *testing.T) {
    13  	const catalog = "TestConsulSinglePeer"
    14  	p := discovery.Peer{ID: "id-1", Metadata: []byte("address-0"), IsSelf: true}
    15  
    16  	// client, err := api.NewClient(api.DefaultConfig())
    17  	// require.NoError(t, err)
    18  
    19  	onUpdateCh := make(chan []discovery.Peer, 1)
    20  	cs, err := discovery.NewConsul(&discovery.ConsulConfig{
    21  		CatalogName: catalog,
    22  		Peer:        p,
    23  		OnUpdate: func(peers []discovery.Peer) {
    24  			onUpdateCh <- peers
    25  		},
    26  	})
    27  	require.NoError(t, err)
    28  
    29  	e := <-onUpdateCh
    30  	assert.Equal(t, p, e[0])
    31  
    32  	err = cs.Close(context.Background())
    33  	require.NoError(t, err)
    34  }
    35  
    36  func TestConsulMultiplePeers(t *testing.T) {
    37  	const catalog = "TestConsulMultiplePeers"
    38  	p0 := discovery.Peer{ID: "id-0", Metadata: []byte("address-0"), IsSelf: true}
    39  	p1 := discovery.Peer{ID: "id-1", Metadata: []byte("address-1")}
    40  
    41  	// client, err := api.NewClient(api.DefaultConfig())
    42  	// require.NoError(t, err)
    43  
    44  	onUpdateCh := make(chan []discovery.Peer, 2)
    45  	cs0, err := discovery.NewConsul(&discovery.ConsulConfig{
    46  		CatalogName: catalog,
    47  		Peer:        p0,
    48  		OnUpdate: func(peers []discovery.Peer) {
    49  			onUpdateCh <- peers
    50  		},
    51  	})
    52  	require.NoError(t, err)
    53  	defer cs0.Close(context.Background())
    54  
    55  	e := <-onUpdateCh
    56  	assert.Equal(t, e[0], p0)
    57  
    58  	cs1, err := discovery.NewConsul(&discovery.ConsulConfig{
    59  		CatalogName: catalog,
    60  		Peer:        p1,
    61  	})
    62  	require.NoError(t, err)
    63  	defer cs1.Close(context.Background())
    64  
    65  	e = <-onUpdateCh
    66  	assert.Equal(t, []discovery.Peer{p0, p1}, e)
    67  }