Skip to content

fix: ut unstable of zk lock #701

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

Merged
merged 4 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 5 additions & 4 deletions components/lock/zookeeper/zookeeper_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -24,7 +25,7 @@ import (
"mosn.io/layotto/components/pkg/utils"
)

// Zookeeper lock store
// ZookeeperLock lock store
type ZookeeperLock struct {
//trylock reestablish connection every time
factory utils.ConnectionFactory
Expand All @@ -34,7 +35,7 @@ type ZookeeperLock struct {
logger log.ErrorLogger
}

// Create ZookeeperLock
//NewZookeeperLock Create ZookeeperLock
func NewZookeeperLock(logger log.ErrorLogger) *ZookeeperLock {
lock := &ZookeeperLock{
logger: logger,
Expand Down Expand Up @@ -67,7 +68,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)
Expand Down Expand Up @@ -106,7 +107,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
Expand Down
11 changes: 11 additions & 0 deletions components/lock/zookeeper/zookeeper_lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -60,6 +61,7 @@ func TestZookeeperLock_ALock_AUnlock(t *testing.T) {
lockConn.EXPECT().Create(path, []byte(lockOwerA), int32(zk.FlagEphemeral), zk.WorldACL(zk.PermAll)).Return("", nil).Times(1)
unlockConn.EXPECT().Get(path).Return([]byte(lockOwerA), &zk.Stat{Version: 123}, nil).Times(1)
unlockConn.EXPECT().Delete(path, int32(123)).Return(nil).Times(1)
lockConn.EXPECT().Close().AnyTimes()

comp.unlockConn = unlockConn
comp.factory = factory
Expand All @@ -78,6 +80,8 @@ func TestZookeeperLock_ALock_AUnlock(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, unlock.Status, lock.SUCCESS)

//wait for connection close
time.Sleep(time.Second * expireTime)
}

// A lock ,B unlock
Expand All @@ -95,6 +99,7 @@ func TestZookeeperLock_ALock_BUnlock(t *testing.T) {
factory.EXPECT().NewConnection(time.Duration(expireTime)*time.Second, comp.metadata).Return(lockConn, nil).Times(1)
lockConn.EXPECT().Create(path, []byte(lockOwerA), int32(zk.FlagEphemeral), zk.WorldACL(zk.PermAll)).Return("", nil).Times(1)
unlockConn.EXPECT().Get(path).Return([]byte(lockOwerA), &zk.Stat{Version: 123}, nil).Times(1)
lockConn.EXPECT().Close().AnyTimes()

comp.unlockConn = unlockConn
comp.factory = factory
Expand All @@ -113,6 +118,8 @@ func TestZookeeperLock_ALock_BUnlock(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, unlock.Status, lock.LOCK_BELONG_TO_OTHERS)

//wait for connection close
time.Sleep(time.Second * expireTime)
}

// A lock , B lock ,A unlock ,B lock,B unlock
Expand All @@ -134,6 +141,7 @@ func TestZookeeperLock_ALock_BLock_AUnlock_BLock_BUnlock(t *testing.T) {
lockConn.EXPECT().Create(path, []byte(lockOwerB), int32(zk.FlagEphemeral), zk.WorldACL(zk.PermAll)).Return("", zk.ErrNodeExists).Times(1)
lockConn.EXPECT().Create(path, []byte(lockOwerB), int32(zk.FlagEphemeral), zk.WorldACL(zk.PermAll)).Return("", nil).Times(1)
lockConn.EXPECT().Close().Return().Times(1)
lockConn.EXPECT().Close().AnyTimes()

unlockConn.EXPECT().Get(path).Return([]byte(lockOwerA), &zk.Stat{Version: 123}, nil).Times(1)
unlockConn.EXPECT().Get(path).Return([]byte(lockOwerB), &zk.Stat{Version: 124}, nil).Times(1)
Expand Down Expand Up @@ -183,4 +191,7 @@ func TestZookeeperLock_ALock_BLock_AUnlock_BLock_BUnlock(t *testing.T) {
})
assert.NoError(t, err)
assert.Equal(t, lock.SUCCESS, unlock.Status)

//wait for connection close
time.Sleep(time.Second * expireTime)
}