Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Support encoding.BinaryMarshaler types #75

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Move type transmission checks to common package
  • Loading branch information
stevvooe committed Dec 3, 2014
commit 41b76d7bc17a5fe7758fd569d7d28a8a16970a4b
31 changes: 0 additions & 31 deletions copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"io"
"net"
"os"
"reflect"
"runtime/pprof"
"testing"
"time"
Expand Down Expand Up @@ -130,36 +129,6 @@ func TestByteStreamProxy(t *testing.T) {
SpawnProxyTest(t, client, server, 1)
}

func TestTypeTransmission(t *testing.T) {
// TODO(stevvooe): Ensure that libchan transports can all have this same
// test run against it.

// Add problem types to this type definition. For now, we just care about
// time.Time.
type A struct {
T time.Time
}

expected := A{T: time.Now()}

receiver, sender := Pipe()

go func() {
if err := sender.Send(expected); err != nil {
t.Fatalf("unexpected error sending: %v", err)
}
}()

var received A
if err := receiver.Receive(&received); err != nil {
t.Fatalf("unexpected error receiving: %v", err)
}

if !reflect.DeepEqual(received, expected) {
t.Fatalf("expected structs to be equal: %#v != %#v", received, expected)
}
}

func SpawnProxyTest(t *testing.T, client SendTestRoutine, server ReceiveTestRoutine, proxyCount int) {
endClient := make(chan bool)
endServer := make(chan bool)
Expand Down
28 changes: 3 additions & 25 deletions spdy/copy_test.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,16 @@
package spdy

import (
"reflect"
"testing"
"time"

"github.com/docker/libchan/testutil"
)

func TestTypeTransmission(t *testing.T) {

// Add problem types to this type definition. For now, we just care about
// time.Time.
type A struct {
T time.Time
}

expected := A{T: time.Now()}

sender, receiver, err := Pipe()
if err != nil {
t.Fatalf("error creating pipe: %v", err)
}

go func() {
if err := sender.Send(expected); err != nil {
t.Fatalf("unexpected error sending: %v", err)
}
}()

var received A
if err := receiver.Receive(&received); err != nil {
t.Fatalf("unexpected error receiving: %v", err)
}

if !reflect.DeepEqual(received, expected) {
t.Fatalf("expected structs to be equal: %#v != %#v", received, expected)
}
testutil.CheckTypeTransmission(t, receiver, sender)
}
62 changes: 62 additions & 0 deletions testutil/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Package testutil contains checks that implementations of libchan transports
// can use to check compliance. For now, this will only work with Go, but
// cross-language tests could be added here, as well.
package testutil

import (
"io"
"io/ioutil"
"reflect"
"strings"
"testing"
"time"

"github.com/docker/libchan"
)

func CheckTypeTransmission(t *testing.T, receiver libchan.Receiver, sender libchan.Sender) {
// Add types that should be transmitted by value to this struct. Their
// equality will be tested with reflect.DeepEquals.
type ValueTypes struct {
I int
T time.Time
}

// Add other types, that may include readers or stateful items.
type A struct {
// TODO(stevvooe): Ideally, this would be embedded but libchan doesn't
// seem to transmit embedded structs correctly.
V ValueTypes
Reader io.ReadCloser // TODO(stevvooe): Only io.ReadCloser is support for now.
}

readerContent := "asdf"
expected := A{
V: ValueTypes{
I: 1234,
T: time.Now(),
},
Reader: ioutil.NopCloser(strings.NewReader(readerContent)),
}

go func() {
if err := sender.Send(expected); err != nil {
t.Fatalf("unexpected error sending: %v", err)
}
}()

var received A
if err := receiver.Receive(&received); err != nil {
t.Fatalf("unexpected error receiving: %v", err)
}

if !reflect.DeepEqual(received.V, expected.V) {
t.Fatalf("expected structs to be equal: %#v != %#v", received.V, expected.V)
}

receivedContent, _ := ioutil.ReadAll(received.Reader)
if string(receivedContent) != readerContent {
t.Fatalf("reader transmitted different content %q != %q", receivedContent, readerContent)
}

}
14 changes: 14 additions & 0 deletions testutil/check_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package testutil

import (
"github.com/docker/libchan"

"testing"
)

// TestTypeTransmission tests the main package (to avoid import cycles) and
// provides and example of how this test should be used in other packages.
func TestTypeTransmission(t *testing.T) {
receiver, sender := libchan.Pipe()
CheckTypeTransmission(t, receiver, sender)
}