MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / TestTasks

Function TestTasks

pkg/component/tasks_test.go:30–93  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

28)
29
30func TestTasks(t *testing.T) {
31 a := assertions.New(t)
32 ctx := test.Context()
33
34 c, err := New(test.GetLogger(t), &Config{})
35 a.So(err, should.BeNil)
36
37 // Register a one-off task.
38 oneOffWg := sync.WaitGroup{}
39 oneOffWg.Add(1)
40 c.RegisterTask(&task.Config{
41 Context: ctx,
42 ID: "one_off",
43 Func: func(_ context.Context) error {
44 oneOffWg.Done()
45 return nil
46 },
47 Restart: task.RestartNever,
48 Backoff: TestTaskBackoffConfig,
49 })
50 // Register an always restarting task.
51 restartingWg := sync.WaitGroup{}
52 restartingWg.Add(5)
53 i := 0
54 c.RegisterTask(&task.Config{
55 Context: ctx,
56 ID: "restarts",
57 Func: func(_ context.Context) error {
58 i++
59 if i <= 5 {
60 restartingWg.Done()
61 }
62 return nil
63 },
64 Restart: task.RestartAlways,
65 Backoff: TestTaskBackoffConfig,
66 })
67
68 // Register a task that restarts on failure.
69 failingWg := sync.WaitGroup{}
70 failingWg.Add(1)
71 j := 0
72 c.RegisterTask(&task.Config{
73 Context: ctx,
74 ID: "restarts_on_failure",
75 Func: func(_ context.Context) error {
76 j++
77 if j < 5 {
78 return errors.New("failed")
79 }
80 failingWg.Done()
81 return nil
82 },
83 Restart: task.RestartOnFailure,
84 Backoff: TestTaskBackoffConfig,
85 })
86
87 // Wait for all invocations.

Callers

nothing calls this directly

Calls 11

ContextFunction · 0.92
GetLoggerFunction · 0.92
NewFunction · 0.92
RegisterTaskMethod · 0.80
DoneMethod · 0.80
NewFunction · 0.70
NewMethod · 0.65
AddMethod · 0.65
CloseMethod · 0.65
WaitMethod · 0.65
StartMethod · 0.45

Tested by

no test coverage detected