Repository: mosn/layotto (go)

Issue:
fix unstable ut; 修复不稳定的单测
<!-- Please only use this template for submitting enhancement requests -->

**What would you like to be added**:
The UT in `components/lock/zookeeper/zookeeper_lock_test.go` is not stable. Sometimes some lines of `components/lock/zookeeper/zookeeper_lock.go` are not covered in the UT and will lead to decreased test coverage.
For example , codecov reports it in https://github.com/mosn/layotto/pull/678#issuecomment-1168279135 
![image](https://user-images.githubusercontent.com/26001097/176330029-27898ffe-99ab-41ff-8a53-84726a0cd7a4.png)

It's because some code runs in a seperated goroutine. At the end of the test, the goroutine has not been executed:
![image](https://user-images.githubusercontent.com/26001097/176327910-24db83b4-130e-4e55-aef9-1b95a892144c.png)

We should make the test coverage stable

**Why is this needed**:
No more codecov warning

Pull request:
fix: ut unstable of zk lock
<!--  Thanks for sending a pull request!
Read contributing.md before commit pull request.
-->

**What this PR does**:
fix ut unstable of zookeeper lock, wait connection to close. 
**Which issue(s) this PR fixes**:
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
_If PR is about `failing-tests or flakes`, please post the related issues/tests in a comment and do not use `Fixes`_*
-->
Fixes #682 

**Special notes for your reviewer**:

**Does this PR introduce a user-facing change?**:
<!--
If no, just write "NONE" in the release-note block below.
If yes, a release note is required:
Enter your extended release note in the block below. If the PR requires additional action from users switching to the new release, include the string "action required".
-->
```release-note

```

Patch:
diff --git a/components/lock/zookeeper/zookeeper_lock.go b/components/lock/zookeeper/zookeeper_lock.go
index 9ea22296..18c060f0 100644
--- a/components/lock/zookeeper/zookeeper_lock.go
+++ b/components/lock/zookeeper/zookeeper_lock.go
@@ -11,6 +11,7 @@
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
+
 package zookeeper
 
 import (
@@ -24,7 +25,15 @@ import (
 	"mosn.io/layotto/components/pkg/utils"
 )
 
-// Zookeeper lock store
+var closeConn = func(conn utils.ZKConnection, expireInSecond int32) {
+	//can also
+	//time.Sleep(time.Second * time.Duration(expireInSecond))
+	<-time.After(time.Second * time.Duration(expireInSecond))
+	// make sure close connecion
+	conn.Close()
+}
+
+// ZookeeperLock lock store
 type ZookeeperLock struct {
 	//trylock reestablish connection  every time
 	factory utils.ConnectionFactory
@@ -34,7 +43,7 @@ type ZookeeperLock struct {
 	logger     log.ErrorLogger
 }
 
-// Create ZookeeperLock
+//NewZookeeperLock Create ZookeeperLock
 func NewZookeeperLock(logger log.ErrorLogger) *ZookeeperLock {
 	lock := &ZookeeperLock{
 		logger: logger,
@@ -67,7 +76,7 @@ func (p *ZookeeperLock) Features() []lock.Feature {
 	return nil
 }
 
-// Node tries to acquire a zookeeper lock
+//TryLock Node tries to acquire a zookeeper lock
 func (p *ZookeeperLock) TryLock(req *lock.TryLockRequest) (*lock.TryLockResponse, error) {
 
 	conn, err := p.factory.NewConnection(time.Duration(req.Expire)*time.Second, p.metadata)
@@ -92,12 +101,7 @@ func (p *ZookeeperLock) TryLock(req *lock.TryLockRequest) (*lock.TryLockResponse
 
 	//2.2 create node success, asyn  to make sure zkclient alive for need time
 	util.GoWithRecover(func() {
-		//can also
-		//time.Sleep(time.Second * time.Duration(req.Expire))
-		timeAfterTrigger := time.After(time.Second * time.Duration(req.Expire))
-		<-timeAfterTrigger
-		// make sure close connecion
-		conn.Close()
+		closeConn(conn, req.Expire)
 	}, nil)
 
 	return &lock.TryLockResponse{
@@ -106,7 +110,7 @@ func (p *ZookeeperLock) TryLock(req *lock.TryLockRequest) (*lock.TryLockResponse
 
 }
 
-// Node tries to release a zookeeper lock
+//Unlock Node tries to release a zookeeper lock
 func (p *ZookeeperLock) Unlock(req *lock.UnlockRequest) (*lock.UnlockResponse, error) {
 
 	conn := p.unlockConn


Test patch:
diff --git a/components/lock/zookeeper/zookeeper_lock_test.go b/components/lock/zookeeper/zookeeper_lock_test.go
index 4246110f..7ccfc4b2 100644
--- a/components/lock/zookeeper/zookeeper_lock_test.go
+++ b/components/lock/zookeeper/zookeeper_lock_test.go
@@ -11,6 +11,7 @@
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
+
 package zookeeper
 
 import (
@@ -18,6 +19,8 @@ import (
 	"testing"
 	"time"
 
+	"mosn.io/layotto/components/pkg/utils"
+
 	"github.com/go-zookeeper/zk"
 	"github.com/golang/mock/gomock"
 	"github.com/stretchr/testify/assert"
@@ -36,8 +39,11 @@ var cfg = lock.Metadata{
 	Properties: make(map[string]string),
 }
 
-func TestMain(m *testing.M) {
+var mockCloseConn = func(conn utils.ZKConnection, expireInSecond int32) {
+}
 
+func TestMain(m *testing.M) {
+	closeConn = mockCloseConn
 	cfg.Properties["zookeeperHosts"] = "127.0.0.1;127.0.0.1"
 	cfg.Properties["zookeeperPassword"] = ""
 	os.Exit(m.Run())
@@ -77,7 +83,6 @@ func TestZookeeperLock_ALock_AUnlock(t *testing.T) {
 	})
 	assert.NoError(t, err)
 	assert.Equal(t, unlock.Status, lock.SUCCESS)
-
 }
 
 // A lock ,B unlock
@@ -112,7 +117,6 @@ func TestZookeeperLock_ALock_BUnlock(t *testing.T) {
 	})
 	assert.NoError(t, err)
 	assert.Equal(t, unlock.Status, lock.LOCK_BELONG_TO_OTHERS)
-
 }
 
 // A lock , B lock ,A unlock ,B lock,B unlock