-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeout_test.go
More file actions
92 lines (78 loc) · 2.63 KB
/
timeout_test.go
File metadata and controls
92 lines (78 loc) · 2.63 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
package lisp
import (
"context"
"strings"
"testing"
"time"
"github.com/jig/lisp/lib/concurrent"
"github.com/jig/lisp/types"
)
func TestContextTimeoutFiresOnTime(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
if _, err := REPL(ctx, newEnv(t.Name()), `(sleep 1000)`, types.NewCursorFile(t.Name())); err == nil {
t.Fatalf("Must fail")
} else {
if !strings.Contains(err.Error(), "timeout while evaluating expression") {
t.Fatalf("%s != %s", err.Error(), "timeout while evaluating expression")
}
}
}
func TestContextNoTimeout(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
if _, err := REPL(ctx, newEnv(t.Name()), `(sleep 1)`, types.NewCursorFile(t.Name())); err != nil {
t.Fatal(err)
}
}
func TestFutureContextTimeoutFiresOnTime(t *testing.T) {
ctxB := context.Background()
env := newEnv(t.Name())
concurrent.Load(env)
concurrentHeader := concurrent.HeaderConcurrent()
if _, err := REPL(ctxB, env, concurrentHeader, types.NewCursorFile(t.Name())); err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
if _, err := REPL(ctx, newEnv(t.Name()), `@(future (sleep 1000))`, types.NewCursorFile(t.Name())); err == nil {
t.Fatalf("Must fail")
} else {
if !strings.Contains(err.Error(), "timeout while dereferencing future") {
t.Fatalf("%s != %s", err.Error(), "timeout while dereferencing future")
}
}
}
func TestFutureContextNoTimeout(t *testing.T) {
ctxB := context.Background()
env := newEnv(t.Name())
concurrent.Load(env)
concurrentHeader := concurrent.HeaderConcurrent()
if _, err := REPL(ctxB, env, concurrentHeader, types.NewCursorFile(t.Name())); err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 1000*time.Millisecond)
defer cancel()
if _, err := REPL(ctx, newEnv(t.Name()), `@(future (sleep 1))`, types.NewCursorFile(t.Name())); err != nil {
t.Fatal(err)
}
}
func TestTimeoutOnTryCatch(t *testing.T) {
ns := newEnv(t.Name())
ast, err := READ(`(try (sleep 10000) (catch e (str "ERR: " (error-string e))))`, types.NewCursorFile(t.Name()), ns)
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()
res, err := EVAL(ctx, ast, ns)
if err != nil {
if err.Error() == "timeout while evaluating expression" {
t.Fatalf("timeout not catched: %s", err)
}
t.Fatalf("unexpected error caught %s", err)
}
if res.(string) != "ERR: timeout while evaluating expression" {
t.Fatalf("unexpected result %s", res)
}
}