-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler_test.go
94 lines (78 loc) · 2.11 KB
/
scheduler_test.go
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
package cron
import (
"context"
"reflect"
"sync"
"testing"
"time"
)
func TestScheduler_check(t *testing.T) {
now := time.Date(2000, 01, 01, 01, 01, 10, 0, time.UTC)
// there are 3 entries
entry1, err := Parse("01 01 01 01 *", time.UTC, "ENTRY_1")
if err != nil {
t.Fatal(err)
}
entry2, err := Parse("01 01 01 01 *", time.UTC, "ENTRY_2")
if err != nil {
t.Fatal(err)
}
entry3, err := Parse("02 01 01 01 *", time.UTC, "ENTRY_2") // this one should not be triggered
if err != nil {
t.Fatal(err)
}
// entry one already triggered
event1 := Event{Entry: entry1, Time: now}
ctx := context.Background()
store := MemStore{}
store.AddEntry(ctx, entry1)
store.AddEntry(ctx, entry2)
store.AddEntry(ctx, entry3)
store.AddEvent(ctx, event1)
// there are 2 scheduler
var triggered1 []string
handler1 := func(e Entry) {
triggered1 = append(triggered1, e.Name)
}
scheduler1 := NewScheduler(handler1, &store)
var triggered2 []string
handler2 := func(e Entry) {
triggered2 = append(triggered2, e.Name)
}
scheduler2 := NewScheduler(handler2, &store)
// scheduler1 run before2. scheduler1 should be the only one who triggers events
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
err := scheduler1.check(ctx, now)
if err != nil {
t.Errorf("scheduler1 got error: %v", err)
}
}()
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(10 * time.Millisecond)
err := scheduler2.check(ctx, now)
if err != nil {
t.Errorf("scheduler2 got error: %v", err)
}
}()
wg.Wait()
if got, want := len(store.events), 2; got != want {
t.Errorf("got %d events want %d", got, want)
}
if got, want := store.events[0].Entry.Name, "ENTRY_1"; got != want {
t.Errorf("got entry 1 name %q want %q", got, want)
}
if got, want := store.events[1].Entry.Name, "ENTRY_2"; got != want {
t.Errorf("got entry 2 name %q want %q", got, want)
}
if got, want := triggered1, []string{"ENTRY_2"}; !reflect.DeepEqual(got, want) {
t.Errorf("got triggered1 %v want %v", got, want)
}
if got, want := len(triggered2), 0; !reflect.DeepEqual(got, want) {
t.Errorf("got length triggered2 %d want %d", got, want)
}
}