Skip to content

Commit

Permalink
Add Go line reader tests
Browse files Browse the repository at this point in the history
  • Loading branch information
driskell committed Sep 29, 2014
1 parent bf26040 commit 7c51deb
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ gem:
gem build log-courier.gemspec

test: all vendor/bundle/.GemfileModT
go test -tags "$(TAGS)" lc-admin lc-curvekey lc-lib/... lc-tlscert log-courier
bundle exec rspec $(TESTS)

doc:
Expand Down
101 changes: 101 additions & 0 deletions src/lc-lib/harvester/linereader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2014 Jason Woods.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package harvester

import (
"bytes"
"testing"
)

func checkLine(t *testing.T, reader *LineReader, expected []byte) {
line, err := reader.ReadSlice()
if line == nil {
t.Log("No line returned")
t.FailNow()
}
if !bytes.Equal(line, expected) {
t.Logf("Line data incorrect: [% X]", line)
t.FailNow()
}
if err != nil {
t.Logf("Unexpected error: %s", err)
t.FailNow()
}
}

func checkLineFull(t *testing.T, reader *LineReader, expected []byte) {
line, err := reader.ReadSlice()
if line == nil {
t.Log("No line returned")
t.FailNow()
}
if !bytes.Equal(line, expected) {
t.Logf("Line data incorrect: [% X]", line)
t.FailNow()
}
if err != ErrBufferFull {
t.Logf("Unexpected error: %s", err)
t.FailNow()
}
}

func checkEnd(t *testing.T, reader *LineReader) {
line, err := reader.ReadSlice()
if line != nil {
t.Log("Unexpected line returned")
t.FailNow()
}
if err == nil {
t.Log("Expected error")
t.FailNow()
}
}

func TestLineRead(t *testing.T) {
data := bytes.NewBufferString("12345678901234567890\n12345678901234567890\n")

// New line read with 100 bytes, enough for the above
reader := NewLineReader(data, 100)

checkLine(t, reader, []byte("12345678901234567890\n"))
checkLine(t, reader, []byte("12345678901234567890\n"))
checkEnd(t, reader)
}

func TestLineReadEmpty(t *testing.T) {
data := bytes.NewBufferString("\n12345678901234567890\n")

// New line read with 100 bytes, enough for the above
reader := NewLineReader(data, 100)

checkLine(t, reader, []byte("\n"))
checkLine(t, reader, []byte("12345678901234567890\n"))
checkEnd(t, reader)
}

func TestLineReadFull(t *testing.T) {
data := bytes.NewBufferString("12345678901234567890\n123456789012345678901234567890\n12345678901234567890\n")

// New line read with 21 bytes buffer to experience full buffer error
reader := NewLineReader(data, 21)

checkLine(t, reader, []byte("12345678901234567890\n"))
checkLineFull(t, reader, []byte("123456789012345678901"))
checkLine(t, reader, []byte("234567890\n"))
checkLine(t, reader, []byte("12345678901234567890\n"))
checkEnd(t, reader)
}

0 comments on commit 7c51deb

Please sign in to comment.