github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/store/etcdv3/service_test.go (about)

     1  package etcdv3
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"sort"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestRegisterServiceWithDeregister(t *testing.T) {
    14  	m := NewMercury(t)
    15  
    16  	ctx := context.Background()
    17  	svc := "svc"
    18  	path := fmt.Sprintf(serviceStatusKey, svc)
    19  	_, deregister, err := m.RegisterService(ctx, svc, time.Minute)
    20  	assert.NoError(t, err)
    21  
    22  	kv, err := m.GetOne(ctx, path)
    23  	assert.NoError(t, err)
    24  	assert.Equal(t, path, string(kv.Key))
    25  
    26  	deregister()
    27  	//time.Sleep(time.Second)
    28  	kv, err = m.GetOne(ctx, path)
    29  	assert.Error(t, err)
    30  	assert.Nil(t, kv)
    31  }
    32  
    33  func TestServiceStatusStream(t *testing.T) {
    34  	m := NewMercury(t)
    35  	ctx, cancel := context.WithCancel(context.Background())
    36  	defer cancel()
    37  	_, unregisterService1, err := m.RegisterService(ctx, "127.0.0.1:5001", time.Second)
    38  	assert.NoError(t, err)
    39  	ch, err := m.ServiceStatusStream(ctx)
    40  	assert.NoError(t, err)
    41  	assert.Equal(t, <-ch, []string{"127.0.0.1:5001"})
    42  	_, _, err = m.RegisterService(ctx, "127.0.0.1:5002", time.Second)
    43  	assert.NoError(t, err)
    44  	endpoints := <-ch
    45  	sort.Strings(endpoints)
    46  	assert.Equal(t, endpoints, []string{"127.0.0.1:5001", "127.0.0.1:5002"})
    47  	unregisterService1()
    48  	assert.Equal(t, <-ch, []string{"127.0.0.1:5002"})
    49  }