forked from YaoApp/gou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_test.go
82 lines (68 loc) · 1.83 KB
/
task_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
package gou
import (
"fmt"
"os"
"path"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/yaoapp/gou/task"
"github.com/yaoapp/kun/utils"
)
func TestLoadTask(t *testing.T) {
taskLoad(t)
assert.Equal(t, 1, len(task.Tasks))
assert.NotPanics(t, func() { SelectTask("mail") })
}
func TestTaskProcess(t *testing.T) {
mail := taskLoad(t)
go mail.Start()
defer mail.Stop()
id, err := NewProcess("tasks.mail.Add", "[email protected]").Exec()
if err != nil {
t.Fatal(err)
}
id2, err := NewProcess("tasks.mail.Add", "[email protected]", 1).Exec()
if err != nil {
t.Fatal(err)
}
// time.Sleep(200 * time.Millisecond)
s1, err := NewProcess("tasks.mail.Get", id).Exec()
// if err != nil {
// t.Fatal(err)
// }
time.Sleep(50 * time.Millisecond)
s2, err := NewProcess("tasks.mail.Get", id).Exec()
// if err != nil {
// t.Fatal(err)
// }
// time.Sleep(500 * time.Millisecond)
utils.Dump(s1, s2, id, id2)
assert.Equal(t, 1025, id)
assert.Equal(t, 1026, id2)
// assert.Equal(t, "RUNNING", s1.(map[string]interface{})["status"])
// assert.Equal(t, "RUNNING", s2.(map[string]interface{})["status"])
// assert.Equal(t, 3, s2.(map[string]interface{})["total"])
// assert.Equal(t, "unit-test", s2.(map[string]interface{})["message"])
// waitting
time.Sleep(3000 * time.Millisecond)
}
func taskLoad(t *testing.T) *task.Task {
RegisterProcessHandler("xiang.system.Sleep", func(process *Process) interface{} {
process.ValidateArgNums(1)
ms := process.ArgsInt(0)
time.Sleep(time.Duration(ms) * time.Millisecond)
return nil
})
root := os.Getenv("GOU_TEST_APP_ROOT")
_, err := LoadTask("file://"+path.Join(root, "tasks", "mail.task.json"), "mail")
if err != nil {
t.Fatal(err)
}
err = Yao.Load(path.Join(root, "scripts", "mail.js"), "mail")
if err != nil {
fmt.Println(err)
t.Fatal(err)
}
return SelectTask("mail")
}