Atlas › Test

TestTryAcquire

Exact test identity: go.temporal.io/server/common/locks/TestPrioritySemaphoreSuite/TestTryAcquire

Package
go.temporal.io/server/common/locks
Suite / test hierarchy
TestPrioritySemaphoreSuite/TestTryAcquire
Test
TestTryAcquire
Introduced at
priority_semaphore_impl.go ×1 Frontier kind: Joint frontier
Covered ranges
9
Covered lines
34
Covered files
1

Covered source

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

go.temporal.io/server/common/locks/priority_semaphore_impl.go 34 covered LOC · 9 ranges

Open complete file

67 // maximum combined weight for concurrent access, capable of handling multiple priority levels.
68 // Most of the logic is taken directly from golang's semaphore.Weighted.
69 > func NewPrioritySemaphore(n int) *PrioritySemaphoreImpl { priority_semaphore_impl.go
70 > waitLists := make([]*list.List, NumPriorities)
71 > for i := range waitLists {
72 > waitLists[i] = list.New()
73 > }
74 > return &PrioritySemaphoreImpl{
75 > size: n,
76 > waitLists: waitLists,
77 > }
78 }
79
158 // TryAcquire acquires the semaphore with a weight of n without blocking.
159 // On success, returns true. On failure, returns false and leaves the semaphore unchanged.
160 > func (s *PrioritySemaphoreImpl) TryAcquire(priority Priority, n int) bool { priority_semaphore_impl.go
161 > if priority >= NumPriorities {
162 // nolint:forbidigo
163 panic(fmt.Sprintf("semaphore: invalid priority %v, priority must be less than %v", priority, NumPriorities))
164 }
165
166 > s.mu.Lock() priority_semaphore_impl.go
167 > defer s.mu.Unlock()
168 > if s.size-s.cur >= n && s.noWaiters(priority) {
169 > s.cur += n
170 > return true
171 > }
172 > return false priority_semaphore_impl.go
173 }
174
175 > func (s *PrioritySemaphoreImpl) Release(n int) { priority_semaphore_impl.go
176 > s.mu.Lock()
177 > defer s.mu.Unlock()
178 > s.cur -= n
179 > if s.cur < 0 {
180 s.mu.Unlock()
181 panic("semaphore: released more than held")
182 }
183 > s.notifyWaiters() priority_semaphore_impl.go
184 }
185
186 > func (s *PrioritySemaphoreImpl) notifyWaiters() { priority_semaphore_impl.go
187 > for _, l := range s.waitLists {
188 > for {
189 > next := l.Front()
190 > if next == nil {
191 > break // No more waiters blocked.
192 }
193
219
220 // noWaiters returns if there is no waiter that has priority higher or equal to lowestPriority.
221 > func (s *PrioritySemaphoreImpl) noWaiters(lowestPriority Priority) bool { priority_semaphore_impl.go
222 > for _, l := range s.waitLists[:lowestPriority+1] {
223 > if l.Len() > 0 {
224 return false
225 }
226 }
227 > return true priority_semaphore_impl.go
228 }