Atlas › Test

TestLock_High_Success

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

Package
go.temporal.io/server/common/locks
Suite / test hierarchy
TestPriorityMutexSuite/TestLock_High_Success
Test
TestLock_High_Success
Introduced at
TestLock_High_Success Frontier kind: Test frontier
Covered ranges
12
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 · 10 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
138 }
139
140 > func (c *PriorityMutexImpl) IsLocked() bool { priority_mutex_impl.go
141 > c.locker.Lock()
142 > defer c.locker.Unlock()
143 >
144 > return c.lockState != PriorityMutexStateUnlocked
145 > }
146
147 > func (c *PriorityMutexImpl) notify() { priority_mutex_impl.go
148 > if c.highWait > 0 {
149 c.highCV.Signal()
150 > } else if c.lowWait > 0 { priority_mutex_impl.go
151 c.lowCV.Signal()
152 }
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.