-
Notifications
You must be signed in to change notification settings - Fork 0
/
LockFixture.go
81 lines (63 loc) · 1.85 KB
/
LockFixture.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package test_lock
import (
"testing"
"github.com/pip-services3-go/pip-services3-components-go/lock"
"github.com/stretchr/testify/assert"
)
const LOCK1 = "lock_1"
const LOCK2 = "lock_2"
const LOCK3 = "lock_3"
type LockFixture struct {
locker lock.ILock
}
func NewLockFixture(locker lock.ILock) *LockFixture {
return &LockFixture{
locker: locker,
}
}
func (c *LockFixture) TestTryAcquireLock(t *testing.T) {
// Try to acquire lock for the first time
result, err := c.locker.TryAcquireLock("", LOCK1, 3000)
assert.Nil(t, err)
assert.True(t, result)
// Try to acquire lock for the second time
result, err = c.locker.TryAcquireLock("", LOCK1, 3000)
assert.Nil(t, err)
assert.False(t, result)
// Release the lock
err = c.locker.ReleaseLock("", LOCK1)
assert.Nil(t, err)
// Try to acquire lock for the third time
result, err = c.locker.TryAcquireLock("", LOCK1, 3000)
assert.Nil(t, err)
assert.True(t, result)
err = c.locker.ReleaseLock("", LOCK1)
assert.Nil(t, err)
}
func (c *LockFixture) TestAcquireLock(t *testing.T) {
// Acquire lock for the first time
c.locker.AcquireLock("", LOCK2, 3000, 1000)
// Acquire lock for the second time
err := c.locker.AcquireLock("", LOCK2, 3000, 1000)
assert.NotNil(t, err)
// Release the lock
err = c.locker.ReleaseLock("", LOCK2)
assert.Nil(t, err)
// Acquire lock for the third time
err = c.locker.AcquireLock("", LOCK2, 3000, 1000)
assert.Nil(t, err)
err = c.locker.ReleaseLock("", LOCK2)
assert.Nil(t, err)
}
func (c *LockFixture) TestReleaseLock(t *testing.T) {
// Acquire lock for the first time
result, err := c.locker.TryAcquireLock("", LOCK3, 3000)
assert.Nil(t, err)
assert.True(t, result)
// Release the lock for the first time
err = c.locker.ReleaseLock("", LOCK3)
assert.Nil(t, err)
// Release the lock for the second time
err = c.locker.ReleaseLock("", LOCK3)
assert.Nil(t, err)
}