-
Notifications
You must be signed in to change notification settings - Fork 129
/
sfn_test.go
147 lines (115 loc) · 3.08 KB
/
sfn_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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package yomo
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/yomorun/yomo/core"
"github.com/yomorun/yomo/core/frame"
"github.com/yomorun/yomo/core/ylog"
"github.com/yomorun/yomo/serverless"
)
var (
mockTargetString = "targetString"
)
func TestStreamFunction(t *testing.T) {
t.Parallel()
sfn := NewStreamFunction(
"sfn-async-log-events",
"localhost:9000",
WithSfnCredential("token:<CREDENTIAL>"),
WithSfnLogger(ylog.Default()),
WithSfnQuicConfig(core.DefaultClientQuicConfig),
WithSfnTLSConfig(nil),
)
sfn.SetObserveDataTags(0x21)
time.AfterFunc(time.Second, func() {
sfn.Close()
})
// set error handler
sfn.SetErrorHandler(func(err error) {})
// set handler
sfn.SetHandler(func(ctx serverless.Context) {
t.Logf("unittest sfn receive <- (%d)", len(ctx.Data()))
assert.Equal(t, uint32(0x21), ctx.Tag())
assert.Equal(t, []byte("test"), ctx.Data())
err := ctx.WriteWithTarget(0x22, []byte("message from sfn"), mockTargetString)
assert.Nil(t, err)
})
// connect to server
err := sfn.Connect()
assert.Nil(t, err)
sfn.Wait()
}
func TestPipeStreamFunction(t *testing.T) {
t.Parallel()
sfn := NewStreamFunction("pipe-sfn", "localhost:9000", WithSfnCredential("token:<CREDENTIAL>"))
sfn.SetObserveDataTags(0x23)
time.AfterFunc(time.Second, func() {
sfn.Close()
})
// set cron handler
sfn.SetPipeHandler(func(in <-chan []byte, out chan<- *frame.DataFrame) {
data := <-in
t.Log("unittest pipe sfn receive <-", string(data))
assert.Equal(t, "pipe test", string(data))
out <- &frame.DataFrame{
Tag: 0x22,
Payload: []byte("message from pip sfn"),
}
})
err := sfn.Connect()
assert.Nil(t, err)
sfn.Wait()
}
func TestSfnWantedTarget(t *testing.T) {
t.Parallel()
sfn := NewStreamFunction("sfn-handler", "localhost:9000", WithSfnCredential("token:<CREDENTIAL>"))
sfn.SetObserveDataTags(0x22)
sfn.SetWantedTarget(mockTargetString)
time.AfterFunc(time.Second, func() {
sfn.Close()
})
// set handler
sfn.SetHandler(func(ctx serverless.Context) {
t.Logf("unittest handler sfn receive <- (%d)", len(ctx.Data()))
assert.Equal(t, uint32(0x22), ctx.Tag())
assert.Contains(t, []string{
"message from source",
"message from sfn",
"message from cron sfn",
"message from pip sfn",
}, string(ctx.Data()))
})
err := sfn.Connect()
assert.Nil(t, err)
sfn.Wait()
}
func TestSfnInit(t *testing.T) {
sfn := NewStreamFunction(
"test-sfn",
"localhost:9000",
)
var total int64
err := sfn.Init(func() error {
total++
return nil
})
assert.Nil(t, err)
assert.Equal(t, int64(1), total)
}
func TestSfnCron(t *testing.T) {
t.Parallel()
sfn := NewStreamFunction("sfn-cron", "localhost:9000", WithSfnCredential("token:<CREDENTIAL>"))
time.AfterFunc(time.Second, func() {
sfn.Close()
})
// set cron handler
sfn.SetCronHandler("@every 200ms", func(ctx serverless.CronContext) {
t.Log("unittest cron sfn, time reached")
ctx.Write(0x22, []byte("message from cron sfn"))
ctx.WriteWithTarget(0x22, []byte("message from cron sfn"), mockTargetString)
})
err := sfn.Connect()
assert.Nil(t, err)
sfn.Wait()
}