From bc864e99f3bc74ba5649e5d8c206c88d18e05619 Mon Sep 17 00:00:00 2001 From: Mark Chmarny Date: Wed, 24 Jun 2020 06:11:15 -0700 Subject: [PATCH] TestMain, test coverage --- Makefile | 5 ++++- client/binding_test.go | 9 ++------- client/client_test.go | 29 ++++++++++++++++------------- client/invoke_test.go | 10 ++-------- client/pubsub_test.go | 5 +---- client/secret_test.go | 7 ++----- client/state_test.go | 8 +++----- 7 files changed, 30 insertions(+), 43 deletions(-) diff --git a/Makefile b/Makefile index 931bbafb..e6dfd8ff 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ RELEASE_VERSION =v0.8.0 GDOC_PORT =8888 PROTO_ROOT =https://raw.githubusercontent.com/dapr/dapr/master/dapr/proto/ -.PHONY: mod test service client lint protps tag lint docs clean protos help +.PHONY: mod test cover service client lint protps tag docs clean help all: test mod: ## Updates the go modules @@ -12,6 +12,9 @@ test: mod ## Tests the entire project go test -v -count=1 -race ./... # go test -v -count=1 -run NameOfSingleTest ./... +cover: mod ## Displays test coverage in the Client package + go test -coverprofile=cover.out ./client && go tool cover -html=cover.out + service: mod ## Runs the uncompiled example service code dapr run --app-id serving \ --protocol grpc \ diff --git a/client/binding_test.go b/client/binding_test.go index 3cccccf8..9caf3124 100644 --- a/client/binding_test.go +++ b/client/binding_test.go @@ -9,13 +9,11 @@ import ( func TestInvokeBinding(t *testing.T) { ctx := context.Background() - client, closer := getTestClient(ctx, t) - defer closer() mIn := make(map[string]string) mIn["test"] = "value" - out, mOut, err := client.InvokeBinding(ctx, "serving", "EchoMethod", []byte("ping"), mIn) + out, mOut, err := testClient.InvokeBinding(ctx, "serving", "EchoMethod", []byte("ping"), mIn) assert.Nil(t, err) assert.NotNil(t, mOut) assert.NotNil(t, out) @@ -23,9 +21,6 @@ func TestInvokeBinding(t *testing.T) { func TestInvokeOutputBinding(t *testing.T) { ctx := context.Background() - client, closer := getTestClient(ctx, t) - defer closer() - - err := client.InvokeOutputBinding(ctx, "serving", "EchoMethod", []byte("ping")) + err := testClient.InvokeOutputBinding(ctx, "serving", "EchoMethod", []byte("ping")) assert.Nil(t, err) } diff --git a/client/client_test.go b/client/client_test.go index cc0b9a8f..eb08b9a7 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -3,6 +3,7 @@ package client import ( "context" "net" + "os" "testing" "github.com/golang/protobuf/ptypes/empty" @@ -19,12 +20,17 @@ const ( testBufSize = 1024 * 1024 ) -func TestNewClientWithConnection(t *testing.T) { +var ( + testClient Client +) + +func TestMain(m *testing.M) { ctx := context.Background() - client, closer := getTestClient(ctx, t) - assert.NotNil(t, closer) - defer closer() - assert.NotNil(t, client) + c, f := getTestClient(ctx) + testClient = c + r := m.Run() + f() + os.Exit(r) } func TestNewClientWithoutArgs(t *testing.T) { @@ -34,16 +40,13 @@ func TestNewClientWithoutArgs(t *testing.T) { assert.NotNil(t, err) } -func getTestClient(ctx context.Context, t *testing.T) (client Client, closer func()) { - l := bufconn.Listen(testBufSize) +func getTestClient(ctx context.Context) (client Client, closer func()) { s := grpc.NewServer() - - server := &testDaprServer{ + pb.RegisterDaprServer(s, &testDaprServer{ state: make(map[string][]byte), - } - - pb.RegisterDaprServer(s, server) + }) + l := bufconn.Listen(testBufSize) go func() { if err := s.Serve(l); err != nil { logger.Fatalf("test server exited with error: %v", err) @@ -56,7 +59,7 @@ func getTestClient(ctx context.Context, t *testing.T) (client Client, closer fun c, err := grpc.DialContext(ctx, "", d, grpc.WithInsecure()) if err != nil { - t.Fatalf("failed to dial test context: %v", err) + logger.Fatalf("failed to dial test context: %v", err) } closer = func() { diff --git a/client/invoke_test.go b/client/invoke_test.go index 7110d2b5..a6982aff 100644 --- a/client/invoke_test.go +++ b/client/invoke_test.go @@ -9,10 +9,7 @@ import ( func TestInvokeServiceWithContent(t *testing.T) { ctx := context.Background() - client, closer := getTestClient(ctx, t) - defer closer() - - resp, err := client.InvokeServiceWithContent(ctx, "serving", "EchoMethod", + resp, err := testClient.InvokeServiceWithContent(ctx, "serving", "EchoMethod", "text/plain; charset=UTF-8", []byte("ping")) assert.Nil(t, err) assert.NotNil(t, resp) @@ -21,10 +18,7 @@ func TestInvokeServiceWithContent(t *testing.T) { func TestInvokeService(t *testing.T) { ctx := context.Background() - client, closer := getTestClient(ctx, t) - defer closer() - - resp, err := client.InvokeService(ctx, "serving", "EchoMethod") + resp, err := testClient.InvokeService(ctx, "serving", "EchoMethod") assert.Nil(t, err) assert.Nil(t, resp) } diff --git a/client/pubsub_test.go b/client/pubsub_test.go index be47e2cc..9246df24 100644 --- a/client/pubsub_test.go +++ b/client/pubsub_test.go @@ -9,9 +9,6 @@ import ( func TestPublishEvent(t *testing.T) { ctx := context.Background() - client, closer := getTestClient(ctx, t) - defer closer() - - err := client.PublishEvent(ctx, "serving", []byte("ping")) + err := testClient.PublishEvent(ctx, "serving", []byte("ping")) assert.Nil(t, err) } diff --git a/client/secret_test.go b/client/secret_test.go index 44e8db30..344b71ae 100644 --- a/client/secret_test.go +++ b/client/secret_test.go @@ -9,17 +9,14 @@ import ( func TestGetSecret(t *testing.T) { ctx := context.Background() - client, closer := getTestClient(ctx, t) - defer closer() - - out, err := client.GetSecret(ctx, "store", "key1", nil) + out, err := testClient.GetSecret(ctx, "store", "key1", nil) assert.Nil(t, err) assert.NotNil(t, out) in := make(map[string]string) in["test"] = "value" - out, err = client.GetSecret(ctx, "store", "key1", in) + out, err = testClient.GetSecret(ctx, "store", "key1", in) assert.Nil(t, err) assert.NotNil(t, out) } diff --git a/client/state_test.go b/client/state_test.go index f7bd1cce..f067d655 100644 --- a/client/state_test.go +++ b/client/state_test.go @@ -39,18 +39,16 @@ func TestStateOptionsConverter(t *testing.T) { func TestSaveStateData(t *testing.T) { ctx := context.Background() data := "test" - client, closer := getTestClient(ctx, t) - defer closer() - err := client.SaveStateData(ctx, "store", "key1", "", []byte(data)) + err := testClient.SaveStateData(ctx, "store", "key1", "", []byte(data)) assert.Nil(t, err) - out, etag, err := client.GetState(ctx, "store", "key1") + out, etag, err := testClient.GetState(ctx, "store", "key1") assert.Nil(t, err) assert.NotEmpty(t, etag) assert.NotNil(t, out) assert.Equal(t, string(out), data) - err = client.DeleteState(ctx, "store", "key1") + err = testClient.DeleteState(ctx, "store", "key1") assert.Nil(t, err) }