Repository navigation
Expand file tree
/
Copy pathentry_test.go
More file actions
125 lines (114 loc) · 3.14 KB
/
Copy pathentry_test.go
File metadata and controls
125 lines (114 loc) · 3.14 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package cron_test
import (
"context"
"log/slog"
"testing"
"time"
"github.com/libtnb/cron"
)
func TestEntryID_StringAndLogValue(t *testing.T) {
id := cron.EntryID(42)
if got := id.String(); got != "42" {
t.Fatalf("String=%q", got)
}
if got := id.LogValue().String(); got != "42" {
t.Fatalf("LogValue=%v", got)
}
}
func TestEntry_ValidAndLogValue(t *testing.T) {
if (cron.Entry{}).Valid() {
t.Fatal("zero Entry must be invalid")
}
e := cron.Entry{ID: 1, Name: "n", Spec: "@every 1m"}
if !e.Valid() {
t.Fatal("Valid")
}
if e.LogValue().Kind() != slog.KindGroup {
t.Fatal("LogValue kind")
}
full := cron.Entry{
ID: 2,
Name: "n",
Spec: "@every 1m",
Prev: time.Now().Add(-time.Hour),
Next: time.Now().Add(time.Hour),
Paused: true,
}
if full.LogValue().Kind() != slog.KindGroup {
t.Fatal("full LogValue kind")
}
}
func TestEntryInfoFromContext(t *testing.T) {
if _, ok := cron.EntryInfoFromContext(context.Background()); ok {
t.Fatal("plain context must carry no EntryInfo")
}
c := cron.MustNew(cron.WithLocation(time.UTC))
got := make(chan cron.EntryInfo, 1)
id, _ := c.AddSchedule(cron.TriggeredSchedule(), cron.JobFunc(func(ctx context.Context) error {
info, ok := cron.EntryInfoFromContext(ctx)
if !ok {
t.Error("job context must carry EntryInfo")
}
got <- info
return nil
}), cron.WithName("who-am-i"))
_ = c.Start()
defer func() { _ = c.Stop(context.Background()) }()
if err := c.TriggerAndWait(context.Background(), id); err != nil {
t.Fatal(err)
}
info := <-got
if info.ID != id || info.Name != "who-am-i" || info.ScheduledAt.IsZero() {
t.Fatalf("EntryInfo = %+v", info)
}
}
func TestEntries_OrderedWithMixedNext(t *testing.T) {
c := cron.MustNew()
idA, _ := c.Add("@every 1m", cron.JobFunc(func(ctx context.Context) error { return nil }), cron.WithName("A"))
_, _ = c.AddSchedule(cron.TriggeredSchedule(),
cron.JobFunc(func(ctx context.Context) error { return nil }), cron.WithName("Z-zero-next"))
idC, _ := c.Add("@every 5m", cron.JobFunc(func(ctx context.Context) error { return nil }), cron.WithName("C"))
var names []string
for e := range c.Entries() {
names = append(names, e.Name)
}
if len(names) != 3 {
t.Fatalf("entries=%v", names)
}
if names[2] != "Z-zero-next" {
t.Fatalf("zero-Next entry should sort last, got %v", names)
}
_ = idA
_ = idC
}
func TestEntries_BothZeroNextStableOrder(t *testing.T) {
c := cron.MustNew()
_, _ = c.AddSchedule(cron.TriggeredSchedule(),
cron.JobFunc(func(ctx context.Context) error { return nil }), cron.WithName("a"))
_, _ = c.AddSchedule(cron.TriggeredSchedule(),
cron.JobFunc(func(ctx context.Context) error { return nil }), cron.WithName("b"))
count := 0
for range c.Entries() {
count++
}
if count != 2 {
t.Fatalf("count=%d", count)
}
}
func TestEntries_EarlyBreakStopsIteration(t *testing.T) {
c := cron.MustNew()
for i := range 5 {
_, _ = c.Add("@every 1m", cron.JobFunc(func(ctx context.Context) error { return nil }),
cron.WithName(string(rune('a'+i))))
}
count := 0
for range c.Entries() {
count++
if count == 2 {
break
}
}
if count != 2 {
t.Fatalf("early break didn't stop iteration: count=%d", count)
}
}