Skip to content

Commit ede144b

Browse files
committed
chore(testing): add encode and decode pattern.
1 parent c1a27f5 commit ede144b

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

Diff for: Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
build:
1+
testing:
22
go test -v -run=^$ -benchmem -count=2 -bench .

Diff for: go.mod

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,13 @@ go 1.19
44

55
require github.com/golang-queue/queue v0.1.4-0.20230129131029-321f27ee1387
66

7-
require github.com/goccy/go-json v0.10.0
7+
require (
8+
github.com/goccy/go-json v0.10.0
9+
github.com/stretchr/testify v1.8.1
10+
)
11+
12+
require (
13+
github.com/davecgh/go-spew v1.1.1 // indirect
14+
github.com/pmezard/go-difflib v1.0.0 // indirect
15+
gopkg.in/yaml.v3 v3.0.1 // indirect
16+
)

Diff for: go.sum

+13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
24
github.com/goccy/go-json v0.10.0 h1:mXKd9Qw4NuzShiRlOXKews24ufknHO7gx30lsDyokKA=
35
github.com/goccy/go-json v0.10.0/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
46
github.com/golang-queue/queue v0.1.4-0.20230129131029-321f27ee1387 h1:qAj1tqpV3BW7Ll4N9xM4L+sIPeJDH3dulJ6s4txt8Yg=
57
github.com/golang-queue/queue v0.1.4-0.20230129131029-321f27ee1387/go.mod h1:iFgyvS8kKQ3Mh+diQ6ZqF6yFaYFm6gA8W0NW2CMQ8KI=
68
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
79
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
10+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
11+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
12+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
13+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
14+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
15+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
816
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
17+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
918
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
19+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
20+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
21+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1022
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
23+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

Diff for: serialize_test.go

+31-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/golang-queue/queue/job"
88

99
"github.com/goccy/go-json"
10+
"github.com/stretchr/testify/assert"
1011
)
1112

1213
type mockMessage struct {
@@ -54,7 +55,7 @@ func BenchmarkDecode(b *testing.B) {
5455

5556
b.Run("JSON", func(b *testing.B) {
5657
data, _ := json.Marshal(m)
57-
var out *job.Message
58+
out := &job.Message{}
5859
b.ReportAllocs()
5960
b.ResetTimer()
6061
for i := 0; i < b.N; i++ {
@@ -71,3 +72,32 @@ func BenchmarkDecode(b *testing.B) {
7172
}
7273
})
7374
}
75+
76+
func TestEncodeAndDecode(t *testing.T) {
77+
m := job.NewMessage(&mockMessage{
78+
message: "foo",
79+
}, job.AllowOption{
80+
RetryCount: job.Int64(100),
81+
RetryDelay: job.Time(30 * time.Millisecond),
82+
Timeout: job.Time(3 * time.Millisecond),
83+
})
84+
85+
t.Run("JSON", func(t *testing.T) {
86+
data, _ := json.Marshal(m)
87+
out := &job.Message{}
88+
_ = json.Unmarshal(data, out)
89+
90+
assert.Equal(t, int64(100), out.RetryCount)
91+
assert.Equal(t, 30*time.Millisecond, out.RetryDelay)
92+
assert.Equal(t, 3*time.Millisecond, out.Timeout)
93+
})
94+
95+
t.Run("UnsafeCast", func(t *testing.T) {
96+
data := job.Encode(m)
97+
out := job.Decode(data)
98+
99+
assert.Equal(t, int64(100), out.RetryCount)
100+
assert.Equal(t, 30*time.Millisecond, out.RetryDelay)
101+
assert.Equal(t, 3*time.Millisecond, out.Timeout)
102+
})
103+
}

0 commit comments

Comments
 (0)