| 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 awssnssqs |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"errors" |
| 20 |
"fmt" |
| 21 |
"maps" |
| 22 |
"net/http" |
| 23 |
"strings" |
| 24 |
"sync/atomic" |
| 25 |
"testing" |
| 26 |
"time" |
| 27 |
|
| 28 |
"github.com/aws/aws-sdk-go-v2/aws" |
| 29 |
"github.com/aws/aws-sdk-go-v2/config" |
| 30 |
"github.com/aws/aws-sdk-go-v2/service/sns" |
| 31 |
snstypes "github.com/aws/aws-sdk-go-v2/service/sns/types" |
| 32 |
"github.com/aws/aws-sdk-go-v2/service/sqs" |
| 33 |
sqstypes "github.com/aws/aws-sdk-go-v2/service/sqs/types" |
| 34 |
"github.com/aws/smithy-go" |
| 35 |
"github.com/google/go-cmp/cmp" |
| 36 |
"gocloud.dev/internal/testing/setup" |
| 37 |
"gocloud.dev/pubsub" |
| 38 |
"gocloud.dev/pubsub/driver" |
| 39 |
"gocloud.dev/pubsub/drivertest" |
| 40 |
) |
| 41 |
|
| 42 |
const ( |
| 43 |
region = "us-east-2" |
| 44 |
accountNumber = "456752665576" |
| 45 |
) |
| 46 |
|
| 47 |
// We run conformance tests against multiple kinds of topics; this enum |
| 48 |
// represents which one we're doing. |
| 49 |
type topicKind string |
| 50 |
|
| 51 |
const ( |
| 52 |
topicKindSNS = topicKind("SNS") // send through an SNS topic |
| 53 |
topicKindSNSRaw = topicKind("SNSRaw") // send through an SNS topic using RawMessageDelivery=true |
| 54 |
topicKindSQS = topicKind("SQS") // send directly to an SQS queue |
| 55 |
) |
| 56 |
|
| 57 |
type harness struct { |
| 58 |
snsClient *sns.Client |
| 59 |
sqsClient *sqs.Client |
| 60 |
topicKind topicKind |
| 61 |
rt http.RoundTripper |
| 62 |
closer func() |
| 63 |
numTopics uint32 |
| 64 |
numSubs uint32 |
| 65 |
useFIFO bool |
| 66 |
topicAttributes map[string]string |
| 67 |
} |
| 68 |
|
| 69 |
type harnessOption func(h *harness) |
| 70 |
|
| 71 |
func newHarness(ctx context.Context, t *testing.T, topicKind topicKind) (drivertest.Harness, error) { |
| 72 |
t.Helper() |
| 73 |
|
| 74 |
cfg, rt, closer, _ := setup.NewAWSv2Config(context.Background(), t, region, false) |
| 75 |
return &harness{snsClient: sns.NewFromConfig(cfg), sqsClient: sqs.NewFromConfig(cfg), rt: rt, topicKind: topicKind, closer: closer}, nil |
| 76 |
} |
| 77 |
|
| 78 |
func (h *harness) CreateTopic(ctx context.Context, testName string) (dt driver.Topic, cleanup func(), err error) { |
| 79 |
topicName := sanitize(fmt.Sprintf("%s-top-%d", testName, atomic.AddUint32(&h.numTopics, 1))) |
| 80 |
if h.useFIFO { |
| 81 |
topicName += ".fifo" |
| 82 |
} |
| 83 |
return createTopic(ctx, topicName, h.snsClient, h.sqsClient, h.topicKind, h.topicAttributes) |
| 84 |
} |
| 85 |
|
| 86 |
func convertStringToPtrMap(m map[string]string) map[string]*string { |
| 87 |
if m == nil { |
| 88 |
return nil |
| 89 |
} |
| 90 |
out := make(map[string]*string, len(m)) |
| 91 |
for k, v := range m { |
| 92 |
out[k] = aws.String(v) |
| 93 |
} |
| 94 |
return out |
| 95 |
} |
| 96 |
|
| 97 |
func createTopic(ctx context.Context, topicName string, snsClient *sns.Client, sqsClient *sqs.Client, topicKind topicKind, attributes map[string]string) (dt driver.Topic, cleanup func(), err error) { |
| 98 |
switch topicKind { |
| 99 |
case topicKindSNS, topicKindSNSRaw: |
| 100 |
// Create an SNS topic. |
| 101 |
input := &sns.CreateTopicInput{Name: aws.String(topicName), Attributes: attributes} |
| 102 |
out, err := snsClient.CreateTopic(ctx, input) |
| 103 |
if err != nil { |
| 104 |
return nil, nil, fmt.Errorf("creating SNS topic %q: %v", topicName, err) |
| 105 |
} |
| 106 |
dt = openSNSTopic(ctx, snsClient, *out.TopicArn, &TopicOptions{}) |
| 107 |
cleanup = func() { |
| 108 |
snsClient.DeleteTopic(ctx, &sns.DeleteTopicInput{TopicArn: out.TopicArn}) |
| 109 |
} |
| 110 |
return dt, cleanup, nil |
| 111 |
case topicKindSQS: |
| 112 |
// Create an SQS queue. |
| 113 |
qURL, _, err := createSQSQueue(ctx, sqsClient, topicName, attributes) |
| 114 |
if err != nil { |
| 115 |
return nil, nil, fmt.Errorf("creating SQS queue %q: %v", topicName, err) |
| 116 |
} |
| 117 |
dt = openSQSTopic(ctx, sqsClient, qURL, &TopicOptions{}) |
| 118 |
cleanup = func() { |
| 119 |
sqsClient.DeleteQueue(ctx, &sqs.DeleteQueueInput{QueueUrl: aws.String(qURL)}) |
| 120 |
} |
| 121 |
return dt, cleanup, nil |
| 122 |
default: |
| 123 |
panic("unreachable") |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
func (h *harness) MakeNonexistentTopic(ctx context.Context) (driver.Topic, error) { |
| 128 |
switch h.topicKind { |
| 129 |
case topicKindSNS, topicKindSNSRaw: |
| 130 |
const fakeTopicARN = "arn:aws:sns:" + region + ":" + accountNumber + ":nonexistenttopic" |
| 131 |
return openSNSTopic(ctx, h.snsClient, fakeTopicARN, &TopicOptions{}), nil |
| 132 |
case topicKindSQS: |
| 133 |
const fakeQueueURL = "https://" + region + ".amazonaws.com/" + accountNumber + "/nonexistent-queue" |
| 134 |
return openSQSTopic(ctx, h.sqsClient, fakeQueueURL, &TopicOptions{}), nil |
| 135 |
default: |
| 136 |
panic("unreachable") |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
func (h *harness) CreateSubscription(ctx context.Context, dt driver.Topic, testName string) (ds driver.Subscription, cleanup func(), err error) { |
| 141 |
subName := sanitize(fmt.Sprintf("%s-sub-%d", testName, atomic.AddUint32(&h.numSubs, 1))) |
| 142 |
return createSubscription(ctx, dt, subName, h.snsClient, h.sqsClient, h.topicKind) |
| 143 |
} |
| 144 |
|
| 145 |
func createSubscription(ctx context.Context, dt driver.Topic, subName string, snsClient *sns.Client, sqsClient *sqs.Client, topicKind topicKind) (ds driver.Subscription, cleanup func(), err error) { |
| 146 |
switch topicKind { |
| 147 |
case topicKindSNS, topicKindSNSRaw: |
| 148 |
// Create an SQS queue, and subscribe it to the SNS topic. |
| 149 |
qURL, qARN, err := createSQSQueue(ctx, sqsClient, subName, nil) |
| 150 |
if err != nil { |
| 151 |
return nil, nil, fmt.Errorf("creating SQS queue %q: %v", subName, err) |
| 152 |
} |
| 153 |
ds = openSubscription(ctx, sqsClient, qURL, &SubscriptionOptions{}) |
| 154 |
|
| 155 |
snsTopicARN := dt.(*snsTopic).arn |
| 156 |
var cleanup func() |
| 157 |
req := &sns.SubscribeInput{ |
| 158 |
TopicArn: aws.String(snsTopicARN), |
| 159 |
Endpoint: aws.String(qARN), |
| 160 |
Protocol: aws.String("sqs"), |
| 161 |
} |
| 162 |
// Enable RawMessageDelivery on the subscription if needed. |
| 163 |
if topicKind == topicKindSNSRaw { |
| 164 |
req.Attributes = map[string]string{"RawMessageDelivery": "true"} |
| 165 |
} |
| 166 |
out, err := snsClient.Subscribe(ctx, req) |
| 167 |
if err != nil { |
| 168 |
return nil, nil, fmt.Errorf("subscribing: %v", err) |
| 169 |
} |
| 170 |
cleanup = func() { |
| 171 |
snsClient.Unsubscribe(ctx, &sns.UnsubscribeInput{SubscriptionArn: out.SubscriptionArn}) |
| 172 |
sqsClient.DeleteQueue(ctx, &sqs.DeleteQueueInput{QueueUrl: aws.String(qURL)}) |
| 173 |
} |
| 174 |
return ds, cleanup, nil |
| 175 |
case topicKindSQS: |
| 176 |
// The SQS queue already exists; we created it for the topic. Re-use it |
| 177 |
// for the subscription. |
| 178 |
qURL := dt.(*sqsTopic).qURL |
| 179 |
return openSubscription(ctx, sqsClient, qURL, &SubscriptionOptions{}), func() {}, nil |
| 180 |
default: |
| 181 |
panic("unreachable") |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
func createSQSQueue(ctx context.Context, sqsClient *sqs.Client, topicName string, attributes map[string]string) (string, string, error) { |
| 186 |
out, err := sqsClient.CreateQueue(ctx, &sqs.CreateQueueInput{QueueName: aws.String(topicName), Attributes: attributes}) |
| 187 |
if err != nil { |
| 188 |
return "", "", fmt.Errorf("creating SQS queue %q: %v", topicName, err) |
| 189 |
} |
| 190 |
qURL := aws.ToString(out.QueueUrl) |
| 191 |
|
| 192 |
// Get the ARN. |
| 193 |
out2, err := sqsClient.GetQueueAttributes(ctx, &sqs.GetQueueAttributesInput{ |
| 194 |
QueueUrl: aws.String(qURL), |
| 195 |
AttributeNames: []sqstypes.QueueAttributeName{"QueueArn"}, |
| 196 |
}) |
| 197 |
if err != nil { |
| 198 |
return "", "", fmt.Errorf("getting queue ARN for %s: %v", qURL, err) |
| 199 |
} |
| 200 |
qARN := out2.Attributes["QueueArn"] |
| 201 |
|
| 202 |
queuePolicy := `{ |
| 203 |
"Version": "2012-10-17", |
| 204 |
"Id": "AllowQueue", |
| 205 |
"Statement": [ |
| 206 |
{ |
| 207 |
"Sid": "MySQSPolicy001", |
| 208 |
"Effect": "Allow", |
| 209 |
"Principal": { |
| 210 |
"AWS": "*" |
| 211 |
}, |
| 212 |
"Action": "sqs:SendMessage", |
| 213 |
"Resource": "` + qARN + `" |
| 214 |
} |
| 215 |
] |
| 216 |
}` |
| 217 |
if _, err := sqsClient.SetQueueAttributes(ctx, &sqs.SetQueueAttributesInput{ |
| 218 |
Attributes: map[string]string{"Policy": queuePolicy}, |
| 219 |
QueueUrl: aws.String(qURL), |
| 220 |
}); err != nil { |
| 221 |
return "", "", fmt.Errorf("setting policy: %v", err) |
| 222 |
} |
| 223 |
return qURL, qARN, nil |
| 224 |
} |
| 225 |
|
| 226 |
func (h *harness) MakeNonexistentSubscription(ctx context.Context) (driver.Subscription, func(), error) { |
| 227 |
const fakeSubscriptionQueueURL = "https://" + region + ".amazonaws.com/" + accountNumber + "/nonexistent-subscription" |
| 228 |
return openSubscription(ctx, h.sqsClient, fakeSubscriptionQueueURL, &SubscriptionOptions{}), func() {}, nil |
| 229 |
} |
| 230 |
|
| 231 |
func (h *harness) Close() { |
| 232 |
h.closer() |
| 233 |
} |
| 234 |
|
| 235 |
func (h *harness) MaxBatchSizes() (int, int) { |
| 236 |
if h.topicKind == topicKindSQS { |
| 237 |
return sendBatcherOptsSQS.MaxBatchSize, ackBatcherOpts.MaxBatchSize |
| 238 |
} |
| 239 |
return sendBatcherOptsSNS.MaxBatchSize, ackBatcherOpts.MaxBatchSize |
| 240 |
} |
| 241 |
|
| 242 |
func (h *harness) SupportsMultipleSubscriptions() bool { |
| 243 |
// If we're publishing to an SQS topic, we're reading from the same topic, |
| 244 |
// so there's no way to get multiple subscriptions. |
| 245 |
return h.topicKind != topicKindSQS |
| 246 |
} |
| 247 |
|
| 248 |
func TestConformanceSNSTopic(t *testing.T) { |
| 249 |
asTests := []drivertest.AsTest{awsAsTest{topicKind: topicKindSNS}} |
| 250 |
newSNSHarness := func(ctx context.Context, t *testing.T) (drivertest.Harness, error) { |
| 251 |
t.Helper() |
| 252 |
|
| 253 |
return newHarness(ctx, t, topicKindSNS) |
| 254 |
} |
| 255 |
drivertest.RunConformanceTests(t, newSNSHarness, asTests) |
| 256 |
} |
| 257 |
|
| 258 |
func TestConformanceSNSTopicRaw(t *testing.T) { |
| 259 |
asTests := []drivertest.AsTest{awsAsTest{topicKind: topicKindSNSRaw}} |
| 260 |
newSNSHarness := func(ctx context.Context, t *testing.T) (drivertest.Harness, error) { |
| 261 |
t.Helper() |
| 262 |
|
| 263 |
return newHarness(ctx, t, topicKindSNSRaw) |
| 264 |
} |
| 265 |
drivertest.RunConformanceTests(t, newSNSHarness, asTests) |
| 266 |
} |
| 267 |
|
| 268 |
func TestConformanceSQSTopic(t *testing.T) { |
| 269 |
asTests := []drivertest.AsTest{awsAsTest{topicKind: topicKindSQS}} |
| 270 |
newSQSHarness := func(ctx context.Context, t *testing.T) (drivertest.Harness, error) { |
| 271 |
t.Helper() |
| 272 |
|
| 273 |
return newHarness(ctx, t, topicKindSQS) |
| 274 |
} |
| 275 |
drivertest.RunConformanceTests(t, newSQSHarness, asTests) |
| 276 |
} |
| 277 |
|
| 278 |
type awsAsTest struct { |
| 279 |
topicKind topicKind |
| 280 |
} |
| 281 |
|
| 282 |
func (awsAsTest) Name() string { |
| 283 |
return "aws test" |
| 284 |
} |
| 285 |
|
| 286 |
func (t awsAsTest) TopicCheck(topic *pubsub.Topic) error { |
| 287 |
switch t.topicKind { |
| 288 |
case topicKindSNS, topicKindSNSRaw: |
| 289 |
var s *sns.Client |
| 290 |
if !topic.As(&s) { |
| 291 |
return fmt.Errorf("cast failed for %T", s) |
| 292 |
} |
| 293 |
case topicKindSQS: |
| 294 |
var s *sqs.Client |
| 295 |
if !topic.As(&s) { |
| 296 |
return fmt.Errorf("cast failed for %T", s) |
| 297 |
} |
| 298 |
default: |
| 299 |
panic("unreachable") |
| 300 |
} |
| 301 |
return nil |
| 302 |
} |
| 303 |
|
| 304 |
func (t awsAsTest) SubscriptionCheck(sub *pubsub.Subscription) error { |
| 305 |
var s *sqs.Client |
| 306 |
if !sub.As(&s) { |
| 307 |
return fmt.Errorf("cast failed for %T", s) |
| 308 |
} |
| 309 |
return nil |
| 310 |
} |
| 311 |
|
| 312 |
func (t awsAsTest) TopicErrorCheck(topic *pubsub.Topic, err error) error { |
| 313 |
var e smithy.APIError |
| 314 |
if !topic.ErrorAs(err, &e) { |
| 315 |
return errors.New("Topic.ErrorAs failed") |
| 316 |
} |
| 317 |
switch t.topicKind { |
| 318 |
case topicKindSNS, topicKindSNSRaw: |
| 319 |
if got, want := e.ErrorCode(), (&snstypes.NotFoundException{}).ErrorCode(); want != got { |
| 320 |
return fmt.Errorf("got %q, want %q", got, want) |
| 321 |
} |
| 322 |
case topicKindSQS: |
| 323 |
if got, want := e.ErrorCode(), "AWS.SimpleQueueService.NonExistentQueue"; got != want { |
| 324 |
return fmt.Errorf("got %q, want %q", got, want) |
| 325 |
} |
| 326 |
default: |
| 327 |
panic("unreachable") |
| 328 |
} |
| 329 |
return nil |
| 330 |
} |
| 331 |
|
| 332 |
func (t awsAsTest) SubscriptionErrorCheck(s *pubsub.Subscription, err error) error { |
| 333 |
var e smithy.APIError |
| 334 |
if !s.ErrorAs(err, &e) { |
| 335 |
return errors.New("Subscription.ErrorAs failed") |
| 336 |
} |
| 337 |
if got, want := e.ErrorCode(), "AWS.SimpleQueueService.NonExistentQueue"; got != want { |
| 338 |
return fmt.Errorf("got %q, want %q", got, want) |
| 339 |
} |
| 340 |
return nil |
| 341 |
} |
| 342 |
|
| 343 |
func (t awsAsTest) MessageCheck(m *pubsub.Message) error { |
| 344 |
var sm sqstypes.Message |
| 345 |
if !m.As(&sm) { |
| 346 |
return fmt.Errorf("cast failed for %T", &sm) |
| 347 |
} |
| 348 |
return nil |
| 349 |
} |
| 350 |
|
| 351 |
func (t awsAsTest) BeforeSend(as func(any) bool) error { |
| 352 |
switch t.topicKind { |
| 353 |
case topicKindSNS, topicKindSNSRaw: |
| 354 |
var pub *sns.PublishInput |
| 355 |
if !as(&pub) { |
| 356 |
return fmt.Errorf("cast failed for %T", &pub) |
| 357 |
} |
| 358 |
var entry *snstypes.PublishBatchRequestEntry |
| 359 |
if !as(&entry) { |
| 360 |
return fmt.Errorf("cast failed for %T", &entry) |
| 361 |
} |
| 362 |
case topicKindSQS: |
| 363 |
var entry *sqstypes.SendMessageBatchRequestEntry |
| 364 |
if !as(&entry) { |
| 365 |
return fmt.Errorf("cast failed for %T", &entry) |
| 366 |
} |
| 367 |
default: |
| 368 |
panic("unreachable") |
| 369 |
} |
| 370 |
return nil |
| 371 |
} |
| 372 |
|
| 373 |
func (t awsAsTest) AfterSend(as func(any) bool) error { |
| 374 |
switch t.topicKind { |
| 375 |
case topicKindSNS, topicKindSNSRaw: |
| 376 |
var pub *sns.PublishOutput |
| 377 |
if !as(&pub) { |
| 378 |
return fmt.Errorf("cast failed for %T", &pub) |
| 379 |
} |
| 380 |
var entry snstypes.PublishBatchResultEntry |
| 381 |
if !as(&entry) { |
| 382 |
return fmt.Errorf("cast failed for %T", &entry) |
| 383 |
} |
| 384 |
case topicKindSQS: |
| 385 |
var entry sqstypes.SendMessageBatchResultEntry |
| 386 |
if !as(&entry) { |
| 387 |
return fmt.Errorf("cast failed for %T", &entry) |
| 388 |
} |
| 389 |
default: |
| 390 |
panic("unreachable") |
| 391 |
} |
| 392 |
return nil |
| 393 |
} |
| 394 |
|
| 395 |
func sanitize(s string) string { |
| 396 |
// AWS doesn't like names that are too long; trim some not-so-useful stuff. |
| 397 |
const maxNameLen = 80 |
| 398 |
s = strings.Replace(s, "TestConformance", "", 1) |
| 399 |
s = strings.Replace(s, "/Test", "", 1) |
| 400 |
s = strings.Replace(s, "/", "_", -1) |
| 401 |
if len(s) > maxNameLen { |
| 402 |
// Drop prefix, not suffix, because suffix includes something to make |
| 403 |
// entities unique within a test. |
| 404 |
s = s[len(s)-maxNameLen:] |
| 405 |
} |
| 406 |
return s |
| 407 |
} |
| 408 |
|
| 409 |
func BenchmarkSNSSQS(b *testing.B) { |
| 410 |
benchmark(b, topicKindSNS) |
| 411 |
} |
| 412 |
|
| 413 |
func BenchmarkSQS(b *testing.B) { |
| 414 |
benchmark(b, topicKindSQS) |
| 415 |
} |
| 416 |
|
| 417 |
func benchmark(b *testing.B, topicKind topicKind) { |
| 418 |
b.Helper() |
| 419 |
|
| 420 |
ctx := context.Background() |
| 421 |
cfg, err := config.LoadDefaultConfig(ctx) |
| 422 |
if err != nil { |
| 423 |
b.Fatal(err) |
| 424 |
} |
| 425 |
topicName := fmt.Sprintf("%s-topic", b.Name()) |
| 426 |
snsClient := sns.NewFromConfig(cfg) |
| 427 |
sqsClient := sqs.NewFromConfig(cfg) |
| 428 |
dt, cleanup1, err := createTopic(ctx, topicName, snsClient, sqsClient, topicKind, nil) |
| 429 |
if err != nil { |
| 430 |
b.Fatal(err) |
| 431 |
} |
| 432 |
defer cleanup1() |
| 433 |
sendBatcherOpts := sendBatcherOptsSNS |
| 434 |
if topicKind == topicKindSQS { |
| 435 |
sendBatcherOpts = sendBatcherOptsSQS |
| 436 |
} |
| 437 |
topic := pubsub.NewTopic(dt, sendBatcherOpts) |
| 438 |
defer topic.Shutdown(ctx) |
| 439 |
subName := fmt.Sprintf("%s-subscription", b.Name()) |
| 440 |
ds, cleanup2, err := createSubscription(ctx, dt, subName, snsClient, sqsClient, topicKind) |
| 441 |
if err != nil { |
| 442 |
b.Fatal(err) |
| 443 |
} |
| 444 |
defer cleanup2() |
| 445 |
sub := pubsub.NewSubscription(ds, recvBatcherOpts, ackBatcherOpts) |
| 446 |
defer sub.Shutdown(ctx) |
| 447 |
drivertest.RunBenchmarks(b, topic, sub) |
| 448 |
} |
| 449 |
|
| 450 |
func TestOpenTopicFromURL(t *testing.T) { |
| 451 |
tests := []struct { |
| 452 |
URL string |
| 453 |
WantErr bool |
| 454 |
}{ |
| 455 |
// SNS... |
| 456 |
|
| 457 |
// OK. |
| 458 |
{"awssns:///arn:aws:service:region:accountid:resourceType/resourcePath", false}, |
| 459 |
// OK, setting region. |
| 460 |
{"awssns:///arn:aws:service:region:accountid:resourceType/resourcePath?region=us-east-2", false}, |
| 461 |
// Invalid parameter. |
| 462 |
{"awssns:///arn:aws:service:region:accountid:resourceType/resourcePath?param=value", true}, |
| 463 |
|
| 464 |
// SQS... |
| 465 |
// OK. |
| 466 |
{"awssqs://sqs.us-east-2.amazonaws.com/99999/my-queue", false}, |
| 467 |
// OK, setting region. |
| 468 |
{"awssqs://sqs.us-east-2.amazonaws.com/99999/my-queue?region=us-east-2", false}, |
| 469 |
// Invalid parameter. |
| 470 |
{"awssqs://sqs.us-east-2.amazonaws.com/99999/my-queue?param=value", true}, |
| 471 |
} |
| 472 |
|
| 473 |
ctx := context.Background() |
| 474 |
for _, test := range tests { |
| 475 |
topic, err := pubsub.OpenTopic(ctx, test.URL) |
| 476 |
if (err != nil) != test.WantErr { |
| 477 |
t.Errorf("%s: got error %v, want error %v", test.URL, err, test.WantErr) |
| 478 |
} |
| 479 |
if topic != nil { |
| 480 |
topic.Shutdown(ctx) |
| 481 |
} |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
func TestOpenSubscriptionFromURL(t *testing.T) { |
| 486 |
tests := []struct { |
| 487 |
URL string |
| 488 |
WantErr bool |
| 489 |
}{ |
| 490 |
// OK. |
| 491 |
{"awssqs://sqs.us-east-2.amazonaws.com/99999/my-queue", false}, |
| 492 |
// OK, setting region. |
| 493 |
{"awssqs://sqs.us-east-2.amazonaws.com/99999/my-queue?region=us-east-2", false}, |
| 494 |
// OK, setting raw. |
| 495 |
{"awssqs://sqs.us-east-2.amazonaws.com/99999/my-queue?raw=true", false}, |
| 496 |
// OK, setting raw. |
| 497 |
{"awssqs://sqs.us-east-2.amazonaws.com/99999/my-queue?raw=1", false}, |
| 498 |
// Invalid raw. |
| 499 |
{"awssqs://sqs.us-east-2.amazonaws.com/99999/my-queue?raw=foo", true}, |
| 500 |
// OK, setting nacklazy. |
| 501 |
{"awssqs://sqs.us-east-2.amazonaws.com/99999/my-queue?nacklazy=1", false}, |
| 502 |
// Invalid nacklazy. |
| 503 |
{"awssqs://sqs.us-east-2.amazonaws.com/99999/my-queue?nacklazy=foo", true}, |
| 504 |
// OK, setting waittime. |
| 505 |
{"awssqs://sqs.us-east-2.amazonaws.com/99999/my-queue?waittime=5s", false}, |
| 506 |
// Invalid waittime. |
| 507 |
{"awssqs://sqs.us-east-2.amazonaws.com/99999/my-queue?waittime=foo", true}, |
| 508 |
// Invalid parameter. |
| 509 |
{"awssqs://sqs.us-east-2.amazonaws.com/99999/my-queue?param=value", true}, |
| 510 |
} |
| 511 |
|
| 512 |
ctx := context.Background() |
| 513 |
for _, test := range tests { |
| 514 |
sub, err := pubsub.OpenSubscription(ctx, test.URL) |
| 515 |
if (err != nil) != test.WantErr { |
| 516 |
t.Errorf("%s: got error %v, want error %v", test.URL, err, test.WantErr) |
| 517 |
} |
| 518 |
if sub != nil { |
| 519 |
sub.Shutdown(ctx) |
| 520 |
} |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
func TestFIFO(t *testing.T) { |
| 525 |
for _, tt := range []struct { |
| 526 |
name string |
| 527 |
kind topicKind |
| 528 |
}{ |
| 529 |
{ |
| 530 |
name: "TestSNSTopic", |
| 531 |
kind: topicKindSNS, |
| 532 |
}, |
| 533 |
// This test is flaky because it sets 2 attributes for CreateTopic, |
| 534 |
// and the HTTP record/replay randomly re-sorts them. I'm not sure how |
| 535 |
// to fix that, so disabling the test. It's also unclear why only |
| 536 |
// this test appears affected, maybe the AWS code iterates over the |
| 537 |
// map here and doesn't in other cases? |
| 538 |
/* |
| 539 |
{ |
| 540 |
name: "TestSQSTopic", |
| 541 |
kind: topicKindSQS, |
| 542 |
}, |
| 543 |
*/ |
| 544 |
} { |
| 545 |
t.Run(tt.name, func(t *testing.T) { |
| 546 |
testFIFOTopic(t, tt.kind) |
| 547 |
}) |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
// testFIFOTopic tests FIFO topics. |
| 552 |
// |
| 553 |
// FIFO topics require a message group ID to be set on the message. |
| 554 |
// |
| 555 |
// The content-based deduplication attribute must be set to true on the topic. |
| 556 |
// - If set to true, the message deduplication ID is generated using the message body (sha256 hash). |
| 557 |
// - If not set, then the DeduplicationID must be set on the message. |
| 558 |
// |
| 559 |
// For more information see: |
| 560 |
// - https://pkg.go.dev/github.com/aws/aws-sdk-go/service/sns#CreateTopicInput.Attributes |
| 561 |
// - https://pkg.go.dev/github.com/aws/aws-sdk-go/service/sqs#CreateQueueInput.Attributes |
| 562 |
func testFIFOTopic(t *testing.T, kind topicKind) { |
| 563 |
t.Helper() |
| 564 |
type harnessArgs struct { |
| 565 |
attributes map[string]string |
| 566 |
} |
| 567 |
|
| 568 |
const ( |
| 569 |
attributeKeyContentBasedDeduplication = "ContentBasedDeduplication" |
| 570 |
attributeKeyFifoTopic = "FifoTopic" |
| 571 |
attributeKeyFifoQueue = "FifoQueue" |
| 572 |
) |
| 573 |
|
| 574 |
tests := []struct { |
| 575 |
name string |
| 576 |
harness harnessArgs |
| 577 |
message *pubsub.Message |
| 578 |
wantErr bool |
| 579 |
}{ |
| 580 |
{ |
| 581 |
name: "TestSendReceiveValid", |
| 582 |
harness: harnessArgs{ |
| 583 |
attributes: map[string]string{ |
| 584 |
attributeKeyContentBasedDeduplication: "true", |
| 585 |
}, |
| 586 |
}, |
| 587 |
message: &pubsub.Message{ |
| 588 |
Body: []byte("hello world"), |
| 589 |
Metadata: map[string]string{ |
| 590 |
MetadataKeyMessageGroupID: "1", |
| 591 |
}, |
| 592 |
}, |
| 593 |
wantErr: false, |
| 594 |
}, |
| 595 |
{ |
| 596 |
name: "TestSendReceiveInvalidNoMessageGroupID", |
| 597 |
harness: harnessArgs{ |
| 598 |
attributes: map[string]string{ |
| 599 |
attributeKeyContentBasedDeduplication: "true", |
| 600 |
}, |
| 601 |
}, |
| 602 |
message: &pubsub.Message{ |
| 603 |
Body: []byte("hello world"), |
| 604 |
Metadata: map[string]string{ |
| 605 |
MetadataKeyDeduplicationID: "1", |
| 606 |
}, |
| 607 |
}, |
| 608 |
wantErr: true, |
| 609 |
}, |
| 610 |
{ |
| 611 |
name: "TestSendReceiveInvalidNoDeduplicationID", |
| 612 |
// We dont set the ContentBasedDeduplication attribute to trigger the error. |
| 613 |
harness: harnessArgs{}, |
| 614 |
message: &pubsub.Message{ |
| 615 |
Body: []byte("hello world"), |
| 616 |
Metadata: map[string]string{}, |
| 617 |
}, |
| 618 |
wantErr: true, |
| 619 |
}, |
| 620 |
} |
| 621 |
for _, tt := range tests { |
| 622 |
t.Run(tt.name, func(t *testing.T) { |
| 623 |
// Create the harness. |
| 624 |
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) |
| 625 |
defer cancel() |
| 626 |
h, err := newHarness(ctx, t, kind) |
| 627 |
if err != nil { |
| 628 |
t.Fatal(err) |
| 629 |
} |
| 630 |
defer h.Close() |
| 631 |
|
| 632 |
// Set the FIFO attributes. |
| 633 |
attributes := make(map[string]string) |
| 634 |
maps.Copy(attributes, tt.harness.attributes) |
| 635 |
switch kind { |
| 636 |
case topicKindSNS: |
| 637 |
attributes[attributeKeyFifoTopic] = "true" |
| 638 |
case topicKindSQS: |
| 639 |
attributes[attributeKeyFifoQueue] = "true" |
| 640 |
} |
| 641 |
h.(*harness).topicAttributes = attributes |
| 642 |
h.(*harness).useFIFO = true |
| 643 |
|
| 644 |
// Create the topic and subscription. |
| 645 |
dt, cleanup, err := h.CreateTopic(ctx, t.Name()) |
| 646 |
if err != nil { |
| 647 |
t.Errorf("harness.CreateTopic() error = %v", err) |
| 648 |
return |
| 649 |
} |
| 650 |
defer cleanup() |
| 651 |
topic := pubsub.NewTopic(dt, sendBatcherOptsSNS) |
| 652 |
defer topic.Shutdown(ctx) |
| 653 |
ds, cleanup, err := h.CreateSubscription(ctx, dt, t.Name()) |
| 654 |
if err != nil { |
| 655 |
t.Errorf("harness.CreateSubscription() error = %v", err) |
| 656 |
return |
| 657 |
} |
| 658 |
defer cleanup() |
| 659 |
sub := pubsub.NewSubscription(ds, recvBatcherOpts, ackBatcherOpts) |
| 660 |
defer sub.Shutdown(ctx) |
| 661 |
|
| 662 |
// Send and receive the message. |
| 663 |
err = topic.Send(ctx, tt.message) |
| 664 |
if (err != nil) != tt.wantErr { |
| 665 |
t.Errorf("Topic.Send() error = %v, wantErr %v", err, tt.wantErr) |
| 666 |
return |
| 667 |
} |
| 668 |
if tt.wantErr { |
| 669 |
return |
| 670 |
} |
| 671 |
m, err := sub.Receive(ctx) |
| 672 |
if err != nil { |
| 673 |
t.Errorf("Subscription.Receive() error = %v", err) |
| 674 |
return |
| 675 |
} |
| 676 |
if diff := cmp.Diff(tt.message.Body, m.Body); diff != "" { |
| 677 |
t.Errorf("Received message body: -got, +want: %s", diff) |
| 678 |
} |
| 679 |
m.Ack() |
| 680 |
}) |
| 681 |
} |
| 682 |
} |