diff --git a/components/lock/zookeeper/zookeeper_lock.go b/components/lock/zookeeper/zookeeper_lock.go index 9ea2229611..18c060f0fc 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 diff --git a/components/lock/zookeeper/zookeeper_lock_test.go b/components/lock/zookeeper/zookeeper_lock_test.go index 4246110f03..7ccfc4b2d0 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