From 86d01e2e994a668302ff521790c3b6af5fcc6785 Mon Sep 17 00:00:00 2001 From: ccx Date: Mon, 28 Feb 2022 17:15:11 +0800 Subject: [PATCH] test: add testcase for FIFO Queue in collection module (#1589) cover the case of non-zero value for q.Header when q.Elements expands --- core/collection/fifo_test.go | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/core/collection/fifo_test.go b/core/collection/fifo_test.go index 3e58afef..6f1acfdf 100644 --- a/core/collection/fifo_test.go +++ b/core/collection/fifo_test.go @@ -61,3 +61,41 @@ func TestPutMore(t *testing.T) { assert.Equal(t, string(element), string(body.([]byte))) } } + +func TestPutMoreWithHeaderNotZero(t *testing.T) { + elements := [][]byte{ + []byte("hello"), + []byte("world"), + []byte("again"), + } + queue := NewQueue(4) + for i := range elements { + queue.Put(elements[i]) + } + + // take 1 + body, ok := queue.Take() + assert.True(t, ok) + element, ok := body.([]byte) + assert.True(t, ok) + assert.Equal(t, element, []byte("hello")) + + // put more + queue.Put([]byte("b4")) + queue.Put([]byte("b5")) // will store in elements[0] + queue.Put([]byte("b6")) // cause expansion + + results := [][]byte{ + []byte("world"), + []byte("again"), + []byte("b4"), + []byte("b5"), + []byte("b6"), + } + + for _, element := range results { + body, ok := queue.Take() + assert.True(t, ok) + assert.Equal(t, string(element), string(body.([]byte))) + } +}