-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob_test.go
71 lines (57 loc) · 1.47 KB
/
job_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
package sqlq_test
import (
"bytes"
"encoding/json"
"fmt"
. "github.com/mergestat/sqlq"
"testing"
)
func TestJob_ResultWriter(t *testing.T) {
type Result = struct{ Msg string }
var err error
var upstream = MustOpen(PostgresUrl)
defer upstream.Close()
if _, err = Enqueue(upstream, "test/job", NewJobDesc("job/result-writer")); err != nil {
t.Fatal(err)
}
var job *Job
if job, err = Dequeue(upstream, []Queue{"test/job"}); err != nil {
t.Fatal(err)
}
var writer = AttachResultWriter(upstream, job).ResultWriter()
if err = json.NewEncoder(writer).Encode(&Result{Msg: "hello"}); err != nil {
t.Fatal(err)
}
if err = writer.Close(); err != nil {
t.Fatal(err)
}
var r Result
if err = json.NewDecoder(bytes.NewReader(job.Result)).Decode(&r); err != nil {
t.Fatal(err)
}
if r.Msg != "hello" {
t.FailNow()
}
}
func TestJob_Logger(t *testing.T) {
var testingBackend = func(t *testing.T) LogBackendAdapter {
return func(job *Job, level LogLevel, p string) (int, error) {
t.Helper()
var msg = fmt.Sprintf("level=%s\tjob=%d\tmsg=%s", level, job.ID, p)
t.Log(msg)
return len(msg), nil
}
}
var err error
var upstream = MustOpen(PostgresUrl)
defer upstream.Close()
if _, err = Enqueue(upstream, "test/job", NewJobDesc("job/logger")); err != nil {
t.Fatal(err)
}
var job *Job
if job, err = Dequeue(upstream, []Queue{"test/job"}); err != nil {
t.Fatal(err)
}
job = AttachLogger(testingBackend(t), job)
job.Logger().Infof("hello %q", "world")
}