| 1 |
// Copyright 2018 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package gcppubsub |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"fmt" |
| 20 |
"path" |
| 21 |
"strings" |
| 22 |
"sync/atomic" |
| 23 |
"testing" |
| 24 |
|
| 25 |
raw "cloud.google.com/go/pubsub/apiv1" |
| 26 |
"cloud.google.com/go/pubsub/apiv1/pubsubpb" |
| 27 |
"gocloud.dev/gcp" |
| 28 |
"gocloud.dev/internal/testing/setup" |
| 29 |
"gocloud.dev/pubsub" |
| 30 |
"gocloud.dev/pubsub/driver" |
| 31 |
"gocloud.dev/pubsub/drivertest" |
| 32 |
"google.golang.org/grpc/codes" |
| 33 |
"google.golang.org/grpc/status" |
| 34 |
) |
| 35 |
|
| 36 |
// projectID is the project ID that was used during the last test run using |
| 37 |
// --record. |
| 38 |
const projectID = "go-cloud-test-216917" |
| 39 |
|
| 40 |
type harness struct { |
| 41 |
closer func() |
| 42 |
pubClient *raw.PublisherClient |
| 43 |
subClient *raw.SubscriberClient |
| 44 |
numTopics uint32 // atomic |
| 45 |
numSubs uint32 // atomic |
| 46 |
} |
| 47 |
|
| 48 |
func newHarness(ctx context.Context, t *testing.T) (drivertest.Harness, error) { |
| 49 |
t.Helper() |
| 50 |
|
| 51 |
conn, done := setup.NewGCPgRPCConn(ctx, t, endPoint, "pubsub") |
| 52 |
pubClient, err := PublisherClient(ctx, conn) |
| 53 |
if err != nil { |
| 54 |
return nil, fmt.Errorf("making publisher client: %v", err) |
| 55 |
} |
| 56 |
subClient, err := SubscriberClient(ctx, conn) |
| 57 |
if err != nil { |
| 58 |
return nil, fmt.Errorf("making subscription client: %v", err) |
| 59 |
} |
| 60 |
return &harness{closer: done, pubClient: pubClient, subClient: subClient, numTopics: 0, numSubs: 0}, nil |
| 61 |
} |
| 62 |
|
| 63 |
func (h *harness) CreateTopic(ctx context.Context, testName string) (dt driver.Topic, cleanup func(), err error) { |
| 64 |
// We may encounter topics that were created by a previous test run and were |
| 65 |
// not properly cleaned up. In such a case delete the existing topic and create |
| 66 |
// a new topic with a higher topic number (to avoid cool-off issues between |
| 67 |
// deletion and re-creation). |
| 68 |
for { |
| 69 |
topicName := fmt.Sprintf("%s-topic-%d", sanitize(testName), atomic.AddUint32(&h.numTopics, 1)) |
| 70 |
topicPath := fmt.Sprintf("projects/%s/topics/%s", projectID, topicName) |
| 71 |
dt, cleanup, err := createTopic(ctx, h.pubClient, topicName, topicPath) |
| 72 |
if err != nil && status.Code(err) == codes.AlreadyExists { |
| 73 |
// Delete the topic and retry. |
| 74 |
h.pubClient.DeleteTopic(ctx, &pubsubpb.DeleteTopicRequest{Topic: topicPath}) |
| 75 |
continue |
| 76 |
} |
| 77 |
return dt, cleanup, err |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
func createTopic(ctx context.Context, pubClient *raw.PublisherClient, topicName, topicPath string) (dt driver.Topic, cleanup func(), err error) { |
| 82 |
_, err = pubClient.CreateTopic(ctx, &pubsubpb.Topic{Name: topicPath}) |
| 83 |
if err != nil { |
| 84 |
return nil, nil, err |
| 85 |
} |
| 86 |
dt = openTopic(pubClient, path.Join("projects", projectID, "topics", topicName)) |
| 87 |
cleanup = func() { |
| 88 |
pubClient.DeleteTopic(ctx, &pubsubpb.DeleteTopicRequest{Topic: topicPath}) |
| 89 |
} |
| 90 |
return dt, cleanup, nil |
| 91 |
} |
| 92 |
|
| 93 |
func (h *harness) MakeNonexistentTopic(ctx context.Context) (driver.Topic, error) { |
| 94 |
return openTopic(h.pubClient, path.Join("projects", projectID, "topics", "nonexistent-topic")), nil |
| 95 |
} |
| 96 |
|
| 97 |
func (h *harness) CreateSubscription(ctx context.Context, dt driver.Topic, testName string) (ds driver.Subscription, cleanup func(), err error) { |
| 98 |
// We may encounter subscriptions that were created by a previous test run |
| 99 |
// and were not properly cleaned up. In such a case delete the existing |
| 100 |
// subscription and create a new subscription with a higher subscription |
| 101 |
// number (to avoid cool-off issues between deletion and re-creation). |
| 102 |
for { |
| 103 |
subName := fmt.Sprintf("%s-subscription-%d", sanitize(testName), atomic.AddUint32(&h.numSubs, 1)) |
| 104 |
subPath := fmt.Sprintf("projects/%s/subscriptions/%s", projectID, subName) |
| 105 |
ds, cleanup, err := createSubscription(ctx, h.subClient, dt, subName, subPath) |
| 106 |
if err != nil && status.Code(err) == codes.AlreadyExists { |
| 107 |
// Delete the subscription and retry. |
| 108 |
h.subClient.DeleteSubscription(ctx, &pubsubpb.DeleteSubscriptionRequest{Subscription: subPath}) |
| 109 |
continue |
| 110 |
} |
| 111 |
return ds, cleanup, err |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
func createSubscription(ctx context.Context, subClient *raw.SubscriberClient, dt driver.Topic, subName, subPath string) (ds driver.Subscription, cleanup func(), err error) { |
| 116 |
t := dt.(*topic) |
| 117 |
_, err = subClient.CreateSubscription(ctx, &pubsubpb.Subscription{ |
| 118 |
Name: subPath, |
| 119 |
Topic: t.path, |
| 120 |
}) |
| 121 |
if err != nil { |
| 122 |
return nil, nil, err |
| 123 |
} |
| 124 |
ds = openSubscription(subClient, path.Join("projects", projectID, "subscriptions", subName), nil) |
| 125 |
cleanup = func() { |
| 126 |
subClient.DeleteSubscription(ctx, &pubsubpb.DeleteSubscriptionRequest{Subscription: subPath}) |
| 127 |
} |
| 128 |
return ds, cleanup, nil |
| 129 |
} |
| 130 |
|
| 131 |
func (h *harness) MakeNonexistentSubscription(ctx context.Context) (driver.Subscription, func(), error) { |
| 132 |
return openSubscription(h.subClient, path.Join("projects", projectID, "subscriptions", "nonexistent-subscription"), nil), func() {}, nil |
| 133 |
} |
| 134 |
|
| 135 |
func (h *harness) Close() { |
| 136 |
h.pubClient.Close() |
| 137 |
h.subClient.Close() |
| 138 |
h.closer() |
| 139 |
} |
| 140 |
|
| 141 |
func (h *harness) MaxBatchSizes() (int, int) { |
| 142 |
return sendBatcherOpts.MaxBatchSize, ackBatcherOpts.MaxBatchSize |
| 143 |
} |
| 144 |
|
| 145 |
func (*harness) SupportsMultipleSubscriptions() bool { return true } |
| 146 |
|
| 147 |
func TestConformance(t *testing.T) { |
| 148 |
asTests := []drivertest.AsTest{gcpAsTest{}} |
| 149 |
drivertest.RunConformanceTests(t, newHarness, asTests) |
| 150 |
} |
| 151 |
|
| 152 |
func BenchmarkGcpPubSub(b *testing.B) { |
| 153 |
ctx := context.Background() |
| 154 |
creds, err := gcp.DefaultCredentials(ctx) |
| 155 |
if err != nil { |
| 156 |
b.Fatal(err) |
| 157 |
} |
| 158 |
|
| 159 |
// Connect. |
| 160 |
conn, cleanup, err := Dial(ctx, gcp.CredentialsTokenSource(creds)) |
| 161 |
if err != nil { |
| 162 |
b.Fatal(err) |
| 163 |
} |
| 164 |
defer cleanup() |
| 165 |
|
| 166 |
// Make topic. |
| 167 |
pc, err := PublisherClient(ctx, conn) |
| 168 |
if err != nil { |
| 169 |
b.Fatal(err) |
| 170 |
} |
| 171 |
topicName := fmt.Sprintf("%s-topic", b.Name()) |
| 172 |
topicPath := fmt.Sprintf("projects/%s/topics/%s", projectID, topicName) |
| 173 |
dt, cleanup1, err := createTopic(ctx, pc, topicName, topicPath) |
| 174 |
if err != nil { |
| 175 |
b.Fatal(err) |
| 176 |
} |
| 177 |
defer cleanup1() |
| 178 |
topic := pubsub.NewTopic(dt, nil) |
| 179 |
defer topic.Shutdown(ctx) |
| 180 |
|
| 181 |
// Make subscription. |
| 182 |
sc, err := SubscriberClient(ctx, conn) |
| 183 |
if err != nil { |
| 184 |
b.Fatal(err) |
| 185 |
} |
| 186 |
subName := fmt.Sprintf("%s-subscription", b.Name()) |
| 187 |
subPath := fmt.Sprintf("projects/%s/subscriptions/%s", projectID, subName) |
| 188 |
ds, cleanup2, err := createSubscription(ctx, sc, dt, subName, subPath) |
| 189 |
if err != nil { |
| 190 |
b.Fatal(err) |
| 191 |
} |
| 192 |
defer cleanup2() |
| 193 |
sub := pubsub.NewSubscription(ds, defaultRecvBatcherOpts, ackBatcherOpts) |
| 194 |
defer sub.Shutdown(ctx) |
| 195 |
|
| 196 |
drivertest.RunBenchmarks(b, topic, sub) |
| 197 |
} |
| 198 |
|
| 199 |
type gcpAsTest struct{} |
| 200 |
|
| 201 |
func (gcpAsTest) Name() string { |
| 202 |
return "gcp test" |
| 203 |
} |
| 204 |
|
| 205 |
func (gcpAsTest) TopicCheck(topic *pubsub.Topic) error { |
| 206 |
var c2 raw.PublisherClient |
| 207 |
if topic.As(&c2) { |
| 208 |
return fmt.Errorf("cast succeeded for %T, want failure", &c2) |
| 209 |
} |
| 210 |
var c3 *raw.PublisherClient |
| 211 |
if !topic.As(&c3) { |
| 212 |
return fmt.Errorf("cast failed for %T", &c3) |
| 213 |
} |
| 214 |
return nil |
| 215 |
} |
| 216 |
|
| 217 |
func (gcpAsTest) SubscriptionCheck(sub *pubsub.Subscription) error { |
| 218 |
var c2 raw.SubscriberClient |
| 219 |
if sub.As(&c2) { |
| 220 |
return fmt.Errorf("cast succeeded for %T, want failure", &c2) |
| 221 |
} |
| 222 |
var c3 *raw.SubscriberClient |
| 223 |
if !sub.As(&c3) { |
| 224 |
return fmt.Errorf("cast failed for %T", &c3) |
| 225 |
} |
| 226 |
return nil |
| 227 |
} |
| 228 |
|
| 229 |
func (gcpAsTest) TopicErrorCheck(t *pubsub.Topic, err error) error { |
| 230 |
var s *status.Status |
| 231 |
if !t.ErrorAs(err, &s) { |
| 232 |
return fmt.Errorf("failed to convert %v (%T) to a gRPC Status", err, err) |
| 233 |
} |
| 234 |
if s.Code() != codes.NotFound { |
| 235 |
return fmt.Errorf("got code %s, want NotFound", s.Code()) |
| 236 |
} |
| 237 |
return nil |
| 238 |
} |
| 239 |
|
| 240 |
func (gcpAsTest) SubscriptionErrorCheck(sub *pubsub.Subscription, err error) error { |
| 241 |
var s *status.Status |
| 242 |
if !sub.ErrorAs(err, &s) { |
| 243 |
return fmt.Errorf("failed to convert %v (%T) to a gRPC Status", err, err) |
| 244 |
} |
| 245 |
if s.Code() != codes.NotFound { |
| 246 |
return fmt.Errorf("got code %s, want NotFound", s.Code()) |
| 247 |
} |
| 248 |
return nil |
| 249 |
} |
| 250 |
|
| 251 |
func (gcpAsTest) MessageCheck(m *pubsub.Message) error { |
| 252 |
var pm pubsubpb.PubsubMessage |
| 253 |
if m.As(&pm) { |
| 254 |
return fmt.Errorf("cast succeeded for %T, want failure", &pm) |
| 255 |
} |
| 256 |
var ppm *pubsubpb.PubsubMessage |
| 257 |
if !m.As(&ppm) { |
| 258 |
return fmt.Errorf("cast failed for %T", &ppm) |
| 259 |
} |
| 260 |
var prm *pubsubpb.ReceivedMessage |
| 261 |
if !m.As(&prm) { |
| 262 |
return fmt.Errorf("cast failed for %T", &prm) |
| 263 |
} |
| 264 |
return nil |
| 265 |
} |
| 266 |
|
| 267 |
func (gcpAsTest) BeforeSend(as func(any) bool) error { |
| 268 |
var ppm *pubsubpb.PubsubMessage |
| 269 |
if !as(&ppm) { |
| 270 |
return fmt.Errorf("cast failed for %T", &ppm) |
| 271 |
} |
| 272 |
return nil |
| 273 |
} |
| 274 |
|
| 275 |
func (gcpAsTest) AfterSend(as func(any) bool) error { |
| 276 |
var msgId string |
| 277 |
if !as(&msgId) { |
| 278 |
return fmt.Errorf("cast failed for %T", &msgId) |
| 279 |
} |
| 280 |
return nil |
| 281 |
} |
| 282 |
|
| 283 |
func sanitize(testName string) string { |
| 284 |
return strings.Replace(testName, "/", "_", -1) |
| 285 |
} |
| 286 |
|
| 287 |
func TestOpenTopic(t *testing.T) { |
| 288 |
ctx := context.Background() |
| 289 |
creds, err := setup.FakeGCPCredentials(ctx) |
| 290 |
if err != nil { |
| 291 |
t.Fatal(err) |
| 292 |
} |
| 293 |
projID, err := gcp.DefaultProjectID(creds) |
| 294 |
if err != nil { |
| 295 |
t.Fatal(err) |
| 296 |
} |
| 297 |
conn, cleanup, err := Dial(ctx, gcp.CredentialsTokenSource(creds)) |
| 298 |
if err != nil { |
| 299 |
t.Fatal(err) |
| 300 |
} |
| 301 |
defer cleanup() |
| 302 |
pc, err := PublisherClient(ctx, conn) |
| 303 |
if err != nil { |
| 304 |
t.Fatal(err) |
| 305 |
} |
| 306 |
topic := OpenTopic(pc, projID, "my-topic", nil) |
| 307 |
defer topic.Shutdown(ctx) |
| 308 |
err = topic.Send(ctx, &pubsub.Message{Body: []byte("hello world")}) |
| 309 |
if err == nil { |
| 310 |
t.Error("got nil, want error") |
| 311 |
} |
| 312 |
|
| 313 |
// Repeat with OpenTopicByPath. |
| 314 |
topic, err = OpenTopicByPath(pc, path.Join("projects", string(projID), "topics", "my-topic"), nil) |
| 315 |
if err != nil { |
| 316 |
t.Fatal(err) |
| 317 |
} |
| 318 |
defer topic.Shutdown(ctx) |
| 319 |
err = topic.Send(ctx, &pubsub.Message{Body: []byte("hello world")}) |
| 320 |
if err == nil { |
| 321 |
t.Error("got nil, want error") |
| 322 |
} |
| 323 |
|
| 324 |
// Try an invalid path. |
| 325 |
_, err = OpenTopicByPath(pc, "my-topic", nil) |
| 326 |
if err == nil { |
| 327 |
t.Error("got nil, want error") |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
func TestOpenSubscription(t *testing.T) { |
| 332 |
ctx := context.Background() |
| 333 |
creds, err := setup.FakeGCPCredentials(ctx) |
| 334 |
if err != nil { |
| 335 |
t.Fatal(err) |
| 336 |
} |
| 337 |
projID, err := gcp.DefaultProjectID(creds) |
| 338 |
if err != nil { |
| 339 |
t.Fatal(err) |
| 340 |
} |
| 341 |
conn, cleanup, err := Dial(ctx, gcp.CredentialsTokenSource(creds)) |
| 342 |
if err != nil { |
| 343 |
t.Fatal(err) |
| 344 |
} |
| 345 |
defer cleanup() |
| 346 |
sc, err := SubscriberClient(ctx, conn) |
| 347 |
if err != nil { |
| 348 |
t.Fatal(err) |
| 349 |
} |
| 350 |
sub := OpenSubscription(sc, projID, "my-subscription", nil) |
| 351 |
defer sub.Shutdown(ctx) |
| 352 |
_, err = sub.Receive(ctx) |
| 353 |
if err == nil { |
| 354 |
t.Error("got nil, want error") |
| 355 |
} |
| 356 |
|
| 357 |
// Repeat with OpenSubscriptionByPath. |
| 358 |
sub, err = OpenSubscriptionByPath(sc, path.Join("projects", string(projID), "subscriptions", "my-subscription"), nil) |
| 359 |
if err != nil { |
| 360 |
t.Fatal(err) |
| 361 |
} |
| 362 |
defer sub.Shutdown(ctx) |
| 363 |
_, err = sub.Receive(ctx) |
| 364 |
if err == nil { |
| 365 |
t.Error("got nil, want error") |
| 366 |
} |
| 367 |
|
| 368 |
// Try an invalid path. |
| 369 |
_, err = OpenSubscriptionByPath(sc, "my-subscription", nil) |
| 370 |
if err == nil { |
| 371 |
t.Error("got nil, want error") |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
func TestOpenTopicFromURL(t *testing.T) { |
| 376 |
cleanup := setup.FakeGCPDefaultCredentials(t) |
| 377 |
defer cleanup() |
| 378 |
|
| 379 |
tests := []struct { |
| 380 |
URL string |
| 381 |
WantErr bool |
| 382 |
}{ |
| 383 |
// OK, short form. |
| 384 |
{"gcppubsub://myproject/mytopic", false}, |
| 385 |
// OK, long form. |
| 386 |
{"gcppubsub://projects/myproject/topic/mytopic", false}, |
| 387 |
// Invalid parameter. |
| 388 |
{"gcppubsub://myproject/mytopic?param=value", true}, |
| 389 |
// Valid max_send_batch_size |
| 390 |
{"gcppubsub://projects/mytopic?max_send_batch_size=1", false}, |
| 391 |
// Invalid max_send_batch_size |
| 392 |
{"gcppubsub://projects/mytopic?max_send_batch_size=0", true}, |
| 393 |
// Invalid max_send_batch_size |
| 394 |
{"gcppubsub://projects/mytopic?max_send_batch_size=1001", true}, |
| 395 |
} |
| 396 |
|
| 397 |
ctx := context.Background() |
| 398 |
for _, test := range tests { |
| 399 |
topic, err := pubsub.OpenTopic(ctx, test.URL) |
| 400 |
if (err != nil) != test.WantErr { |
| 401 |
t.Errorf("%s: got error %v, want error %v", test.URL, err, test.WantErr) |
| 402 |
} |
| 403 |
if topic != nil { |
| 404 |
topic.Shutdown(ctx) |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
func TestOpenSubscriptionFromURL(t *testing.T) { |
| 410 |
cleanup := setup.FakeGCPDefaultCredentials(t) |
| 411 |
defer cleanup() |
| 412 |
|
| 413 |
tests := []struct { |
| 414 |
URL string |
| 415 |
WantErr bool |
| 416 |
}{ |
| 417 |
// OK, short form. |
| 418 |
{"gcppubsub://myproject/mysub", false}, |
| 419 |
// OK, long form. |
| 420 |
{"gcppubsub://projects/myproject/subscriptions/mysub", false}, |
| 421 |
// Invalid parameter. |
| 422 |
{"gcppubsub://myproject/mysub?param=value", true}, |
| 423 |
// Valid max_recv_batch_size |
| 424 |
{"gcppubsub://projects/myproject/subscriptions/mysub?max_recv_batch_size=1", false}, |
| 425 |
// Invalid max_recv_batch_size |
| 426 |
{"gcppubsub://projects/myproject/subscriptions/mysub?max_recv_batch_size=0", true}, |
| 427 |
// Invalid max_recv_batch_size |
| 428 |
{"gcppubsub://projects/myproject/subscriptions/mysub?max_recv_batch_size=1001", true}, |
| 429 |
// Valid nacklazy |
| 430 |
{"gcppubsub://projects/myproject/subscriptions/mysub?nacklazy=true", false}, |
| 431 |
// Invalid nacklazy |
| 432 |
{"gcppubsub://projects/myproject/subscriptions/mysub?nacklazy=foo", true}, |
| 433 |
} |
| 434 |
|
| 435 |
ctx := context.Background() |
| 436 |
for _, test := range tests { |
| 437 |
sub, err := pubsub.OpenSubscription(ctx, test.URL) |
| 438 |
if (err != nil) != test.WantErr { |
| 439 |
t.Errorf("%s: got error %v, want error %v", test.URL, err, test.WantErr) |
| 440 |
} |
| 441 |
if sub != nil { |
| 442 |
sub.Shutdown(ctx) |
| 443 |
} |
| 444 |
} |
| 445 |
} |