Skip to content

Commit

Permalink
fix: ut unstable of zk lock (#701)
Browse files Browse the repository at this point in the history
* fix ut unstable of zk lock

* use DI to run test

Signed-off-by: seeflood <[email protected]>

* make format

Signed-off-by: seeflood <[email protected]>

Co-authored-by: seeflood <[email protected]>
  • Loading branch information
ZLBer and seeflood authored Jul 7, 2022
1 parent b30dda7 commit 8aa188f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
24 changes: 14 additions & 10 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,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
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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{
Expand All @@ -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
Expand Down
10 changes: 7 additions & 3 deletions components/lock/zookeeper/zookeeper_lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
// 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 (
"os"
"testing"
"time"

"mosn.io/layotto/components/pkg/utils"

"github.com/go-zookeeper/zk"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
Expand All @@ -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())
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8aa188f

Please sign in to comment.