-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemaphore_test.go
More file actions
56 lines (42 loc) · 1.01 KB
/
semaphore_test.go
File metadata and controls
56 lines (42 loc) · 1.01 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
package async_test
import (
"testing"
"github.com/b97tsk/async"
)
func TestSemaphore(t *testing.T) {
t.Run("Bug-1", func(t *testing.T) {
var myExecutor async.Executor
myExecutor.Autorun(myExecutor.Run)
sema := async.NewSemaphore(1)
myExecutor.Spawn(async.Select(
async.Block(
sema.Acquire(1),
sema.Acquire(1),
),
async.Do(func() { sema.Release(1) }),
))
if !sema.TryAcquire(1) {
t.Fatal("TryAcquire did not succeed when there are no waiters.")
}
})
t.Run("Bug-2", func(t *testing.T) {
var myExecutor async.Executor
myExecutor.Autorun(myExecutor.Run)
sema := async.NewSemaphore(10)
var sig async.Signal
myExecutor.Spawn(async.Select(
async.Await(&sig),
async.Block(
sema.Acquire(1),
sema.Acquire(10),
),
))
if sema.TryAcquire(1) {
t.Fatal("TryAcquire should not succeed when there are waiters.")
}
myExecutor.Spawn(async.Do(sig.Notify))
if !sema.TryAcquire(1) {
t.Fatal("TryAcquire did not succeed when there are no waiters.")
}
})
}