Exemple #1
0
func TestSuccesfulChunkParsingPushesAControl(t *testing.T) {
	parser := &MockParser{}
	parser.On("Parse", mock.Anything).Return(&control.Acknowledgement{}, nil)

	stream := control.NewStream(newStreamWithChunk(2, TestChunk), nil, parser, nil)
	go stream.Recv()

	ctrl := <-stream.In()

	assert.Equal(t, &control.Acknowledgement{}, ctrl)
	parser.AssertExpectations(t)
}
Exemple #2
0
func TestFailedChunkParsingPushesAnError(t *testing.T) {
	parser := &MockParser{}
	parser.On("Parse", mock.Anything).
		Return(new(control.Acknowledgement), errors.New("test"))

	stream := control.NewStream(newStreamWithChunk(2, TestChunk), nil, parser, nil)
	go stream.Recv()

	err := <-stream.Errs()

	assert.Equal(t, "test", err.Error())
	parser.AssertExpectations(t)
}
Exemple #3
0
func TestWritingAControlErrorsWhenErrored(t *testing.T) {
	ctrl := new(control.Acknowledgement)

	chunker := &MockChunker{}
	chunker.On("Chunk", ctrl).Return(new(chunk.Chunk), errors.New("test"))

	stream := control.NewStream(
		newStreamWithChunk(2), nil, nil, chunker,
	)

	err := stream.Send(ctrl)

	assert.Equal(t, "test", err.Error())
	chunker.AssertExpectations(t)
}
Exemple #4
0
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)
}
Exemple #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,
	}
}
Exemple #6
0
func TestStreamConstruction(t *testing.T) {
	s := control.NewStream(nil, nil, nil, nil)

	assert.IsType(t, &control.Stream{}, s)
}