Skip to content

Commit

Permalink
Update client api interface definition and test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
snower committed Apr 24, 2024
1 parent ce9b7ec commit 1d91771
Show file tree
Hide file tree
Showing 6 changed files with 331 additions and 48 deletions.
38 changes: 32 additions & 6 deletions client/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ import (
)

type Database struct {
dbId uint8
client IClient
glock *sync.Mutex
closed bool
dbId uint8
client IClient
glock *sync.Mutex
closed bool
defaultTimeoutFlag uint16
defaultExpriedFlag uint16
}

func NewDatabase(dbId uint8, client IClient) *Database {
return &Database{dbId, client, &sync.Mutex{}, false}
return &Database{dbId, client, &sync.Mutex{}, false,
client.GetDefaultTimeoutFlag(), client.GetDefaultExpriedFlag()}
}

func (self *Database) Close() error {
Expand All @@ -40,7 +43,14 @@ func (self *Database) sendCommand(command protocol.ICommand) error {
}

func (self *Database) Lock(lockKey [16]byte, timeout uint32, expried uint32) *Lock {
return NewLock(self, lockKey, timeout, expried)
lock := NewLock(self, lockKey, timeout, expried)
if self.defaultTimeoutFlag > 0 {
lock.SetTimeoutFlag(self.defaultTimeoutFlag)
}
if self.defaultExpriedFlag > 0 {
lock.SetExpriedFlag(self.defaultExpriedFlag)
}
return lock
}

func (self *Database) Event(eventKey [16]byte, timeout uint32, expried uint32, defaultSeted bool) *Event {
Expand Down Expand Up @@ -181,3 +191,19 @@ func (self *Database) GenRequestId() [16]byte {
func (self *Database) GenLockId() [16]byte {
return protocol.GenLockId()
}

func (self *Database) SetDefaultTimeoutFlag(timeoutFlag uint16) {
self.defaultTimeoutFlag = timeoutFlag
}

func (self *Database) SetDefaultExpriedFlag(expriedFlag uint16) {
self.defaultExpriedFlag = expriedFlag
}

func (self *Database) GetDefaultTimeoutFlag() uint16 {
return self.defaultTimeoutFlag
}

func (self *Database) GetDefaultExpriedFlag() uint16 {
return self.defaultExpriedFlag
}
26 changes: 26 additions & 0 deletions client/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,32 @@ func (self *Event) Clear() (*protocol.LockResultCommand, error) {
return result, err
}

func (self *Event) ClearWithUnsetData() (*protocol.LockResultCommand, error) {
if self.setedMode == EVENT_MODE_DEFAULT_SET {
self.glock.Lock()
if self.eventLock == nil {
self.eventLock = &Lock{self.db, self.eventKey, self.eventKey, self.timeout, self.expried, 0, 0}
}
self.glock.Unlock()
return self.eventLock.LockUpdateWithData(protocol.NewLockCommandDataUnsetData())
}

self.glock.Lock()
if self.eventLock == nil {
self.eventLock = &Lock{self.db, self.eventKey, self.eventKey, self.timeout, self.expried, 1, 0}
}
self.glock.Unlock()

result, err := self.eventLock.UnlockWithData(protocol.NewLockCommandDataUnsetData())
if err == nil {
return result, nil
}
if result != nil && result.Result == protocol.RESULT_UNLOCK_ERROR {
return result, nil
}
return result, err
}

func (self *Event) Set() (*protocol.LockResultCommand, error) {
if self.setedMode == EVENT_MODE_DEFAULT_SET {
self.glock.Lock()
Expand Down
24 changes: 16 additions & 8 deletions client/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,21 @@ func TestEvent_DefaultSetWithData(t *testing.T) {
return
}

_, err = event.Clear()
_, err = event.ClearWithUnsetData()
if err != nil {
t.Errorf("Event Wait Clear Fail %v", err)
return
}

go func() {
time.Sleep(20 * time.Millisecond)
_, err = event.SetWithData(protocol.NewLockCommandDataSetString("bbb"))
if err != nil {
t.Errorf("Event Wakeup Set Fail %v", err)
result, serr := event.SetWithData(protocol.NewLockCommandDataSetString("bbb"))
if serr != nil {
t.Errorf("Event Wakeup Set Fail %v", serr)
return
}
if result.GetLockData() != nil {
t.Errorf("Event Wakeup Set Data Fail %v", result.GetLockData())
return
}
}()
Expand Down Expand Up @@ -243,7 +247,7 @@ func TestEvent_DefaultClearWithData(t *testing.T) {
return
}

_, err = event.Clear()
_, err = event.ClearWithUnsetData()
if err != nil {
t.Errorf("Event Clear Fail %v", err)
return
Expand All @@ -261,9 +265,13 @@ func TestEvent_DefaultClearWithData(t *testing.T) {

go func() {
time.Sleep(20 * time.Millisecond)
_, err = event.SetWithData(protocol.NewLockCommandDataSetString("bbb"))
if err != nil {
t.Errorf("Event Wakeup Set Fail %v", err)
result, serr := event.SetWithData(protocol.NewLockCommandDataSetString("bbb"))
if serr != nil {
t.Errorf("Event Wakeup Set Fail %v", serr)
return
}
if result.GetLockData() != nil {
t.Errorf("Event Wakeup Set Data Fail %v", result.GetLockData())
return
}
}()
Expand Down
11 changes: 11 additions & 0 deletions client/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ func (self *GroupEvent) Clear() (*protocol.LockResultCommand, error) {
return self.eventLock.LockUpdate()
}

func (self *GroupEvent) ClearWithUnsetData() (*protocol.LockResultCommand, error) {
self.glock.Lock()
lockId := [16]byte{byte(self.versionId), byte(self.versionId >> 8), byte(self.versionId >> 16), byte(self.versionId >> 24),
byte(self.versionId >> 32), byte(self.versionId >> 40), byte(self.versionId >> 48), byte(self.versionId >> 56),
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
timeout := self.timeout | uint32(protocol.TIMEOUT_FLAG_LESS_LOCK_VERSION_IS_LOCK_SUCCED)<<16
self.eventLock = &Lock{self.db, lockId, self.groupKey, timeout, self.expried, 0, 0}
self.glock.Unlock()
return self.eventLock.LockUpdateWithData(protocol.NewLockCommandDataUnsetData())
}

func (self *GroupEvent) Set() (*protocol.LockResultCommand, error) {
self.glock.Lock()
if self.eventLock == nil {
Expand Down
166 changes: 159 additions & 7 deletions client/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func TestLock_WithData(t *testing.T) {
t.Errorf("Lock LockWithData Fail %v", err)
return
}
if result.GetLockData() != nil && result.GetLockData().GetStringData() == "aaa" {
if result.GetLockData() != nil {
t.Errorf("Lock LockWithData Result LockData Fail %v", result.GetLockData())
return
}
Expand All @@ -432,26 +432,88 @@ func TestLock_WithData(t *testing.T) {
return
}

lock = client.Lock(testString2Key("TestData"), 50, 10)
lock = client.Lock(testString2Key("TestData1"), 50, 10)
lock.SetCount(10)
result, err = lock.LockWithData(protocol.NewLockCommandDataSetString("aaa"))
if err != nil {
t.Errorf("Lock LockWithData Fail %v", err)
return
}
if result.GetLockData() != nil {
t.Errorf("Lock LockWithData Result LockData Fail %v", result.GetLockData())
return
}
ulock1 := client.Lock(testString2Key("TestData1"), 50, 10)
ulock1.SetCount(10)
result, err = ulock1.LockWithData(protocol.NewLockCommandDataSetString("bbb"))
if err != nil {
t.Errorf("Lock LockWithData1 Expried Fail %v", err)
return
}
if result.GetLockData() == nil || result.GetLockData().GetStringData() != "aaa" {
t.Errorf("Lock LockWithData1 Expried Result LockData Fail %v", result.GetLockData())
return
}
ulock2 := client.Lock(testString2Key("TestData1"), 50, 10)
ulock2.SetCount(10)
result, err = ulock2.LockWithData(protocol.NewLockCommandDataSetString("ccc"))
if err != nil {
t.Errorf("Lock LockWithData2 Expried Fail %v", err)
return
}
if result.GetLockData() == nil || result.GetLockData().GetStringData() != "bbb" {
t.Errorf("Lock LockWithData2 Expried Result LockData Fail %v", result.GetLockData())
return
}

result, err = lock.Unlock()
if err != nil {
t.Errorf("Lock Unlock Fail %v", err)
return
}
if result.GetLockData() == nil || result.GetLockData().GetStringData() != "ccc" {
t.Errorf("Lock Unlock Result LockData Fail %v", result.GetLockData())
return
}
result, err = ulock1.UnlockWithData(protocol.NewLockCommandDataUnsetData())
if err != nil {
t.Errorf("Lock Unlock1 Fail %v", err)
return
}
if result.GetLockData() == nil || result.GetLockData().GetStringData() != "ccc" {
t.Errorf("Lock Unlock1 Result LockData Fail %v", result.GetLockData())
return
}
result, err = ulock2.Unlock()
if err != nil {
t.Errorf("Lock Unlock2 Fail %v", err)
return
}
if result.GetLockData() != nil {
t.Errorf("Lock Unlock2 Result LockData Fail %v", result.GetLockData())
return
}

lock = client.Lock(testString2Key("TestData2"), 50, 10)
lock.SetCount(10)
result, err = lock.LockWithData(protocol.NewLockCommandDataSetString("aaa"))
if err != nil {
t.Errorf("Lock LockWithData Fail %v", err)
return
}
if result.GetLockData() != nil {
t.Errorf("Lock LockWithData Result LockData Fail %v", result.GetLockData())
return
}
ulock := client.Lock(testString2Key("TestData"), 50, 0)
ulock.SetCount(10)
result, err = ulock.LockWithData(protocol.NewLockCommandDataSetString("bbb"))
ulock1 = client.Lock(testString2Key("TestData2"), 50, 0)
ulock1.SetCount(10)
result, err = ulock1.LockWithData(protocol.NewLockCommandDataSetString("bbb"))
if err != nil {
t.Errorf("Lock LockWithData Expried Fail %v", err)
t.Errorf("Lock LockWithData1 Expried Fail %v", err)
return
}
if result.GetLockData() == nil || result.GetLockData().GetStringData() != "aaa" {
t.Errorf("Lock LockWithData Expried Result LockData Fail %v", result.GetLockData())
t.Errorf("Lock LockWithData1 Expried Result LockData Fail %v", result.GetLockData())
return
}
result, err = lock.Unlock()
Expand All @@ -465,3 +527,93 @@ func TestLock_WithData(t *testing.T) {
}
})
}

func TestLock_RequireAck(t *testing.T) {
testWithClient(t, func(client *Client) {
lock := client.Lock(testString2Key("TestAck"), 50, 10)
lock.SetTimeoutFlag(protocol.TIMEOUT_FLAG_REQUIRE_ACKED)
result, err := lock.Lock()
if err != nil {
t.Errorf("Lock LockWithData Fail %v", err)
return
}
if result.GetLockData() != nil {
t.Errorf("Lock LockWithData Result LockData Fail %v", result.GetLockData())
return
}
result, err = lock.Unlock()
if err != nil {
t.Errorf("Lock Unlock Fail %v", err)
return
}
if result.GetLockData() != nil {
t.Errorf("Lock Unlock Result LockData Fail %v", result.GetLockData())
return
}

lock = client.Lock(testString2Key("TestAck1"), 50, 10)
lock.SetTimeoutFlag(protocol.TIMEOUT_FLAG_REQUIRE_ACKED)
lock.SetCount(10)
result, err = lock.LockWithData(protocol.NewLockCommandDataSetString("aaa"))
if err != nil {
t.Errorf("Lock LockWithData Fail %v", err)
return
}
if result.GetLockData() != nil {
t.Errorf("Lock LockWithData Result LockData Fail %v", result.GetLockData())
return
}
ulock1 := client.Lock(testString2Key("TestAck1"), 50, 10)
lock.SetTimeoutFlag(protocol.TIMEOUT_FLAG_REQUIRE_ACKED)
ulock1.SetCount(10)
result, err = ulock1.LockWithData(protocol.NewLockCommandDataSetString("bbb"))
if err != nil {
t.Errorf("Lock LockWithData1 Expried Fail %v", err)
return
}
if result.GetLockData() == nil || result.GetLockData().GetStringData() != "aaa" {
t.Errorf("Lock LockWithData1 Expried Result LockData Fail %v", result.GetLockData())
return
}
ulock2 := client.Lock(testString2Key("TestAck1"), 50, 10)
lock.SetTimeoutFlag(protocol.TIMEOUT_FLAG_REQUIRE_ACKED)
ulock2.SetCount(10)
result, err = ulock2.LockWithData(protocol.NewLockCommandDataSetString("ccc"))
if err != nil {
t.Errorf("Lock LockWithData2 Expried Fail %v", err)
return
}
if result.GetLockData() == nil || result.GetLockData().GetStringData() != "bbb" {
t.Errorf("Lock LockWithData2 Expried Result LockData Fail %v", result.GetLockData())
return
}

result, err = lock.Unlock()
if err != nil {
t.Errorf("Lock Unlock Fail %v", err)
return
}
if result.GetLockData() == nil || result.GetLockData().GetStringData() != "ccc" {
t.Errorf("Lock Unlock Result LockData Fail %v", result.GetLockData())
return
}
result, err = ulock1.UnlockWithData(protocol.NewLockCommandDataUnsetData())
if err != nil {
t.Errorf("Lock Unlock1 Fail %v", err)
return
}
if result.GetLockData() == nil || result.GetLockData().GetStringData() != "ccc" {
t.Errorf("Lock Unlock1 Result LockData Fail %v", result.GetLockData())
return
}
result, err = ulock2.Unlock()
if err != nil {
t.Errorf("Lock Unlock2 Fail %v", err)
return
}
if result.GetLockData() != nil {
t.Errorf("Lock Unlock2 Result LockData Fail %v", result.GetLockData())
return
}
})
}
Loading

0 comments on commit 1d91771

Please sign in to comment.