代码拉取完成,页面将自动刷新
package gobot
import (
"context"
"testing"
"time"
"github.com/gobuffalo/uuid"
"github.com/stretchr/testify/assert"
)
func TestRobotWork(t *testing.T) {
id, _ := uuid.NewV4()
rw := &RobotWork{
id: id,
kind: EveryWorkKind,
function: func() {},
}
duration := time.Second * 1
ctx, cancelFunc := context.WithCancel(context.Background())
rw.ctx = ctx
rw.cancelFunc = cancelFunc
rw.duration = duration
t.Run("ID()", func(t *testing.T) {
assert.Equal(t, rw.ID(), id)
})
t.Run("Ticker()", func(t *testing.T) {
t.Skip()
})
t.Run("Duration()", func(t *testing.T) {
assert.Equal(t, rw.duration, duration)
})
}
func TestRobotWorkRegistry(t *testing.T) {
robot := NewRobot("testbot")
rw := robot.Every(context.Background(), time.Millisecond*250, func() {
_ = 1 + 1
})
t.Run("Get retrieves", func(t *testing.T) {
assert.Equal(t, robot.workRegistry.Get(rw.id), rw)
})
t.Run("delete deletes", func(t *testing.T) {
robot.workRegistry.delete(rw.id)
postDeleteKeys := collectStringKeysFromWorkRegistry(robot.workRegistry)
assert.NotContains(t, postDeleteKeys, rw.id.String())
})
}
func TestRobotAutomationFunctions(t *testing.T) {
t.Run("Every with cancel", func(t *testing.T) {
robot := NewRobot("testbot")
rw := robot.Every(context.Background(), time.Millisecond*10, func() {
_ = 1 + 1 // perform mindless computation!
})
time.Sleep(time.Millisecond * 25)
rw.CallCancelFunc()
robot.WorkEveryWaitGroup.Wait()
assert.Equal(t, 2, rw.tickCount)
postDeleteKeys := collectStringKeysFromWorkRegistry(robot.workRegistry)
assert.NotContains(t, postDeleteKeys, rw.id.String())
})
t.Run("After with cancel", func(t *testing.T) {
robot := NewRobot("testbot")
rw := robot.After(context.Background(), time.Millisecond*10, func() {
_ = 1 + 1 // perform mindless computation!
})
rw.CallCancelFunc()
robot.WorkAfterWaitGroup.Wait()
postDeleteKeys := collectStringKeysFromWorkRegistry(robot.workRegistry)
assert.NotContains(t, postDeleteKeys, rw.id.String())
})
}
func collectStringKeysFromWorkRegistry(rwr *RobotWorkRegistry) []string {
keys := make([]string, len(rwr.r))
for k, _ := range rwr.r {
keys = append(keys, k)
}
return keys
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。