TestMain, test coverage (#24)

This commit is contained in:
Mark Chmarny 2020-06-24 09:39:26 -07:00 committed by GitHub
parent 51f6786159
commit 335a86ac3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 30 additions and 43 deletions

View File

@ -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 \

View File

@ -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)
}

View File

@ -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() {

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}