Atlas › Test

Test_LockLow_UnlockHigh

Exact test identity: go.temporal.io/server/common/locks/TestPriorityMutexSuite/Test_LockLow_UnlockHigh

Package
go.temporal.io/server/common/locks
Suite / test hierarchy
TestPriorityMutexSuite/Test_LockLow_UnlockHigh
Test
Test_LockLow_UnlockHigh
Introduced at
priority_mutex_impl.go ×1 Frontier kind: Joint frontier
Covered ranges
13
Covered lines
51
Covered files
2

Covered source

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

go.temporal.io/server/common/locks/priority_mutex_impl.go 40 covered LOC · 11 ranges

Open complete file

29 var _ PriorityMutex = (*PriorityMutexImpl)(nil)
30
31 > func NewPriorityMutex() *PriorityMutexImpl { priority_mutex_impl.go
32 > lock := &sync.Mutex{}
33 > syncCV := NewConditionVariable(lock)
34 > asyncCV := NewConditionVariable(lock)
35 > return &PriorityMutexImpl{
36 > locker: lock,
37 > highCV: syncCV,
38 > lowCV: asyncCV,
39 > highWait: 0,
40 > lowWait: 0,
41 >
42 > lockState: PriorityMutexStateUnlocked,
43 > }
44 > }
45
46 // LockHigh try to lock with high priority, use LockHigh / UnlockHigh pair to lock / unlock
69 func (c *PriorityMutexImpl) LockLow(
70 ctx context.Context,
71 > ) error { priority_mutex_impl.go
72 > c.locker.Lock()
73 > defer c.locker.Unlock()
74 >
75 > c.lowWait += 1
76 > defer func() { c.lowWait -= 1 }()
77
78 > for c.lockState != PriorityMutexStateUnlocked && ctx.Err() == nil { priority_mutex_impl.go
79 c.lowCV.Wait(ctx.Done())
80 }
81
82 > if ctx.Err() != nil { priority_mutex_impl.go
83 return ctx.Err()
84 }
85
86 > c.lockState = PriorityMutexStateLockedByLow priority_mutex_impl.go
87 > return nil
88 }
89
113
114 // UnlockHigh unlock with high priority, use LockHigh / UnlockHigh pair to lock / unlock
115 > func (c *PriorityMutexImpl) UnlockHigh() { priority_mutex_impl.go
116 > c.locker.Lock()
117 > defer c.locker.Unlock()
118 >
119 > if c.lockState != PriorityMutexStateLockedByHigh {
120 > panic(fmt.Sprintf("unable to unlock high priority, state: %v\n", c.lockState)) priority_mutex_impl.go
121 }
122
126
127 // UnlockLow unlock with low priority, use LockLow / UnlockLow pair to lock / unlock
128 > func (c *PriorityMutexImpl) UnlockLow() { priority_mutex_impl.go
129 > c.locker.Lock()
130 > defer c.locker.Unlock()
131 >
132 > if c.lockState != PriorityMutexStateLockedByLow {
133 panic(fmt.Sprintf("unable to unlock high priority, state: %v\n", c.lockState))
134 }
135
136 > c.lockState = PriorityMutexStateUnlocked priority_mutex_impl.go
137 > c.notify()
138 }
139
go.temporal.io/server/common/locks/condition_variable_impl.go 11 covered LOC · 2 ranges

Open complete file

18 func NewConditionVariable(
19 lock Locker,
20 > ) *ConditionVariableImpl { condition_variable_impl.go
21 > return &ConditionVariableImpl{
22 > lock: lock,
23 >
24 > chanLock: sync.Mutex{},
25 > channel: newCVChannel(),
26 > }
27 > }
28
29 // Signal wakes one goroutine waiting on this condition variable, if there is any.