master
go 58 lines 1 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package vsphere
4
5 import (
6 "sync/atomic"
7 "testing"
8 "time"
9
10 "github.com/stretchr/testify/assert"
11 )
12
13 func Test_task(t *testing.T) {
14 var i int64
15 job := func() {
16 atomic.AddInt64(&i, 1)
17 }
18
19 task := newTask(job, time.Millisecond)
20 defer func() {
21 task.stop()
22 task.wait()
23 }()
24
25 assert.Eventually(t, func() bool {
26 return atomic.LoadInt64(&i) > 0
27 }, time.Second, time.Millisecond)
28 }
29
30 func Test_task_state(t *testing.T) {
31 tests := map[string]struct {
32 state func(*task) bool
33 wantBefore bool
34 wantAfter bool
35 }{
36 "is stopped": {
37 state: (*task).isStopped,
38 wantBefore: false,
39 wantAfter: true,
40 },
41 "is running": {
42 state: (*task).isRunning,
43 wantBefore: true,
44 wantAfter: false,
45 },
46 }
47
48 for name, tc := range tests {
49 t.Run(name, func(t *testing.T) {
50 task := newTask(func() {}, time.Second)
51 assert.Equal(t, tc.wantBefore, tc.state(task))
52
53 task.stop()
54 task.wait()
55 assert.Equal(t, tc.wantAfter, tc.state(task))
56 })
57 }
58 }