forked from taylorchu/work
-
Notifications
You must be signed in to change notification settings - Fork 1
/
job_test.go
108 lines (88 loc) · 2.17 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
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
101
102
103
104
105
106
107
108
package work
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestJobDelay(t *testing.T) {
job := NewJob()
job2 := job.Delay(time.Hour)
require.EqualValues(t, 0, job2.CreatedAt.Sub(job.CreatedAt))
require.EqualValues(t, 0, job2.UpdatedAt.Sub(job.UpdatedAt))
require.Equal(t, time.Hour, job2.EnqueuedAt.Sub(job.EnqueuedAt))
}
func TestJobMarshalDiffType(t *testing.T) {
type message1 struct {
Text1 string
Text2 string
Padding int64
}
type message2 struct {
Padding int64
Text1 string
}
type message3 struct {
Padding int64
Text2 string
}
job := NewJob()
err := job.MarshalPayload(message1{Text1: "hello", Text2: "world", Padding: 3})
require.NoError(t, err)
var msg1 message1
err = job.UnmarshalPayload(&msg1)
require.NoError(t, err)
require.Equal(t, "hello", msg1.Text1)
require.Equal(t, "world", msg1.Text2)
require.EqualValues(t, 3, msg1.Padding)
var msg2 message2
err = job.UnmarshalPayload(&msg2)
require.NoError(t, err)
require.Equal(t, "hello", msg2.Text1)
require.EqualValues(t, 3, msg2.Padding)
var msg3 message3
err = job.UnmarshalPayload(&msg3)
require.NoError(t, err)
require.Equal(t, "world", msg3.Text2)
require.EqualValues(t, 3, msg3.Padding)
}
func TestJobMarshalEmbed(t *testing.T) {
type text struct {
Text1 string
Text2 string
}
type message1 struct {
text
Padding int64
}
type message2 struct {
Padding int64
Text1 string
}
type message3 struct {
Padding int64
Text2 string
}
type message4 struct {
Padding2 int64
text
}
job := NewJob()
err := job.MarshalPayload(message1{text: text{Text1: "hello", Text2: "world"}, Padding: 3})
require.NoError(t, err)
var msg2 message2
err = job.UnmarshalPayload(&msg2)
require.NoError(t, err)
require.Equal(t, "hello", msg2.Text1)
require.EqualValues(t, 3, msg2.Padding)
var msg3 message3
err = job.UnmarshalPayload(&msg3)
require.NoError(t, err)
require.Equal(t, "world", msg3.Text2)
require.EqualValues(t, 3, msg3.Padding)
var msg4 message4
err = job.UnmarshalPayload(&msg4)
require.NoError(t, err)
require.Equal(t, "hello", msg4.Text1)
require.Equal(t, "world", msg4.Text2)
require.EqualValues(t, 0, msg4.Padding2)
}