You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.2 KiB
72 lines
1.2 KiB
3 years ago
|
package registry
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"testing"
|
||
|
"time"
|
||
|
|
||
|
"github.com/go-kratos/kratos/v2/registry"
|
||
|
)
|
||
|
|
||
|
func TestRegistry(t *testing.T) {
|
||
|
ctx := context.Background()
|
||
|
s := ®istry.ServiceInstance{
|
||
|
ID: "0",
|
||
|
Name: "helloworld",
|
||
|
Endpoints: []string{"http://127.0.0.1:1111"},
|
||
|
}
|
||
|
|
||
|
r, _ := New([]string{"127.0.0.1:2181"})
|
||
|
|
||
|
w, err := r.Watch(ctx, s.Name)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
defer w.Stop()
|
||
|
go func() {
|
||
|
for {
|
||
|
res, err := w.Next()
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
t.Logf("watch: %d", len(res))
|
||
|
for _, r := range res {
|
||
|
t.Logf("next: %+v", r)
|
||
|
}
|
||
|
}
|
||
|
}()
|
||
|
time.Sleep(time.Second)
|
||
|
|
||
|
if err := r.Register(ctx, s); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
time.Sleep(time.Second)
|
||
|
|
||
|
res, err := r.GetService(ctx, s.Name)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
for i, re := range res {
|
||
|
t.Logf("first %d re:%v\n", i, re)
|
||
|
}
|
||
|
if len(res) != 1 && res[0].Name != s.Name {
|
||
|
t.Errorf("not expected: %+v", res)
|
||
|
}
|
||
|
|
||
|
if err := r.Deregister(ctx, s); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
time.Sleep(time.Second)
|
||
|
|
||
|
res, err = r.GetService(ctx, s.Name)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
for i, re := range res {
|
||
|
t.Logf("second %d re:%v\n", i, re)
|
||
|
}
|
||
|
if len(res) != 0 {
|
||
|
t.Errorf("not expected empty")
|
||
|
}
|
||
|
}
|