Atlas › Test

TestLockOnSameID

Exact test identity: go.temporal.io/server/common/locks/TestIDMutexSuite/TestLockOnSameID

Package
go.temporal.io/server/common/locks
Suite / test hierarchy
TestIDMutexSuite/TestLockOnSameID
Test
TestLockOnSameID
Introduced at
id_mutex.go ×2 Frontier kind: Joint frontier
Covered ranges
9
Covered lines
48
Covered files
1

Co-introduced tests

1 other test enter at the same concept.

Covered source

Expand a file to inspect source; the > gutter marks covered lines.

go.temporal.io/server/common/locks/id_mutex.go 48 covered LOC · 9 ranges

Open complete file

40
41 // NewIDMutex create a new IDLock
42 > func NewIDMutex(numShard uint32, hashFn HashFunc) IDMutex { id_mutex.go
43 > impl := &idMutexImpl{
44 > numShard: numShard,
45 > hashFn: hashFn,
46 > shards: make(map[uint32]*idMutexShardImpl),
47 > }
48 > for i := range numShard {
49 > impl.shards[i] = &idMutexShardImpl{
50 > mutexInfos: make(map[any]*mutexInfo),
51 > }
52 > }
53
54 > return impl id_mutex.go
55 }
56
57 > func newMutexInfo() *mutexInfo { id_mutex.go
58 > return &mutexInfo{
59 > waitCount: 1,
60 > }
61 > }
62
63 // LockID lock by specific identifier
64 > func (idMutex *idMutexImpl) LockID(identifier any) { id_mutex.go
65 > shard := idMutex.shards[idMutex.getShardIndex(identifier)]
66 >
67 > shard.Lock()
68 > mutexInfo, ok := shard.mutexInfos[identifier]
69 > if !ok {
70 > mutexInfo := newMutexInfo()
71 > shard.mutexInfos[identifier] = mutexInfo
72 > shard.Unlock()
73 > mutexInfo.Lock()
74 > return
75 > }
76
77 > mutexInfo.waitCount++ id_mutex.go
78 > shard.Unlock()
79 > mutexInfo.Lock()
80 }
81
82 // UnlockID unlock by specific identifier
83 > func (idMutex *idMutexImpl) UnlockID(identifier any) { id_mutex.go
84 > shard := idMutex.shards[idMutex.getShardIndex(identifier)]
85 >
86 > shard.Lock()
87 > defer shard.Unlock()
88 > mutexInfo, ok := shard.mutexInfos[identifier]
89 > if !ok {
90 panic("cannot find workflow lock")
91 }
92 > mutexInfo.Unlock() id_mutex.go
93 > if mutexInfo.waitCount == 1 {
94 > delete(shard.mutexInfos, identifier)
95 > } else {
96 > mutexInfo.waitCount-- id_mutex.go
97 > }
98 }
99
100 > func (idMutex *idMutexImpl) getShardIndex(key any) uint32 { id_mutex.go
101 > return idMutex.hashFn(key) % idMutex.numShard
102 > }