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.
59 lines
1.1 KiB
59 lines
1.1 KiB
package discovery
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/go-kratos/kratos/v2/log"
|
|
"github.com/go-kratos/kratos/v2/registry"
|
|
"google.golang.org/grpc/resolver"
|
|
)
|
|
|
|
type testClientConn struct {
|
|
resolver.ClientConn // For unimplemented functions
|
|
te *testing.T
|
|
}
|
|
|
|
func (t *testClientConn) UpdateState(s resolver.State) {
|
|
t.te.Log("UpdateState", s)
|
|
}
|
|
|
|
type testWatch struct {
|
|
}
|
|
|
|
func (m *testWatch) Next() ([]*registry.ServiceInstance, error) {
|
|
time.Sleep(time.Millisecond * 200)
|
|
ins := []*registry.ServiceInstance{
|
|
{
|
|
ID: "mock_ID",
|
|
Name: "mock_Name",
|
|
Version: "mock_Version",
|
|
},
|
|
}
|
|
return ins, nil
|
|
}
|
|
|
|
// Watch creates a watcher according to the service name.
|
|
func (m *testWatch) Stop() error {
|
|
return nil
|
|
}
|
|
|
|
func TestWatch(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
r := &discoveryResolver{
|
|
w: &testWatch{},
|
|
cc: &testClientConn{te: t},
|
|
log: log.NewHelper(log.DefaultLogger),
|
|
ctx: ctx,
|
|
cancel: cancel,
|
|
}
|
|
go func() {
|
|
time.Sleep(time.Second * 2)
|
|
r.Close()
|
|
}()
|
|
r.watch()
|
|
|
|
t.Log("watch goroutine exited after 2 second")
|
|
}
|
|
|