Skip to content

Commit

Permalink
Add some tests for RobotWork management
Browse files Browse the repository at this point in the history
  • Loading branch information
trevrosen authored and deadprogram committed May 22, 2019
1 parent f5b738f commit b23015d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions robot_work.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func (r *Robot) Every(ctx context.Context, d time.Duration, f func()) *RobotWork
select {
case <-rw.ctx.Done():
r.workRegistry.delete(rw.id)
rw.ticker.Stop()
break EVERYWORK
case <-rw.ticker.C:
rw.tickCount++
Expand Down
66 changes: 66 additions & 0 deletions robot_work_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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 collectStringKeysFromWorkRegistry(rwr *RobotWorkRegistry) []string {
keys := make([]string, len(rwr.r))
for k, _ := range rwr.r {
keys = append(keys, k)
}
return keys
}

0 comments on commit b23015d

Please sign in to comment.