-
Notifications
You must be signed in to change notification settings - Fork 21
/
server_test.go
60 lines (49 loc) · 1.09 KB
/
server_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
package tasqueue
import (
"encoding/json"
"fmt"
"log/slog"
"os"
"testing"
"time"
rb "github.com/kalbhor/tasqueue/v2/brokers/in-memory"
rr "github.com/kalbhor/tasqueue/v2/results/in-memory"
)
const (
taskName = "mock_handler"
)
func newServer(t *testing.T, taskName string, handler func([]byte, JobCtx) error) *Server {
lo := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelError,
}))
srv, err := NewServer(ServerOpts{
Broker: rb.New(),
Results: rr.New(),
Logger: lo.Handler(),
})
if err != nil {
t.Fatal(err)
}
if err := srv.RegisterTask(taskName, handler, TaskOpts{}); err != nil {
t.Fatal(err)
}
return srv
}
type MockPayload struct {
ShouldErr bool
}
func MockHandler(msg []byte, _ JobCtx) error {
var m MockPayload
if err := json.Unmarshal(msg, &m); err != nil {
return err
}
if m.ShouldErr {
return fmt.Errorf("task ended with error")
}
return nil
}
// MockHandlerWithSleep is a mock handler that sleeps for a long time.
func MockHandlerWithSleep(msg []byte, _ JobCtx) error {
time.Sleep(3000 * time.Second)
return nil
}