func TestTypeLookupForInvalidIds(t *testing.T) {
	p := control.NewParser()

	typ := p.TypeFor(30)

	assert.Nil(t, typ)
}
func TestTypeLookupForValidIds(t *testing.T) {
	p := control.NewParser()
	expected := reflect.TypeOf(control.SetChunkSize{})

	typ := p.TypeFor(1)

	assert.True(t, typ.AssignableTo(expected))
}
func TestParsingForInvalidChunks(t *testing.T) {
	p := control.NewParser()

	ctrl, err := p.Parse(&chunk.Chunk{
		Header: &chunk.Header{
			MessageHeader: chunk.MessageHeader{TypeId: 30},
		},
	})

	assert.Nil(t, ctrl)
	assert.Equal(t, "control: unknown control type (30)", err.Error())
}
func TestParsingForValidChunks(t *testing.T) {
	n := rand.Uint32()
	p := control.NewParser()

	ctrl, err := p.Parse(&chunk.Chunk{
		Header: &chunk.Header{
			MessageHeader: chunk.MessageHeader{TypeId: 3},
		},
		Data: []byte{
			byte((n >> 24) & 0xff), byte((n >> 16) & 0xff),
			byte((n >> 8) & 0xff), byte((n >> 0) & 0xff),
		},
	})

	assert.Nil(t, err)
	assert.Equal(t, &control.Acknowledgement{n}, ctrl)
}
Example #5
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,
	}
}
func TestParserConstruction(t *testing.T) {
	p := control.NewParser()

	assert.IsType(t, &control.DefaultParser{}, p)
}