Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: assertion bug for asynchronous test for getty #1248

Merged
merged 3 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions registry/zookeeper/service_discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,15 @@ import (
"github.com/apache/dubbo-go/registry"
)

var testName = "test"

var tc *zk.TestCluster
const testName = "test"

func prepareData(t *testing.T) *zk.TestCluster {
var err error
tc, err = zk.StartTestCluster(1, nil, nil)
tc, err := zk.StartTestCluster(1, nil, nil)
assert.NoError(t, err)
assert.NotNil(t, tc.Servers[0])
address := "127.0.0.1:" + strconv.Itoa(tc.Servers[0].Port)
//address := "127.0.0.1:2181"

config.GetBaseConfig().ServiceDiscoveries[testName] = &config.ServiceDiscoveryConfig{
Protocol: "zookeeper",
Expand All @@ -63,6 +62,7 @@ func TestNewZookeeperServiceDiscovery(t *testing.T) {
_, err := newZookeeperServiceDiscovery(name)

// the ServiceDiscoveryConfig not found
// err: could not init the instance because the config is invalid
assert.NotNil(t, err)

sdc := &config.ServiceDiscoveryConfig{
Expand All @@ -73,11 +73,20 @@ func TestNewZookeeperServiceDiscovery(t *testing.T) {
_, err = newZookeeperServiceDiscovery(name)

// RemoteConfig not found
// err: could not find the remote config for name: mock
assert.NotNil(t, err)
}

func TestCURDZookeeperServiceDiscovery(t *testing.T) {
prepareData(t)
func TestZookeeperServiceDiscovery_CURDAndListener(t *testing.T) {
tc := prepareData(t)
defer func() {
_ = tc.Stop()
}()
t.Run("testCURDZookeeperServiceDiscovery", testCURDZookeeperServiceDiscovery)
t.Run("testAddListenerZookeeperServiceDiscovery", testAddListenerZookeeperServiceDiscovery)
}

func testCURDZookeeperServiceDiscovery(t *testing.T) {
sd, err := newZookeeperServiceDiscovery(testName)
assert.Nil(t, err)
defer func() {
Expand Down Expand Up @@ -142,10 +151,7 @@ func TestCURDZookeeperServiceDiscovery(t *testing.T) {
assert.Nil(t, err)
}

func TestAddListenerZookeeperServiceDiscovery(t *testing.T) {
defer func() {
_ = tc.Stop()
}()
func testAddListenerZookeeperServiceDiscovery(t *testing.T) {
sd, err := newZookeeperServiceDiscovery(testName)
assert.Nil(t, err)
defer func() {
Expand Down
10 changes: 6 additions & 4 deletions remoting/getty/getty_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func testGetUser61(t *testing.T, c *Client) {

func testClient_AsyncCall(t *testing.T, client *Client) {
user := &User{}
lock := sync.Mutex{}
wg := sync.WaitGroup{}
request := remoting.NewRequest("2.0.2")
invocation := createInvocation("GetUser0", nil, nil, []interface{}{"4", nil, "username"},
[]reflect.Value{reflect.ValueOf("4"), reflect.ValueOf(nil), reflect.ValueOf("username")})
Expand All @@ -327,13 +327,13 @@ func testClient_AsyncCall(t *testing.T, client *Client) {
r := response.(remoting.AsyncCallbackResponse)
rst := *r.Reply.(*remoting.Response).Result.(*protocol.RPCResult)
assert.Equal(t, User{Id: "4", Name: "username"}, *(rst.Rest.(*User)))
lock.Unlock()
wg.Done()
}
lock.Lock()
wg.Add(1)
err := client.Request(request, 3*time.Second, rsp)
assert.NoError(t, err)
assert.Equal(t, User{}, *user)
time.Sleep(1 * time.Second)
wg.Wait()
}

func InitTest(t *testing.T) (*Server, *common.URL) {
Expand Down Expand Up @@ -450,6 +450,8 @@ func (u *UserProvider) GetUser(ctx context.Context, req []interface{}, rsp *User
}

func (u *UserProvider) GetUser0(id string, k *User, name string) (User, error) {
// fix testClient_AsyncCall assertion
time.Sleep(1 * time.Second)
return User{Id: id, Name: name}, nil
}

Expand Down
1 change: 1 addition & 0 deletions remoting/zookeeper/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (l *ZkEventListener) listenServiceNodeEvent(zkPath string, listener ...remo
l.pathMapLock.Lock()
a, ok := l.pathMap[zkPath]
if !ok || a.Load() > 1 {
l.pathMapLock.Unlock()
return false
}
a.Inc()
Expand Down