func TestReadMultiChunkSinglePass(t *testing.T) { b := new(bytes.Buffer) c1 := &chunk.Chunk{ Header: &chunk.Header{ BasicHeader: chunk.BasicHeader{0, 18}, MessageHeader: chunk.MessageHeader{0, 1234, false, 8, 2, 3}, ExtendedTimestamp: chunk.ExtendedTimestamp{}, }, Data: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, } c2 := &chunk.Chunk{ Header: &chunk.Header{ BasicHeader: chunk.BasicHeader{0, 19}, MessageHeader: chunk.MessageHeader{0, 1234, false, 8, 2, 3}, ExtendedTimestamp: chunk.ExtendedTimestamp{}, }, Data: []byte{0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, } chunk.NewWriter(b, chunk.DefaultReadSize).Write(c1) chunk.NewWriter(b, chunk.DefaultReadSize).Write(c2) r := NewReader(b) go r.Recv() r1 := <-r.Chunks() r2 := <-r.Chunks() assert.Equal(t, 0, len(r.Errs())) assert.Equal(t, c1, r1) assert.Equal(t, c2, r2) }
func TestMultipleWrites(t *testing.T) { buf := new(bytes.Buffer) c := &chunk.Chunk{ Header: &chunk.Header{ BasicHeader: chunk.BasicHeader{0, 18}, MessageHeader: chunk.MessageHeader{0, 1234, false, 8, 2, 3}, ExtendedTimestamp: chunk.ExtendedTimestamp{}, }, Data: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, } err := chunk.NewWriter(buf, 4).Write(c) assert.Nil(t, err) expected := new(bytes.Buffer) (&chunk.BasicHeader{0, 18}).Write(expected) (&chunk.MessageHeader{0, 1234, false, 8, 2, 3}).Write(expected) expected.Write([]byte{0x00, 0x01, 0x02, 0x03}) expected.Write([]byte{byte((3 << 6) | 18&63)}) expected.Write([]byte{0x04, 0x05, 0x06, 0x07}) assert.True(t, bytes.Equal(expected.Bytes(), buf.Bytes()), fmt.Sprintf("test: slice should be equal (%v, %v)", expected.Bytes(), buf.Bytes())) }
func TestStreamSendsOnStatusUpdates(t *testing.T) { buf := new(bytes.Buffer) writer := chunk.NewWriter(buf, chunk.DefaultReadSize) chunks := make(chan *chunk.Chunk) s := New(chunks, writer) go s.Listen() err := s.WriteStatus(NewStatus()) assert.Nil(t, err) assert.NotEmpty(t, buf.Bytes()) }
func newStreamWithChunk(streamId uint32, chunks ...*chunk.Chunk) chunk.Stream { buf := new(bytes.Buffer) for _, c := range chunks { chunk.NewWriter(buf, chunk.DefaultReadSize).Write(c) } parser := chunk.NewParser(chunk.NewReader( buf, chunk.DefaultReadSize, chunk.NoopNormalizer, )) go parser.Recv() st, _ := parser.Stream(streamId) return st }
func TestChunkingErrorsArePropogated(t *testing.T) { out := new(CreateStreamResponse) chunker := new(MockChunker) chunker.On("Chunk", out).Return(nil, errors.New("foo")).Once() nc := NewNetConnection( make(chan *chunk.Chunk), chunk.NewWriter(new(bytes.Buffer), chunk.DefaultReadSize)) nc.chunker = chunker go nc.Listen() err := nc.Send(out) assert.Equal(t, "foo", err.Error()) }
func TestReadSingleChunkMultiPass(t *testing.T) { b := new(bytes.Buffer) c := &chunk.Chunk{ Header: &chunk.Header{ BasicHeader: chunk.BasicHeader{0, 18}, MessageHeader: chunk.MessageHeader{0, 1234, false, 8, 2, 3}, ExtendedTimestamp: chunk.ExtendedTimestamp{}, }, Data: []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, } chunk.NewWriter(b, 4).Write(c) r := chunk.NewReader(b, 4, chunk.NoopNormalizer) go r.Recv() read := <-r.Chunks() assert.Equal(t, 0, len(r.Errs())) assert.Equal(t, c, read) }
func TestWritingAControlChunksIt(t *testing.T) { ctrl := new(control.Acknowledgement) chunker := &MockChunker{} chunker.On("Chunk", ctrl).Return(&chunk.Chunk{ Header: &chunk.Header{ BasicHeader: chunk.BasicHeader{0, 2}, MessageHeader: chunk.MessageHeader{}, }, }, nil) stream := control.NewStream( newStreamWithChunk(2), chunk.NewWriter(ioutil.Discard, chunk.DefaultReadSize), nil, chunker, ) err := stream.Send(ctrl) assert.Nil(t, err) chunker.AssertExpectations(t) }
func TestSendablesAreWrittenToChunkStream(t *testing.T) { buf := new(bytes.Buffer) nc := NewNetConnection( make(chan *chunk.Chunk), chunk.NewWriter(buf, chunk.DefaultReadSize)) go nc.Listen() err := nc.Send(&CreateStreamResponse{ TransactionId: 1, }) assert.Nil(t, err) assert.Equal(t, []byte{ 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1d, 0x14, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x07, 0x5f, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x00, 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }, buf.Bytes()) }
func TestRecvWritesToChunkWriter(t *testing.T) { buf := new(bytes.Buffer) writer := chunk.NewWriter(buf, 4096) ch := &chunk.Chunk{ Header: new(chunk.Header), Data: []byte{0x0, 0x1, 0x2, 0x3}, } d := new(MockData) d.On("Marshal").Return(ch, nil).Once() s := data.NewStream(make(chan *chunk.Chunk), writer) go s.Recv() err := s.Write(d) assert.Equal(t, []byte{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, }, buf.Bytes()) assert.Nil(t, err) }
// New instantiates and returns a pointer to a new instance of type Client. The // client is initialized with the given connection. func New(conn io.ReadWriter) *Client { chunkWriter := chunk.NewWriter(conn, 4096) chunks := chunk.NewParser(chunk.NewReader( conn, chunk.DefaultReadSize, chunk.NewNormalizer(), )) controlChunks, _ := chunks.Stream(2) netChunks, _ := chunks.Stream(3, 4, 5, 8) return &Client{ chunks: chunks, controlStream: control.NewStream( controlChunks, chunkWriter, control.NewParser(), control.NewChunker(), ), cmdManager: cmd.New(netChunks, chunkWriter), Conn: conn, } }
func TestWriterConstruction(t *testing.T) { w := chunk.NewWriter(new(bytes.Buffer), chunk.DefaultReadSize) assert.IsType(t, &chunk.DefaultWriter{}, w) }