Skip to content

Commit 0f0d83d

Browse files
committed
test(json): add missing MarshalJSON empty queue path test
1 parent 12cc831 commit 0f0d83d

File tree

3 files changed

+85
-28
lines changed

3 files changed

+85
-28
lines changed

blocking.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package queue
22

33
import (
44
"encoding/json"
5-
"fmt"
65
"sync"
76
)
87

@@ -286,11 +285,5 @@ func (bq *Blocking[T]) MarshalJSON() ([]byte, error) {
286285
return []byte("[]"), nil
287286
}
288287

289-
// Marshal the slice of elements into JSON.
290-
data, err := json.Marshal(bq.elems)
291-
if err != nil {
292-
return nil, fmt.Errorf("failed to marshal blocking queue: %w", err)
293-
}
294-
295-
return data, nil
288+
return json.Marshal(bq.elems)
296289
}

blocking_test.go

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -710,22 +710,66 @@ func TestBlocking(t *testing.T) {
710710
t.Run("MarshalJSON", func(t *testing.T) {
711711
t.Parallel()
712712

713-
elems := []int{3, 2, 1}
713+
t.Run("HasElements", func(t *testing.T) {
714+
t.Parallel()
714715

715-
q := queue.NewBlocking(elems)
716+
elems := []int{3, 2, 1}
716717

717-
marshaled, err := json.Marshal(q)
718-
if err != nil {
719-
t.Fatalf("expected no error, got %v", err)
720-
}
718+
q := queue.NewBlocking(elems)
721719

722-
expectedMarshaled := []byte(`[3,2,1]`)
723-
if !bytes.Equal(expectedMarshaled, marshaled) {
724-
t.Fatalf("expected marshaled to be %s, got %s", expectedMarshaled, marshaled)
725-
}
720+
marshaled, err := json.Marshal(q)
721+
if err != nil {
722+
t.Fatalf("expected no error, got %v", err)
723+
}
724+
725+
expectedMarshaled := []byte(`[3,2,1]`)
726+
if !bytes.Equal(expectedMarshaled, marshaled) {
727+
t.Fatalf("expected marshaled to be %s, got %s", expectedMarshaled, marshaled)
728+
}
729+
})
730+
731+
t.Run("FailMarshal", func(t *testing.T) {
732+
t.Parallel()
733+
734+
q := queue.NewBlocking([]failMarshal{{}})
735+
736+
marshaled, err := json.Marshal(q)
737+
if err == nil {
738+
t.Fatalf("expected error, got nil")
739+
}
740+
741+
if marshaled != nil {
742+
t.Fatalf("expected marshaled to be nil, got %s", marshaled)
743+
}
744+
})
745+
746+
t.Run("Empty", func(t *testing.T) {
747+
t.Parallel()
748+
749+
q := queue.NewBlocking[int](nil)
750+
751+
marshaled, err := json.Marshal(q)
752+
if err != nil {
753+
t.Fatalf("expected no error, got %v", err)
754+
}
755+
756+
expectedMarshaled := []byte(`[]`)
757+
if !bytes.Equal(expectedMarshaled, marshaled) {
758+
t.Fatalf("expected marshaled to be %s, got %s", expectedMarshaled, marshaled)
759+
}
760+
})
726761
})
727762
}
728763

764+
// failMarshal is a helper to fail the json marshalling of the queues
765+
type failMarshal struct{}
766+
767+
var errFailMarshal = errors.New("intentional marshal error")
768+
769+
func (f failMarshal) MarshalJSON() ([]byte, error) {
770+
return nil, errFailMarshal
771+
}
772+
729773
func testResetOnMultipleRoutinesFunc[T comparable](
730774
ids []T,
731775
totalRoutines int,

circular_test.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -296,19 +296,39 @@ func TestCircular(t *testing.T) {
296296
t.Run("MarshalJSON", func(t *testing.T) {
297297
t.Parallel()
298298

299-
elems := []int{3, 2, 1}
299+
t.Run("HasElements", func(t *testing.T) {
300+
t.Parallel()
300301

301-
q := queue.NewCircular(elems, 4)
302+
elems := []int{3, 2, 1}
302303

303-
marshaled, err := json.Marshal(q)
304-
if err != nil {
305-
t.Fatalf("expected no error, got %v", err)
306-
}
304+
q := queue.NewCircular(elems, 4)
307305

308-
expectedMarshaled := []byte(`[3,2,1]`)
309-
if !bytes.Equal(expectedMarshaled, marshaled) {
310-
t.Fatalf("expected marshaled to be %s, got %s", expectedMarshaled, marshaled)
311-
}
306+
marshaled, err := json.Marshal(q)
307+
if err != nil {
308+
t.Fatalf("expected no error, got %v", err)
309+
}
310+
311+
expectedMarshaled := []byte(`[3,2,1]`)
312+
if !bytes.Equal(expectedMarshaled, marshaled) {
313+
t.Fatalf("expected marshaled to be %s, got %s", expectedMarshaled, marshaled)
314+
}
315+
})
316+
317+
t.Run("Empty", func(t *testing.T) {
318+
t.Parallel()
319+
320+
q := queue.NewCircular[int](nil, 1)
321+
322+
marshaled, err := json.Marshal(q)
323+
if err != nil {
324+
t.Fatalf("expected no error, got %v", err)
325+
}
326+
327+
expectedMarshaled := []byte(`[]`)
328+
if !bytes.Equal(expectedMarshaled, marshaled) {
329+
t.Fatalf("expected marshaled to be %s, got %s", expectedMarshaled, marshaled)
330+
}
331+
})
312332
})
313333
}
314334

0 commit comments

Comments
 (0)