-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtask_scheduler_test.go
57 lines (47 loc) · 1.41 KB
/
task_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
// Copyright (c) 2022, R.I. Pienaar and the Project contributors
//
// SPDX-License-Identifier: Apache-2.0
package asyncjobs
import (
"context"
"log"
"sync"
"time"
"github.com/nats-io/jsm.go"
"github.com/nats-io/nats.go"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("TaskScheduler", func() {
var (
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
)
BeforeEach(func() {
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
})
AfterEach(func() { cancel() })
Describe("Run", func() {
It("Should function", func() {
withJetStream(func(nc *nats.Conn, mgr *jsm.Manager) {
client, err := NewClient(NatsConn(nc))
Expect(err).ToNot(HaveOccurred())
task, _ := NewTask("ginko:test", nil)
Expect(client.NewScheduledTask("ginkgo", "@every 1s", "DEFAULT", task)).ToNot(HaveOccurred())
scheduler, err := NewTaskScheduler("ginkgo", client)
Expect(err).ToNot(HaveOccurred())
scheduler.skipLeaderElection = true
Expect(scheduler.Run(ctx, &wg)).ToNot(HaveOccurred())
scheduler.Stop()
tasks, err := client.StorageAdmin().TasksInfo()
Expect(err).ToNot(HaveOccurred())
log.Printf("msgs: %d", tasks.Stream.State.Msgs)
Expect(tasks.Stream.State.Msgs).To( // depends when the test is started exactly
And(
BeNumerically(">=", uint64(4)),
BeNumerically("<=", uint64(6))))
})
})
})
})