Skip to content

Commit dc3c120

Browse files
committed
refactor: improve code documentation and update test values
- Move the `Data` field and its comment in the `Message` struct - Add comments for `NewMessage`, `NewTask`, `Encode`, and `Decode` functions - Update `RetryMax` and `RetryFactor` values in the test and adjust assertions accordingly - Add comments for `Options`, `newDefaultOptions`, `AllowOption`, and `NewOptions` functions - Add `Float64` and `Time` helper functions Signed-off-by: appleboy <appleboy.tw@gmail.com>
1 parent 32d20fd commit dc3c120

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

job/job.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ type Message struct {
3131
// default is 100ms
3232
RetryDelay time.Duration `json:"retry_delay"`
3333

34-
// Data to save Unsafe cast
35-
Data []byte
36-
3734
// RetryFactor is the multiplying factor for each increment step.
3835
//
3936
// Defaults to 2.
@@ -48,6 +45,9 @@ type Message struct {
4845
//
4946
// Defaults to 10 seconds.
5047
RetryMax time.Duration `json:"retry_max"`
48+
49+
// Data to save Unsafe cast
50+
Data []byte
5151
}
5252

5353
const (
@@ -64,6 +64,7 @@ func (m *Message) Encode() {
6464
m.Data = Encode(m)
6565
}
6666

67+
// NewMessage create new message
6768
func NewMessage(m core.QueuedMessage, opts ...AllowOption) *Message {
6869
o := NewOptions(opts...)
6970

@@ -78,6 +79,7 @@ func NewMessage(m core.QueuedMessage, opts ...AllowOption) *Message {
7879
}
7980
}
8081

82+
// NewTask create new task
8183
func NewTask(task TaskFunc, opts ...AllowOption) *Message {
8284
o := NewOptions(opts...)
8385

@@ -92,10 +94,12 @@ func NewTask(task TaskFunc, opts ...AllowOption) *Message {
9294
}
9395
}
9496

97+
// Encode for encoding the structure
9598
func Encode(m *Message) []byte {
9699
return (*[movementSize]byte)(unsafe.Pointer(m))[:]
97100
}
98101

102+
// Decode for decoding the structure
99103
func Decode(m []byte) *Message {
100104
return (*Message)(unsafe.Pointer(&m[0]))
101105
}

job/job_test.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ func TestMessageEncodeDecode(t *testing.T) {
2020
message: "foo",
2121
},
2222
AllowOption{
23-
RetryCount: Int64(100),
24-
RetryDelay: Time(30 * time.Millisecond),
25-
Timeout: Time(3 * time.Millisecond),
26-
RetryMin: Time(200 * time.Millisecond),
23+
RetryCount: Int64(100),
24+
RetryDelay: Time(30 * time.Millisecond),
25+
Timeout: Time(3 * time.Millisecond),
26+
RetryMin: Time(200 * time.Millisecond),
27+
RetryMax: Time(20 * time.Second),
28+
RetryFactor: Float64(4.0),
2729
},
2830
)
2931

@@ -35,6 +37,6 @@ func TestMessageEncodeDecode(t *testing.T) {
3537
assert.Equal(t, 3*time.Millisecond, out.Timeout)
3638
assert.Equal(t, "foo", string(out.Payload))
3739
assert.Equal(t, 200*time.Millisecond, out.RetryMin)
38-
assert.Equal(t, 10*time.Second, out.RetryMax)
39-
assert.Equal(t, 2.0, out.RetryFactor)
40+
assert.Equal(t, 20*time.Second, out.RetryMax)
41+
assert.Equal(t, 4.0, out.RetryFactor)
4042
}

job/option.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package job
22

33
import "time"
44

5+
// Options is a set of options for the queue
56
type Options struct {
67
retryCount int64
78
retryDelay time.Duration
@@ -12,6 +13,7 @@ type Options struct {
1213
timeout time.Duration
1314
}
1415

16+
// newDefaultOptions create new default options
1517
func newDefaultOptions() Options {
1618
return Options{
1719
retryCount: 0,
@@ -23,6 +25,7 @@ func newDefaultOptions() Options {
2325
}
2426
}
2527

28+
// AllowOption is a function that sets some option on the Options
2629
type AllowOption struct {
2730
RetryCount *int64
2831
RetryDelay *time.Duration
@@ -32,7 +35,7 @@ type AllowOption struct {
3235
Timeout *time.Duration
3336
}
3437

35-
// NewOptions with custom parameter
38+
// NewOptions create new options
3639
func NewOptions(opts ...AllowOption) Options {
3740
o := newDefaultOptions()
3841

@@ -65,10 +68,17 @@ func NewOptions(opts ...AllowOption) Options {
6568
return o
6669
}
6770

71+
// Int64 is a helper routine that allocates a new int64 value
6872
func Int64(val int64) *int64 {
6973
return &val
7074
}
7175

76+
// Float64 is a helper routine that allocates a new float64 value
77+
func Float64(val float64) *float64 {
78+
return &val
79+
}
80+
81+
// Time is a helper routine that allocates a new time value
7282
func Time(v time.Duration) *time.Duration {
7383
return &v
7484
}

job/option_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/stretchr/testify/assert"
88
)
99

10+
// TestMessageEncodeDecode test message encode and decode
1011
func TestOptions(t *testing.T) {
1112
o := NewOptions(
1213
AllowOption{

0 commit comments

Comments
 (0)