-
Notifications
You must be signed in to change notification settings - Fork 1
/
task_test.go
73 lines (64 loc) · 1.6 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
package asyncp
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func TestTask(t *testing.T) {
var (
result int
ctx = context.Background()
event = WithPayload("test", int(1))
testTask = func(_ context.Context, event Event, rw ResponseWriter) error {
var i int
if err := event.Payload().Decode(&i); err != nil {
return err
}
if i >= 2 {
return rw.WriteResonse(i + 1)
}
return rw.RepeatWithResponse(i + 1)
}
failoverTask = func(_ context.Context, event Event, rw ResponseWriter) error {
return event.Payload().Decode(&result)
}
mux = NewTaskMux(
WithMainExecContext(ctx),
WithResponseFactory(NewProxyResponseFactory()),
)
)
_ = mux.Failver(failoverTask)
mux.Handle("test", testTask)
err := mux.ExecuteEvent(event)
assert.NoError(t, err)
assert.Equal(t, int(3), result)
}
func TestExtFuncTask(t *testing.T) {
type item struct {
Text string `json:"text"`
}
var (
res = ""
ctx = context.Background()
event1 = WithPayload("test1", item{Text: "test1"})
event2 = WithPayload("test2", item{Text: "test2"})
mux = NewTaskMux(
WithMainExecContext(ctx),
WithResponseFactory(NewProxyResponseFactory()),
)
)
mux.Handle("test1", func(it *item) (*item, error) {
res = it.Text
return nil, nil
})
mux.Handle("test2", func(ctx context.Context, it *item, event Event, rw ResponseWriter) (*item, error) {
res = it.Text
return it, nil
})
err := mux.ExecuteEvent(event1)
assert.NoError(t, err)
assert.Equal(t, "test1", res)
err = mux.ExecuteEvent(event2)
assert.NoError(t, err)
assert.Equal(t, "test2", res)
}