forked from ry/v8worker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_test.go
84 lines (72 loc) · 1.41 KB
/
worker_test.go
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
package v8worker
import (
"testing"
)
func TestVersion(t *testing.T) {
println(Version())
}
func TestBasic(t *testing.T) {
recvCount := 0
worker := New(func(msg Message) {
println("recv cb", msg)
if msg != "hello" {
t.Fatal("bad msg", msg)
}
recvCount++
})
code := ` $print("ready"); `
err := worker.Load("code.js", code)
if err != nil {
t.Fatal(err)
}
codeWithSyntaxError := ` $print(hello world"); `
err = worker.Load("codeWithSyntaxError.js", codeWithSyntaxError)
if err == nil {
t.Fatal("Expected error")
}
//println(err.Error())
codeWithRecv := `
$recv(function(msg) {
$print("recv msg", msg);
});
$print("ready");
`
err = worker.Load("codeWithRecv.js", codeWithRecv)
if err != nil {
t.Fatal(err)
}
worker.Send("hi")
codeWithSend := `
$send("hello");
$send("hello");
`
err = worker.Load("codeWithSend.js", codeWithSend)
if err != nil {
t.Fatal(err)
}
if recvCount != 2 {
t.Fatal("bad recvCount", recvCount)
}
}
func TestMultipleWorkers(t *testing.T) {
recvCount := 0
worker1 := New(func(msg Message) {
println("w1", msg)
recvCount++
})
worker2 := New(func(msg Message) {
println("w2", msg)
recvCount++
})
err := worker1.Load("1.js", `$send("hello1")`)
if err != nil {
t.Fatal(err)
}
err = worker2.Load("2.js", `$send("hello2")`)
if err != nil {
t.Fatal(err)
}
if recvCount != 2 {
t.Fatal("bad recvCount", recvCount)
}
}