Repository navigation
Expand file tree
/
Copy pathintegration_test.go
More file actions
100 lines (84 loc) · 2.22 KB
/
Copy pathintegration_test.go
File metadata and controls
100 lines (84 loc) · 2.22 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
package scheduler
import (
"context"
"strings"
"sync"
"testing"
"time"
)
// mockSender records the most recent Send call.
type mockSender struct {
mu sync.Mutex
target string
text string
called chan struct{}
once sync.Once
}
func newMockSender() *mockSender {
return &mockSender{called: make(chan struct{})}
}
func (m *mockSender) Send(_ context.Context, target, text string) error {
m.mu.Lock()
m.target = target
m.text = text
m.mu.Unlock()
m.once.Do(func() { close(m.called) })
return nil
}
func (m *mockSender) Name() string { return "tg" }
// helloExecutor always returns "hello" as output.
type helloExecutor struct{}
func (helloExecutor) Execute(_ context.Context, job *Job) (*JobResult, error) {
return &JobResult{
Status: StatusSuccess,
Output: "hello from job " + job.Name,
ChannelKey: job.ChannelKey,
}, nil
}
// Scenario X: TestRuntime_ScheduledJob_DeliversToTelegram — a scheduled job
// fires, its output is routed through RouterDelivery to a mock Telegram
// sender, and the sender receives the expected target and text.
func TestRuntime_ScheduledJob_DeliversToTelegram(t *testing.T) {
t.Parallel()
tg := newMockSender()
delivery := NewRouterDelivery(
map[string]MessageSender{"tg": tg},
nil,
)
store := NewInMemoryJobStore()
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
defer cancel()
_ = store.Save(ctx, &Job{
ID: mustID("01HZY0CWD0A0VKBQHHP3MS5GC0"),
Name: "greet",
Enabled: true,
ExecutorType: "hello",
ChannelKey: "tg:42",
Prompt: "say hello",
Schedule: Every(50 * time.Millisecond),
State: JobState{NextRun: time.Now()},
})
s := New(Config{
Store: store,
MaxConcurrent: 2,
PollInterval: 30 * time.Millisecond,
Executors: map[string]JobExecutor{"hello": helloExecutor{}},
}, WithDelivery(delivery))
go s.Start(ctx)
select {
case <-tg.called:
case <-ctx.Done():
t.Fatal("timed out waiting for Telegram delivery")
}
s.Stop()
tg.mu.Lock()
gotTarget := tg.target
gotText := tg.text
tg.mu.Unlock()
if gotTarget != "42" {
t.Fatalf("target = %q; want 42", gotTarget)
}
if !strings.Contains(gotText, "hello") {
t.Fatalf("text = %q; want to contain 'hello'", gotText)
}
}