-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol_test.go
More file actions
196 lines (165 loc) · 4.29 KB
/
Copy pathcontrol_test.go
File metadata and controls
196 lines (165 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Copyright (c) 2024-2026 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/
package syncing
import (
"context"
"sync"
"testing"
"time"
)
func TestUnit_New_MinimumCapacityOne(t *testing.T) {
c := NewControl(0) // count 0 -> max(0,1) = 1
sem := c.(*sem)
if cap(sem.c) != 1 {
t.Fatalf("expected capacity 1, got %d", cap(sem.c))
}
}
func TestUnit_Acquire_NoContention(t *testing.T) {
c := NewControl(2)
ctx := context.Background()
if !c.Acquire(ctx) {
t.Fatal("first acquire should succeed")
}
if !c.Acquire(ctx) {
t.Fatal("second acquire should succeed")
}
// now semaphore is full; further acquire would block
}
func TestUnit_Acquire_BlocksWhenFull(t *testing.T) {
c := NewControl(1)
ctx := context.Background()
if !c.Acquire(ctx) {
t.Fatal("acquire should succeed")
}
acquired := make(chan bool, 1)
go func() {
acquired <- c.Acquire(ctx)
}()
// The goroutine should be blocked; ensure it doesn't return immediately.
select {
case <-acquired:
t.Fatal("acquire should have blocked but returned true")
case <-time.After(50 * time.Millisecond):
// expected: still blocked
}
}
func TestUnit_Release_UnblocksAcquire(t *testing.T) {
c := NewControl(1)
ctx := context.Background()
if !c.Acquire(ctx) {
t.Fatal("first acquire failed")
}
acquired := make(chan bool, 1)
go func() {
acquired <- c.Acquire(ctx)
}()
// Give the goroutine time to block
time.Sleep(10 * time.Millisecond)
c.Release()
select {
case ok := <-acquired:
if !ok {
t.Fatal("acquire after release should succeed")
}
case <-time.After(time.Second):
t.Fatal("acquire did not unblock after release")
}
}
func TestUnit_Release_OnEmptyDoesNothing(t *testing.T) {
c := NewControl(2)
// Release on empty semaphore should not panic or change capacity.
c.Release()
c.Release()
c.Release()
// Still possible to acquire up to capacity.
ctx := context.Background()
if !c.Acquire(ctx) {
t.Fatal("acquire after empty release should succeed")
}
if !c.Acquire(ctx) {
t.Fatal("second acquire should succeed")
}
}
func TestUnit_ContextCancellation_BeforeAcquire(t *testing.T) {
c := NewControl(1)
ctx, cancel := context.WithCancel(context.Background())
cancel() // immediately cancelled
if c.Acquire(ctx) {
t.Fatal("acquire with cancelled context should return false")
}
// The semaphore should still be free.
ctx2 := context.Background()
if !c.Acquire(ctx2) {
t.Fatal("acquire after cancelled attempt should succeed")
}
}
func TestUnit_ContextCancellation_WhileWaiting(t *testing.T) {
c := NewControl(1)
// Fill the semaphore
if !c.Acquire(context.Background()) {
t.Fatal("first acquire failed")
}
ctx, cancel := context.WithCancel(context.Background())
acquired := make(chan bool, 1)
go func() {
acquired <- c.Acquire(ctx)
}()
// Wait for goroutine to start blocking
time.Sleep(10 * time.Millisecond)
cancel() // cancel while waiting
select {
case ok := <-acquired:
if ok {
t.Fatal("acquire should return false after context cancellation")
}
case <-time.After(time.Second):
t.Fatal("acquire did not return after context cancellation")
}
// Verify that the slot was NOT taken by the cancelled acquire
c.Release()
if !c.Acquire(context.Background()) {
t.Fatal("semaphore should be available after cancelled wait")
}
}
func TestUnit_Concurrency_RespectsCapacity(t *testing.T) {
const capacity = 3
const goroutines = 10
c := NewControl(capacity)
ctx := context.Background()
var wg sync.WaitGroup
maxConcurrent := 0
var mu sync.Mutex
current := 0
for i := 0; i < goroutines; i++ {
wg.Add(1)
go func() {
defer wg.Done()
if c.Acquire(ctx) {
mu.Lock()
current++
if current > maxConcurrent {
maxConcurrent = current
}
mu.Unlock()
time.Sleep(10 * time.Millisecond) // simulate work
mu.Lock()
current--
mu.Unlock()
c.Release()
}
}()
}
wg.Wait()
if maxConcurrent > capacity {
t.Fatalf("max concurrent acquisitions %d exceeded capacity %d", maxConcurrent, capacity)
}
if maxConcurrent < 1 {
t.Fatal("no acquisitions succeeded")
}
}
func TestUnit_InterfaceImplementation(t *testing.T) {
// Compile-time check (this test mainly ensures the type satisfies Control)
var _ Control = NewControl(1)
}