Skip to content

Commit cab4f57

Browse files
committed
refactor: refactor JSON unmarshalling to use Payload instead of Bytes
- Change `m.Bytes()` to `m.Payload()` in the main function for unmarshalling JSON - Update test cases to use `m.Payload()` instead of `m.Bytes()` for logging data Signed-off-by: appleboy <[email protected]>
1 parent 2b882be commit cab4f57

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func main() {
7575
nsq.WithMaxInFlight(10),
7676
nsq.WithRunFunc(func(ctx context.Context, m queue.QueuedMessage) error {
7777
var v *job
78-
if err := json.Unmarshal(m.Bytes(), &v); err != nil {
78+
if err := json.Unmarshal(m.Payload(), &v); err != nil {
7979
return err
8080
}
8181
rets <- v.Message

nsq_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func TestJobReachTimeout(t *testing.T) {
139139
for {
140140
select {
141141
case <-ctx.Done():
142-
log.Println("get data:", string(m.Bytes()))
142+
log.Println("get data:", string(m.Payload()))
143143
if errors.Is(ctx.Err(), context.Canceled) {
144144
log.Println("queue has been shutdown and cancel the job")
145145
} else if errors.Is(ctx.Err(), context.DeadlineExceeded) {
@@ -178,7 +178,7 @@ func TestCancelJobAfterShutdown(t *testing.T) {
178178
for {
179179
select {
180180
case <-ctx.Done():
181-
log.Println("get data:", string(m.Bytes()))
181+
log.Println("get data:", string(m.Payload()))
182182
if errors.Is(ctx.Err(), context.Canceled) {
183183
log.Println("queue has been shutdown and cancel the job")
184184
} else if errors.Is(ctx.Err(), context.DeadlineExceeded) {
@@ -217,15 +217,15 @@ func TestGoroutineLeak(t *testing.T) {
217217
for {
218218
select {
219219
case <-ctx.Done():
220-
log.Println("get data:", string(m.Bytes()))
220+
log.Println("get data:", string(m.Payload()))
221221
if errors.Is(ctx.Err(), context.Canceled) {
222222
log.Println("queue has been shutdown and cancel the job")
223223
} else if errors.Is(ctx.Err(), context.DeadlineExceeded) {
224224
log.Println("job deadline exceeded")
225225
}
226226
return nil
227227
default:
228-
log.Println("get data:", string(m.Bytes()))
228+
log.Println("get data:", string(m.Payload()))
229229
time.Sleep(50 * time.Millisecond)
230230
return nil
231231
}

0 commit comments

Comments
 (0)