Ejemplo n.º 1
0
func TestReaderConstruction(t *testing.T) {
	r := chunk.NewReader(
		new(bytes.Buffer),
		chunk.DefaultReadSize,
		chunk.NoopNormalizer,
	)

	assert.IsType(t, &chunk.DefaultReader{}, r)
}
Ejemplo n.º 2
0
func NewReader(r io.Reader) chunk.Reader {
	if r == nil {
		r = new(bytes.Buffer)
	}

	return chunk.NewReader(
		r, chunk.DefaultReadSize, chunk.NoopNormalizer,
	)
}
Ejemplo n.º 3
0
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
}
Ejemplo n.º 4
0
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)
}
Ejemplo n.º 5
0
func TestReadMultiChunkMultiPass(t *testing.T) {
	buf := new(bytes.Buffer)
	buf.Write([]byte{
		18, 0, 4, 210, 0, 0, 8, 2, 3, 0, 0, 0, 0, 1, 2, 3, // Chunk: 1, Part: 1
		19, 0, 4, 210, 0, 0, 8, 2, 3, 0, 0, 0, 8, 9, 10, 11, // Chunk: 2, Part: 1
		byte((3 << 6) | 18&63), 4, 5, 6, 7, // Chunk: 1, Part: 2
		byte((3 << 6) | 19&63), 12, 13, 14, 15,
	})

	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},
	}

	r := chunk.NewReader(buf, 4, chunk.NoopNormalizer)
	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)
}
Ejemplo n.º 6
0
// 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,
	}
}