Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling in BytesQueue.Push function in queue.go #405

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions queue/bytes_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ const (
)

var (
errEmptyQueue = &queueError{"Empty queue"}
alonohana627 marked this conversation as resolved.
Show resolved Hide resolved
errEmptyQueue = &queueError{"Queue is empty."}
errInvalidIndex = &queueError{"Index must be greater than zero. Invalid index."}
errIndexOutOfBounds = &queueError{"Index out of range"}
errFullQueue = &queueError{"Queue is full. Maximum size limit reached."}
janisz marked this conversation as resolved.
Show resolved Hide resolved
)

// BytesQueue is a non-thread safe queue type of fifo based on bytes array.
Expand Down Expand Up @@ -92,7 +93,7 @@ func (q *BytesQueue) Push(data []byte) (int, error) {
if q.canInsertBeforeHead(neededSize) {
q.tail = leftMarginIndex
} else if q.capacity+neededSize >= q.maxCapacity && q.maxCapacity > 0 {
return -1, &queueError{"Full queue. Maximum size limit reached."}
return -1, errFullQueue
} else {
q.allocateAdditionalMemory(neededSize)
}
Expand Down
12 changes: 6 additions & 6 deletions queue/bytes_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestPushAndPop(t *testing.T) {
_, err := queue.Pop()

// then
assertEqual(t, "Empty queue", err.Error())
assertEqual(t, "Queue is empty.", err.Error())

// when
queue.Push(entry)
Expand Down Expand Up @@ -56,7 +56,7 @@ func TestPeek(t *testing.T) {
err2 := queue.peekCheckErr(queue.head)
// then
assertEqual(t, err, err2)
assertEqual(t, "Empty queue", err.Error())
assertEqual(t, "Queue is empty.", err.Error())
assertEqual(t, 0, len(read))

// when
Expand Down Expand Up @@ -113,7 +113,7 @@ func TestReset(t *testing.T) {
read, err := queue.Peek()

// then
assertEqual(t, "Empty queue", err.Error())
assertEqual(t, "Queue is empty.", err.Error())
assertEqual(t, 0, len(read))

// when
Expand All @@ -129,7 +129,7 @@ func TestReset(t *testing.T) {
read, err = queue.Peek()

// then
assertEqual(t, "Empty queue", err.Error())
assertEqual(t, "Queue is empty.", err.Error())
assertEqual(t, 0, len(read))
}

Expand Down Expand Up @@ -382,7 +382,7 @@ func TestGetEntryFromEmptyQueue(t *testing.T) {
// then
assertEqual(t, err, err2)
assertEqual(t, []byte(nil), result)
assertEqual(t, "Empty queue", err.Error())
assertEqual(t, "Queue is empty.", err.Error())
}

func TestMaxSizeLimit(t *testing.T) {
Expand All @@ -399,7 +399,7 @@ func TestMaxSizeLimit(t *testing.T) {

// then
assertEqual(t, 50, capacity)
assertEqual(t, "Full queue. Maximum size limit reached.", err.Error())
assertEqual(t, "Queue is full. Maximum size limit reached.", err.Error())
janisz marked this conversation as resolved.
Show resolved Hide resolved
assertEqual(t, blob('a', 25), pop(queue))
assertEqual(t, blob('b', 5), pop(queue))
}
Expand Down