Atlas › Test

Test_ContextCanceledBeforeAcquire

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

Package
go.temporal.io/server/common/locks
Suite / test hierarchy
TestPrioritySemaphoreSuite/Test_ContextCanceledBeforeAcquire
Test
Test_ContextCanceledBeforeAcquire
Introduced at
priority_semaphore_impl.go ×1 Frontier kind: Joint frontier
Covered ranges
4
Covered lines
22
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 22 covered LOC · 4 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
81 // are available or ctx is done. On success, returns nil. On failure, returns
82 // ctx.Err() and leaves the semaphore unchanged.
83 > func (s *PrioritySemaphoreImpl) Acquire(ctx context.Context, priority Priority, n int) error { priority_semaphore_impl.go
84 > if priority >= NumPriorities {
85 // nolint:forbidigo
86 panic(fmt.Sprintf("semaphore: invalid priority %v, priority must be less than %v", priority, NumPriorities))
87 }
88
89 > done := ctx.Done() priority_semaphore_impl.go
90 >
91 > s.mu.Lock()
92 > select {
93 > case <-done: priority_semaphore_impl.go
94 > // ctx becoming done has "happened before" acquiring the semaphore,
95 > // whether it became done before the call began or while we were
96 > // waiting for the mutex. We prefer to fail even if we could acquire
97 > // the mutex without blocking.
98 > s.mu.Unlock()
99 > return ctx.Err()
100 default:
101 }