Skip to content

Commit decde4c

Browse files
authored
test: rich yomo pkg unittest (#606)
1 parent 084b751 commit decde4c

4 files changed

Lines changed: 78 additions & 13 deletions

File tree

options.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ var (
9797
}
9898
}
9999

100-
// WithUptreamOption provides upstream zipper options for Zipper.
101-
WithUptreamOption = func(opts ...ClientOption) ZipperOption {
100+
// WithUpstreamOption provides upstream zipper options for Zipper.
101+
WithUpstreamOption = func(opts ...ClientOption) ZipperOption {
102102
return func(o *zipperOptions) {
103103
o.clientOption = opts
104104
}

sfn_test.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,47 @@ package yomo
22

33
import (
44
"testing"
5+
"time"
56

67
"github.com/stretchr/testify/assert"
8+
"github.com/yomorun/yomo/core"
9+
"github.com/yomorun/yomo/core/ylog"
10+
"github.com/yomorun/yomo/serverless"
711
)
812

9-
func TestSfnConnectToServer(t *testing.T) {
13+
func TestStreamFunction(t *testing.T) {
14+
t.Parallel()
15+
1016
sfn := NewStreamFunction(
11-
"sfn-ai-stream-response",
17+
"sfn-async-log-events",
1218
"localhost:9000",
1319
WithSfnCredential("token:<CREDENTIAL>"),
20+
WithSfnLogger(ylog.Default()),
21+
WithSfnQuicConfig(core.DefalutQuicConfig),
22+
WithSfnTLSConfig(nil),
1423
)
15-
sfn.SetObserveDataTags(0x33)
16-
defer sfn.Close()
24+
sfn.SetObserveDataTags(0x21)
25+
26+
time.AfterFunc(time.Second, func() {
27+
sfn.Close()
28+
})
29+
30+
// set error handler
31+
sfn.SetErrorHandler(func(err error) {})
1732

1833
// set handler
19-
sfn.SetHandler(nil)
34+
sfn.SetHandler(func(ctx serverless.Context) {
35+
t.Logf("unittest sfn receive <- (%d)", len(ctx.Data()))
36+
assert.Equal(t, uint32(0x21), ctx.Tag())
37+
assert.Equal(t, []byte("test"), ctx.Data())
38+
ctx.Write(0x22, []byte("backflow"))
39+
})
2040

2141
// connect to server
2242
err := sfn.Connect()
2343
assert.Nil(t, err)
44+
45+
sfn.Wait()
2446
}
2547

2648
func TestSfnInit(t *testing.T) {

source_test.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,51 @@ package yomo
22

33
import (
44
"testing"
5+
"time"
56

67
"github.com/stretchr/testify/assert"
8+
"github.com/yomorun/yomo/core"
9+
"github.com/yomorun/yomo/core/frame"
10+
"github.com/yomorun/yomo/core/ylog"
711
)
812

9-
func TestSourceSendDataToServer(t *testing.T) {
10-
source := NewSource("test-source", "localhost:9000", WithCredential("token:<CREDENTIAL>"))
11-
defer source.Close()
13+
func TestSource(t *testing.T) {
14+
t.Parallel()
1215

13-
// connect to server
16+
source := NewSource(
17+
"test-source",
18+
"localhost:9000",
19+
WithCredential("token:<CREDENTIAL>"),
20+
WithLogger(ylog.Default()),
21+
WithObserveDataTags(0x22),
22+
WithSourceQuicConfig(core.DefalutQuicConfig),
23+
WithSourceTLSConfig(nil),
24+
)
25+
26+
exit := make(chan struct{})
27+
time.AfterFunc(time.Second, func() {
28+
source.Close()
29+
close(exit)
30+
})
31+
32+
source.SetErrorHandler(func(err error) {})
33+
34+
source.SetReceiveHandler(func(tag frame.Tag, data []byte) {
35+
assert.Equal(t, uint32(0x22), tag)
36+
assert.Equal(t, []byte("backflow"), data)
37+
})
38+
39+
// connect to zipper
1440
err := source.Connect()
1541
assert.Nil(t, err)
1642

17-
// send data to server
43+
// send data to zipper
1844
err = source.Write(0x21, []byte("test"))
1945
assert.Nil(t, err)
46+
47+
// broadcast data to zipper
48+
err = source.Broadcast(0x21, []byte("test"))
49+
assert.Nil(t, err)
50+
51+
<-exit
2052
}

zipper_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,21 @@ import (
55
"time"
66

77
"github.com/stretchr/testify/assert"
8+
"github.com/yomorun/yomo/core"
9+
"github.com/yomorun/yomo/core/ylog"
810
)
911

1012
func TestZipperRun(t *testing.T) {
11-
zipper, err := NewZipper("zipper", nil, nil)
13+
zipper, err := NewZipper(
14+
"zipper",
15+
nil,
16+
nil,
17+
// WithAuth("token", "<CREDENTIAL>"),
18+
WithUpstreamOption(core.ClientOption(WithCredential("token:<CREDENTIAL>"))),
19+
WithZipperLogger(ylog.Default()),
20+
WithZipperQuicConfig(core.DefalutQuicConfig),
21+
WithZipperTLSConfig(nil),
22+
)
1223
assert.Nil(t, err)
1324
time.Sleep(time.Second)
1425
assert.NotNil(t, zipper)

0 commit comments

Comments
 (0)