Unverified Commit f8aaa1f7 authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #10 from petroav/improve-test-coverage

Improve test coverage
parents 731533cb 8a7e95f2
# go-msgio - Message IO
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](https://protocol.ai)
[![](https://img.shields.io/badge/project-libp2p-blue.svg?style=flat-square)](http://libp2p.io/)
[![](https://img.shields.io/badge/freenode-%23libp2p-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p)
[![codecov](https://codecov.io/gh/libp2p/go-libp2p-netutil/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/go-msgio)
[![Travis CI](https://travis-ci.org/libp2p/go-libp2p-netutil.svg?branch=master)](https://travis-ci.org/libp2p/go-msgio)
This is a simple package that helps read and write length-delimited slices. It's helpful for building wire protocols.
## Usage
......
package msgio
import (
"bytes"
"testing"
)
func TestLimitReader(t *testing.T) {
buf := bytes.NewBuffer(nil)
reader, _ := LimitedReader(buf) // limit is set to 0
n, err := reader.Read([]byte{})
if n != 0 || err.Error() != "EOF" {
t.Fatal("Expected not to read anything")
}
}
func TestLimitWriter(t *testing.T) {
buf := bytes.NewBuffer(nil)
writer := NewLimitedWriter(buf)
n, err := writer.Write([]byte{1, 2, 3})
if n != 3 || err != nil {
t.Fatal("Expected to write 3 bytes with no errors")
}
err = writer.Flush()
}
......@@ -2,9 +2,11 @@ package msgio
import (
"bytes"
"errors"
"fmt"
"io"
"math/rand"
str "strings"
"sync"
"testing"
"time"
......@@ -47,6 +49,45 @@ func TestWriteClose(t *testing.T) {
SubtestWriteClose(t, writer, reader)
}
type testIoReadWriter struct {
io.Reader
io.Writer
}
func TestReadWriterClose(t *testing.T) {
r, w := io.Pipe()
var rw ReadWriteCloser
rw = NewReadWriter(testIoReadWriter{r, w})
SubtestReaderWriterClose(t, rw)
}
func TestReadWriterCombine(t *testing.T) {
r, w := io.Pipe()
writer := NewWriter(w)
reader := NewReader(r)
rw := Combine(writer, reader)
rw.Close()
}
func TestMultiError(t *testing.T) {
emptyError := multiErr([]error{})
if emptyError.Error() != "no errors" {
t.Fatal("Expected no errors")
}
twoErrors := multiErr([]error{errors.New("one"), errors.New("two")})
if eStr := twoErrors.Error(); !str.Contains(eStr, "one") && !str.Contains(eStr, "two") {
t.Fatal("Expected error messages not included")
}
}
func TestShortBufferError(t *testing.T) {
buf := bytes.NewBuffer(nil)
writer := NewWriter(buf)
reader := NewReader(buf)
SubtestReadShortBuffer(t, writer, reader)
}
func SubtestReadWrite(t *testing.T, writer WriteCloser, reader ReadCloser) {
msgs := [1000][]byte{}
......@@ -240,7 +281,47 @@ func SubtestWriteClose(t *testing.T, writer WriteCloser, reader ReadCloser) {
}()
n, err := writer.Write(buf[:])
if n != 0 || err == nil {
t.Error("expected to read nothing")
t.Error("expected to write nothing")
}
<-done
}
func SubtestReaderWriterClose(t *testing.T, rw ReadWriteCloser) {
buf := [10]byte{}
done := make(chan struct{})
go func() {
defer close(done)
time.Sleep(10 * time.Millisecond)
buf := [10]byte{}
rw.Read(buf[:])
rw.Close()
}()
n, err := rw.Write(buf[:])
if n != 10 || err != nil {
t.Error("Expected to write 10 bytes")
}
<-done
}
func SubtestReadShortBuffer(t *testing.T, writer WriteCloser, reader ReadCloser) {
defer reader.Close()
shortReadBuf := [1]byte{}
done := make(chan struct{})
go func() {
defer writer.Close()
defer close(done)
time.Sleep(10 * time.Millisecond)
largeWriteBuf := [10]byte{}
writer.Write(largeWriteBuf[:])
}()
<-done
n, _ := reader.NextMsgLen()
if n != 10 {
t.Fatal("Expected next message to have length of 10")
}
_, err := reader.Read(shortReadBuf[:])
if err != io.ErrShortBuffer {
t.Fatal("Expected short buffer error")
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment