-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_test.go
More file actions
127 lines (102 loc) · 2.83 KB
/
task_test.go
File metadata and controls
127 lines (102 loc) · 2.83 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package core_test
import (
"context"
"sync"
"testing"
"time"
. "dappco.re/go/core"
"github.com/stretchr/testify/assert"
)
// --- PerformAsync ---
func TestTask_PerformAsync_Good(t *testing.T) {
c := New()
var mu sync.Mutex
var result string
c.Action("work", func(_ context.Context, _ Options) Result {
mu.Lock()
result = "done"
mu.Unlock()
return Result{Value: "done", OK: true}
})
r := c.PerformAsync("work", NewOptions())
assert.True(t, r.OK)
assert.True(t, HasPrefix(r.Value.(string), "id-"), "should return task ID")
time.Sleep(100 * time.Millisecond)
mu.Lock()
assert.Equal(t, "done", result)
mu.Unlock()
}
func TestTask_PerformAsync_Good_Progress(t *testing.T) {
c := New()
c.Action("tracked", func(_ context.Context, _ Options) Result {
return Result{OK: true}
})
r := c.PerformAsync("tracked", NewOptions())
taskID := r.Value.(string)
c.Progress(taskID, 0.5, "halfway", "tracked")
}
func TestTask_PerformAsync_Good_Completion(t *testing.T) {
c := New()
completed := make(chan ActionTaskCompleted, 1)
c.Action("completable", func(_ context.Context, _ Options) Result {
return Result{Value: "output", OK: true}
})
c.RegisterAction(func(_ *Core, msg Message) Result {
if evt, ok := msg.(ActionTaskCompleted); ok {
completed <- evt
}
return Result{OK: true}
})
c.PerformAsync("completable", NewOptions())
select {
case evt := <-completed:
assert.True(t, evt.Result.OK)
assert.Equal(t, "output", evt.Result.Value)
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for completion")
}
}
func TestTask_PerformAsync_Bad_ActionNotRegistered(t *testing.T) {
c := New()
completed := make(chan ActionTaskCompleted, 1)
c.RegisterAction(func(_ *Core, msg Message) Result {
if evt, ok := msg.(ActionTaskCompleted); ok {
completed <- evt
}
return Result{OK: true}
})
c.PerformAsync("nonexistent", NewOptions())
select {
case evt := <-completed:
assert.False(t, evt.Result.OK, "unregistered action should fail")
case <-time.After(2 * time.Second):
t.Fatal("timed out")
}
}
func TestTask_PerformAsync_Bad_AfterShutdown(t *testing.T) {
c := New()
c.Action("work", func(_ context.Context, _ Options) Result { return Result{OK: true} })
c.ServiceStartup(context.Background(), nil)
c.ServiceShutdown(context.Background())
r := c.PerformAsync("work", NewOptions())
assert.False(t, r.OK)
}
// --- RegisterAction + RegisterActions (broadcast handlers) ---
func TestTask_RegisterAction_Good(t *testing.T) {
c := New()
called := false
c.RegisterAction(func(_ *Core, _ Message) Result {
called = true
return Result{OK: true}
})
c.ACTION(nil)
assert.True(t, called)
}
func TestTask_RegisterActions_Good(t *testing.T) {
c := New()
count := 0
h := func(_ *Core, _ Message) Result { count++; return Result{OK: true} }
c.RegisterActions(h, h)
c.ACTION(nil)
assert.Equal(t, 2, count)
}