File: scheduler_worker_test.go

package info (click to toggle)
gitlab-agent 16.1.3-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 6,324 kB
  • sloc: makefile: 175; sh: 52; ruby: 3
file content (49 lines) | stat: -rw-r--r-- 898 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package agent

import (
	"context"
	"testing"
	"time"

	"github.com/golang/mock/gomock"
	"github.com/stretchr/testify/assert"
	"k8s.io/apimachinery/pkg/util/wait"
)

func TestRun(t *testing.T) {
	ctrl := gomock.NewController(t)
	ctx, cancel := context.WithCancel(context.Background())

	var scanHasRun bool
	schedule := NewMockSchedule(ctrl)
	schedule.EXPECT().Next(gomock.Any()).
		AnyTimes().
		DoAndReturn(func(t time.Time) time.Time {
			if !scanHasRun {
				scanHasRun = true
				return t // run now
			}

			// After the first run, never run again (effectively)
			return t.Add(1000 * time.Hour)
		})

	var success bool
	job := &TestJob{
		RunFunc: func() {
			success = true
			cancel()
		},
	}

	w := &schedulerWorker{
		job:     job,
		cadence: schedule,
	}

	var wg wait.Group
	wg.StartWithContext(ctx, w.Run)
	wg.Wait()

	assert.True(t, success, "Calls the job's runFunc and cancels")
}