-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoption_test.go
More file actions
87 lines (68 loc) · 1.74 KB
/
Copy pathoption_test.go
File metadata and controls
87 lines (68 loc) · 1.74 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package cron
import (
"context"
"log/slog"
"strings"
"testing"
"time"
)
func TestWithLocation(t *testing.T) {
t.Parallel()
c := New(WithLocation(time.UTC))
if c.location != time.UTC {
t.Errorf("expected UTC, got %v", c.location)
}
}
func TestWithParser(t *testing.T) {
t.Parallel()
parser := NewSpecParser(Dow)
c := New(WithParser(parser))
if c.parser != parser {
t.Error("expected provided parser")
}
}
func TestWithLoggerCapturesSchedulerEvents(t *testing.T) {
t.Parallel()
var buf syncWriter
logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}))
fc := newFakeClock(baseTime)
cron := New(WithLogger(logger), WithClock(fc))
if cron.logger != logger {
t.Error("expected provided logger")
}
mustAddFunc(t, cron, "@every 1s", func(context.Context) error { return nil })
cron.Start(context.Background())
fc.BlockUntilTimers(1)
fc.Advance(1 * time.Second)
time.Sleep(10 * time.Millisecond)
err := cron.Stop(context.Background())
if err != nil {
t.Fatalf("stop: %v", err)
}
out := buf.String()
if !strings.Contains(out, "schedule") || !strings.Contains(out, "run") {
t.Error("expected to see some actions, got:", out)
}
}
func TestNilOptionsPreserveDefaults(t *testing.T) {
t.Parallel()
cronInstance := New(
WithLocation(nil),
WithParser(nil),
WithLogger(nil),
WithClock(nil),
)
if cronInstance.location != time.Local {
t.Errorf("expected default location, got %v", cronInstance.location)
}
if cronInstance.logger == nil {
t.Fatal("expected default logger")
}
if cronInstance.clock == nil {
t.Fatal("expected default clock")
}
_, err := cronInstance.parser.Parse("* * * * *")
if err != nil {
t.Fatalf("expected default parser to remain active: %v", err)
}
}