JCS-pub/common/pkgs/distlock/lockprovider/shard_store_test.go

104 lines
2.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package lockprovider
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
"gitlink.org.cn/cloudream/jcs-pub/common/pkgs/distlock/types"
)
func Test_ShardStoreLock(t *testing.T) {
cases := []struct {
title string
initLocks []types.Lock
doLock types.Lock
wantOK bool
}{
{
title: "同节点同一个Buzy锁",
initLocks: []types.Lock{
{
Path: []string{ShardStoreLockPathPrefix, "hub1"},
Name: ShardStoreBuzyLock,
},
},
doLock: types.Lock{
Path: []string{ShardStoreLockPathPrefix, "hub1"},
Name: ShardStoreBuzyLock,
},
wantOK: true,
},
{
title: "同节点同一个GC锁",
initLocks: []types.Lock{
{
Path: []string{ShardStoreLockPathPrefix, "hub1"},
Name: ShardStoreGCLock,
},
},
doLock: types.Lock{
Path: []string{ShardStoreLockPathPrefix, "hub1"},
Name: ShardStoreGCLock,
},
wantOK: true,
},
{
title: "同时设置Buzy和GC",
initLocks: []types.Lock{
{
Path: []string{ShardStoreLockPathPrefix, "hub1"},
Name: ShardStoreBuzyLock,
Target: NewStringLockTarget(),
},
},
doLock: types.Lock{
Path: []string{ShardStoreLockPathPrefix, "hub1"},
Name: ShardStoreGCLock,
Target: NewStringLockTarget(),
},
wantOK: false,
},
}
for _, ca := range cases {
Convey(ca.title, t, func() {
ipfsLock := NewShardStoreLock()
for _, l := range ca.initLocks {
ipfsLock.Lock("req1", l)
}
err := ipfsLock.CanLock(ca.doLock)
if ca.wantOK {
So(err, ShouldBeNil)
} else {
So(err, ShouldNotBeNil)
}
})
}
Convey("解锁", t, func() {
ipfsLock := NewShardStoreLock()
lock := types.Lock{
Path: []string{ShardStoreLockPathPrefix, "hub1"},
Name: ShardStoreBuzyLock,
}
ipfsLock.Lock("req1", lock)
err := ipfsLock.CanLock(lock)
So(err, ShouldBeNil)
ipfsLock.Unlock("req1", lock)
lock = types.Lock{
Path: []string{ShardStoreLockPathPrefix, "hub1"},
Name: ShardStoreGCLock,
}
err = ipfsLock.CanLock(lock)
So(err, ShouldBeNil)
})
}