45 Star 300 Fork 75

GVPYoMo / yomo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
sfn_test.go 1.87 KB
一键复制 编辑 原始数据 按行查看 历史
package yomo
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/yomorun/yomo/core"
"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 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"}, 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)
}
Go
1
https://gitee.com/yomorun/yomo.git
git@gitee.com:yomorun/yomo.git
yomorun
yomo
yomo
master

搜索帮助