Created
January 11, 2020 05:45
-
-
Save sugamon/e9fe0c3066069d0b4d77469eec472e43 to your computer and use it in GitHub Desktop.
pubsub apiv1 publisher test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pubsub | |
import ( | |
"context" | |
"testing" | |
"cloud.google.com/go/pubsub/pstest" | |
"google.golang.org/api/option" | |
pubsubpb "google.golang.org/genproto/googleapis/pubsub/v1" | |
"google.golang.org/grpc" | |
) | |
func TestPublisher_Publish(t *testing.T) { | |
t.Helper() | |
// テストケース作成 | |
cases := map[string]struct { | |
topic string | |
msg string | |
createFlag bool | |
expected bool | |
}{ | |
"topicが存在しない場合失敗": { | |
topic: "projects/apiv1-sample/topics/xxxxx", | |
msg: "message-1", | |
createFlag: false, | |
expected: true, | |
}, | |
"成功": { | |
topic: "projects/apiv1-sample/topics/test-topic", | |
msg: "message-2", | |
createFlag: true, | |
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, option.WithGRPCConn(conn)) | |
if c.createFlag { | |
req := &pubsubpb.Topic{ | |
Name: c.topic, | |
} | |
p.Client.CreateTopic(ctx, req) | |
} | |
err := p.Publish(ctx, c.topic, c.msg) | |
if (err != nil) != c.expected { | |
t.Errorf("TestPublisher_Publish 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