github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/util/line_buffer_writer_test.go (about)

     1  package util
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/smartystreets/goconvey/convey"
     7  )
     8  
     9  type TestWrappedWriter struct {
    10  	input []byte
    11  }
    12  
    13  func (self *TestWrappedWriter) Write(p []byte) (n int, err error) {
    14  	self.input = p
    15  	return len(p), nil
    16  }
    17  
    18  func (self *TestWrappedWriter) Flush() {
    19  	self.input = []byte{}
    20  }
    21  
    22  func TestNewLineBufferingWriter(t *testing.T) {
    23  	Convey("Using a LineBufferingWriter should", t, func() {
    24  		var err error
    25  		testWriter := &TestWrappedWriter{
    26  			input: []byte{},
    27  		}
    28  		bufferWriter := NewLineBufferingWriter(testWriter)
    29  		Convey("flush properly", func() {
    30  			_, err = bufferWriter.Write([]byte("hello"))
    31  			So(err, ShouldBeNil)
    32  			So(bufferWriter.buf, ShouldNotBeEmpty)
    33  			So(bufferWriter.Flush(), ShouldBeNil)
    34  			So(bufferWriter.buf, ShouldBeEmpty)
    35  		})
    36  		Convey("write to writer if ending with a newline", func() {
    37  			_, err = bufferWriter.Write([]byte("this has a newline\n"))
    38  			So(err, ShouldBeNil)
    39  			So(bufferWriter.buf, ShouldBeEmpty)
    40  			So(testWriter.input, ShouldNotBeEmpty)
    41  			So(string(testWriter.input[:]), ShouldEqual, "this has a newline")
    42  			testWriter.Flush()
    43  			So(bufferWriter.Flush(), ShouldBeNil)
    44  		})
    45  		Convey("write to writer if there is no newline, but should when there is a newline", func() {
    46  			_, err = bufferWriter.Write([]byte("this should stay in the buffer..."))
    47  			So(err, ShouldBeNil)
    48  			So(bufferWriter.buf, ShouldNotBeEmpty)
    49  			So(testWriter.input, ShouldBeEmpty)
    50  			_, err = bufferWriter.Write([]byte("this should be appended to the previous\n"))
    51  			So(err, ShouldBeNil)
    52  			So(bufferWriter.buf, ShouldBeEmpty)
    53  			So(testWriter.input, ShouldNotBeEmpty)
    54  			So(string(testWriter.input[:]), ShouldEqual, "this should stay in the buffer...this should be appended to the previous")
    55  			testWriter.Flush()
    56  			So(bufferWriter.Flush(), ShouldBeNil)
    57  		})
    58  		Convey("write out if the size of the input + buffer is greater than 4K", func() {
    59  			first_input := make([]byte, 100)
    60  			second_input := make([]byte, (4 * 1024))
    61  			_, err = bufferWriter.Write(first_input)
    62  			So(err, ShouldBeNil)
    63  			So(testWriter.input, ShouldBeEmpty)
    64  			So(bufferWriter.buf, ShouldNotBeEmpty)
    65  			So(len(bufferWriter.buf), ShouldEqual, 100)
    66  			_, err = bufferWriter.Write(second_input)
    67  			So(err, ShouldBeNil)
    68  			So(testWriter.input, ShouldNotBeEmpty)
    69  			So(len(testWriter.input), ShouldEqual, 100)
    70  			So(bufferWriter.buf, ShouldNotBeEmpty)
    71  			So(len(bufferWriter.buf), ShouldEqual, (4 * 1024))
    72  			testWriter.Flush()
    73  			So(bufferWriter.Flush(), ShouldBeNil)
    74  		})
    75  
    76  	})
    77  }