clients_test.go 716 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package discov
  2. import (
  3. "sync"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/tal-tech/go-zero/core/discov/internal"
  7. )
  8. var mockLock sync.Mutex
  9. func setMockClient(cli internal.EtcdClient) func() {
  10. mockLock.Lock()
  11. internal.NewClient = func([]string) (internal.EtcdClient, error) {
  12. return cli, nil
  13. }
  14. return func() {
  15. internal.NewClient = internal.DialClient
  16. mockLock.Unlock()
  17. }
  18. }
  19. func TestExtract(t *testing.T) {
  20. id, ok := extractId("key/123/val")
  21. assert.True(t, ok)
  22. assert.Equal(t, "123", id)
  23. _, ok = extract("any", -1)
  24. assert.False(t, ok)
  25. _, ok = extract("any", 10)
  26. assert.False(t, ok)
  27. }
  28. func TestMakeKey(t *testing.T) {
  29. assert.Equal(t, "key/123", makeEtcdKey("key", 123))
  30. }