2020-07-26 17:09:05 +08:00
|
|
|
package discov
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-01-04 15:51:32 +08:00
|
|
|
"github.com/zeromicro/go-zero/core/discov/internal"
|
2020-07-26 17:09:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var mockLock sync.Mutex
|
|
|
|
|
|
|
|
func setMockClient(cli internal.EtcdClient) func() {
|
|
|
|
mockLock.Lock()
|
|
|
|
internal.NewClient = func([]string) (internal.EtcdClient, error) {
|
|
|
|
return cli, nil
|
|
|
|
}
|
|
|
|
return func() {
|
|
|
|
internal.NewClient = internal.DialClient
|
|
|
|
mockLock.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtract(t *testing.T) {
|
|
|
|
id, ok := extractId("key/123/val")
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, "123", id)
|
|
|
|
|
|
|
|
_, ok = extract("any", -1)
|
|
|
|
assert.False(t, ok)
|
2021-02-07 20:24:47 +08:00
|
|
|
|
|
|
|
_, ok = extract("any", 10)
|
|
|
|
assert.False(t, ok)
|
2020-07-26 17:09:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeKey(t *testing.T) {
|
|
|
|
assert.Equal(t, "key/123", makeEtcdKey("key", 123))
|
|
|
|
}
|