| 1 |
// Copyright 2017 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package semaphore_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"math/rand" |
| 10 |
"runtime" |
| 11 |
"sync" |
| 12 |
"testing" |
| 13 |
"time" |
| 14 |
|
| 15 |
"golang.org/x/sync/errgroup" |
| 16 |
"golang.org/x/sync/semaphore" |
| 17 |
) |
| 18 |
|
| 19 |
const maxSleep = 1 * time.Millisecond |
| 20 |
|
| 21 |
func HammerWeighted(sem *semaphore.Weighted, n int64, loops int) { |
| 22 |
for i := 0; i < loops; i++ { |
| 23 |
sem.Acquire(context.Background(), n) |
| 24 |
time.Sleep(time.Duration(rand.Int63n(int64(maxSleep/time.Nanosecond))) * time.Nanosecond) |
| 25 |
sem.Release(n) |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
func TestWeighted(t *testing.T) { |
| 30 |
t.Parallel() |
| 31 |
|
| 32 |
n := runtime.GOMAXPROCS(0) |
| 33 |
loops := 10000 / n |
| 34 |
sem := semaphore.NewWeighted(int64(n)) |
| 35 |
var wg sync.WaitGroup |
| 36 |
wg.Add(n) |
| 37 |
for i := 0; i < n; i++ { |
| 38 |
i := i |
| 39 |
go func() { |
| 40 |
defer wg.Done() |
| 41 |
HammerWeighted(sem, int64(i), loops) |
| 42 |
}() |
| 43 |
} |
| 44 |
wg.Wait() |
| 45 |
} |
| 46 |
|
| 47 |
func TestWeightedPanic(t *testing.T) { |
| 48 |
t.Parallel() |
| 49 |
|
| 50 |
defer func() { |
| 51 |
if recover() == nil { |
| 52 |
t.Fatal("release of an unacquired weighted semaphore did not panic") |
| 53 |
} |
| 54 |
}() |
| 55 |
w := semaphore.NewWeighted(1) |
| 56 |
w.Release(1) |
| 57 |
} |
| 58 |
|
| 59 |
func TestWeightedNegativeWeightPanic(t *testing.T) { |
| 60 |
t.Parallel() |
| 61 |
|
| 62 |
ctx := context.Background() |
| 63 |
w := semaphore.NewWeighted(1) |
| 64 |
|
| 65 |
func() { |
| 66 |
defer func() { |
| 67 |
if recover() == nil { |
| 68 |
t.Fatal("Acquire with negative weight did not panic") |
| 69 |
} |
| 70 |
}() |
| 71 |
w.Acquire(ctx, -1) |
| 72 |
}() |
| 73 |
|
| 74 |
func() { |
| 75 |
defer func() { |
| 76 |
if recover() == nil { |
| 77 |
t.Fatal("TryAcquire with negative weight did not panic") |
| 78 |
} |
| 79 |
}() |
| 80 |
w.TryAcquire(-1) |
| 81 |
}() |
| 82 |
|
| 83 |
func() { |
| 84 |
defer func() { |
| 85 |
if recover() == nil { |
| 86 |
t.Fatal("Release with negative weight did not panic") |
| 87 |
} |
| 88 |
}() |
| 89 |
w.Release(-1) |
| 90 |
}() |
| 91 |
} |
| 92 |
|
| 93 |
func TestWeightedTryAcquire(t *testing.T) { |
| 94 |
t.Parallel() |
| 95 |
|
| 96 |
ctx := context.Background() |
| 97 |
sem := semaphore.NewWeighted(2) |
| 98 |
tries := []bool{} |
| 99 |
sem.Acquire(ctx, 1) |
| 100 |
tries = append(tries, sem.TryAcquire(1)) |
| 101 |
tries = append(tries, sem.TryAcquire(1)) |
| 102 |
|
| 103 |
sem.Release(2) |
| 104 |
|
| 105 |
tries = append(tries, sem.TryAcquire(1)) |
| 106 |
sem.Acquire(ctx, 1) |
| 107 |
tries = append(tries, sem.TryAcquire(1)) |
| 108 |
|
| 109 |
want := []bool{true, false, true, false} |
| 110 |
for i := range tries { |
| 111 |
if tries[i] != want[i] { |
| 112 |
t.Errorf("tries[%d]: got %t, want %t", i, tries[i], want[i]) |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
func TestWeightedAcquire(t *testing.T) { |
| 118 |
t.Parallel() |
| 119 |
|
| 120 |
ctx := context.Background() |
| 121 |
sem := semaphore.NewWeighted(2) |
| 122 |
tryAcquire := func(n int64) bool { |
| 123 |
ctx, cancel := context.WithTimeout(ctx, 10*time.Millisecond) |
| 124 |
defer cancel() |
| 125 |
return sem.Acquire(ctx, n) == nil |
| 126 |
} |
| 127 |
|
| 128 |
tries := []bool{} |
| 129 |
sem.Acquire(ctx, 1) |
| 130 |
tries = append(tries, tryAcquire(1)) |
| 131 |
tries = append(tries, tryAcquire(1)) |
| 132 |
|
| 133 |
sem.Release(2) |
| 134 |
|
| 135 |
tries = append(tries, tryAcquire(1)) |
| 136 |
sem.Acquire(ctx, 1) |
| 137 |
tries = append(tries, tryAcquire(1)) |
| 138 |
|
| 139 |
want := []bool{true, false, true, false} |
| 140 |
for i := range tries { |
| 141 |
if tries[i] != want[i] { |
| 142 |
t.Errorf("tries[%d]: got %t, want %t", i, tries[i], want[i]) |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
func TestWeightedDoesntBlockIfTooBig(t *testing.T) { |
| 148 |
t.Parallel() |
| 149 |
|
| 150 |
const n = 2 |
| 151 |
sem := semaphore.NewWeighted(n) |
| 152 |
{ |
| 153 |
ctx, cancel := context.WithCancel(context.Background()) |
| 154 |
defer cancel() |
| 155 |
go sem.Acquire(ctx, n+1) |
| 156 |
} |
| 157 |
|
| 158 |
g, ctx := errgroup.WithContext(context.Background()) |
| 159 |
for i := n * 3; i > 0; i-- { |
| 160 |
g.Go(func() error { |
| 161 |
err := sem.Acquire(ctx, 1) |
| 162 |
if err == nil { |
| 163 |
time.Sleep(1 * time.Millisecond) |
| 164 |
sem.Release(1) |
| 165 |
} |
| 166 |
return err |
| 167 |
}) |
| 168 |
} |
| 169 |
if err := g.Wait(); err != nil { |
| 170 |
t.Errorf("semaphore.NewWeighted(%v) failed to AcquireCtx(_, 1) with AcquireCtx(_, %v) pending", n, n+1) |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
// TestLargeAcquireDoesntStarve times out if a large call to Acquire starves. |
| 175 |
// Merely returning from the test function indicates success. |
| 176 |
func TestLargeAcquireDoesntStarve(t *testing.T) { |
| 177 |
t.Parallel() |
| 178 |
|
| 179 |
ctx := context.Background() |
| 180 |
n := int64(runtime.GOMAXPROCS(0)) |
| 181 |
sem := semaphore.NewWeighted(n) |
| 182 |
running := true |
| 183 |
|
| 184 |
var wg sync.WaitGroup |
| 185 |
wg.Add(int(n)) |
| 186 |
for i := n; i > 0; i-- { |
| 187 |
sem.Acquire(ctx, 1) |
| 188 |
go func() { |
| 189 |
defer func() { |
| 190 |
sem.Release(1) |
| 191 |
wg.Done() |
| 192 |
}() |
| 193 |
for running { |
| 194 |
time.Sleep(1 * time.Millisecond) |
| 195 |
sem.Release(1) |
| 196 |
sem.Acquire(ctx, 1) |
| 197 |
} |
| 198 |
}() |
| 199 |
} |
| 200 |
|
| 201 |
sem.Acquire(ctx, n) |
| 202 |
running = false |
| 203 |
sem.Release(n) |
| 204 |
wg.Wait() |
| 205 |
} |
| 206 |
|
| 207 |
// translated from https://github.com/zhiqiangxu/util/blob/master/mutex/crwmutex_test.go#L43 |
| 208 |
func TestAllocCancelDoesntStarve(t *testing.T) { |
| 209 |
sem := semaphore.NewWeighted(10) |
| 210 |
|
| 211 |
// Block off a portion of the semaphore so that Acquire(_, 10) can eventually succeed. |
| 212 |
sem.Acquire(context.Background(), 1) |
| 213 |
|
| 214 |
// In the background, Acquire(_, 10). |
| 215 |
ctx, cancel := context.WithCancel(context.Background()) |
| 216 |
defer cancel() |
| 217 |
go func() { |
| 218 |
sem.Acquire(ctx, 10) |
| 219 |
}() |
| 220 |
|
| 221 |
// Wait until the Acquire(_, 10) call blocks. |
| 222 |
for sem.TryAcquire(1) { |
| 223 |
sem.Release(1) |
| 224 |
runtime.Gosched() |
| 225 |
} |
| 226 |
|
| 227 |
// Now try to grab a read lock, and simultaneously unblock the Acquire(_, 10) call. |
| 228 |
// Both Acquire calls should unblock and return, in either order. |
| 229 |
go cancel() |
| 230 |
|
| 231 |
err := sem.Acquire(context.Background(), 1) |
| 232 |
if err != nil { |
| 233 |
t.Fatalf("Acquire(_, 1) failed unexpectedly: %v", err) |
| 234 |
} |
| 235 |
sem.Release(1) |
| 236 |
} |
| 237 |
|
| 238 |
func TestWeightedAcquireCanceled(t *testing.T) { |
| 239 |
// https://go.dev/issue/63615 |
| 240 |
sem := semaphore.NewWeighted(2) |
| 241 |
ctx, cancel := context.WithCancel(context.Background()) |
| 242 |
sem.Acquire(context.Background(), 1) |
| 243 |
ch := make(chan struct{}) |
| 244 |
go func() { |
| 245 |
// Synchronize with the Acquire(2) below. |
| 246 |
for sem.TryAcquire(1) { |
| 247 |
sem.Release(1) |
| 248 |
} |
| 249 |
// Now cancel ctx, and then release the token. |
| 250 |
cancel() |
| 251 |
sem.Release(1) |
| 252 |
close(ch) |
| 253 |
}() |
| 254 |
// Since the context being closed happens before enough tokens become available, |
| 255 |
// this Acquire must fail. |
| 256 |
if err := sem.Acquire(ctx, 2); err != context.Canceled { |
| 257 |
t.Errorf("Acquire with canceled context returned wrong error: want context.Canceled, got %v", err) |
| 258 |
} |
| 259 |
// There must always be two tokens in the semaphore after the other |
| 260 |
// goroutine releases the one we held at the start. |
| 261 |
<-ch |
| 262 |
if !sem.TryAcquire(2) { |
| 263 |
t.Fatal("TryAcquire after canceled Acquire failed") |
| 264 |
} |
| 265 |
// Additionally verify that we don't acquire with a done context even when |
| 266 |
// we wouldn't need to block to do so. |
| 267 |
sem.Release(2) |
| 268 |
if err := sem.Acquire(ctx, 1); err != context.Canceled { |
| 269 |
t.Errorf("Acquire with canceled context returned wrong error: want context.Canceled, got %v", err) |
| 270 |
} |
| 271 |
} |