Atlas › Test

Test_LockHigh_UnlockLow

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

Package
go.temporal.io/server/common/locks
Suite / test hierarchy
TestPriorityMutexSuite/Test_LockHigh_UnlockLow
Test
Test_LockHigh_UnlockLow
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
47 func (c *PriorityMutexImpl) LockHigh(
48 ctx context.Context,
49 > ) error { priority_mutex_impl.go
50 > c.locker.Lock()
51 > defer c.locker.Unlock()
52 >
53 > c.highWait += 1
54 > defer func() { c.highWait -= 1 }()
55
56 > for c.lockState != PriorityMutexStateUnlocked && ctx.Err() == nil { priority_mutex_impl.go
57 c.highCV.Wait(ctx.Done())
58 }
59
60 > if ctx.Err() != nil { priority_mutex_impl.go
61 return ctx.Err()
62 }
63
64 > c.lockState = PriorityMutexStateLockedByHigh priority_mutex_impl.go
65 > return nil
66 }
67
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))
121 }
122
123 > c.lockState = PriorityMutexStateUnlocked priority_mutex_impl.go
124 > c.notify()
125 }
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)) priority_mutex_impl.go
134 }
135
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.