-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuture_test.go
More file actions
47 lines (43 loc) · 975 Bytes
/
Copy pathfuture_test.go
File metadata and controls
47 lines (43 loc) · 975 Bytes
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
package syncx
import (
"context"
"errors"
"testing"
"time"
)
func TestFutureAwaitSuccess(t *testing.T) {
f := NewFuture(func() (int, error) {
return 42, nil
})
v, err := f.Await(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if v != 42 {
t.Fatalf("got %d want 42", v)
}
}
func TestFutureAwaitRepeated(t *testing.T) {
f := NewFuture(func() (int, error) {
return 42, nil
})
<-f.Done()
for i := 0; i < 3; i++ {
v, err := f.Await(context.Background())
if err != nil || v != 42 {
t.Fatalf("await %d: got (%d, %v), want (42, nil)", i, v, err)
}
}
}
func TestFutureAwaitCanceled(t *testing.T) {
f := NewFuture(func() (int, error) {
time.Sleep(50 * time.Millisecond)
return 1, nil
})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond)
defer cancel()
_, err := f.Await(ctx)
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("expected deadline exceeded, got %v", err)
}
}