-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoroutine_test.go
More file actions
50 lines (37 loc) · 1 KB
/
coroutine_test.go
File metadata and controls
50 lines (37 loc) · 1 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
package async_test
import (
"context"
"sync"
"testing"
"time"
"github.com/b97tsk/async"
)
func TestSleep(t *testing.T) {
t.Run("NonCancelable", func(t *testing.T) {
var wg sync.WaitGroup // For keeping track of goroutines.
var myExecutor async.Executor
myExecutor.Autorun(func() { wg.Go(myExecutor.Run) })
n := 0
myExecutor.Spawn(async.Select(
async.Sleep(context.Background(), &wg, 100*time.Millisecond),
async.NonCancelable(async.Block(
async.Sleep(context.Background(), &wg, 200*time.Millisecond),
async.Do(func() { n++ }),
)),
))
wg.Wait()
if n != 1 {
t.Fatalf("want 1, got %v", n)
}
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
myExecutor.Spawn(async.NonCancelable(async.Block(
async.Sleep(ctx, &wg, 200*time.Millisecond), // Still cancelable with a context.Context.
async.Do(func() { n++ }), // Didn't run.
)))
wg.Wait()
if n != 1 {
t.Fatalf("want 1, got %v", n)
}
})
}