Skip to content

Instantly share code, notes, and snippets.

@sugamon
Created December 24, 2019 02:24
Show Gist options
  • Save sugamon/8708a2aa25e4a811d5d8273b0080f37a to your computer and use it in GitHub Desktop.
Save sugamon/8708a2aa25e4a811d5d8273b0080f37a to your computer and use it in GitHub Desktop.
sample publisher test
package pubsub
import (
"context"
"testing"
"cloud.google.com/go/pubsub/pstest"
"google.golang.org/api/option"
"google.golang.org/grpc"
)
func TestPublisher(t *testing.T) {
t.Helper()
// テストケース作成
cases := map[string]struct {
projectID string
topicID string
msg string
expected bool
}{
"正常": {
projectID: "pstest-sample",
topicID: "sample-topic",
msg: "sample sample",
expected: false,
},
}
for k, c := range cases {
srv := pstest.NewServer()
conn, _ := grpc.Dial(srv.Addr, grpc.WithInsecure())
defer srv.Close()
defer conn.Close()
t.Run(k, func(t *testing.T) {
ctx := context.Background()
p, _ := NewPublisher(ctx, c.projectID, option.WithGRPCConn(conn))
// topicを事前に作成
p.client.CreateTopic(ctx, c.topicID)
err := p.Publish(ctx, c.topicID, c.msg)
if (err != nil) != c.expected {
t.Errorf("TestPublisher error. got: %v, expected: %v", err, c.expected)
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment