File: executor_test.go

package info (click to toggle)
golang-github-go-co-op-gocron 1.18.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 408 kB
  • sloc: makefile: 10
file content (69 lines) | stat: -rw-r--r-- 1,130 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package gocron

import (
	"fmt"
	"sync"
	"testing"

	"github.com/stretchr/testify/assert"
)

func Test_ExecutorExecute(t *testing.T) {
	e := newExecutor()

	wg := &sync.WaitGroup{}
	wg.Add(1)
	go e.start()

	var runState = int64(0)
	e.jobFunctions <- jobFunction{
		name: "test_fn",
		function: func(arg string) {
			assert.Equal(t, arg, "test")
			wg.Done()
		},
		parameters: []interface{}{"test"},
		runState:   &runState,
	}

	wg.Wait()
	e.stop()
}

func Test_ExecutorPanicHandling(t *testing.T) {
	panicHandled := make(chan bool, 1)

	handler := func(jobName string, recoverData interface{}) {
		fmt.Println("PanicHandler called:")
		fmt.Println("panic in " + jobName)
		fmt.Println(recoverData)
		panicHandled <- true
	}

	SetPanicHandler(handler)

	e := newExecutor()

	wg := &sync.WaitGroup{}
	wg.Add(1)
	go e.start()

	var runState = int64(0)
	e.jobFunctions <- jobFunction{
		name: "test_fn",
		function: func() {
			defer wg.Done()
			a := make([]string, 0)
			a[0] = "This will panic"
		},
		parameters: nil,
		runState:   &runState,
	}

	wg.Wait()
	e.stop()

	state := <-panicHandled
	assert.Equal(t, state, true)

}