diff --git a/ringbuffer/RingBuffer.go b/ringbuffer/RingBuffer.go index 3492b0c1..13190cf3 100644 --- a/ringbuffer/RingBuffer.go +++ b/ringbuffer/RingBuffer.go @@ -55,7 +55,7 @@ New returns the addres of a new ring buffer with the parameters specified. It initialises fields and allocates the required dataMemory. */ func NewRingBuffer(bufferSize int, elementSize int) (rb *ringBuffer) { - if size <= 0 || elementSize <= 0 { + if bufferSize <= 0 || elementSize <= 0 { return nil } rb = new(ringBuffer) @@ -63,14 +63,13 @@ func NewRingBuffer(bufferSize int, elementSize int) (rb *ringBuffer) { for i := range rb.dataMemory { rb.dataMemory[i] = make([]byte, elementSize) } - rb.sizeMemory = make([]int, size) + rb.sizeMemory = make([]int, bufferSize) rb.size = bufferSize rb.noOfElements = 0 rb.first = -1 rb.last = -1 rb.currentlyWriting = false rb.currentlyReading = false - rb.sizeChannel = make(chan int, size) rb.mutex = sync.Mutex{} return } diff --git a/ringbuffer/RingBuffer_test.go b/ringbuffer/RingBuffer_test.go index 21ba1b9b..97dd0526 100644 --- a/ringbuffer/RingBuffer_test.go +++ b/ringbuffer/RingBuffer_test.go @@ -33,6 +33,14 @@ import ( "time" ) +const ( + testBufferSize = 10 + testElementSize = 1 + testOverFlowSize = 5 + testDataSize = 1 + testConcurrencyBufferSize = 1000; +) + var rb *ringBuffer // Call when we get an error we shouldn't have @@ -48,11 +56,11 @@ func noErrorFail(t *testing.T) { // Test that the Make method correctly allocates memory for arbitrary buffer // size of 10 and arbitrary element size of 10 func TestMake(t *testing.T) { - rb = NewRingBuffer(10, 10) - if len(rb.dataMemory) != 10 { + rb = NewRingBuffer(testBufferSize, testElementSize) + if len(rb.dataMemory) != testBufferSize { t.Errorf("Len of buffer is wrong!") } - if len(rb.dataMemory[0]) != 10 { + if len(rb.dataMemory[0]) != testElementSize { t.Errorf("Len of individual element is wrong!") } } @@ -60,7 +68,7 @@ func TestMake(t *testing.T) { // Test call to done writing when no writing has occured. // We pass arbitrary clipSize. func TestDoneWriting1(t *testing.T) { - if rb.DoneWriting(1) == nil { + if rb.DoneWriting(testDataSize) == nil { noErrorFail(t) } } @@ -70,28 +78,14 @@ func TestDoneWriting2(t *testing.T) { if _, err := rb.Get(); err != nil { falseErrorFail(t, err) } - if err := rb.DoneWriting(1); err != nil { + if err := rb.DoneWriting(testDataSize); err != nil { falseErrorFail(t, err) } } -// Try to 'write' to the buffer when full. Set buffer size to arbitrary number of 3. -// We write over buffer size of 3 (write 4 times) -func TestWritingWhenFull(t *testing.T) { - rb = NewRingBuffer(3, 10) - var err error - for i := 0; i < 4; i++ { - _, err = rb.Get() - rb.DoneWriting(1) - } - if err == nil { - noErrorFail(t) - } -} - // Test call to done reading when there hasn't been any reading func TestDoneReading1(t *testing.T) { - rb = NewRingBuffer(10, 10) + rb = NewRingBuffer(testBufferSize, testElementSize) if rb.DoneReading() == nil { noErrorFail(t) } @@ -99,7 +93,7 @@ func TestDoneReading1(t *testing.T) { // Test read when there hasn't been anything written to buffer func TestReadingWithoutWrite(t *testing.T) { - rb = NewRingBuffer(10, 10) + rb = NewRingBuffer(testBufferSize, testElementSize) _, err := rb.Read() if err == nil { noErrorFail(t) @@ -108,7 +102,7 @@ func TestReadingWithoutWrite(t *testing.T) { // Try Writing twice without calling DoneWriting func TestWritingTwice(t *testing.T) { - rb = NewRingBuffer(10, 10) + rb = NewRingBuffer(testBufferSize, testElementSize) _, err := rb.Get() _, err = rb.Get() if err == nil { @@ -118,9 +112,9 @@ func TestWritingTwice(t *testing.T) { // Test call to done reading when there has been some reading func TestDoneReading2(t *testing.T) { - rb = NewRingBuffer(10, 10) + rb = NewRingBuffer(testBufferSize, testElementSize) _, err := rb.Get() - rb.DoneWriting(1) + rb.DoneWriting(testDataSize) _, err = rb.Read() if err != nil { falseErrorFail(t, err) @@ -130,11 +124,11 @@ func TestDoneReading2(t *testing.T) { // Testing writing then reading for single 'write' and single 'read' routines func TestWritingAndReading1(t *testing.T) { - rb = NewRingBuffer(10, 10) + rb = NewRingBuffer(testBufferSize, testElementSize) if _, err := rb.Get(); err != nil { falseErrorFail(t, err) } - if err := rb.DoneWriting(1); err != nil { + if err := rb.DoneWriting(testDataSize); err != nil { falseErrorFail(t, err) } if _, err := rb.Read(); err != nil { @@ -147,12 +141,12 @@ func TestWritingAndReading1(t *testing.T) { // Testing two 'writes' and two 'reads' func TestWritingAndReading2(t *testing.T) { - rb = NewRingBuffer(10, 10) + rb = NewRingBuffer(testBufferSize, testElementSize) for i := 0; i < 2; i++ { if _, err := rb.Get(); err != nil { falseErrorFail(t, err) } - if err := rb.DoneWriting(1); err != nil { + if err := rb.DoneWriting(testDataSize); err != nil { falseErrorFail(t, err) } } @@ -168,11 +162,11 @@ func TestWritingAndReading2(t *testing.T) { // Testing one 'write' and two 'reads' func TestWritingAndReading3(t *testing.T) { - rb = NewRingBuffer(10, 10) + rb = NewRingBuffer(testBufferSize, testElementSize) if _, err := rb.Get(); err != nil { falseErrorFail(t, err) } - if err := rb.DoneWriting(1); err != nil { + if err := rb.DoneWriting(testDataSize); err != nil { falseErrorFail(t, err) } var err1 error @@ -191,12 +185,12 @@ func TestWritingAndReading3(t *testing.T) { // Test two 'writes' and one 'read' func TestWritingAndReading4(t *testing.T) { - rb = NewRingBuffer(10, 10) + rb = NewRingBuffer(testBufferSize, testElementSize) for i := 0; i < 2; i++ { if _, err := rb.Get(); err != nil { falseErrorFail(t, err) } - if err := rb.DoneWriting(1); err != nil { + if err := rb.DoneWriting(testDataSize); err != nil { falseErrorFail(t, err) } } @@ -210,12 +204,12 @@ func TestWritingAndReading4(t *testing.T) { // Test writing past capacity func TestWritingAndReading5(t *testing.T) { - rb = NewRingBuffer(4, 10) + rb = NewRingBuffer(testOverFlowSize, testElementSize) var err1 error var err2 error - for i := 0; i < 5; i++ { + for i := 0; i < testOverFlowSize+1; i++ { _, err1 = rb.Get() - err2 = rb.DoneWriting(1) + err2 = rb.DoneWriting(testDataSize) } if err1 == nil { noErrorFail(t) @@ -228,12 +222,12 @@ func TestWritingAndReading5(t *testing.T) { // Test writing to size, then read some, then 'write' to test that we go back // to start of buffer func TestWritingAndReading6(t *testing.T) { - rb = NewRingBuffer(4, 10) + rb = NewRingBuffer(testOverFlowSize, 10) var err1 error var err2 error - for i := 0; i < 4; i++ { + for i := 0; i < testOverFlowSize; i++ { _, err1 = rb.Get() - err2 = rb.DoneWriting(1) + err2 = rb.DoneWriting(testDataSize) } if err1 != nil { falseErrorFail(t, err1) @@ -252,7 +246,7 @@ func TestWritingAndReading6(t *testing.T) { falseErrorFail(t, err2) } _, err1 = rb.Get() - err2 = rb.DoneWriting(1) + err2 = rb.DoneWriting(testDataSize) if err1 != nil { falseErrorFail(t, err1) } @@ -264,12 +258,12 @@ func TestWritingAndReading6(t *testing.T) { // Now let's do the previous test again, but this time we will try to write // past capacity func TestWritingAndReading7(t *testing.T) { - rb = NewRingBuffer(4, 10) + rb = NewRingBuffer(testOverFlowSize, testElementSize) var err1 error var err2 error - for i := 0; i < 4; i++ { + for i := 0; i < testOverFlowSize; i++ { _, err1 = rb.Get() - err2 = rb.DoneWriting(1) + err2 = rb.DoneWriting(testDataSize) } if err1 != nil { falseErrorFail(t, err1) @@ -287,9 +281,9 @@ func TestWritingAndReading7(t *testing.T) { if err2 != nil { falseErrorFail(t, err2) } - for i := 0; i < 3; i++ { + for i := 0; i < testOverFlowSize; i++ { _, err1 = rb.Get() - err2 = rb.DoneWriting(1) + err2 = rb.DoneWriting(testDataSize) } if err1 == nil { noErrorFail(t) @@ -301,12 +295,12 @@ func TestWritingAndReading7(t *testing.T) { // Test reading twice without call to DoneReading func TestWritingAndReading8(t *testing.T) { - rb = NewRingBuffer(4, 10) + rb = NewRingBuffer(testBufferSize, 10) var err1 error var err2 error - for i := 0; i < 4; i++ { + for i := 0; i < 2; i++ { _, err1 = rb.Get() - err2 = rb.DoneWriting(1) + err2 = rb.DoneWriting(testDataSize) } if err1 != nil { falseErrorFail(t, err1) @@ -326,7 +320,7 @@ func TestWritingAndReading8(t *testing.T) { // any data race issues. We will continously write and read at concurrently // time for a second func TestConcurrency1(t *testing.T) { - rb = NewRingBuffer(1000, 10) + rb = NewRingBuffer(testConcurrencyBufferSize, testElementSize) go func() { for i := 0; i < 100; i++ { if data, err1 := rb.Read(); data != nil { @@ -343,7 +337,7 @@ func TestConcurrency1(t *testing.T) { go func() { for i := 0; i < 100; i++ { _, err1 := rb.Get() - err2 := rb.DoneWriting(1) + err2 := rb.DoneWriting(testDataSize) if err1 != nil { falseErrorFail(t, err1) }