| 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 drivertest provides a conformance test for implementations of |
| 16 |
// driver. |
| 17 |
package drivertest // import "gocloud.dev/pubsub/drivertest" |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"context" |
| 22 |
"errors" |
| 23 |
"slices" |
| 24 |
"sort" |
| 25 |
"strconv" |
| 26 |
"testing" |
| 27 |
"time" |
| 28 |
|
| 29 |
"github.com/google/go-cmp/cmp" |
| 30 |
"github.com/google/go-cmp/cmp/cmpopts" |
| 31 |
"gocloud.dev/gcerrors" |
| 32 |
"gocloud.dev/internal/escape" |
| 33 |
"gocloud.dev/internal/retry" |
| 34 |
"gocloud.dev/pubsub" |
| 35 |
"gocloud.dev/pubsub/batcher" |
| 36 |
"gocloud.dev/pubsub/driver" |
| 37 |
"golang.org/x/sync/errgroup" |
| 38 |
) |
| 39 |
|
| 40 |
// Harness descibes the functionality test harnesses must provide to run |
| 41 |
// conformance tests. |
| 42 |
type Harness interface { |
| 43 |
// CreateTopic creates a new topic and returns a driver.Topic |
| 44 |
// for testing. The topic may have to be removed manually if the test is |
| 45 |
// abruptly terminated or the network connection fails. |
| 46 |
CreateTopic(ctx context.Context, testName string) (dt driver.Topic, cleanup func(), err error) |
| 47 |
|
| 48 |
// MakeNonexistentTopic makes a driver.Topic referencing a topic that |
| 49 |
// does not exist. |
| 50 |
MakeNonexistentTopic(ctx context.Context) (driver.Topic, error) |
| 51 |
|
| 52 |
// CreateSubscription creates a new subscription, subscribed |
| 53 |
// to the given topic, and returns a driver.Subscription for testing. The |
| 54 |
// subscription may have to be cleaned up manually if the test is abruptly |
| 55 |
// terminated or the network connection fails. |
| 56 |
CreateSubscription(ctx context.Context, t driver.Topic, testName string) (ds driver.Subscription, cleanup func(), err error) |
| 57 |
|
| 58 |
// MakeNonexistentSubscription makes a driver.Subscription referencing a |
| 59 |
// subscription that does not exist. |
| 60 |
MakeNonexistentSubscription(ctx context.Context) (ds driver.Subscription, cleanup func(), err error) |
| 61 |
|
| 62 |
// Close closes resources used by the harness, but does not call Close |
| 63 |
// on the Topics and Subscriptions generated by the Harness. |
| 64 |
Close() |
| 65 |
|
| 66 |
// MaxBatchSizes returns the maximum size of SendBatch/Send(Na|A)cks, or 0 |
| 67 |
// if there's no max. |
| 68 |
MaxBatchSizes() (int, int) |
| 69 |
|
| 70 |
// SupportsMultipleSubscriptions reports whether the driver supports |
| 71 |
// multiple subscriptions for the same topic. |
| 72 |
SupportsMultipleSubscriptions() bool |
| 73 |
} |
| 74 |
|
| 75 |
// HarnessMaker describes functions that construct a harness for running tests. |
| 76 |
// It is called exactly once per test; Harness.Close() will be called when the test is complete. |
| 77 |
type HarnessMaker func(ctx context.Context, t *testing.T) (Harness, error) |
| 78 |
|
| 79 |
// AsTest represents a test of As functionality. |
| 80 |
// The conformance test: |
| 81 |
// 1. Calls TopicCheck. |
| 82 |
// 2. Calls SubscriptionCheck. |
| 83 |
// 3. Sends a message, setting Message.BeforeSend to BeforeSend |
| 84 |
// |
| 85 |
// and Message.AfterSend to AfterSend. |
| 86 |
// |
| 87 |
// 4. Receives the message and calls MessageCheck. |
| 88 |
// 5. Calls TopicErrorCheck. |
| 89 |
// 6. Calls SubscriptionErrorCheck. |
| 90 |
type AsTest interface { |
| 91 |
// Name should return a descriptive name for the test. |
| 92 |
Name() string |
| 93 |
// TopicCheck will be called to allow verifcation of Topic.As. |
| 94 |
TopicCheck(t *pubsub.Topic) error |
| 95 |
// SubscriptionCheck will be called to allow verification of Subscription.As. |
| 96 |
SubscriptionCheck(s *pubsub.Subscription) error |
| 97 |
// TopicErrorCheck will be called to allow verification of Topic.ErrorAs. |
| 98 |
// The error will be the one returned from SendBatch when called with |
| 99 |
// a non-existent topic. |
| 100 |
TopicErrorCheck(t *pubsub.Topic, err error) error |
| 101 |
// SubscriptionErrorCheck will be called to allow verification of |
| 102 |
// Subscription.ErrorAs. |
| 103 |
// The error will be the one returned from ReceiveBatch when called with |
| 104 |
// a non-existent subscription. |
| 105 |
SubscriptionErrorCheck(s *pubsub.Subscription, err error) error |
| 106 |
// MessageCheck will be called to allow verification of Message.As. |
| 107 |
MessageCheck(m *pubsub.Message) error |
| 108 |
// BeforeSend will be used as Message.BeforeSend as part of sending a test |
| 109 |
// message. |
| 110 |
BeforeSend(as func(any) bool) error |
| 111 |
// AfterSend will be used as Message.AfterSend as part of sending a test |
| 112 |
// message. |
| 113 |
AfterSend(as func(any) bool) error |
| 114 |
} |
| 115 |
|
| 116 |
// Many tests set the maximum batch size to 1 to make record/replay stable. |
| 117 |
var batchSizeOne = &batcher.Options{MaxBatchSize: 1, MaxHandlers: 1} |
| 118 |
|
| 119 |
type verifyAsFailsOnNil struct{} |
| 120 |
|
| 121 |
func (verifyAsFailsOnNil) Name() string { |
| 122 |
return "verify As returns false when passed nil" |
| 123 |
} |
| 124 |
|
| 125 |
func (verifyAsFailsOnNil) TopicCheck(t *pubsub.Topic) error { |
| 126 |
if t.As(nil) { |
| 127 |
return errors.New("want Topic.As to return false when passed nil") |
| 128 |
} |
| 129 |
return nil |
| 130 |
} |
| 131 |
|
| 132 |
func (verifyAsFailsOnNil) SubscriptionCheck(s *pubsub.Subscription) error { |
| 133 |
if s.As(nil) { |
| 134 |
return errors.New("want Subscription.As to return false when passed nil") |
| 135 |
} |
| 136 |
return nil |
| 137 |
} |
| 138 |
|
| 139 |
func (verifyAsFailsOnNil) TopicErrorCheck(t *pubsub.Topic, err error) (ret error) { |
| 140 |
defer func() { |
| 141 |
if recover() == nil { |
| 142 |
ret = errors.New("want Topic.ErrorAs to panic when passed nil") |
| 143 |
} |
| 144 |
}() |
| 145 |
t.ErrorAs(err, nil) |
| 146 |
return nil |
| 147 |
} |
| 148 |
|
| 149 |
func (verifyAsFailsOnNil) SubscriptionErrorCheck(s *pubsub.Subscription, err error) (ret error) { |
| 150 |
defer func() { |
| 151 |
if recover() == nil { |
| 152 |
ret = errors.New("want Subscription.ErrorAs to panic when passed nil") |
| 153 |
} |
| 154 |
}() |
| 155 |
s.ErrorAs(err, nil) |
| 156 |
return nil |
| 157 |
} |
| 158 |
|
| 159 |
func (verifyAsFailsOnNil) MessageCheck(m *pubsub.Message) error { |
| 160 |
if m.As(nil) { |
| 161 |
return errors.New("want Message.As to return false when passed nil") |
| 162 |
} |
| 163 |
return nil |
| 164 |
} |
| 165 |
|
| 166 |
func (verifyAsFailsOnNil) BeforeSend(as func(any) bool) error { |
| 167 |
if as(nil) { |
| 168 |
return errors.New("want Message.BeforeSend's As function to return false when passed nil") |
| 169 |
} |
| 170 |
return nil |
| 171 |
} |
| 172 |
|
| 173 |
func (verifyAsFailsOnNil) AfterSend(as func(any) bool) error { |
| 174 |
if as(nil) { |
| 175 |
return errors.New("want Message.AfterSend's As function to return false when passed nil") |
| 176 |
} |
| 177 |
return nil |
| 178 |
} |
| 179 |
|
| 180 |
// RunConformanceTests runs conformance tests for driver implementations of pubsub. |
| 181 |
func RunConformanceTests(t *testing.T, newHarness HarnessMaker, asTests []AsTest) { |
| 182 |
t.Helper() |
| 183 |
|
| 184 |
tests := map[string]func(t *testing.T, newHarness HarnessMaker){ |
| 185 |
"TestSendReceive": testSendReceive, |
| 186 |
"TestSendReceiveTwo": testSendReceiveTwo, |
| 187 |
"TestSendReceiveJSON": testSendReceiveJSON, |
| 188 |
"TestNack": testNack, |
| 189 |
"TestBatching": testBatching, |
| 190 |
"TestDoubleAck": testDoubleAck, |
| 191 |
"TestErrorOnSendToClosedTopic": testErrorOnSendToClosedTopic, |
| 192 |
"TestErrorOnReceiveFromClosedSubscription": testErrorOnReceiveFromClosedSubscription, |
| 193 |
"TestCancelSendReceive": testCancelSendReceive, |
| 194 |
"TestNonExistentTopicSucceedsOnOpenButFailsOnSend": testNonExistentTopicSucceedsOnOpenButFailsOnSend, |
| 195 |
"TestNonExistentSubscriptionSucceedsOnOpenButFailsOnReceive": testNonExistentSubscriptionSucceedsOnOpenButFailsOnReceive, |
| 196 |
"TestMetadata": testMetadata, |
| 197 |
"TestNonUTF8MessageBody": testNonUTF8MessageBody, |
| 198 |
} |
| 199 |
for name, test := range tests { |
| 200 |
t.Run(name, func(t *testing.T) { test(t, newHarness) }) |
| 201 |
} |
| 202 |
|
| 203 |
asTests = append(asTests, verifyAsFailsOnNil{}) |
| 204 |
t.Run("TestAs", func(t *testing.T) { |
| 205 |
for _, st := range asTests { |
| 206 |
if st.Name() == "" { |
| 207 |
t.Fatalf("AsTest.Name is required") |
| 208 |
} |
| 209 |
t.Run(st.Name(), func(t *testing.T) { testAs(t, newHarness, st) }) |
| 210 |
} |
| 211 |
}) |
| 212 |
} |
| 213 |
|
| 214 |
// RunBenchmarks runs benchmarks for driver implementations of pubsub. |
| 215 |
func RunBenchmarks(b *testing.B, topic *pubsub.Topic, sub *pubsub.Subscription) { |
| 216 |
b.Helper() |
| 217 |
|
| 218 |
b.Run("BenchmarkReceive", func(b *testing.B) { |
| 219 |
benchmark(b, topic, sub, false) |
| 220 |
}) |
| 221 |
b.Run("BenchmarkSend", func(b *testing.B) { |
| 222 |
benchmark(b, topic, sub, true) |
| 223 |
}) |
| 224 |
} |
| 225 |
|
| 226 |
func testNonExistentTopicSucceedsOnOpenButFailsOnSend(t *testing.T, newHarness HarnessMaker) { |
| 227 |
t.Helper() |
| 228 |
|
| 229 |
// Set up. |
| 230 |
ctx := context.Background() |
| 231 |
h, err := newHarness(ctx, t) |
| 232 |
if err != nil { |
| 233 |
t.Fatal(err) |
| 234 |
} |
| 235 |
defer h.Close() |
| 236 |
|
| 237 |
dt, err := h.MakeNonexistentTopic(ctx) |
| 238 |
if err != nil { |
| 239 |
// Failure shouldn't happen for non-existent topics until messages are sent |
| 240 |
// to them. |
| 241 |
t.Fatalf("creating a local topic that doesn't exist on the server: %v", err) |
| 242 |
} |
| 243 |
topic := pubsub.NewTopic(dt, nil) |
| 244 |
defer func() { |
| 245 |
if err := topic.Shutdown(ctx); err != nil { |
| 246 |
t.Error(err) |
| 247 |
} |
| 248 |
}() |
| 249 |
|
| 250 |
m := &pubsub.Message{} |
| 251 |
err = topic.Send(ctx, m) |
| 252 |
if err == nil || gcerrors.Code(err) != gcerrors.NotFound { |
| 253 |
t.Errorf("got error %v for send to non-existent topic, want code=NotFound", err) |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
func testNonExistentSubscriptionSucceedsOnOpenButFailsOnReceive(t *testing.T, newHarness HarnessMaker) { |
| 258 |
t.Helper() |
| 259 |
|
| 260 |
// Set up. |
| 261 |
ctx := context.Background() |
| 262 |
h, err := newHarness(ctx, t) |
| 263 |
if err != nil { |
| 264 |
t.Fatal(err) |
| 265 |
} |
| 266 |
defer h.Close() |
| 267 |
|
| 268 |
ds, cleanup, err := h.MakeNonexistentSubscription(ctx) |
| 269 |
if err != nil { |
| 270 |
t.Fatalf("failed to make non-existent subscription: %v", err) |
| 271 |
} |
| 272 |
defer cleanup() |
| 273 |
sub := pubsub.NewSubscription(ds, batchSizeOne, batchSizeOne) |
| 274 |
defer func() { |
| 275 |
if err := sub.Shutdown(ctx); err != nil { |
| 276 |
t.Error(err) |
| 277 |
} |
| 278 |
}() |
| 279 |
|
| 280 |
// The test will hang here if the message isn't available, so use a shorter timeout. |
| 281 |
ctx2, cancel := context.WithTimeout(ctx, 30*time.Second) |
| 282 |
defer cancel() |
| 283 |
_, err = sub.Receive(ctx2) |
| 284 |
if err == nil || ctx2.Err() != nil || gcerrors.Code(err) != gcerrors.NotFound { |
| 285 |
t.Errorf("got error %v for receive from non-existent subscription, want code=NotFound", err) |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
func testSendReceive(t *testing.T, newHarness HarnessMaker) { |
| 290 |
t.Helper() |
| 291 |
|
| 292 |
// Set up. |
| 293 |
ctx := context.Background() |
| 294 |
h, err := newHarness(ctx, t) |
| 295 |
if err != nil { |
| 296 |
t.Fatal(err) |
| 297 |
} |
| 298 |
defer h.Close() |
| 299 |
topic, sub, cleanup, err := makePair(ctx, t, h) |
| 300 |
if err != nil { |
| 301 |
t.Fatal(err) |
| 302 |
} |
| 303 |
defer cleanup() |
| 304 |
|
| 305 |
want := publishN(ctx, t, topic, 3) |
| 306 |
got := receiveN(ctx, t, sub, len(want)) |
| 307 |
|
| 308 |
// Verify LoggableID is set. |
| 309 |
for _, msg := range got { |
| 310 |
if msg.LoggableID == "" { |
| 311 |
t.Errorf("msg.LoggableID was empty, should be set") |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
// Check that the received messages match the sent ones. |
| 316 |
if diff := diffMessageSets(got, want); diff != "" { |
| 317 |
t.Error(diff) |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
// Receive from two subscriptions to the same topic. |
| 322 |
// Verify both get all the messages. |
| 323 |
func testSendReceiveTwo(t *testing.T, newHarness HarnessMaker) { |
| 324 |
t.Helper() |
| 325 |
|
| 326 |
// Set up. |
| 327 |
ctx := context.Background() |
| 328 |
h, err := newHarness(ctx, t) |
| 329 |
if err != nil { |
| 330 |
t.Fatal(err) |
| 331 |
} |
| 332 |
defer h.Close() |
| 333 |
if !h.SupportsMultipleSubscriptions() { |
| 334 |
t.Skip("multiple subscriptions to a topic not supported") |
| 335 |
} |
| 336 |
|
| 337 |
dt, cleanup, err := h.CreateTopic(ctx, t.Name()) |
| 338 |
if err != nil { |
| 339 |
t.Fatal(err) |
| 340 |
} |
| 341 |
defer cleanup() |
| 342 |
topic := pubsub.NewTopic(dt, batchSizeOne) |
| 343 |
defer func() { |
| 344 |
if err := topic.Shutdown(ctx); err != nil { |
| 345 |
t.Error(err) |
| 346 |
} |
| 347 |
}() |
| 348 |
|
| 349 |
var ss []*pubsub.Subscription |
| 350 |
for range 2 { |
| 351 |
ds, cleanup, err := h.CreateSubscription(ctx, dt, t.Name()) |
| 352 |
if err != nil { |
| 353 |
t.Fatal(err) |
| 354 |
} |
| 355 |
defer cleanup() |
| 356 |
s := pubsub.NewSubscription(ds, batchSizeOne, batchSizeOne) |
| 357 |
defer func() { |
| 358 |
if err := s.Shutdown(ctx); err != nil { |
| 359 |
t.Error(err) |
| 360 |
} |
| 361 |
}() |
| 362 |
ss = append(ss, s) |
| 363 |
} |
| 364 |
want := publishN(ctx, t, topic, 3) |
| 365 |
for i, s := range ss { |
| 366 |
got := receiveN(ctx, t, s, len(want)) |
| 367 |
if diff := diffMessageSets(got, want); diff != "" { |
| 368 |
t.Errorf("sub #%d: %s", i, diff) |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
func testSendReceiveJSON(t *testing.T, newHarness HarnessMaker) { |
| 374 |
t.Helper() |
| 375 |
|
| 376 |
const json = `{"Foo": "Bar"}` |
| 377 |
// Set up. |
| 378 |
ctx := context.Background() |
| 379 |
h, err := newHarness(ctx, t) |
| 380 |
if err != nil { |
| 381 |
t.Fatal(err) |
| 382 |
} |
| 383 |
defer h.Close() |
| 384 |
topic, sub, cleanup, err := makePair(ctx, t, h) |
| 385 |
if err != nil { |
| 386 |
t.Fatal(err) |
| 387 |
} |
| 388 |
defer cleanup() |
| 389 |
|
| 390 |
sendM := &pubsub.Message{Body: []byte(json)} |
| 391 |
if err := topic.Send(ctx, sendM); err != nil { |
| 392 |
t.Fatal(err) |
| 393 |
} |
| 394 |
ctx2, cancel := context.WithTimeout(ctx, 30*time.Second) |
| 395 |
defer cancel() |
| 396 |
receiveM, err := sub.Receive(ctx2) |
| 397 |
if err != nil { |
| 398 |
t.Fatal(err) |
| 399 |
} |
| 400 |
receiveM.Ack() |
| 401 |
if diff := diffMessageSets([]*pubsub.Message{receiveM}, []*pubsub.Message{sendM}); diff != "" { |
| 402 |
t.Error(diff) |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
func testNack(t *testing.T, newHarness HarnessMaker) { |
| 407 |
t.Helper() |
| 408 |
|
| 409 |
const nMessages = 2 |
| 410 |
|
| 411 |
// Set up. |
| 412 |
ctx := context.Background() |
| 413 |
h, err := newHarness(ctx, t) |
| 414 |
if err != nil { |
| 415 |
t.Fatal(err) |
| 416 |
} |
| 417 |
defer h.Close() |
| 418 |
dt, topicCleanup, err := h.CreateTopic(ctx, t.Name()) |
| 419 |
if err != nil { |
| 420 |
t.Fatal(err) |
| 421 |
} |
| 422 |
defer topicCleanup() |
| 423 |
ds, subCleanup, err := h.CreateSubscription(ctx, dt, t.Name()) |
| 424 |
if err != nil { |
| 425 |
t.Fatal(err) |
| 426 |
} |
| 427 |
defer subCleanup() |
| 428 |
if !ds.CanNack() { |
| 429 |
t.Skip("Nack not supported") |
| 430 |
} |
| 431 |
topic := pubsub.NewTopic(dt, batchSizeOne) |
| 432 |
defer func() { |
| 433 |
if err := topic.Shutdown(ctx); err != nil { |
| 434 |
t.Error(err) |
| 435 |
} |
| 436 |
}() |
| 437 |
sub := pubsub.NewSubscription(ds, batchSizeOne, batchSizeOne) |
| 438 |
defer func() { |
| 439 |
if err := sub.Shutdown(ctx); err != nil { |
| 440 |
t.Error(err) |
| 441 |
} |
| 442 |
}() |
| 443 |
|
| 444 |
want := publishN(ctx, t, topic, nMessages) |
| 445 |
|
| 446 |
// Get the messages, but nack them. |
| 447 |
// Make sure to nack after receiving all of them; otherwise, we could |
| 448 |
// receive one of the messages twice instead of receiving all nMessages. |
| 449 |
// The test will hang here if the messages aren't redelivered, so use a shorter timeout. |
| 450 |
ctx2, cancel := context.WithTimeout(ctx, 30*time.Second) |
| 451 |
defer cancel() |
| 452 |
var got []*pubsub.Message |
| 453 |
for range nMessages { |
| 454 |
m, err := sub.Receive(ctx2) |
| 455 |
if err != nil { |
| 456 |
t.Fatal(err) |
| 457 |
} |
| 458 |
got = append(got, m) |
| 459 |
} |
| 460 |
for _, m := range got { |
| 461 |
m.Nack() |
| 462 |
} |
| 463 |
// Check that the received messages match the sent ones. |
| 464 |
if diff := diffMessageSets(got, want); diff != "" { |
| 465 |
t.Error(diff) |
| 466 |
} |
| 467 |
// The test will hang here if the messages aren't redelivered, so use a shorter timeout. |
| 468 |
ctx2, cancel = context.WithTimeout(ctx, 30*time.Second) |
| 469 |
defer cancel() |
| 470 |
|
| 471 |
got = nil |
| 472 |
for range nMessages { |
| 473 |
m, err := sub.Receive(ctx2) |
| 474 |
if err != nil { |
| 475 |
t.Fatal(err) |
| 476 |
} |
| 477 |
got = append(got, m) |
| 478 |
m.Ack() |
| 479 |
} |
| 480 |
if diff := diffMessageSets(got, want); diff != "" { |
| 481 |
t.Error(diff) |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
func testBatching(t *testing.T, newHarness HarnessMaker) { |
| 486 |
t.Helper() |
| 487 |
|
| 488 |
const nMessages = 12 // must be divisible by 2 |
| 489 |
const batchSize = nMessages / 2 |
| 490 |
|
| 491 |
// Set up. |
| 492 |
ctx := context.Background() |
| 493 |
h, err := newHarness(ctx, t) |
| 494 |
if err != nil { |
| 495 |
t.Fatal(err) |
| 496 |
} |
| 497 |
defer h.Close() |
| 498 |
maxSendBatch, maxAckBatch := h.MaxBatchSizes() |
| 499 |
|
| 500 |
dt, topicCleanup, err := h.CreateTopic(ctx, t.Name()) |
| 501 |
if err != nil { |
| 502 |
t.Fatal(err) |
| 503 |
} |
| 504 |
defer topicCleanup() |
| 505 |
ds, subCleanup, err := h.CreateSubscription(ctx, dt, t.Name()) |
| 506 |
if err != nil { |
| 507 |
t.Fatal(err) |
| 508 |
} |
| 509 |
defer subCleanup() |
| 510 |
|
| 511 |
sendBatchOpts := &batcher.Options{MinBatchSize: batchSize, MaxBatchSize: batchSize} |
| 512 |
// If the driver doesn't support batchSize batches, fall back to size 1. |
| 513 |
if maxSendBatch != 0 && batchSize > maxSendBatch { |
| 514 |
sendBatchOpts = batchSizeOne |
| 515 |
} |
| 516 |
topic := pubsub.NewTopic(dt, sendBatchOpts) |
| 517 |
defer func() { |
| 518 |
if err := topic.Shutdown(ctx); err != nil { |
| 519 |
t.Error(err) |
| 520 |
} |
| 521 |
}() |
| 522 |
ackBatchOpts := &batcher.Options{MinBatchSize: batchSize, MaxBatchSize: batchSize} |
| 523 |
// If the driver doesn't support batchSize batches, fall back to size 1. |
| 524 |
if maxAckBatch != 0 && batchSize > maxAckBatch { |
| 525 |
ackBatchOpts = batchSizeOne |
| 526 |
} |
| 527 |
sub := pubsub.NewSubscription(ds, batchSizeOne, ackBatchOpts) |
| 528 |
defer func() { |
| 529 |
if err := sub.Shutdown(ctx); err != nil { |
| 530 |
t.Error(err) |
| 531 |
} |
| 532 |
}() |
| 533 |
|
| 534 |
// Publish nMessages. We have to do them asynchronously because topic.Send |
| 535 |
// blocks until the message is sent, and these messages won't be sent until |
| 536 |
// all batchSize are queued. |
| 537 |
// Note: this test uses the same Body for each message, because the order |
| 538 |
// that they appear in the SendBatch is not stable. |
| 539 |
gr, grctx := errgroup.WithContext(ctx) |
| 540 |
var want []*pubsub.Message |
| 541 |
for range nMessages { |
| 542 |
m := &pubsub.Message{Body: []byte("hello world")} |
| 543 |
want = append(want, m) |
| 544 |
gr.Go(func() error { return topic.Send(grctx, m) }) |
| 545 |
} |
| 546 |
if err := gr.Wait(); err != nil { |
| 547 |
t.Fatal(err) |
| 548 |
} |
| 549 |
|
| 550 |
// Get the messages. |
| 551 |
// The test will hang here if the messages aren't delivered, so use a shorter timeout. |
| 552 |
ctx2, cancel := context.WithTimeout(ctx, 30*time.Second) |
| 553 |
defer cancel() |
| 554 |
var got []*pubsub.Message |
| 555 |
for range nMessages { |
| 556 |
m, err := sub.Receive(ctx2) |
| 557 |
if err != nil { |
| 558 |
t.Fatal(err) |
| 559 |
} |
| 560 |
got = append(got, m) |
| 561 |
m.Ack() |
| 562 |
} |
| 563 |
if diff := diffMessageSets(got, want); diff != "" { |
| 564 |
t.Error(diff) |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
func testDoubleAck(t *testing.T, newHarness HarnessMaker) { |
| 569 |
t.Helper() |
| 570 |
|
| 571 |
// Set up. |
| 572 |
ctx := context.Background() |
| 573 |
h, err := newHarness(ctx, t) |
| 574 |
if err != nil { |
| 575 |
t.Fatal(err) |
| 576 |
} |
| 577 |
defer h.Close() |
| 578 |
dt, topicCleanup, err := h.CreateTopic(ctx, t.Name()) |
| 579 |
if err != nil { |
| 580 |
t.Fatal(err) |
| 581 |
} |
| 582 |
defer topicCleanup() |
| 583 |
ds, subCleanup, err := h.CreateSubscription(ctx, dt, t.Name()) |
| 584 |
if err != nil { |
| 585 |
t.Fatal(err) |
| 586 |
} |
| 587 |
defer subCleanup() |
| 588 |
|
| 589 |
// Publish 3 messages. |
| 590 |
for i := range 3 { |
| 591 |
err := dt.SendBatch(ctx, []*driver.Message{{Body: []byte(strconv.Itoa(i))}}) |
| 592 |
if err != nil { |
| 593 |
t.Fatal(err) |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
// Retrieve the messages. |
| 598 |
// The test will hang here if the messages aren't delivered, so use a shorter timeout. |
| 599 |
ctx2, cancel := context.WithTimeout(ctx, 30*time.Second) |
| 600 |
defer cancel() |
| 601 |
var dms []*driver.Message |
| 602 |
for len(dms) != 3 { |
| 603 |
curdms, err := ds.ReceiveBatch(ctx2, 3) |
| 604 |
if err != nil { |
| 605 |
t.Fatal(err) |
| 606 |
} |
| 607 |
if err := ctx2.Err(); err != nil { |
| 608 |
t.Fatalf("never received expected messages: %v", err) |
| 609 |
} |
| 610 |
dms = append(dms, curdms...) |
| 611 |
} |
| 612 |
|
| 613 |
// Ack the first two messages. |
| 614 |
err = ds.SendAcks(ctx, []driver.AckID{dms[0].AckID, dms[1].AckID}) |
| 615 |
if err != nil { |
| 616 |
t.Fatal(err) |
| 617 |
} |
| 618 |
|
| 619 |
// Ack them again; this should succeed even though we've acked them before. |
| 620 |
// If services return an error for this, drivers should drop them. |
| 621 |
err = ds.SendAcks(ctx, []driver.AckID{dms[0].AckID, dms[1].AckID}) |
| 622 |
if err != nil { |
| 623 |
t.Fatal(err) |
| 624 |
} |
| 625 |
|
| 626 |
if !ds.CanNack() { |
| 627 |
return |
| 628 |
} |
| 629 |
|
| 630 |
// Nack all 3 messages. This should also succeed, and the nack of the third |
| 631 |
// message should take effect, so we should be able to fetch it again. |
| 632 |
// Note that the other messages *may* also be re-sent, because we're nacking |
| 633 |
// them here (even though we acked them earlier); it depends on service |
| 634 |
// semantics and time-sensitivity. |
| 635 |
err = ds.SendNacks(ctx, []driver.AckID{dms[0].AckID, dms[1].AckID, dms[2].AckID}) |
| 636 |
if err != nil { |
| 637 |
t.Fatal(err) |
| 638 |
} |
| 639 |
|
| 640 |
// The test will hang here if the message isn't redelivered, so use a shorter timeout. |
| 641 |
ctx2, cancel = context.WithTimeout(ctx, 30*time.Second) |
| 642 |
defer cancel() |
| 643 |
|
| 644 |
// We're looking to re-receive dms[2]. |
| 645 |
Loop: |
| 646 |
for { |
| 647 |
curdms, err := ds.ReceiveBatch(ctx2, 1) |
| 648 |
if err != nil { |
| 649 |
t.Fatal(err) |
| 650 |
} |
| 651 |
for _, curdm := range curdms { |
| 652 |
if bytes.Equal(curdm.Body, dms[2].Body) { |
| 653 |
// Found it! |
| 654 |
break Loop |
| 655 |
} |
| 656 |
} |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
// Publish n different messages to the topic. Return the messages. |
| 661 |
func publishN(ctx context.Context, t *testing.T, topic *pubsub.Topic, n int) []*pubsub.Message { |
| 662 |
t.Helper() |
| 663 |
|
| 664 |
var ms []*pubsub.Message |
| 665 |
for i := range n { |
| 666 |
m := &pubsub.Message{ |
| 667 |
Body: []byte(strconv.Itoa(i)), |
| 668 |
Metadata: map[string]string{"a": strconv.Itoa(i)}, |
| 669 |
} |
| 670 |
if err := topic.Send(ctx, m); err != nil { |
| 671 |
t.Fatal(err) |
| 672 |
} |
| 673 |
ms = append(ms, m) |
| 674 |
} |
| 675 |
return ms |
| 676 |
} |
| 677 |
|
| 678 |
// Receive and ack n messages from sub. |
| 679 |
func receiveN(ctx context.Context, t *testing.T, sub *pubsub.Subscription, n int) []*pubsub.Message { |
| 680 |
t.Helper() |
| 681 |
|
| 682 |
// The test will hang here if the message(s) aren't available, so use a shorter timeout. |
| 683 |
ctx2, cancel := context.WithTimeout(ctx, 30*time.Second) |
| 684 |
defer cancel() |
| 685 |
var ms []*pubsub.Message |
| 686 |
for range n { |
| 687 |
m, err := sub.Receive(ctx2) |
| 688 |
if err != nil { |
| 689 |
t.Fatal(err) |
| 690 |
} |
| 691 |
ms = append(ms, m) |
| 692 |
m.Ack() |
| 693 |
} |
| 694 |
return ms |
| 695 |
} |
| 696 |
|
| 697 |
// Find the differences between two sets of messages. |
| 698 |
func diffMessageSets(got, want []*pubsub.Message) string { |
| 699 |
for _, m := range got { |
| 700 |
m.LoggableID = "" |
| 701 |
} |
| 702 |
less := func(x, y *pubsub.Message) bool { return bytes.Compare(x.Body, y.Body) < 0 } |
| 703 |
return cmp.Diff(got, want, cmpopts.SortSlices(less), cmpopts.IgnoreUnexported(pubsub.Message{})) |
| 704 |
} |
| 705 |
|
| 706 |
func testErrorOnSendToClosedTopic(t *testing.T, newHarness HarnessMaker) { |
| 707 |
t.Helper() |
| 708 |
|
| 709 |
// Set up. |
| 710 |
ctx := context.Background() |
| 711 |
h, err := newHarness(ctx, t) |
| 712 |
if err != nil { |
| 713 |
t.Fatal(err) |
| 714 |
} |
| 715 |
defer h.Close() |
| 716 |
|
| 717 |
dt, cleanup, err := h.CreateTopic(ctx, t.Name()) |
| 718 |
if err != nil { |
| 719 |
t.Fatal(err) |
| 720 |
} |
| 721 |
defer cleanup() |
| 722 |
|
| 723 |
topic := pubsub.NewTopic(dt, batchSizeOne) |
| 724 |
if err := topic.Shutdown(ctx); err != nil { |
| 725 |
t.Error(err) |
| 726 |
} |
| 727 |
|
| 728 |
// Check that sending to the closed topic fails. |
| 729 |
m := &pubsub.Message{} |
| 730 |
if err := topic.Send(ctx, m); err == nil { |
| 731 |
t.Error("topic.Send returned nil, want error") |
| 732 |
} |
| 733 |
if err := topic.Shutdown(ctx); err == nil { |
| 734 |
t.Error("wanted error on double Shutdown") |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
func testErrorOnReceiveFromClosedSubscription(t *testing.T, newHarness HarnessMaker) { |
| 739 |
t.Helper() |
| 740 |
|
| 741 |
ctx := context.Background() |
| 742 |
h, err := newHarness(ctx, t) |
| 743 |
if err != nil { |
| 744 |
t.Fatal(err) |
| 745 |
} |
| 746 |
defer h.Close() |
| 747 |
|
| 748 |
dt, cleanup, err := h.CreateTopic(ctx, t.Name()) |
| 749 |
if err != nil { |
| 750 |
t.Fatal(err) |
| 751 |
} |
| 752 |
defer cleanup() |
| 753 |
|
| 754 |
ds, cleanup, err := h.CreateSubscription(ctx, dt, t.Name()) |
| 755 |
if err != nil { |
| 756 |
t.Fatal(err) |
| 757 |
} |
| 758 |
defer cleanup() |
| 759 |
|
| 760 |
sub := pubsub.NewSubscription(ds, batchSizeOne, batchSizeOne) |
| 761 |
if err := sub.Shutdown(ctx); err != nil { |
| 762 |
t.Error(err) |
| 763 |
} |
| 764 |
if _, err = sub.Receive(ctx); err == nil { |
| 765 |
t.Error("sub.Receive returned nil, want error") |
| 766 |
} |
| 767 |
if err := sub.Shutdown(ctx); err == nil { |
| 768 |
t.Error("wanted error on double Shutdown") |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
func testCancelSendReceive(t *testing.T, newHarness HarnessMaker) { |
| 773 |
t.Helper() |
| 774 |
|
| 775 |
ctx := context.Background() |
| 776 |
h, err := newHarness(ctx, t) |
| 777 |
if err != nil { |
| 778 |
t.Fatal(err) |
| 779 |
} |
| 780 |
defer h.Close() |
| 781 |
topic, sub, cleanup, err := makePair(ctx, t, h) |
| 782 |
if err != nil { |
| 783 |
t.Fatal(err) |
| 784 |
} |
| 785 |
defer cleanup() |
| 786 |
|
| 787 |
ctx, cancel := context.WithCancel(ctx) |
| 788 |
cancel() |
| 789 |
|
| 790 |
m := &pubsub.Message{} |
| 791 |
if err := topic.Send(ctx, m); !isCanceled(err) { |
| 792 |
t.Errorf("topic.Send returned %v (%T), want context.Canceled", err, err) |
| 793 |
} |
| 794 |
if _, err := sub.Receive(ctx); !isCanceled(err) { |
| 795 |
t.Errorf("sub.Receive returned %v (%T), want context.Canceled", err, err) |
| 796 |
} |
| 797 |
|
| 798 |
// It would be nice to add a test that cancels an in-flight blocking Receive. |
| 799 |
// However, because pubsub.Subscription.Receive repeatedly calls |
| 800 |
// driver.ReceiveBatch if it returns 0 messages, it's difficult to write |
| 801 |
// such a test without it being flaky for drivers with record/replay |
| 802 |
// (the number of times driver.ReceiveBatch is called is timing-dependent). |
| 803 |
} |
| 804 |
|
| 805 |
func testMetadata(t *testing.T, newHarness HarnessMaker) { |
| 806 |
t.Helper() |
| 807 |
|
| 808 |
// Set up. |
| 809 |
ctx := context.Background() |
| 810 |
h, err := newHarness(ctx, t) |
| 811 |
if err != nil { |
| 812 |
t.Fatal(err) |
| 813 |
} |
| 814 |
defer h.Close() |
| 815 |
|
| 816 |
// Some services limit the number of metadata per message. |
| 817 |
// Sort the escape.WeirdStrings values for record/replay consistency, |
| 818 |
// then break the weird strings up into groups of at most maxMetadataKeys. |
| 819 |
const maxMetadataKeys = 10 |
| 820 |
var weirdStrings []string |
| 821 |
for _, v := range escape.WeirdStrings { |
| 822 |
weirdStrings = append(weirdStrings, v) |
| 823 |
} |
| 824 |
slices.Sort(weirdStrings) |
| 825 |
|
| 826 |
weirdMetaDataGroups := []map[string]string{{}} |
| 827 |
i := 0 |
| 828 |
for _, k := range weirdStrings { |
| 829 |
weirdMetaDataGroups[i][k] = k |
| 830 |
if len(weirdMetaDataGroups[i]) == maxMetadataKeys { |
| 831 |
weirdMetaDataGroups = append(weirdMetaDataGroups, map[string]string{}) |
| 832 |
i++ |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
topic, sub, cleanup, err := makePair(ctx, t, h) |
| 837 |
if err != nil { |
| 838 |
t.Fatal(err) |
| 839 |
} |
| 840 |
defer cleanup() |
| 841 |
|
| 842 |
for _, wm := range weirdMetaDataGroups { |
| 843 |
m := &pubsub.Message{ |
| 844 |
Body: []byte("hello world"), |
| 845 |
Metadata: wm, |
| 846 |
} |
| 847 |
if err := topic.Send(ctx, m); err != nil { |
| 848 |
t.Fatal(err) |
| 849 |
} |
| 850 |
|
| 851 |
// The test will hang here if the messages aren't delivered, so use a shorter timeout. |
| 852 |
ctx2, cancel := context.WithTimeout(ctx, 30*time.Second) |
| 853 |
defer cancel() |
| 854 |
m, err = sub.Receive(ctx2) |
| 855 |
if err != nil { |
| 856 |
t.Fatal(err) |
| 857 |
} |
| 858 |
m.Ack() |
| 859 |
|
| 860 |
if diff := cmp.Diff(m.Metadata, wm); diff != "" { |
| 861 |
t.Fatalf("got\n%v\nwant\n%v\ndiff\n%s", m.Metadata, wm, diff) |
| 862 |
} |
| 863 |
} |
| 864 |
|
| 865 |
// Verify that non-UTF8 strings in metadata key or value fail. |
| 866 |
m := &pubsub.Message{ |
| 867 |
Body: []byte("hello world"), |
| 868 |
Metadata: map[string]string{escape.NonUTF8String: "bar"}, |
| 869 |
} |
| 870 |
if err := topic.Send(ctx, m); err == nil { |
| 871 |
t.Error("got nil error, expected error for using non-UTF8 string as metadata key") |
| 872 |
} |
| 873 |
m.Metadata = map[string]string{"foo": escape.NonUTF8String} |
| 874 |
if err := topic.Send(ctx, m); err == nil { |
| 875 |
t.Error("got nil error, expected error for using non-UTF8 string as metadata value") |
| 876 |
} |
| 877 |
} |
| 878 |
|
| 879 |
func testNonUTF8MessageBody(t *testing.T, newHarness HarnessMaker) { |
| 880 |
t.Helper() |
| 881 |
|
| 882 |
// Set up. |
| 883 |
ctx := context.Background() |
| 884 |
h, err := newHarness(ctx, t) |
| 885 |
if err != nil { |
| 886 |
t.Fatal(err) |
| 887 |
} |
| 888 |
defer h.Close() |
| 889 |
|
| 890 |
topic, sub, cleanup, err := makePair(ctx, t, h) |
| 891 |
if err != nil { |
| 892 |
t.Fatal(err) |
| 893 |
} |
| 894 |
defer cleanup() |
| 895 |
|
| 896 |
// Sort the WeirdStrings map for record/replay consistency. |
| 897 |
var weirdStrings [][]string // [0] = key, [1] = value |
| 898 |
for k, v := range escape.WeirdStrings { |
| 899 |
weirdStrings = append(weirdStrings, []string{k, v}) |
| 900 |
} |
| 901 |
sort.Slice(weirdStrings, func(i, j int) bool { return weirdStrings[i][0] < weirdStrings[j][0] }) |
| 902 |
|
| 903 |
// Construct a message body with the weird strings and some non-UTF-8 bytes. |
| 904 |
var body []byte |
| 905 |
for _, v := range weirdStrings { |
| 906 |
body = append(body, []byte(v[1])...) |
| 907 |
} |
| 908 |
body = append(body, []byte(escape.NonUTF8String)...) |
| 909 |
m := &pubsub.Message{Body: body} |
| 910 |
|
| 911 |
if err := topic.Send(ctx, m); err != nil { |
| 912 |
t.Fatal(err) |
| 913 |
} |
| 914 |
// The test will hang here if the messages aren't delivered, so use a shorter timeout. |
| 915 |
ctx2, cancel := context.WithTimeout(ctx, 30*time.Second) |
| 916 |
defer cancel() |
| 917 |
m, err = sub.Receive(ctx2) |
| 918 |
if err != nil { |
| 919 |
t.Fatal(err) |
| 920 |
} |
| 921 |
m.Ack() |
| 922 |
|
| 923 |
if diff := cmp.Diff(m.Body, body); diff != "" { |
| 924 |
t.Fatalf("got\n%v\nwant\n%v\ndiff\n%s", m.Body, body, diff) |
| 925 |
} |
| 926 |
} |
| 927 |
|
| 928 |
func isCanceled(err error) bool { |
| 929 |
if err == context.Canceled { |
| 930 |
return true |
| 931 |
} |
| 932 |
if cerr, ok := err.(*retry.ContextError); ok { |
| 933 |
return cerr.CtxErr == context.Canceled |
| 934 |
} |
| 935 |
return gcerrors.Code(err) == gcerrors.Canceled |
| 936 |
} |
| 937 |
|
| 938 |
func makePair(ctx context.Context, t *testing.T, h Harness) (*pubsub.Topic, *pubsub.Subscription, func(), error) { |
| 939 |
t.Helper() |
| 940 |
|
| 941 |
dt, topicCleanup, err := h.CreateTopic(ctx, t.Name()) |
| 942 |
if err != nil { |
| 943 |
return nil, nil, nil, err |
| 944 |
} |
| 945 |
ds, subCleanup, err := h.CreateSubscription(ctx, dt, t.Name()) |
| 946 |
if err != nil { |
| 947 |
topicCleanup() |
| 948 |
return nil, nil, nil, err |
| 949 |
} |
| 950 |
topic := pubsub.NewTopic(dt, batchSizeOne) |
| 951 |
sub := pubsub.NewSubscription(ds, batchSizeOne, batchSizeOne) |
| 952 |
cleanup := func() { |
| 953 |
if err := topic.Shutdown(ctx); err != nil { |
| 954 |
t.Error(err) |
| 955 |
} |
| 956 |
if err := sub.Shutdown(ctx); err != nil { |
| 957 |
t.Error(err) |
| 958 |
} |
| 959 |
subCleanup() |
| 960 |
topicCleanup() |
| 961 |
} |
| 962 |
return topic, sub, cleanup, nil |
| 963 |
} |
| 964 |
|
| 965 |
// testAs tests the various As functions, using AsTest. |
| 966 |
func testAs(t *testing.T, newHarness HarnessMaker, st AsTest) { |
| 967 |
t.Helper() |
| 968 |
|
| 969 |
ctx := context.Background() |
| 970 |
h, err := newHarness(ctx, t) |
| 971 |
if err != nil { |
| 972 |
t.Fatal(err) |
| 973 |
} |
| 974 |
defer h.Close() |
| 975 |
topic, sub, cleanup, err := makePair(ctx, t, h) |
| 976 |
if err != nil { |
| 977 |
t.Fatal(err) |
| 978 |
} |
| 979 |
defer cleanup() |
| 980 |
|
| 981 |
if err := st.TopicCheck(topic); err != nil { |
| 982 |
t.Error(err) |
| 983 |
} |
| 984 |
|
| 985 |
if err := st.SubscriptionCheck(sub); err != nil { |
| 986 |
t.Error(err) |
| 987 |
} |
| 988 |
|
| 989 |
msg := &pubsub.Message{ |
| 990 |
Body: []byte("x"), |
| 991 |
BeforeSend: st.BeforeSend, |
| 992 |
AfterSend: st.AfterSend, |
| 993 |
} |
| 994 |
if err := topic.Send(ctx, msg); err != nil { |
| 995 |
t.Fatal(err) |
| 996 |
} |
| 997 |
// The test will hang here if the messages aren't delivered, so use a shorter timeout. |
| 998 |
ctx2, cancel := context.WithTimeout(ctx, 30*time.Second) |
| 999 |
defer cancel() |
| 1000 |
m, err := sub.Receive(ctx2) |
| 1001 |
if err != nil { |
| 1002 |
t.Fatal(err) |
| 1003 |
} |
| 1004 |
if err := st.MessageCheck(m); err != nil { |
| 1005 |
t.Error(err) |
| 1006 |
} |
| 1007 |
m.Ack() |
| 1008 |
|
| 1009 |
// Make a nonexistent topic and try to to send on it, to get an error we can |
| 1010 |
// use to call TopicErrorCheck. |
| 1011 |
dt, err := h.MakeNonexistentTopic(ctx) |
| 1012 |
if err != nil { |
| 1013 |
t.Fatal(err) |
| 1014 |
} |
| 1015 |
nonexistentTopic := pubsub.NewTopic(dt, batchSizeOne) |
| 1016 |
defer func() { |
| 1017 |
if err := nonexistentTopic.Shutdown(ctx); err != nil { |
| 1018 |
t.Error(err) |
| 1019 |
} |
| 1020 |
}() |
| 1021 |
// The test will hang here if Send doesn't fail quickly, so set a shorter timeout. |
| 1022 |
ctx2, cancel = context.WithTimeout(ctx, 30*time.Second) |
| 1023 |
defer cancel() |
| 1024 |
topicErr := nonexistentTopic.Send(ctx2, &pubsub.Message{}) |
| 1025 |
if topicErr == nil || gcerrors.Code(topicErr) != gcerrors.NotFound { |
| 1026 |
t.Errorf("got error %v sending to nonexistent topic, want Code=NotFound", topicErr) |
| 1027 |
} else if err := st.TopicErrorCheck(topic, topicErr); err != nil { |
| 1028 |
t.Error(err) |
| 1029 |
} |
| 1030 |
|
| 1031 |
// Make a nonexistent subscription and try to receive from it, to get an error |
| 1032 |
// we can use to call SubscriptionErrorCheck. |
| 1033 |
ds, cleanup, err := h.MakeNonexistentSubscription(ctx) |
| 1034 |
if err != nil { |
| 1035 |
t.Fatal(err) |
| 1036 |
} |
| 1037 |
defer cleanup() |
| 1038 |
nonExistentSub := pubsub.NewSubscription(ds, batchSizeOne, batchSizeOne) |
| 1039 |
defer func() { |
| 1040 |
if err := nonExistentSub.Shutdown(ctx); err != nil { |
| 1041 |
t.Error(err) |
| 1042 |
} |
| 1043 |
}() |
| 1044 |
|
| 1045 |
// The test will hang here if Receive doesn't fail quickly, so set a shorter timeout. |
| 1046 |
ctx2, cancel = context.WithTimeout(ctx, 30*time.Second) |
| 1047 |
defer cancel() |
| 1048 |
_, subErr := nonExistentSub.Receive(ctx2) |
| 1049 |
if subErr == nil || ctx2.Err() != nil || gcerrors.Code(subErr) != gcerrors.NotFound { |
| 1050 |
t.Errorf("got error %v receiving from nonexistent subscription, want Code=NotFound", subErr) |
| 1051 |
} else if err := st.SubscriptionErrorCheck(nonExistentSub, subErr); err != nil { |
| 1052 |
t.Error(err) |
| 1053 |
} |
| 1054 |
} |
| 1055 |
|
| 1056 |
// Publishes a large number of messages to topic concurrently, and then times |
| 1057 |
// how long it takes to send (if timeSend is true) or receive (if timeSend |
| 1058 |
// is false) them all. |
| 1059 |
func benchmark(b *testing.B, topic *pubsub.Topic, sub *pubsub.Subscription, timeSend bool) { |
| 1060 |
b.Helper() |
| 1061 |
|
| 1062 |
attrs := map[string]string{"label": "value"} |
| 1063 |
body := []byte("hello, world") |
| 1064 |
const ( |
| 1065 |
nMessages = 10000 |
| 1066 |
concurrencySend = 100 |
| 1067 |
concurrencyReceive = 100 |
| 1068 |
) |
| 1069 |
if nMessages%concurrencySend != 0 || nMessages%concurrencyReceive != 0 { |
| 1070 |
b.Fatal("nMessages must be divisible by # of sending/receiving goroutines") |
| 1071 |
} |
| 1072 |
b.ResetTimer() |
| 1073 |
for i := 0; i < b.N; i++ { |
| 1074 |
if !timeSend { |
| 1075 |
b.StopTimer() |
| 1076 |
} |
| 1077 |
if err := publishNConcurrently(topic, nMessages, concurrencySend, attrs, body); err != nil { |
| 1078 |
b.Fatalf("publishing: %v", err) |
| 1079 |
} |
| 1080 |
b.Logf("published %d messages", nMessages) |
| 1081 |
if timeSend { |
| 1082 |
b.StopTimer() |
| 1083 |
} else { |
| 1084 |
b.StartTimer() |
| 1085 |
} |
| 1086 |
if err := receiveNConcurrently(sub, nMessages, concurrencyReceive); err != nil { |
| 1087 |
b.Fatalf("receiving: %v", err) |
| 1088 |
} |
| 1089 |
b.SetBytes(nMessages * 1e6) |
| 1090 |
if timeSend { |
| 1091 |
b.Log("MB/s is actually number of messages sent per second") |
| 1092 |
} else { |
| 1093 |
b.Log("MB/s is actually number of messages received per second") |
| 1094 |
} |
| 1095 |
if timeSend { |
| 1096 |
b.StartTimer() |
| 1097 |
} |
| 1098 |
} |
| 1099 |
} |
| 1100 |
|
| 1101 |
func publishNConcurrently(topic *pubsub.Topic, nMessages, nGoroutines int, attrs map[string]string, body []byte) error { |
| 1102 |
return runConcurrently(nMessages, nGoroutines, func(ctx context.Context) error { |
| 1103 |
return topic.Send(ctx, &pubsub.Message{Metadata: attrs, Body: body}) |
| 1104 |
}) |
| 1105 |
} |
| 1106 |
|
| 1107 |
func receiveNConcurrently(sub *pubsub.Subscription, nMessages, nGoroutines int) error { |
| 1108 |
return runConcurrently(nMessages, nGoroutines, func(ctx context.Context) error { |
| 1109 |
m, err := sub.Receive(ctx) |
| 1110 |
if err != nil { |
| 1111 |
return err |
| 1112 |
} |
| 1113 |
m.Ack() |
| 1114 |
return nil |
| 1115 |
}) |
| 1116 |
} |
| 1117 |
|
| 1118 |
// Call function f n times concurrently, using g goroutines. g must divide n. |
| 1119 |
// Wait until all calls complete. If any fail, cancel the remaining ones. |
| 1120 |
func runConcurrently(n, g int, f func(context.Context) error) error { |
| 1121 |
gr, ctx := errgroup.WithContext(context.Background()) |
| 1122 |
ng := n / g |
| 1123 |
for range g { |
| 1124 |
gr.Go(func() error { |
| 1125 |
for range ng { |
| 1126 |
if err := f(ctx); err != nil { |
| 1127 |
return err |
| 1128 |
} |
| 1129 |
} |
| 1130 |
return nil |
| 1131 |
}) |
| 1132 |
} |
| 1133 |
return gr.Wait() |
| 1134 |
} |