ttrpc/channel_test.go
Stephen J Day 7f752bf263
ttrpc: handle concurrent requests and responses
With this changeset, ttrpc can now handle mutliple outstanding requests
and responses on the same connection without blocking. On the
server-side, we dispatch a goroutine per outstanding reequest. On the
client side, a management goroutine dispatches responses to blocked
waiters.

The protocol has been changed to support this behavior by including a
"stream id" that can used to identify which request a response belongs
to on the client-side of the connection. With these changes, we should
also be able to support streams in the future.

Signed-off-by: Stephen J Day <stephen.day@docker.com>
2017-11-21 21:38:38 -08:00

86 lines
1.7 KiB
Go

package ttrpc
import (
"bufio"
"bytes"
"context"
"io"
"reflect"
"testing"
"github.com/pkg/errors"
)
func TestReadWriteMessage(t *testing.T) {
var (
ctx = context.Background()
buffer bytes.Buffer
w = bufio.NewWriter(&buffer)
ch = newChannel(w, nil)
messages = [][]byte{
[]byte("hello"),
[]byte("this is a test"),
[]byte("of message framing"),
}
)
for i, msg := range messages {
if err := ch.send(ctx, uint32(i), 1, msg); err != nil {
t.Fatal(err)
}
}
var (
received [][]byte
r = bufio.NewReader(bytes.NewReader(buffer.Bytes()))
rch = newChannel(nil, r)
)
for {
var p [4096]byte
mh, err := rch.recv(ctx, p[:])
if err != nil {
if errors.Cause(err) != io.EOF {
t.Fatal(err)
}
break
}
received = append(received, p[:mh.Length])
}
if !reflect.DeepEqual(received, messages) {
t.Fatal("didn't received expected set of messages: %v != %v", received, messages)
}
}
func TestSmallBuffer(t *testing.T) {
var (
ctx = context.Background()
buffer bytes.Buffer
w = bufio.NewWriter(&buffer)
ch = newChannel(w, nil)
msg = []byte("a message of massive length")
)
if err := ch.send(ctx, 1, 1, msg); err != nil {
t.Fatal(err)
}
// now, read it off the channel with a small buffer
var (
p = make([]byte, len(msg)-1)
r = bufio.NewReader(bytes.NewReader(buffer.Bytes()))
rch = newChannel(nil, r)
)
_, err := rch.recv(ctx, p[:])
if err == nil {
t.Fatalf("error expected reading with small buffer")
}
if errors.Cause(err) != io.ErrShortBuffer {
t.Fatalf("errors.Cause(err) should equal io.ErrShortBuffer: %v != %v", err, io.ErrShortBuffer)
}
}