ttrpc: add test to ensure not found errors

Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
Stephen J Day 2017-11-29 11:23:20 -08:00
parent 7e38ac9e99
commit ed51a24609
No known key found for this signature in database
GPG Key ID: 67B3DED84EDC823F

View File

@ -10,6 +10,8 @@ import (
"time" "time"
"github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/proto"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
) )
const serviceName = "testService" const serviceName = "testService"
@ -104,15 +106,34 @@ func TestServer(t *testing.T) {
} }
} }
func newTestClient(t *testing.T, addr string) (*Client, func()) { func TestServerNotFound(t *testing.T) {
conn, err := net.Dial("unix", addr) var (
if err != nil { ctx = context.Background()
server = NewServer()
addr, listener = newTestListener(t)
errs = make(chan error, 1)
client, cleanup = newTestClient(t, addr)
)
defer cleanup()
defer listener.Close()
go func() {
errs <- server.Serve(listener)
}()
var tp testPayload
if err := client.Call(ctx, "Not", "Found", &tp, &tp); err == nil {
t.Fatalf("expected error from non-existent service call")
} else if status, ok := status.FromError(err); !ok {
t.Fatalf("expected status present in error: %v", err)
} else if status.Code() != codes.NotFound {
t.Fatalf("expected not found for method")
}
if err := server.Shutdown(ctx); err != nil {
t.Fatal(err) t.Fatal(err)
} }
client := NewClient(conn) if err := <-errs; err != ErrServerClosed {
return client, func() { t.Fatal(err)
conn.Close()
client.Close()
} }
} }
@ -264,6 +285,18 @@ func roundTrip(ctx context.Context, t *testing.T, client *testingClient, value s
} }
} }
func newTestClient(t *testing.T, addr string) (*Client, func()) {
conn, err := net.Dial("unix", addr)
if err != nil {
t.Fatal(err)
}
client := NewClient(conn)
return client, func() {
conn.Close()
client.Close()
}
}
func newTestListener(t *testing.T) (string, net.Listener) { func newTestListener(t *testing.T) (string, net.Listener) {
addr := "\x00" + t.Name() addr := "\x00" + t.Name()
listener, err := net.Listen("unix", addr) listener, err := net.Listen("unix", addr)