gobot.io/x/gobot/v2@v2.1.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  		counter := 0
    64  
    65  		rw := robot.Every(context.Background(), time.Millisecond*100, func() {
    66  			counter++
    67  		})
    68  
    69  		time.Sleep(time.Millisecond * 225)
    70  		rw.CallCancelFunc()
    71  
    72  		robot.WorkEveryWaitGroup.Wait()
    73  
    74  		assert.Equal(t, 2, counter)
    75  		postDeleteKeys := collectStringKeysFromWorkRegistry(robot.workRegistry)
    76  		assert.NotContains(t, postDeleteKeys, rw.id.String())
    77  	})
    78  
    79  	t.Run("After with cancel", func(t *testing.T) {
    80  		robot := NewRobot("testbot")
    81  
    82  		rw := robot.After(context.Background(), time.Millisecond*10, func() {
    83  			_ = 1 + 1 // perform mindless computation!
    84  		})
    85  
    86  		rw.CallCancelFunc()
    87  
    88  		robot.WorkAfterWaitGroup.Wait()
    89  
    90  		postDeleteKeys := collectStringKeysFromWorkRegistry(robot.workRegistry)
    91  		assert.NotContains(t, postDeleteKeys, rw.id.String())
    92  	})
    93  }
    94  
    95  func collectStringKeysFromWorkRegistry(rwr *RobotWorkRegistry) []string {
    96  	keys := make([]string, len(rwr.r))
    97  	for k := range rwr.r {
    98  		keys = append(keys, k)
    99  	}
   100  	return keys
   101  }