Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
Use `DefaultConfig` in test
Make queue tests logs verbose
Add somne corner case tests
  • Loading branch information
janisz committed Apr 13, 2016
1 parent 5f83047 commit ec334b9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 15 deletions.
2 changes: 1 addition & 1 deletion bigcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestWriteAndGetOnCache(t *testing.T) {
t.Parallel()

// given
cache, _ := NewBigCache(Config{16, 5 * time.Second, 10, 256, false, nil})
cache, _ := NewBigCache(DefaultConfig(5 * time.Second))
value := []byte("value")

// when
Expand Down
82 changes: 68 additions & 14 deletions queue/bytes_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,55 @@ func TestPushAndPop(t *testing.T) {
queue := NewBytesQueue(10, true)
entry := []byte("hello")

// when
_, err := queue.Pop()

// then
assert.EqualError(t, err, "Empty queue")

// when
queue.Push(entry)

// then
assert.Equal(t, entry, pop(queue))
}

func TestPeek(t *testing.T) {

func TestLen(t *testing.T) {
t.Parallel()

// given
queue := NewBytesQueue(100, false)
queue := NewBytesQueue(100, true)
entry := []byte("hello")
assert.Zero(t, queue.Len())

// when
queue.Push(entry)

// then
assert.Equal(t, queue.Len(), 1)
}

func TestPeek(t *testing.T) {
t.Parallel()

// given
queue := NewBytesQueue(100, true)
entry := []byte("hello")

// when
read, err := queue.Peek()

// then
assert.EqualError(t, err, "Empty queue")
assert.Nil(t, read)

// when
read, _ := queue.Peek()
queue.Push(entry)
read, err = queue.Peek()

// then
assert.NoError(t, err)
assert.Equal(t, pop(queue), read)
assert.Equal(t, entry, read)
}
Expand All @@ -40,7 +70,7 @@ func TestReuseAvailableSpace(t *testing.T) {
t.Parallel()

// given
queue := NewBytesQueue(100, false)
queue := NewBytesQueue(100, true)

// when
queue.Push(blob('a', 70))
Expand All @@ -57,7 +87,7 @@ func TestAllocateAdditionalSpace(t *testing.T) {
t.Parallel()

// given
queue := NewBytesQueue(11, false)
queue := NewBytesQueue(11, true)

// when
queue.Push([]byte("hello1"))
Expand All @@ -71,7 +101,7 @@ func TestAllocateAdditionalSpaceForInsufficientFreeFragmentedSpaceWhereHeadIsBef
t.Parallel()

// given
queue := NewBytesQueue(25, false)
queue := NewBytesQueue(25, true)

// when
queue.Push(blob('a', 3)) // header + entry + left margin = 8 bytes
Expand All @@ -89,7 +119,7 @@ func TestUnchangedEntriesIndexesAfterAdditionalMemoryAllocationWhereHeadIsBefore
t.Parallel()

// given
queue := NewBytesQueue(25, false)
queue := NewBytesQueue(25, true)

// when
queue.Push(blob('a', 3)) // header + entry + left margin = 8 bytes
Expand All @@ -107,7 +137,7 @@ func TestAllocateAdditionalSpaceForInsufficientFreeFragmentedSpaceWhereTailIsBef
t.Parallel()

// given
queue := NewBytesQueue(100, false)
queue := NewBytesQueue(100, true)

// when
queue.Push(blob('a', 70)) // header + entry + left margin = 75 bytes
Expand All @@ -131,7 +161,7 @@ func TestUnchangedEntriesIndexesAfterAdditionalMemoryAllocationWhereTailIsBefore
t.Parallel()

// given
queue := NewBytesQueue(100, false)
queue := NewBytesQueue(100, true)

// when
queue.Push(blob('a', 70)) // header + entry + left margin = 75 bytes
Expand All @@ -150,7 +180,7 @@ func TestAllocateAdditionalSpaceForValueBiggerThanInitQueue(t *testing.T) {
t.Parallel()

// given
queue := NewBytesQueue(11, false)
queue := NewBytesQueue(11, true)

// when
queue.Push(blob('a', 100))
Expand All @@ -164,7 +194,7 @@ func TestAllocateAdditionalSpaceForValueBiggerThanQueue(t *testing.T) {
t.Parallel()

// given
queue := NewBytesQueue(21, false)
queue := NewBytesQueue(21, true)

// when
queue.Push(make([]byte, 2))
Expand All @@ -182,7 +212,7 @@ func TestPopWholeQueue(t *testing.T) {
t.Parallel()

// given
queue := NewBytesQueue(13, false)
queue := NewBytesQueue(13, true)

// when
queue.Push([]byte("a"))
Expand All @@ -200,7 +230,7 @@ func TestGetEntryFromIndex(t *testing.T) {
t.Parallel()

// given
queue := NewBytesQueue(20, false)
queue := NewBytesQueue(20, true)

// when
queue.Push([]byte("a"))
Expand All @@ -216,7 +246,7 @@ func TestGetEntryFromInvalidIndex(t *testing.T) {
t.Parallel()

// given
queue := NewBytesQueue(13, false)
queue := NewBytesQueue(13, true)

// when
result, err := queue.Get(0)
Expand All @@ -226,6 +256,30 @@ func TestGetEntryFromInvalidIndex(t *testing.T) {
assert.EqualError(t, err, "Index must be grater than zero. Invalid index.")
}

func TestGetEntryFromIndexOutOfRange(t *testing.T) {
t.Parallel()

// given
queue := NewBytesQueue(0, true)

// when
assert.Panics(t, func() { queue.Get(42) } )
}

func TestGetEntryFromEmptyQueue(t *testing.T) {
t.Parallel()

// given
queue := NewBytesQueue(13, true)

// when
result, err := queue.Get(1)

// then
assert.NotNil(t, result)
assert.NoError(t, err)
}

func pop(queue *BytesQueue) []byte {
entry, _ := queue.Pop()
return entry
Expand Down

0 comments on commit ec334b9

Please sign in to comment.