gobot.io/x/gobot@v1.16.0/robot_work_test.go (about)

     1  package gobot
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"time"
     8  
     9  	"github.com/gofrs/uuid"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestRobotWork(t *testing.T) {
    14  	id, _ := uuid.NewV4()
    15  
    16  	rw := &RobotWork{
    17  		id:       id,
    18  		kind:     EveryWorkKind,
    19  		function: func() {},
    20  	}
    21  
    22  	duration := time.Second * 1
    23  	ctx, cancelFunc := context.WithCancel(context.Background())
    24  
    25  	rw.ctx = ctx
    26  	rw.cancelFunc = cancelFunc
    27  	rw.duration = duration
    28  
    29  	t.Run("ID()", func(t *testing.T) {
    30  		assert.Equal(t, rw.ID(), id)
    31  	})
    32  
    33  	t.Run("Ticker()", func(t *testing.T) {
    34  		t.Skip()
    35  	})
    36  
    37  	t.Run("Duration()", func(t *testing.T) {
    38  		assert.Equal(t, rw.duration, duration)
    39  	})
    40  }
    41  
    42  func TestRobotWorkRegistry(t *testing.T) {
    43  	robot := NewRobot("testbot")
    44  
    45  	rw := robot.Every(context.Background(), time.Millisecond*250, func() {
    46  		_ = 1 + 1
    47  	})
    48  
    49  	t.Run("Get retrieves", func(t *testing.T) {
    50  		assert.Equal(t, robot.workRegistry.Get(rw.id), rw)
    51  	})
    52  
    53  	t.Run("delete deletes", func(t *testing.T) {
    54  		robot.workRegistry.delete(rw.id)
    55  		postDeleteKeys := collectStringKeysFromWorkRegistry(robot.workRegistry)
    56  		assert.NotContains(t, postDeleteKeys, rw.id.String())
    57  	})
    58  }
    59  
    60  func TestRobotAutomationFunctions(t *testing.T) {
    61  	t.Run("Every with cancel", func(t *testing.T) {
    62  		robot := NewRobot("testbot")
    63  
    64  		rw := robot.Every(context.Background(), time.Millisecond*10, func() {
    65  			_ = 1 + 1 // perform mindless computation!
    66  		})
    67  
    68  		time.Sleep(time.Millisecond * 25)
    69  		rw.CallCancelFunc()
    70  
    71  		robot.WorkEveryWaitGroup.Wait()
    72  
    73  		assert.Equal(t, 2, rw.tickCount)
    74  		postDeleteKeys := collectStringKeysFromWorkRegistry(robot.workRegistry)
    75  		assert.NotContains(t, postDeleteKeys, rw.id.String())
    76  	})
    77  
    78  	t.Run("After with cancel", func(t *testing.T) {
    79  		robot := NewRobot("testbot")
    80  
    81  		rw := robot.After(context.Background(), time.Millisecond*10, func() {
    82  			_ = 1 + 1 // perform mindless computation!
    83  		})
    84  
    85  		rw.CallCancelFunc()
    86  
    87  		robot.WorkAfterWaitGroup.Wait()
    88  
    89  		postDeleteKeys := collectStringKeysFromWorkRegistry(robot.workRegistry)
    90  		assert.NotContains(t, postDeleteKeys, rw.id.String())
    91  	})
    92  }
    93  
    94  func collectStringKeysFromWorkRegistry(rwr *RobotWorkRegistry) []string {
    95  	keys := make([]string, len(rwr.r))
    96  	for k, _ := range rwr.r {
    97  		keys = append(keys, k)
    98  	}
    99  	return keys
   100  }