| 1 |
// Copyright 2025 Google LLC |
| 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 |
// http://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 pstest |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"context" |
| 20 |
"errors" |
| 21 |
"fmt" |
| 22 |
"io" |
| 23 |
"math/rand" |
| 24 |
"net" |
| 25 |
"reflect" |
| 26 |
"strings" |
| 27 |
"sync" |
| 28 |
"testing" |
| 29 |
"time" |
| 30 |
|
| 31 |
iampb "cloud.google.com/go/iam/apiv1/iampb" |
| 32 |
"cloud.google.com/go/internal/testutil" |
| 33 |
pb "cloud.google.com/go/pubsub/v2/apiv1/pubsubpb" |
| 34 |
"google.golang.org/grpc" |
| 35 |
"google.golang.org/grpc/codes" |
| 36 |
"google.golang.org/grpc/credentials/insecure" |
| 37 |
"google.golang.org/grpc/status" |
| 38 |
"google.golang.org/protobuf/types/known/durationpb" |
| 39 |
field_mask "google.golang.org/protobuf/types/known/fieldmaskpb" |
| 40 |
"google.golang.org/protobuf/types/known/timestamppb" |
| 41 |
) |
| 42 |
|
| 43 |
func TestNewServerWithPort(t *testing.T) { |
| 44 |
port := getFreePort(t) |
| 45 |
srv := NewServerWithPort(port) |
| 46 |
|
| 47 |
conn, err := grpc.NewClient(srv.Addr, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 48 |
if err != nil { |
| 49 |
t.Fatal(err) |
| 50 |
} |
| 51 |
|
| 52 |
t.Cleanup(func() { |
| 53 |
conn.Close() |
| 54 |
srv.Close() |
| 55 |
}) |
| 56 |
} |
| 57 |
|
| 58 |
func TestNewServerWithAddress(t *testing.T) { |
| 59 |
hosts := []string{ |
| 60 |
"", |
| 61 |
"0.0.0.0", |
| 62 |
"127.0.0.1", |
| 63 |
"localhost", |
| 64 |
} |
| 65 |
for _, h := range hosts { |
| 66 |
port := getFreePort(t) |
| 67 |
address := fmt.Sprintf("%s:%d", h, port) |
| 68 |
t.Run(fmt.Sprintf("Init new server succeed with address %s", address), func(t *testing.T) { |
| 69 |
srv := NewServerWithAddress(address) |
| 70 |
|
| 71 |
conn, err := grpc.NewClient(srv.Addr, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 72 |
if err != nil { |
| 73 |
t.Fatal(err) |
| 74 |
} |
| 75 |
|
| 76 |
t.Cleanup(func() { |
| 77 |
conn.Close() |
| 78 |
srv.Close() |
| 79 |
}) |
| 80 |
}) |
| 81 |
} |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
func TestNewServerWithCallback(t *testing.T) { |
| 86 |
port := getFreePort(t) |
| 87 |
additionalFake := struct { |
| 88 |
iampb.UnimplementedIAMPolicyServer |
| 89 |
}{} |
| 90 |
|
| 91 |
verifyCallback := false |
| 92 |
callback := func(grpc *grpc.Server) { |
| 93 |
// register something |
| 94 |
iampb.RegisterIAMPolicyServer(grpc, &additionalFake) |
| 95 |
verifyCallback = true |
| 96 |
} |
| 97 |
|
| 98 |
// Pass a non 0 port to demonstrate we can pass a hardcoded port for the server to listen on |
| 99 |
srv := NewServerWithCallback(port, callback) |
| 100 |
|
| 101 |
conn, err := grpc.NewClient(srv.Addr, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 102 |
if err != nil { |
| 103 |
t.Fatal(err) |
| 104 |
} |
| 105 |
|
| 106 |
if !verifyCallback { |
| 107 |
t.Fatal("callback was not invoked") |
| 108 |
} |
| 109 |
|
| 110 |
t.Cleanup(func() { |
| 111 |
conn.Close() |
| 112 |
srv.Close() |
| 113 |
}) |
| 114 |
} |
| 115 |
|
| 116 |
// getFreePort allocates an available port then close it so it's available. |
| 117 |
// Note: There is no guarantee that the port does not become used between closing |
| 118 |
// the listener and creating the new server with the invocation function, but |
| 119 |
// the chances are very small. |
| 120 |
func getFreePort(t *testing.T) int { |
| 121 |
l, err := net.Listen("tcp", ":0") |
| 122 |
if err != nil { |
| 123 |
t.Fatal(err) |
| 124 |
} |
| 125 |
port := l.Addr().(*net.TCPAddr).Port |
| 126 |
l.Close() |
| 127 |
|
| 128 |
return port |
| 129 |
} |
| 130 |
|
| 131 |
func TestTopics(t *testing.T) { |
| 132 |
pclient, sclient, server, cleanup := newFake(context.TODO(), t) |
| 133 |
defer cleanup() |
| 134 |
|
| 135 |
ctx := context.Background() |
| 136 |
var topics []*pb.Topic |
| 137 |
for i := 1; i < 3; i++ { |
| 138 |
topics = append(topics, mustCreateTopic(context.TODO(), t, pclient, &pb.Topic{ |
| 139 |
Name: fmt.Sprintf("projects/P/topics/T%d", i), |
| 140 |
Labels: map[string]string{"num": fmt.Sprintf("%d", i)}, |
| 141 |
})) |
| 142 |
} |
| 143 |
if got, want := len(server.GServer.topics), len(topics); got != want { |
| 144 |
t.Fatalf("got %d topics, want %d", got, want) |
| 145 |
} |
| 146 |
for _, top := range topics { |
| 147 |
got, err := pclient.GetTopic(ctx, &pb.GetTopicRequest{Topic: top.Name}) |
| 148 |
if err != nil { |
| 149 |
t.Fatal(err) |
| 150 |
} |
| 151 |
if !testutil.Equal(got, top) { |
| 152 |
t.Errorf("\ngot %+v\nwant %+v", got, top) |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
res, err := pclient.ListTopics(ctx, &pb.ListTopicsRequest{Project: "projects/P"}) |
| 157 |
if err != nil { |
| 158 |
t.Fatal(err) |
| 159 |
} |
| 160 |
if got, want := res.Topics, topics; !testutil.Equal(got, want) { |
| 161 |
t.Errorf("\ngot %+v\nwant %+v", got, want) |
| 162 |
} |
| 163 |
|
| 164 |
for _, top := range topics { |
| 165 |
if _, err := pclient.DeleteTopic(ctx, &pb.DeleteTopicRequest{Topic: top.Name}); err != nil { |
| 166 |
t.Fatal(err) |
| 167 |
} |
| 168 |
} |
| 169 |
if got, want := len(server.GServer.topics), 0; got != want { |
| 170 |
t.Fatalf("got %d topics, want %d", got, want) |
| 171 |
} |
| 172 |
|
| 173 |
t.Run(`Given a topic that is used by a subscription as deadLetter, |
| 174 |
When topic deleted, |
| 175 |
Then error raised`, func(t *testing.T) { |
| 176 |
var topics []*pb.Topic |
| 177 |
for i := 1; i < 3; i++ { |
| 178 |
topics = append(topics, mustCreateTopic(context.TODO(), t, pclient, &pb.Topic{ |
| 179 |
Name: fmt.Sprintf("projects/P/topics/T%d", i), |
| 180 |
Labels: map[string]string{"num": fmt.Sprintf("%d", i)}, |
| 181 |
})) |
| 182 |
} |
| 183 |
|
| 184 |
if got, want := len(server.GServer.topics), len(topics); got != want { |
| 185 |
t.Fatalf("got %d topics, want %d", got, want) |
| 186 |
} |
| 187 |
|
| 188 |
s := mustCreateSubscription(ctx, t, sclient, &pb.Subscription{ |
| 189 |
Name: fmt.Sprintf("project/P/subscriptions/sub_with_deadLetter"), |
| 190 |
Topic: topics[0].Name, |
| 191 |
AckDeadlineSeconds: 10, |
| 192 |
DeadLetterPolicy: &pb.DeadLetterPolicy{ |
| 193 |
DeadLetterTopic: topics[1].Name, |
| 194 |
}, |
| 195 |
}) |
| 196 |
|
| 197 |
_, err := pclient.DeleteTopic(ctx, &pb.DeleteTopicRequest{ |
| 198 |
Topic: topics[1].Name, |
| 199 |
}) |
| 200 |
expectedErr := status.Errorf(codes.FailedPrecondition, "topic %q used as deadLetter for %s", topics[1].Name, s.Name) |
| 201 |
if err == nil || !errors.Is(err, expectedErr) { |
| 202 |
t.Fatalf("returned a different error than the expected one. \nReceived '%s'; \nExpected: '%s'", err, expectedErr) |
| 203 |
} |
| 204 |
}) |
| 205 |
} |
| 206 |
|
| 207 |
func TestSubscriptions(t *testing.T) { |
| 208 |
pclient, sclient, server, cleanup := newFake(context.TODO(), t) |
| 209 |
defer cleanup() |
| 210 |
|
| 211 |
ctx := context.Background() |
| 212 |
topic := mustCreateTopic(context.TODO(), t, pclient, &pb.Topic{Name: "projects/P/topics/T"}) |
| 213 |
var subs []*pb.Subscription |
| 214 |
for i := 0; i < 3; i++ { |
| 215 |
subs = append(subs, mustCreateSubscription(context.TODO(), t, sclient, &pb.Subscription{ |
| 216 |
Name: fmt.Sprintf("projects/P/subscriptions/S%d", i), |
| 217 |
Topic: topic.Name, |
| 218 |
AckDeadlineSeconds: int32(10 * (i + 1)), |
| 219 |
})) |
| 220 |
} |
| 221 |
|
| 222 |
if got, want := len(server.GServer.subs), len(subs); got != want { |
| 223 |
t.Fatalf("got %d subscriptions, want %d", got, want) |
| 224 |
} |
| 225 |
for _, s := range subs { |
| 226 |
got, err := sclient.GetSubscription(ctx, &pb.GetSubscriptionRequest{Subscription: s.Name}) |
| 227 |
if err != nil { |
| 228 |
t.Fatal(err) |
| 229 |
} |
| 230 |
if !testutil.Equal(got, s) { |
| 231 |
t.Errorf("\ngot %+v\nwant %+v", got, s) |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
res, err := sclient.ListSubscriptions(ctx, &pb.ListSubscriptionsRequest{Project: "projects/P"}) |
| 236 |
if err != nil { |
| 237 |
t.Fatal(err) |
| 238 |
} |
| 239 |
if got, want := res.Subscriptions, subs; !testutil.Equal(got, want) { |
| 240 |
t.Errorf("\ngot %+v\nwant %+v", got, want) |
| 241 |
} |
| 242 |
|
| 243 |
res2, err := pclient.ListTopicSubscriptions(ctx, &pb.ListTopicSubscriptionsRequest{Topic: topic.Name}) |
| 244 |
if err != nil { |
| 245 |
t.Fatal(err) |
| 246 |
} |
| 247 |
if got, want := len(res2.Subscriptions), len(subs); got != want { |
| 248 |
t.Fatalf("got %d subs, want %d", got, want) |
| 249 |
} |
| 250 |
for i, got := range res2.Subscriptions { |
| 251 |
want := subs[i].Name |
| 252 |
if !testutil.Equal(got, want) { |
| 253 |
t.Errorf("\ngot %+v\nwant %+v", got, want) |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
subToDetach := "projects/P/subscriptions/S0" |
| 258 |
_, err = pclient.DetachSubscription(ctx, &pb.DetachSubscriptionRequest{ |
| 259 |
Subscription: subToDetach, |
| 260 |
}) |
| 261 |
if err != nil { |
| 262 |
t.Fatalf("attempted to detach sub %s, got error: %v", subToDetach, err) |
| 263 |
} |
| 264 |
|
| 265 |
for _, s := range subs { |
| 266 |
if _, err := sclient.DeleteSubscription(ctx, &pb.DeleteSubscriptionRequest{Subscription: s.Name}); err != nil { |
| 267 |
t.Fatal(err) |
| 268 |
} |
| 269 |
} |
| 270 |
if got, want := len(server.GServer.subs), 0; got != want { |
| 271 |
t.Fatalf("got %d subscriptions, want %d", got, want) |
| 272 |
} |
| 273 |
|
| 274 |
t.Run(`Given a subscription creation, |
| 275 |
When called with a deadLetter topic that does not exist, |
| 276 |
Then error returned`, func(t *testing.T) { |
| 277 |
topic := mustCreateTopic(ctx, t, pclient, &pb.Topic{Name: "projects/P/topics/test"}) |
| 278 |
_, err := server.GServer.CreateSubscription(ctx, &pb.Subscription{ |
| 279 |
Name: "projects/P/subscriptions/test", |
| 280 |
Topic: topic.Name, |
| 281 |
AckDeadlineSeconds: 10, |
| 282 |
DeadLetterPolicy: &pb.DeadLetterPolicy{ |
| 283 |
DeadLetterTopic: "projects/P/topics/nonexisting", |
| 284 |
}, |
| 285 |
}) |
| 286 |
expectedErr := status.Errorf(codes.NotFound, "deadLetter topic \"projects/P/topics/nonexisting\"") |
| 287 |
if err == nil || !errors.Is(err, expectedErr) { |
| 288 |
t.Fatalf("expected subscription creation to fail with a specific err but it didn't. \nError: %s \nExpected err: %s", err, expectedErr) |
| 289 |
} |
| 290 |
_, err = server.GServer.DeleteTopic(ctx, &pb.DeleteTopicRequest{ |
| 291 |
Topic: topic.Name, |
| 292 |
}) |
| 293 |
if err != nil { |
| 294 |
t.Fatalf("unexpected error during deleting topic") |
| 295 |
} |
| 296 |
}) |
| 297 |
} |
| 298 |
|
| 299 |
func TestSubscriptionErrors(t *testing.T) { |
| 300 |
_, sclient, _, cleanup := newFake(context.TODO(), t) |
| 301 |
defer cleanup() |
| 302 |
|
| 303 |
ctx := context.Background() |
| 304 |
|
| 305 |
checkCode := func(err error, want codes.Code) { |
| 306 |
t.Helper() |
| 307 |
if status.Code(err) != want { |
| 308 |
t.Errorf("got %v, want code %s", err, want) |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
_, err := sclient.GetSubscription(ctx, &pb.GetSubscriptionRequest{}) |
| 313 |
checkCode(err, codes.InvalidArgument) |
| 314 |
_, err = sclient.GetSubscription(ctx, &pb.GetSubscriptionRequest{Subscription: "s"}) |
| 315 |
checkCode(err, codes.NotFound) |
| 316 |
_, err = sclient.UpdateSubscription(ctx, &pb.UpdateSubscriptionRequest{}) |
| 317 |
checkCode(err, codes.InvalidArgument) |
| 318 |
_, err = sclient.UpdateSubscription(ctx, &pb.UpdateSubscriptionRequest{Subscription: &pb.Subscription{}}) |
| 319 |
checkCode(err, codes.InvalidArgument) |
| 320 |
_, err = sclient.UpdateSubscription(ctx, &pb.UpdateSubscriptionRequest{Subscription: &pb.Subscription{Name: "s"}}) |
| 321 |
checkCode(err, codes.NotFound) |
| 322 |
_, err = sclient.DeleteSubscription(ctx, &pb.DeleteSubscriptionRequest{}) |
| 323 |
checkCode(err, codes.InvalidArgument) |
| 324 |
_, err = sclient.DeleteSubscription(ctx, &pb.DeleteSubscriptionRequest{Subscription: "s"}) |
| 325 |
checkCode(err, codes.NotFound) |
| 326 |
_, err = sclient.Acknowledge(ctx, &pb.AcknowledgeRequest{}) |
| 327 |
checkCode(err, codes.InvalidArgument) |
| 328 |
_, err = sclient.Acknowledge(ctx, &pb.AcknowledgeRequest{Subscription: "s"}) |
| 329 |
checkCode(err, codes.NotFound) |
| 330 |
_, err = sclient.ModifyAckDeadline(ctx, &pb.ModifyAckDeadlineRequest{}) |
| 331 |
checkCode(err, codes.InvalidArgument) |
| 332 |
_, err = sclient.ModifyAckDeadline(ctx, &pb.ModifyAckDeadlineRequest{Subscription: "s"}) |
| 333 |
checkCode(err, codes.NotFound) |
| 334 |
_, err = sclient.Pull(ctx, &pb.PullRequest{}) |
| 335 |
checkCode(err, codes.InvalidArgument) |
| 336 |
_, err = sclient.Pull(ctx, &pb.PullRequest{Subscription: "s"}) |
| 337 |
checkCode(err, codes.NotFound) |
| 338 |
_, err = sclient.Seek(ctx, &pb.SeekRequest{}) |
| 339 |
checkCode(err, codes.InvalidArgument) |
| 340 |
srt := &pb.SeekRequest_Time{Time: timestamppb.Now()} |
| 341 |
_, err = sclient.Seek(ctx, &pb.SeekRequest{Target: srt}) |
| 342 |
checkCode(err, codes.InvalidArgument) |
| 343 |
_, err = sclient.Seek(ctx, &pb.SeekRequest{Target: srt, Subscription: "s"}) |
| 344 |
checkCode(err, codes.NotFound) |
| 345 |
} |
| 346 |
|
| 347 |
func TestSubscriptionDeadLetter(t *testing.T) { |
| 348 |
_, _, server, cleanup := newFake(context.TODO(), t) |
| 349 |
defer cleanup() |
| 350 |
|
| 351 |
ctx := context.Background() |
| 352 |
|
| 353 |
topic, err := server.GServer.CreateTopic(ctx, &pb.Topic{ |
| 354 |
Name: "projects/P/topics/in", |
| 355 |
}) |
| 356 |
if err != nil { |
| 357 |
t.Fatalf("failed to create in topic") |
| 358 |
} |
| 359 |
deadLetterTopic, err := server.GServer.CreateTopic(ctx, &pb.Topic{ |
| 360 |
Name: "projects/P/topics/deadLetter", |
| 361 |
}) |
| 362 |
if err != nil { |
| 363 |
t.Fatalf("failed to create deadLetter topic") |
| 364 |
} |
| 365 |
retries := 3 |
| 366 |
sub, err := server.GServer.CreateSubscription(ctx, &pb.Subscription{ |
| 367 |
Name: "projects/P/subscriptions/S", |
| 368 |
Topic: topic.Name, |
| 369 |
AckDeadlineSeconds: 10, |
| 370 |
DeadLetterPolicy: &pb.DeadLetterPolicy{ |
| 371 |
DeadLetterTopic: deadLetterTopic.Name, |
| 372 |
MaxDeliveryAttempts: int32(retries), |
| 373 |
}, |
| 374 |
}) |
| 375 |
if err != nil { |
| 376 |
t.Fatalf("failed to create subscription") |
| 377 |
} |
| 378 |
dlSub, err := server.GServer.CreateSubscription(ctx, &pb.Subscription{ |
| 379 |
Name: "projects/P/subscriptions/SD", |
| 380 |
Topic: deadLetterTopic.Name, |
| 381 |
AckDeadlineSeconds: 10, |
| 382 |
}) |
| 383 |
if err != nil { |
| 384 |
t.Fatalf("failed to create subscription") |
| 385 |
} |
| 386 |
|
| 387 |
messageData := []byte("message data") |
| 388 |
_, err = server.GServer.Publish(ctx, &pb.PublishRequest{ |
| 389 |
Topic: topic.Name, |
| 390 |
Messages: []*pb.PubsubMessage{ |
| 391 |
{ |
| 392 |
Data: messageData, |
| 393 |
}, |
| 394 |
}, |
| 395 |
}) |
| 396 |
if err != nil { |
| 397 |
t.Fatalf("failed to publish message") |
| 398 |
} |
| 399 |
rand.Seed(time.Now().UTC().UnixNano()) |
| 400 |
maxAttempts := rand.Intn(5) + retries |
| 401 |
for i := 0; i < maxAttempts; i++ { |
| 402 |
pull, err := server.GServer.Pull(ctx, &pb.PullRequest{ |
| 403 |
Subscription: sub.Name, |
| 404 |
MaxMessages: 10, |
| 405 |
}) |
| 406 |
if err != nil { |
| 407 |
t.Fatalf("failed during pull") |
| 408 |
} |
| 409 |
if i < retries { |
| 410 |
if len(pull.ReceivedMessages) != 1 { |
| 411 |
t.Fatalf("expected 1 message received a different number %d", len(pull.ReceivedMessages)) |
| 412 |
|
| 413 |
} |
| 414 |
for _, m := range pull.ReceivedMessages { |
| 415 |
if int32(i+1) != m.DeliveryAttempt { |
| 416 |
t.Fatalf("message delivery attempt not the expected one. expected: %d, actual: %d", i+1, m.DeliveryAttempt) |
| 417 |
} |
| 418 |
_, err := server.GServer.ModifyAckDeadline(ctx, &pb.ModifyAckDeadlineRequest{ |
| 419 |
Subscription: sub.Name, |
| 420 |
AckIds: []string{m.AckId}, |
| 421 |
AckDeadlineSeconds: 0, |
| 422 |
}) |
| 423 |
if err != nil { |
| 424 |
t.Fatalf("failed to modify ack deadline") |
| 425 |
} |
| 426 |
} |
| 427 |
} else { |
| 428 |
if len(pull.ReceivedMessages) > 0 { |
| 429 |
t.Fatalf("received a non empty list of messages %d", len(pull.ReceivedMessages)) |
| 430 |
} |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
dlPull, err := server.GServer.Pull(ctx, &pb.PullRequest{ |
| 435 |
Subscription: dlSub.Name, |
| 436 |
MaxMessages: 10, |
| 437 |
}) |
| 438 |
if err != nil { |
| 439 |
t.Fatalf("failed during pulling from deadLetter sub") |
| 440 |
} |
| 441 |
if len(dlPull.ReceivedMessages) != 1 { |
| 442 |
t.Fatalf("expected 1 message received a different number %d", len(dlPull.ReceivedMessages)) |
| 443 |
} |
| 444 |
receivedMessage := dlPull.ReceivedMessages[0] |
| 445 |
if bytes.Compare(receivedMessage.Message.Data, messageData) != 0 { |
| 446 |
t.Fatalf("unexpected message received from deadLetter") |
| 447 |
} |
| 448 |
if receivedMessage.DeliveryAttempt > 0 { |
| 449 |
t.Fatalf("message sent to deadLetter should not have the deliveryAttempt value from the original subscription message") |
| 450 |
} |
| 451 |
_, err = server.GServer.Acknowledge(ctx, &pb.AcknowledgeRequest{ |
| 452 |
Subscription: dlSub.Name, |
| 453 |
AckIds: []string{receivedMessage.GetAckId()}, |
| 454 |
}) |
| 455 |
if err != nil { |
| 456 |
t.Fatalf("failed to acknowledge message from deadLetter") |
| 457 |
} |
| 458 |
|
| 459 |
for _, s := range []string{"projects/P/subscriptions/S", "projects/P/subscriptions/SD"} { |
| 460 |
_, err = server.GServer.DeleteSubscription(ctx, &pb.DeleteSubscriptionRequest{ |
| 461 |
Subscription: s, |
| 462 |
}) |
| 463 |
if err != nil { |
| 464 |
t.Fatalf("failed to delete subscription %s; error: %s", s, err) |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
for _, delTopic := range []string{"projects/P/topics/in", "projects/P/topics/deadLetter"} { |
| 469 |
_, err = server.GServer.DeleteTopic(ctx, &pb.DeleteTopicRequest{ |
| 470 |
Topic: delTopic, |
| 471 |
}) |
| 472 |
if err != nil { |
| 473 |
t.Fatalf("failed to delete topic %s; error: %s", delTopic, err) |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
if got, want := len(server.GServer.subs), 0; got != want { |
| 478 |
t.Fatalf("got %d subscriptions, want %d", got, want) |
| 479 |
} |
| 480 |
|
| 481 |
if got, want := len(server.GServer.topics), 0; got != want { |
| 482 |
t.Fatalf("got %d topics, want %d", got, want) |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
func TestPublish(t *testing.T) { |
| 487 |
s := NewServer() |
| 488 |
defer s.Close() |
| 489 |
|
| 490 |
var ids []string |
| 491 |
for i := 0; i < 3; i++ { |
| 492 |
ids = append(ids, s.Publish("projects/p/topics/t", []byte("hello"), nil)) |
| 493 |
} |
| 494 |
s.Wait() |
| 495 |
ms := s.Messages() |
| 496 |
if got, want := len(ms), len(ids); got != want { |
| 497 |
t.Errorf("got %d messages, want %d", got, want) |
| 498 |
} |
| 499 |
for i, id := range ids { |
| 500 |
if got, want := ms[i].ID, id; got != want { |
| 501 |
t.Errorf("got %s, want %s", got, want) |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
m := s.Message(ids[1]) |
| 506 |
if m == nil { |
| 507 |
t.Error("got nil, want a message") |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
func TestPublishOrdered(t *testing.T) { |
| 512 |
s := NewServer() |
| 513 |
defer s.Close() |
| 514 |
|
| 515 |
const orderingKey = "ordering-key" |
| 516 |
var ids []string |
| 517 |
for i := 0; i < 3; i++ { |
| 518 |
ids = append(ids, s.PublishOrdered("projects/p/topics/t", []byte("hello"), nil, orderingKey)) |
| 519 |
} |
| 520 |
s.Wait() |
| 521 |
ms := s.Messages() |
| 522 |
if got, want := len(ms), len(ids); got != want { |
| 523 |
t.Errorf("got %d messages, want %d", got, want) |
| 524 |
} |
| 525 |
for i, id := range ids { |
| 526 |
if got, want := ms[i].ID, id; got != want { |
| 527 |
t.Errorf("got %s, want %s", got, want) |
| 528 |
} |
| 529 |
if got, want := ms[i].OrderingKey, orderingKey; got != want { |
| 530 |
t.Errorf("got %s, want %s", got, want) |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
m := s.Message(ids[1]) |
| 535 |
if m == nil { |
| 536 |
t.Error("got nil, want a message") |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
func TestClearMessages(t *testing.T) { |
| 541 |
pclient, sclient, s, cleanup := newFake(context.TODO(), t) |
| 542 |
defer cleanup() |
| 543 |
|
| 544 |
top := mustCreateTopic(context.TODO(), t, pclient, &pb.Topic{Name: "projects/P/topics/T"}) |
| 545 |
sub := mustCreateSubscription(context.TODO(), t, sclient, &pb.Subscription{ |
| 546 |
Name: "projects/P/subscriptions/S", |
| 547 |
Topic: top.Name, |
| 548 |
AckDeadlineSeconds: 10, |
| 549 |
}) |
| 550 |
|
| 551 |
for i := 0; i < 3; i++ { |
| 552 |
s.Publish(top.Name, []byte("hello"), nil) |
| 553 |
} |
| 554 |
msgs := s.Messages() |
| 555 |
if got, want := len(msgs), 3; got != want { |
| 556 |
t.Errorf("got %d messages, want %d", got, want) |
| 557 |
} |
| 558 |
s.ClearMessages() |
| 559 |
msgs = s.Messages() |
| 560 |
if got, want := len(msgs), 0; got != want { |
| 561 |
t.Errorf("got %d messages, want %d", got, want) |
| 562 |
} |
| 563 |
|
| 564 |
res, err := sclient.Pull(context.Background(), &pb.PullRequest{Subscription: sub.Name}) |
| 565 |
if err != nil { |
| 566 |
t.Fatal(err) |
| 567 |
} |
| 568 |
if len(res.ReceivedMessages) != 0 { |
| 569 |
t.Errorf("got %d messages, want zero", len(res.ReceivedMessages)) |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
// Note: this sets the fake's "now" time, so it is sensitive to concurrent changes to "now". |
| 574 |
func publish(t *testing.T, srv *Server, pclient pb.PublisherClient, topic *pb.Topic, messages []*pb.PubsubMessage) map[string]*pb.PubsubMessage { |
| 575 |
pubTime := time.Now() |
| 576 |
srv.SetTimeNowFunc(func() time.Time { return pubTime }) |
| 577 |
defer srv.SetTimeNowFunc(time.Now) |
| 578 |
|
| 579 |
res, err := pclient.Publish(context.Background(), &pb.PublishRequest{ |
| 580 |
Topic: topic.Name, |
| 581 |
Messages: messages, |
| 582 |
}) |
| 583 |
if err != nil { |
| 584 |
t.Fatal(err) |
| 585 |
} |
| 586 |
tsPubTime := timestamppb.New(pubTime) |
| 587 |
want := map[string]*pb.PubsubMessage{} |
| 588 |
for i, id := range res.MessageIds { |
| 589 |
want[id] = &pb.PubsubMessage{ |
| 590 |
Data: messages[i].Data, |
| 591 |
Attributes: messages[i].Attributes, |
| 592 |
MessageId: id, |
| 593 |
PublishTime: tsPubTime, |
| 594 |
} |
| 595 |
} |
| 596 |
return want |
| 597 |
} |
| 598 |
|
| 599 |
func TestPull(t *testing.T) { |
| 600 |
pclient, sclient, srv, cleanup := newFake(context.TODO(), t) |
| 601 |
defer cleanup() |
| 602 |
|
| 603 |
top := mustCreateTopic(context.TODO(), t, pclient, &pb.Topic{Name: "projects/P/topics/T"}) |
| 604 |
sub := mustCreateSubscription(context.TODO(), t, sclient, &pb.Subscription{ |
| 605 |
Name: "projects/P/subscriptions/S", |
| 606 |
Topic: top.Name, |
| 607 |
AckDeadlineSeconds: 10, |
| 608 |
}) |
| 609 |
|
| 610 |
want := publish(t, srv, pclient, top, []*pb.PubsubMessage{ |
| 611 |
{Data: []byte("d1")}, |
| 612 |
{Data: []byte("d2")}, |
| 613 |
{Data: []byte("d3")}, |
| 614 |
}) |
| 615 |
got := pubsubMessages(pullN(context.TODO(), t, len(want), sclient, sub)) |
| 616 |
if diff := testutil.Diff(got, want); diff != "" { |
| 617 |
t.Error(diff) |
| 618 |
} |
| 619 |
|
| 620 |
res, err := sclient.Pull(context.Background(), &pb.PullRequest{Subscription: sub.Name}) |
| 621 |
if err != nil { |
| 622 |
t.Fatal(err) |
| 623 |
} |
| 624 |
if len(res.ReceivedMessages) != 0 { |
| 625 |
t.Errorf("got %d messages, want zero", len(res.ReceivedMessages)) |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
func TestStreamingPull(t *testing.T) { |
| 630 |
// A simple test of streaming pull. |
| 631 |
pclient, sclient, srv, cleanup := newFake(context.TODO(), t) |
| 632 |
defer cleanup() |
| 633 |
|
| 634 |
deadLetterTopic := mustCreateTopic(context.TODO(), t, pclient, &pb.Topic{ |
| 635 |
Name: "projects/P/topics/deadLetter", |
| 636 |
}) |
| 637 |
|
| 638 |
top := mustCreateTopic(context.TODO(), t, pclient, &pb.Topic{Name: "projects/P/topics/T"}) |
| 639 |
sub := mustCreateSubscription(context.TODO(), t, sclient, &pb.Subscription{ |
| 640 |
Name: "projects/P/subscriptions/S", |
| 641 |
Topic: top.Name, |
| 642 |
AckDeadlineSeconds: 10, |
| 643 |
DeadLetterPolicy: &pb.DeadLetterPolicy{ |
| 644 |
DeadLetterTopic: deadLetterTopic.Name, |
| 645 |
MaxDeliveryAttempts: 3, |
| 646 |
}, |
| 647 |
}) |
| 648 |
|
| 649 |
want := publish(t, srv, pclient, top, []*pb.PubsubMessage{ |
| 650 |
{Data: []byte("d1")}, |
| 651 |
{Data: []byte("d2")}, |
| 652 |
{Data: []byte("d3")}, |
| 653 |
}) |
| 654 |
received := streamingPullN(context.TODO(), t, len(want), sclient, sub) |
| 655 |
for _, m := range received { |
| 656 |
if m.DeliveryAttempt != 1 { |
| 657 |
t.Errorf("got DeliveryAttempt==%d, want 1", m.DeliveryAttempt) |
| 658 |
} |
| 659 |
} |
| 660 |
got := pubsubMessages(received) |
| 661 |
if diff := testutil.Diff(got, want); diff != "" { |
| 662 |
t.Error(diff) |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
// This test acks each message as it arrives and makes sure we don't see dups. |
| 667 |
func TestStreamingPullAck(t *testing.T) { |
| 668 |
minAckDeadlineSecs = 1 |
| 669 |
pclient, sclient, srv, cleanup := newFake(context.TODO(), t) |
| 670 |
defer cleanup() |
| 671 |
|
| 672 |
top := mustCreateTopic(context.TODO(), t, pclient, &pb.Topic{Name: "projects/P/topics/T"}) |
| 673 |
sub := mustCreateSubscription(context.TODO(), t, sclient, &pb.Subscription{ |
| 674 |
Name: "projects/P/subscriptions/S", |
| 675 |
Topic: top.Name, |
| 676 |
AckDeadlineSeconds: 1, |
| 677 |
}) |
| 678 |
|
| 679 |
_ = publish(t, srv, pclient, top, []*pb.PubsubMessage{ |
| 680 |
{Data: []byte("d1")}, |
| 681 |
{Data: []byte("d2")}, |
| 682 |
{Data: []byte("d3")}, |
| 683 |
}) |
| 684 |
|
| 685 |
got := map[string]bool{} |
| 686 |
ctx, cancel := context.WithCancel(context.Background()) |
| 687 |
spc := mustStartStreamingPull(ctx, t, sclient, sub) |
| 688 |
time.AfterFunc(time.Duration(2*minAckDeadlineSecs)*time.Second, cancel) |
| 689 |
|
| 690 |
for i := 0; i < 4; i++ { |
| 691 |
res, err := spc.Recv() |
| 692 |
if errors.Is(err, io.EOF) { |
| 693 |
break |
| 694 |
} |
| 695 |
if err != nil { |
| 696 |
if status.Code(err) == codes.Canceled { |
| 697 |
break |
| 698 |
} |
| 699 |
t.Fatal(err) |
| 700 |
} |
| 701 |
if i == 3 { |
| 702 |
t.Fatal("expected to only see 3 messages, got 4") |
| 703 |
} |
| 704 |
req := &pb.StreamingPullRequest{} |
| 705 |
for _, m := range res.ReceivedMessages { |
| 706 |
if got[m.Message.MessageId] { |
| 707 |
t.Fatal("duplicate message") |
| 708 |
} |
| 709 |
got[m.Message.MessageId] = true |
| 710 |
req.AckIds = append(req.AckIds, m.AckId) |
| 711 |
} |
| 712 |
if err := spc.Send(req); err != nil { |
| 713 |
t.Fatal(err) |
| 714 |
} |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
func TestAcknowledge(t *testing.T) { |
| 719 |
ctx := context.Background() |
| 720 |
pclient, sclient, srv, cleanup := newFake(context.TODO(), t) |
| 721 |
defer cleanup() |
| 722 |
|
| 723 |
top := mustCreateTopic(context.TODO(), t, pclient, &pb.Topic{Name: "projects/P/topics/T"}) |
| 724 |
sub := mustCreateSubscription(context.TODO(), t, sclient, &pb.Subscription{ |
| 725 |
Name: "projects/P/subscriptions/S", |
| 726 |
Topic: top.Name, |
| 727 |
AckDeadlineSeconds: 10, |
| 728 |
}) |
| 729 |
|
| 730 |
publish(t, srv, pclient, top, []*pb.PubsubMessage{ |
| 731 |
{Data: []byte("d1")}, |
| 732 |
{Data: []byte("d2")}, |
| 733 |
{Data: []byte("d3")}, |
| 734 |
}) |
| 735 |
msgs := streamingPullN(context.TODO(), t, 3, sclient, sub) |
| 736 |
var ackIDs []string |
| 737 |
for _, m := range msgs { |
| 738 |
ackIDs = append(ackIDs, m.AckId) |
| 739 |
} |
| 740 |
if _, err := sclient.Acknowledge(ctx, &pb.AcknowledgeRequest{ |
| 741 |
Subscription: sub.Name, |
| 742 |
AckIds: ackIDs, |
| 743 |
}); err != nil { |
| 744 |
t.Fatal(err) |
| 745 |
} |
| 746 |
smsgs := srv.Messages() |
| 747 |
if got, want := len(smsgs), 3; got != want { |
| 748 |
t.Fatalf("got %d messages, want %d", got, want) |
| 749 |
} |
| 750 |
for _, sm := range smsgs { |
| 751 |
if sm.Acks != 1 { |
| 752 |
t.Errorf("message %s: got %d acks, want 1", sm.ID, sm.Acks) |
| 753 |
} |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
func TestModAck(t *testing.T) { |
| 758 |
ctx := context.Background() |
| 759 |
pclient, sclient, srv, cleanup := newFake(context.TODO(), t) |
| 760 |
defer cleanup() |
| 761 |
|
| 762 |
top := mustCreateTopic(context.TODO(), t, pclient, &pb.Topic{Name: "projects/P/topics/T"}) |
| 763 |
sub := mustCreateSubscription(context.TODO(), t, sclient, &pb.Subscription{ |
| 764 |
Name: "projects/P/subscriptions/S", |
| 765 |
Topic: top.Name, |
| 766 |
AckDeadlineSeconds: 10, |
| 767 |
}) |
| 768 |
|
| 769 |
publish(t, srv, pclient, top, []*pb.PubsubMessage{ |
| 770 |
{Data: []byte("d1")}, |
| 771 |
{Data: []byte("d2")}, |
| 772 |
{Data: []byte("d3")}, |
| 773 |
}) |
| 774 |
msgs := streamingPullN(context.TODO(), t, 3, sclient, sub) |
| 775 |
var ackIDs []string |
| 776 |
for _, m := range msgs { |
| 777 |
ackIDs = append(ackIDs, m.AckId) |
| 778 |
} |
| 779 |
if _, err := sclient.ModifyAckDeadline(ctx, &pb.ModifyAckDeadlineRequest{ |
| 780 |
Subscription: sub.Name, |
| 781 |
AckIds: ackIDs, |
| 782 |
AckDeadlineSeconds: 0, |
| 783 |
}); err != nil { |
| 784 |
t.Fatal(err) |
| 785 |
} |
| 786 |
// Having nacked all three messages, we should see them again. |
| 787 |
msgs = streamingPullN(context.TODO(), t, 3, sclient, sub) |
| 788 |
if got, want := len(msgs), 3; got != want { |
| 789 |
t.Errorf("got %d messages, want %d", got, want) |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
func TestAckDeadline(t *testing.T) { |
| 794 |
// Messages should be resent after they expire. |
| 795 |
pclient, sclient, srv, cleanup := newFake(context.TODO(), t) |
| 796 |
defer cleanup() |
| 797 |
|
| 798 |
minAckDeadlineSecs = 2 |
| 799 |
top := mustCreateTopic(context.TODO(), t, pclient, &pb.Topic{Name: "projects/P/topics/T"}) |
| 800 |
sub := mustCreateSubscription(context.TODO(), t, sclient, &pb.Subscription{ |
| 801 |
Name: "projects/P/subscriptions/S", |
| 802 |
Topic: top.Name, |
| 803 |
AckDeadlineSeconds: minAckDeadlineSecs, |
| 804 |
}) |
| 805 |
|
| 806 |
_ = publish(t, srv, pclient, top, []*pb.PubsubMessage{ |
| 807 |
{Data: []byte("d1")}, |
| 808 |
{Data: []byte("d2")}, |
| 809 |
{Data: []byte("d3")}, |
| 810 |
}) |
| 811 |
|
| 812 |
got := map[string]int{} |
| 813 |
spc := mustStartStreamingPull(context.TODO(), t, sclient, sub) |
| 814 |
// In 5 seconds the ack deadline will expire twice, so we should see each message |
| 815 |
// exactly three times. |
| 816 |
time.AfterFunc(5*time.Second, func() { |
| 817 |
if err := spc.CloseSend(); err != nil { |
| 818 |
t.Errorf("CloseSend: %v", err) |
| 819 |
} |
| 820 |
}) |
| 821 |
for { |
| 822 |
res, err := spc.Recv() |
| 823 |
if errors.Is(err, io.EOF) { |
| 824 |
break |
| 825 |
} |
| 826 |
if err != nil { |
| 827 |
t.Fatal(err) |
| 828 |
} |
| 829 |
for _, m := range res.ReceivedMessages { |
| 830 |
got[m.Message.MessageId]++ |
| 831 |
} |
| 832 |
} |
| 833 |
for id, n := range got { |
| 834 |
if n != 3 { |
| 835 |
t.Errorf("message %s: saw %d times, want 3", id, n) |
| 836 |
} |
| 837 |
} |
| 838 |
} |
| 839 |
|
| 840 |
func TestMultiSubs(t *testing.T) { |
| 841 |
// Each subscription gets every message. |
| 842 |
pclient, sclient, srv, cleanup := newFake(context.TODO(), t) |
| 843 |
defer cleanup() |
| 844 |
|
| 845 |
top := mustCreateTopic(context.TODO(), t, pclient, &pb.Topic{Name: "projects/P/topics/T"}) |
| 846 |
sub1 := mustCreateSubscription(context.TODO(), t, sclient, &pb.Subscription{ |
| 847 |
Name: "projects/P/subscriptions/S1", |
| 848 |
Topic: top.Name, |
| 849 |
AckDeadlineSeconds: 10, |
| 850 |
}) |
| 851 |
sub2 := mustCreateSubscription(context.TODO(), t, sclient, &pb.Subscription{ |
| 852 |
Name: "projects/P/subscriptions/S2", |
| 853 |
Topic: top.Name, |
| 854 |
AckDeadlineSeconds: 10, |
| 855 |
}) |
| 856 |
|
| 857 |
want := publish(t, srv, pclient, top, []*pb.PubsubMessage{ |
| 858 |
{Data: []byte("d1")}, |
| 859 |
{Data: []byte("d2")}, |
| 860 |
{Data: []byte("d3")}, |
| 861 |
}) |
| 862 |
got1 := pubsubMessages(streamingPullN(context.TODO(), t, len(want), sclient, sub1)) |
| 863 |
got2 := pubsubMessages(streamingPullN(context.TODO(), t, len(want), sclient, sub2)) |
| 864 |
if diff := testutil.Diff(got1, want); diff != "" { |
| 865 |
t.Error(diff) |
| 866 |
} |
| 867 |
if diff := testutil.Diff(got2, want); diff != "" { |
| 868 |
t.Error(diff) |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
// Messages are handed out to all streams of a subscription in a best-effort |
| 873 |
// round-robin behavior. The fake server prefers to fail-fast onto another |
| 874 |
// stream when one stream is already busy, though, so we're unable to test |
| 875 |
// strict round robin behavior. |
| 876 |
func TestMultiStreams(t *testing.T) { |
| 877 |
ctx, cancel := context.WithCancel(context.Background()) |
| 878 |
defer cancel() |
| 879 |
pclient, sclient, srv, cleanup := newFake(ctx, t) |
| 880 |
defer cleanup() |
| 881 |
|
| 882 |
top := mustCreateTopic(ctx, t, pclient, &pb.Topic{Name: "projects/P/topics/T"}) |
| 883 |
sub := mustCreateSubscription(ctx, t, sclient, &pb.Subscription{ |
| 884 |
Name: "projects/P/subscriptions/S", |
| 885 |
Topic: top.Name, |
| 886 |
AckDeadlineSeconds: 10, |
| 887 |
}) |
| 888 |
st1 := mustStartStreamingPull(ctx, t, sclient, sub) |
| 889 |
defer st1.CloseSend() |
| 890 |
st1Received := make(chan struct{}) |
| 891 |
go func() { |
| 892 |
_, err := st1.Recv() |
| 893 |
if err != nil { |
| 894 |
t.Error(err) |
| 895 |
} |
| 896 |
close(st1Received) |
| 897 |
}() |
| 898 |
|
| 899 |
st2 := mustStartStreamingPull(ctx, t, sclient, sub) |
| 900 |
defer st2.CloseSend() |
| 901 |
st2Received := make(chan struct{}) |
| 902 |
go func() { |
| 903 |
_, err := st2.Recv() |
| 904 |
if err != nil { |
| 905 |
t.Error(err) |
| 906 |
} |
| 907 |
close(st2Received) |
| 908 |
}() |
| 909 |
|
| 910 |
publish(t, srv, pclient, top, []*pb.PubsubMessage{ |
| 911 |
{Data: []byte("d1")}, |
| 912 |
{Data: []byte("d2")}, |
| 913 |
}) |
| 914 |
|
| 915 |
timeout := time.After(5 * time.Second) |
| 916 |
select { |
| 917 |
case <-timeout: |
| 918 |
t.Fatal("timed out waiting for stream 1 to receive any message") |
| 919 |
case <-st1Received: |
| 920 |
} |
| 921 |
select { |
| 922 |
case <-timeout: |
| 923 |
t.Fatal("timed out waiting for stream 1 to receive any message") |
| 924 |
case <-st2Received: |
| 925 |
} |
| 926 |
} |
| 927 |
|
| 928 |
func TestStreamingPullTimeout(t *testing.T) { |
| 929 |
pclient, sclient, srv, cleanup := newFake(context.TODO(), t) |
| 930 |
defer cleanup() |
| 931 |
|
| 932 |
timeout := 200 * time.Millisecond |
| 933 |
srv.SetStreamTimeout(timeout) |
| 934 |
top := mustCreateTopic(context.TODO(), t, pclient, &pb.Topic{Name: "projects/P/topics/T"}) |
| 935 |
sub := mustCreateSubscription(context.TODO(), t, sclient, &pb.Subscription{ |
| 936 |
Name: "projects/P/subscriptions/S", |
| 937 |
Topic: top.Name, |
| 938 |
AckDeadlineSeconds: 10, |
| 939 |
}) |
| 940 |
stream := mustStartStreamingPull(context.TODO(), t, sclient, sub) |
| 941 |
time.Sleep(2 * timeout) |
| 942 |
_, err := stream.Recv() |
| 943 |
if err != io.EOF { |
| 944 |
t.Errorf("got %v, want io.EOF", err) |
| 945 |
} |
| 946 |
} |
| 947 |
|
| 948 |
func TestSeek(t *testing.T) { |
| 949 |
pclient, sclient, _, cleanup := newFake(context.TODO(), t) |
| 950 |
defer cleanup() |
| 951 |
|
| 952 |
top := mustCreateTopic(context.TODO(), t, pclient, &pb.Topic{Name: "projects/P/topics/T"}) |
| 953 |
sub := mustCreateSubscription(context.TODO(), t, sclient, &pb.Subscription{ |
| 954 |
Name: "projects/P/subscriptions/S", |
| 955 |
Topic: top.Name, |
| 956 |
AckDeadlineSeconds: 10, |
| 957 |
}) |
| 958 |
ts := timestamppb.Now() |
| 959 |
_, err := sclient.Seek(context.Background(), &pb.SeekRequest{ |
| 960 |
Subscription: sub.Name, |
| 961 |
Target: &pb.SeekRequest_Time{Time: ts}, |
| 962 |
}) |
| 963 |
if err != nil { |
| 964 |
t.Errorf("Seeking: %v", err) |
| 965 |
} |
| 966 |
} |
| 967 |
|
| 968 |
func TestTryDeliverMessage(t *testing.T) { |
| 969 |
for _, test := range []struct { |
| 970 |
availStreamIdx int |
| 971 |
expectedOutIdx int |
| 972 |
}{ |
| 973 |
{availStreamIdx: 0, expectedOutIdx: 0}, |
| 974 |
// Stream 1 will always be marked for deletion. |
| 975 |
{availStreamIdx: 2, expectedOutIdx: 1}, // s0, s1 (deleted), s2, s3 becomes s0, s2, s3. So we expect outIdx=1. |
| 976 |
{availStreamIdx: 3, expectedOutIdx: 2}, // s0, s1 (deleted), s2, s3 becomes s0, s2, s3. So we expect outIdx=2. |
| 977 |
} { |
| 978 |
top := newTopic(&pb.Topic{Name: "some-topic"}) |
| 979 |
sub := newSubscription(top, &sync.Mutex{}, time.Now, nil, &pb.Subscription{Name: "some-sub", Topic: "some-topic"}) |
| 980 |
|
| 981 |
done := make(chan struct{}, 1) |
| 982 |
done <- struct{}{} |
| 983 |
sub.streams = []*stream{{}, {done: done}, {}, {}} |
| 984 |
|
| 985 |
msgc := make(chan *pb.ReceivedMessage, 1) |
| 986 |
sub.streams[test.availStreamIdx].msgc = msgc |
| 987 |
|
| 988 |
var d int |
| 989 |
idx, ok := sub.tryDeliverMessage(&message{deliveries: &d}, 0, time.Now()) |
| 990 |
if !ok { |
| 991 |
t.Fatalf("[avail=%d]: expected msg to be put on stream %d's channel, but it was not", test.availStreamIdx, test.expectedOutIdx) |
| 992 |
} |
| 993 |
if idx != test.expectedOutIdx { |
| 994 |
t.Fatalf("[avail=%d]: expected msg to be put on stream %d, but it was put on %d", test.availStreamIdx, test.expectedOutIdx, idx) |
| 995 |
} |
| 996 |
select { |
| 997 |
case <-msgc: |
| 998 |
default: |
| 999 |
t.Fatalf("[avail=%d]: expected msg to be put on stream %d's channel, but it was not", test.availStreamIdx, idx) |
| 1000 |
} |
| 1001 |
} |
| 1002 |
} |
| 1003 |
|
| 1004 |
func TestTimeNowFunc(t *testing.T) { |
| 1005 |
s := NewServer() |
| 1006 |
defer s.Close() |
| 1007 |
|
| 1008 |
timeFunc := func() time.Time { |
| 1009 |
t, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z") |
| 1010 |
return t |
| 1011 |
} |
| 1012 |
s.SetTimeNowFunc(timeFunc) |
| 1013 |
|
| 1014 |
id := s.Publish("projects/p/topics/t", []byte("hello"), nil) |
| 1015 |
s.Wait() |
| 1016 |
|
| 1017 |
m := s.Message(id) |
| 1018 |
if m == nil { |
| 1019 |
t.Fatalf("got nil, want a message") |
| 1020 |
} |
| 1021 |
if got, want := m.PublishTime, timeFunc(); got != want { |
| 1022 |
t.Fatalf("got %v, want %v", got, want) |
| 1023 |
} |
| 1024 |
} |
| 1025 |
|
| 1026 |
func TestModAck_Race(t *testing.T) { |
| 1027 |
ctx := context.Background() |
| 1028 |
pclient, sclient, server, cleanup := newFake(ctx, t) |
| 1029 |
defer cleanup() |
| 1030 |
|
| 1031 |
top := mustCreateTopic(ctx, t, pclient, &pb.Topic{Name: "projects/P/topics/T"}) |
| 1032 |
sub := mustCreateSubscription(ctx, t, sclient, &pb.Subscription{ |
| 1033 |
Name: "projects/P/subscriptions/S", |
| 1034 |
Topic: top.Name, |
| 1035 |
AckDeadlineSeconds: 10, |
| 1036 |
}) |
| 1037 |
|
| 1038 |
publish(t, server, pclient, top, []*pb.PubsubMessage{ |
| 1039 |
{Data: []byte("d1")}, |
| 1040 |
{Data: []byte("d2")}, |
| 1041 |
{Data: []byte("d3")}, |
| 1042 |
}) |
| 1043 |
msgs := streamingPullN(ctx, t, 3, sclient, sub) |
| 1044 |
var ackIDs []string |
| 1045 |
for _, m := range msgs { |
| 1046 |
ackIDs = append(ackIDs, m.AckId) |
| 1047 |
} |
| 1048 |
|
| 1049 |
// Try to access m.Modacks while simultaneously calling ModifyAckDeadline |
| 1050 |
// so as to try and create a race condition. |
| 1051 |
// Invoke ModifyAckDeadline from the server rather than the client |
| 1052 |
// to increase replicability of simultaneous data access. |
| 1053 |
go func() { |
| 1054 |
req := &pb.ModifyAckDeadlineRequest{ |
| 1055 |
Subscription: sub.Name, |
| 1056 |
AckIds: ackIDs, |
| 1057 |
AckDeadlineSeconds: 0, |
| 1058 |
} |
| 1059 |
server.GServer.ModifyAckDeadline(ctx, req) |
| 1060 |
}() |
| 1061 |
|
| 1062 |
sm := server.Messages() |
| 1063 |
for _, m := range sm { |
| 1064 |
t.Logf("got modacks: %v\n", m.Modacks) |
| 1065 |
} |
| 1066 |
} |
| 1067 |
|
| 1068 |
func TestUpdateDeadLetterPolicy(t *testing.T) { |
| 1069 |
pclient, sclient, _, cleanup := newFake(context.TODO(), t) |
| 1070 |
defer cleanup() |
| 1071 |
|
| 1072 |
top := mustCreateTopic(context.TODO(), t, pclient, &pb.Topic{Name: "projects/P/topics/T"}) |
| 1073 |
deadTop := mustCreateTopic(context.TODO(), t, pclient, &pb.Topic{Name: "projects/P/topics/TD"}) |
| 1074 |
sub := mustCreateSubscription(context.TODO(), t, sclient, &pb.Subscription{ |
| 1075 |
AckDeadlineSeconds: minAckDeadlineSecs, |
| 1076 |
Name: "projects/P/subscriptions/S", |
| 1077 |
Topic: top.Name, |
| 1078 |
DeadLetterPolicy: &pb.DeadLetterPolicy{ |
| 1079 |
DeadLetterTopic: deadTop.Name, |
| 1080 |
MaxDeliveryAttempts: 5, |
| 1081 |
}, |
| 1082 |
}) |
| 1083 |
|
| 1084 |
update := &pb.Subscription{ |
| 1085 |
AckDeadlineSeconds: sub.AckDeadlineSeconds, |
| 1086 |
Name: sub.Name, |
| 1087 |
Topic: top.Name, |
| 1088 |
DeadLetterPolicy: &pb.DeadLetterPolicy{ |
| 1089 |
DeadLetterTopic: deadTop.Name, |
| 1090 |
// update max delivery attempts |
| 1091 |
MaxDeliveryAttempts: 10, |
| 1092 |
}, |
| 1093 |
} |
| 1094 |
|
| 1095 |
updated := mustUpdateSubscription(context.TODO(), t, sclient, &pb.UpdateSubscriptionRequest{ |
| 1096 |
Subscription: update, |
| 1097 |
UpdateMask: &field_mask.FieldMask{Paths: []string{"dead_letter_policy"}}, |
| 1098 |
}) |
| 1099 |
|
| 1100 |
if got, want := updated.DeadLetterPolicy.MaxDeliveryAttempts, update.DeadLetterPolicy.MaxDeliveryAttempts; got != want { |
| 1101 |
t.Fatalf("got %v, want %v", got, want) |
| 1102 |
} |
| 1103 |
} |
| 1104 |
|
| 1105 |
func TestUpdateRetryPolicy(t *testing.T) { |
| 1106 |
ctx := context.Background() |
| 1107 |
pclient, sclient, _, cleanup := newFake(ctx, t) |
| 1108 |
defer cleanup() |
| 1109 |
|
| 1110 |
top := mustCreateTopic(ctx, t, pclient, &pb.Topic{Name: "projects/P/topics/T"}) |
| 1111 |
sub := mustCreateSubscription(ctx, t, sclient, &pb.Subscription{ |
| 1112 |
AckDeadlineSeconds: minAckDeadlineSecs, |
| 1113 |
Name: "projects/P/subscriptions/S", |
| 1114 |
Topic: top.Name, |
| 1115 |
RetryPolicy: &pb.RetryPolicy{ |
| 1116 |
MinimumBackoff: durationpb.New(10 * time.Second), |
| 1117 |
MaximumBackoff: durationpb.New(60 * time.Second), |
| 1118 |
}, |
| 1119 |
}) |
| 1120 |
|
| 1121 |
update := &pb.Subscription{ |
| 1122 |
AckDeadlineSeconds: sub.AckDeadlineSeconds, |
| 1123 |
Name: sub.Name, |
| 1124 |
Topic: top.Name, |
| 1125 |
RetryPolicy: &pb.RetryPolicy{ |
| 1126 |
MinimumBackoff: durationpb.New(20 * time.Second), |
| 1127 |
MaximumBackoff: durationpb.New(100 * time.Second), |
| 1128 |
}, |
| 1129 |
} |
| 1130 |
|
| 1131 |
updated := mustUpdateSubscription(ctx, t, sclient, &pb.UpdateSubscriptionRequest{ |
| 1132 |
Subscription: update, |
| 1133 |
UpdateMask: &field_mask.FieldMask{Paths: []string{"retry_policy"}}, |
| 1134 |
}) |
| 1135 |
|
| 1136 |
if got, want := updated.RetryPolicy, update.RetryPolicy; testutil.Diff(got, want) != "" { |
| 1137 |
t.Fatalf("got %v, want %v", got, want) |
| 1138 |
} |
| 1139 |
} |
| 1140 |
|
| 1141 |
func TestSubscriptionFilter(t *testing.T) { |
| 1142 |
ctx := context.Background() |
| 1143 |
pclient, sclient, _, cleanup := newFake(ctx, t) |
| 1144 |
defer cleanup() |
| 1145 |
|
| 1146 |
top := mustCreateTopic(ctx, t, pclient, &pb.Topic{Name: "projects/P/topics/T"}) |
| 1147 |
|
| 1148 |
// Creating a subscription with invalid filter should return an error. |
| 1149 |
_, err := sclient.CreateSubscription(ctx, &pb.Subscription{ |
| 1150 |
Name: "projects/p/subscriptions/s", |
| 1151 |
Topic: top.Name, |
| 1152 |
AckDeadlineSeconds: 30, |
| 1153 |
EnableMessageOrdering: true, |
| 1154 |
Filter: "bad filter", |
| 1155 |
}) |
| 1156 |
if err == nil { |
| 1157 |
t.Fatal("expected bad filter error, got nil") |
| 1158 |
} |
| 1159 |
if st := status.Convert(err); st.Code() != codes.InvalidArgument { |
| 1160 |
t.Fatalf("got err status: %v, want: %v", st.Code(), codes.InvalidArgument) |
| 1161 |
} |
| 1162 |
|
| 1163 |
sub := mustCreateSubscription(ctx, t, sclient, &pb.Subscription{ |
| 1164 |
AckDeadlineSeconds: minAckDeadlineSecs, |
| 1165 |
Name: "projects/P/subscriptions/S", |
| 1166 |
Topic: top.Name, |
| 1167 |
Filter: "NOT attributes:foo", |
| 1168 |
}) |
| 1169 |
|
| 1170 |
update := &pb.Subscription{ |
| 1171 |
AckDeadlineSeconds: sub.AckDeadlineSeconds, |
| 1172 |
Name: sub.Name, |
| 1173 |
Topic: top.Name, |
| 1174 |
Filter: "NOT attributes:bar", |
| 1175 |
} |
| 1176 |
|
| 1177 |
updated := mustUpdateSubscription(ctx, t, sclient, &pb.UpdateSubscriptionRequest{ |
| 1178 |
Subscription: update, |
| 1179 |
UpdateMask: &field_mask.FieldMask{Paths: []string{"filter"}}, |
| 1180 |
}) |
| 1181 |
|
| 1182 |
if got, want := updated.Filter, update.Filter; got != want { |
| 1183 |
t.Fatalf("got %v, want %v", got, want) |
| 1184 |
} |
| 1185 |
|
| 1186 |
// Updating a subscription with bad filter should return an error. |
| 1187 |
update.Filter = "bad filter" |
| 1188 |
updated, err = sclient.UpdateSubscription(ctx, &pb.UpdateSubscriptionRequest{ |
| 1189 |
Subscription: update, |
| 1190 |
UpdateMask: &field_mask.FieldMask{Paths: []string{"filter"}}, |
| 1191 |
}) |
| 1192 |
if err == nil { |
| 1193 |
t.Fatal("expected bad filter error, got nil") |
| 1194 |
} |
| 1195 |
if st := status.Convert(err); st.Code() != codes.InvalidArgument { |
| 1196 |
t.Fatalf("got err status: %v, want: %v", st.Code(), codes.InvalidArgument) |
| 1197 |
} |
| 1198 |
} |
| 1199 |
|
| 1200 |
func TestUpdateEnableExactlyOnceDelivery(t *testing.T) { |
| 1201 |
ctx := context.Background() |
| 1202 |
pclient, sclient, _, cleanup := newFake(ctx, t) |
| 1203 |
defer cleanup() |
| 1204 |
|
| 1205 |
top := mustCreateTopic(ctx, t, pclient, &pb.Topic{Name: "projects/P/topics/T"}) |
| 1206 |
sub := mustCreateSubscription(ctx, t, sclient, &pb.Subscription{ |
| 1207 |
AckDeadlineSeconds: minAckDeadlineSecs, |
| 1208 |
Name: "projects/P/subscriptions/S", |
| 1209 |
Topic: top.Name, |
| 1210 |
}) |
| 1211 |
|
| 1212 |
update := &pb.Subscription{ |
| 1213 |
AckDeadlineSeconds: sub.AckDeadlineSeconds, |
| 1214 |
Name: sub.Name, |
| 1215 |
Topic: top.Name, |
| 1216 |
EnableExactlyOnceDelivery: true, |
| 1217 |
} |
| 1218 |
|
| 1219 |
updated := mustUpdateSubscription(ctx, t, sclient, &pb.UpdateSubscriptionRequest{ |
| 1220 |
Subscription: update, |
| 1221 |
UpdateMask: &field_mask.FieldMask{Paths: []string{"enable_exactly_once_delivery"}}, |
| 1222 |
}) |
| 1223 |
|
| 1224 |
if got, want := updated.EnableExactlyOnceDelivery, update.EnableExactlyOnceDelivery; got != want { |
| 1225 |
t.Fatalf("got %v, want %v", got, want) |
| 1226 |
} |
| 1227 |
} |
| 1228 |
|
| 1229 |
// Test Create, Get, List, and Delete methods for schema client. |
| 1230 |
// Updating a schema is not available at this moment. |
| 1231 |
func TestSchemaAdminClient(t *testing.T) { |
| 1232 |
ctx := context.Background() |
| 1233 |
_, _, srv, cleanup := newFake(ctx, t) |
| 1234 |
defer cleanup() |
| 1235 |
|
| 1236 |
conn, err := grpc.DialContext(ctx, srv.Addr, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 1237 |
if err != nil { |
| 1238 |
t.Fatal(err) |
| 1239 |
} |
| 1240 |
project := "projects/some-project" |
| 1241 |
schemaID := "some-schema" |
| 1242 |
sclient := pb.NewSchemaServiceClient(conn) |
| 1243 |
pbs, err := sclient.CreateSchema(ctx, &pb.CreateSchemaRequest{ |
| 1244 |
Parent: project, |
| 1245 |
Schema: &pb.Schema{ |
| 1246 |
Type: pb.Schema_AVRO, |
| 1247 |
Definition: "avro-definition", |
| 1248 |
}, |
| 1249 |
SchemaId: schemaID, |
| 1250 |
}) |
| 1251 |
if err != nil { |
| 1252 |
t.Errorf("cannot create schema: %v", err) |
| 1253 |
} |
| 1254 |
pbs2, err := sclient.GetSchema(ctx, &pb.GetSchemaRequest{ |
| 1255 |
Name: fmt.Sprintf("%s/schemas/%s", project, schemaID), |
| 1256 |
View: pb.SchemaView_FULL, |
| 1257 |
}) |
| 1258 |
if err != nil { |
| 1259 |
t.Errorf("cannot get schema: %v", err) |
| 1260 |
} |
| 1261 |
if diff := testutil.Diff(pbs, pbs2); diff != "" { |
| 1262 |
t.Errorf("returned schema different, -want, +got, %s", diff) |
| 1263 |
} |
| 1264 |
|
| 1265 |
resp, err := sclient.ListSchemas(ctx, &pb.ListSchemasRequest{ |
| 1266 |
Parent: project, |
| 1267 |
View: pb.SchemaView_FULL, |
| 1268 |
}) |
| 1269 |
if err != nil { |
| 1270 |
t.Errorf("cannot list schema: %v", err) |
| 1271 |
} |
| 1272 |
schemas := resp.Schemas |
| 1273 |
if len(schemas) != 1 { |
| 1274 |
for _, schema := range schemas { |
| 1275 |
fmt.Printf("schema: %v\n", schema) |
| 1276 |
} |
| 1277 |
t.Errorf("got wrong number of schemas in list: %d", len(schemas)) |
| 1278 |
} |
| 1279 |
|
| 1280 |
_, err = sclient.DeleteSchema(ctx, &pb.DeleteSchemaRequest{ |
| 1281 |
Name: fmt.Sprintf("%s/schemas/%s", project, schemaID), |
| 1282 |
}) |
| 1283 |
if err != nil { |
| 1284 |
t.Errorf("cannot delete schema: %v", err) |
| 1285 |
} |
| 1286 |
if got, want := len(srv.GServer.schemas), 0; got != want { |
| 1287 |
t.Fatalf("got %d topics, want %d", got, want) |
| 1288 |
} |
| 1289 |
|
| 1290 |
} |
| 1291 |
|
| 1292 |
func mustStartStreamingPull(ctx context.Context, t *testing.T, sc pb.SubscriberClient, sub *pb.Subscription) pb.Subscriber_StreamingPullClient { |
| 1293 |
spc, err := sc.StreamingPull(ctx) |
| 1294 |
if err != nil { |
| 1295 |
t.Fatal(err) |
| 1296 |
} |
| 1297 |
if err := spc.Send(&pb.StreamingPullRequest{ |
| 1298 |
Subscription: sub.Name, |
| 1299 |
StreamAckDeadlineSeconds: sub.GetAckDeadlineSeconds(), |
| 1300 |
}); err != nil { |
| 1301 |
t.Fatal(err) |
| 1302 |
} |
| 1303 |
return spc |
| 1304 |
} |
| 1305 |
|
| 1306 |
func pullN(ctx context.Context, t *testing.T, n int, sc pb.SubscriberClient, sub *pb.Subscription) map[string]*pb.ReceivedMessage { |
| 1307 |
got := map[string]*pb.ReceivedMessage{} |
| 1308 |
for i := 0; len(got) < n; i++ { |
| 1309 |
res, err := sc.Pull(ctx, &pb.PullRequest{Subscription: sub.Name, MaxMessages: int32(n - len(got))}) |
| 1310 |
if err != nil { |
| 1311 |
t.Fatal(err) |
| 1312 |
} |
| 1313 |
for _, m := range res.ReceivedMessages { |
| 1314 |
got[m.Message.MessageId] = m |
| 1315 |
} |
| 1316 |
} |
| 1317 |
return got |
| 1318 |
} |
| 1319 |
|
| 1320 |
func streamingPullN(ctx context.Context, t *testing.T, n int, sc pb.SubscriberClient, sub *pb.Subscription) map[string]*pb.ReceivedMessage { |
| 1321 |
spc := mustStartStreamingPull(ctx, t, sc, sub) |
| 1322 |
got := map[string]*pb.ReceivedMessage{} |
| 1323 |
for i := 0; i < n; i++ { |
| 1324 |
res, err := spc.Recv() |
| 1325 |
if err != nil { |
| 1326 |
t.Fatal(err) |
| 1327 |
} |
| 1328 |
for _, m := range res.ReceivedMessages { |
| 1329 |
got[m.Message.MessageId] = m |
| 1330 |
} |
| 1331 |
} |
| 1332 |
if err := spc.CloseSend(); err != nil { |
| 1333 |
t.Fatal(err) |
| 1334 |
} |
| 1335 |
res, err := spc.Recv() |
| 1336 |
if err != io.EOF { |
| 1337 |
t.Fatalf("Recv returned <%v> instead of EOF; res = %v", err, res) |
| 1338 |
} |
| 1339 |
return got |
| 1340 |
} |
| 1341 |
|
| 1342 |
func pubsubMessages(rms map[string]*pb.ReceivedMessage) map[string]*pb.PubsubMessage { |
| 1343 |
ms := map[string]*pb.PubsubMessage{} |
| 1344 |
for k, rm := range rms { |
| 1345 |
ms[k] = rm.Message |
| 1346 |
} |
| 1347 |
return ms |
| 1348 |
} |
| 1349 |
|
| 1350 |
func mustCreateTopic(ctx context.Context, t *testing.T, pc pb.PublisherClient, topic *pb.Topic) *pb.Topic { |
| 1351 |
top, err := pc.CreateTopic(ctx, topic) |
| 1352 |
if err != nil { |
| 1353 |
t.Fatal(err) |
| 1354 |
} |
| 1355 |
return top |
| 1356 |
} |
| 1357 |
|
| 1358 |
func mustUpdateTopic(ctx context.Context, t *testing.T, pc pb.PublisherClient, req *pb.UpdateTopicRequest) *pb.Topic { |
| 1359 |
top, err := pc.UpdateTopic(ctx, req) |
| 1360 |
if err != nil { |
| 1361 |
t.Fatal(err) |
| 1362 |
} |
| 1363 |
return top |
| 1364 |
} |
| 1365 |
|
| 1366 |
func mustCreateSubscription(ctx context.Context, t *testing.T, sc pb.SubscriberClient, sub *pb.Subscription) *pb.Subscription { |
| 1367 |
sub, err := sc.CreateSubscription(ctx, sub) |
| 1368 |
if err != nil { |
| 1369 |
t.Fatal(err) |
| 1370 |
} |
| 1371 |
return sub |
| 1372 |
} |
| 1373 |
|
| 1374 |
func mustUpdateSubscription(ctx context.Context, t *testing.T, sc pb.SubscriberClient, req *pb.UpdateSubscriptionRequest) *pb.Subscription { |
| 1375 |
sub, err := sc.UpdateSubscription(ctx, req) |
| 1376 |
if err != nil { |
| 1377 |
t.Fatal(err) |
| 1378 |
} |
| 1379 |
return sub |
| 1380 |
} |
| 1381 |
|
| 1382 |
// newFake creates a new fake server along with a publisher and subscriber |
| 1383 |
// client. Its final return is a cleanup function. |
| 1384 |
// |
| 1385 |
// Note: be sure to call cleanup! |
| 1386 |
func newFake(ctx context.Context, t *testing.T, opts ...ServerReactorOption) (pb.PublisherClient, pb.SubscriberClient, *Server, func()) { |
| 1387 |
srv := NewServer(opts...) |
| 1388 |
conn, err := grpc.DialContext(ctx, srv.Addr, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 1389 |
if err != nil { |
| 1390 |
t.Fatal(err) |
| 1391 |
} |
| 1392 |
return pb.NewPublisherClient(conn), pb.NewSubscriberClient(conn), srv, func() { |
| 1393 |
srv.Close() |
| 1394 |
conn.Close() |
| 1395 |
} |
| 1396 |
} |
| 1397 |
|
| 1398 |
func TestErrorInjection(t *testing.T) { |
| 1399 |
testcases := []struct { |
| 1400 |
funcName string |
| 1401 |
param interface{} |
| 1402 |
code codes.Code |
| 1403 |
}{ |
| 1404 |
{ |
| 1405 |
funcName: "CreateTopic", |
| 1406 |
code: codes.Internal, |
| 1407 |
}, |
| 1408 |
{ |
| 1409 |
funcName: "GetTopic", |
| 1410 |
code: codes.Aborted, |
| 1411 |
}, |
| 1412 |
{ |
| 1413 |
funcName: "UpdateTopic", |
| 1414 |
code: codes.DeadlineExceeded, |
| 1415 |
}, |
| 1416 |
{ |
| 1417 |
funcName: "ListTopics", |
| 1418 |
}, |
| 1419 |
{ |
| 1420 |
funcName: "ListTopicSubscriptions", |
| 1421 |
}, |
| 1422 |
{ |
| 1423 |
funcName: "DeleteTopic", |
| 1424 |
}, |
| 1425 |
{ |
| 1426 |
funcName: "CreateSubscription", |
| 1427 |
}, |
| 1428 |
{ |
| 1429 |
funcName: "GetSubscription", |
| 1430 |
}, |
| 1431 |
{ |
| 1432 |
funcName: "UpdateSubscription", |
| 1433 |
param: &pb.UpdateSubscriptionRequest{Subscription: &pb.Subscription{}}, |
| 1434 |
}, |
| 1435 |
{ |
| 1436 |
funcName: "ListSubscriptions", |
| 1437 |
}, |
| 1438 |
{ |
| 1439 |
funcName: "DeleteSubscription", |
| 1440 |
}, |
| 1441 |
{ |
| 1442 |
funcName: "DetachSubscription", |
| 1443 |
}, |
| 1444 |
{ |
| 1445 |
funcName: "Publish", |
| 1446 |
}, |
| 1447 |
{ |
| 1448 |
funcName: "Acknowledge", |
| 1449 |
}, |
| 1450 |
{ |
| 1451 |
funcName: "ModifyAckDeadline", |
| 1452 |
}, |
| 1453 |
{ |
| 1454 |
funcName: "Pull", |
| 1455 |
}, |
| 1456 |
{ |
| 1457 |
funcName: "Seek", |
| 1458 |
param: &pb.SeekRequest{Target: &pb.SeekRequest_Time{Time: timestamppb.Now()}}, |
| 1459 |
}, |
| 1460 |
} |
| 1461 |
|
| 1462 |
for _, tc := range testcases { |
| 1463 |
ctx := context.TODO() |
| 1464 |
errMsg := "error-injection-" + tc.funcName |
| 1465 |
// set error code to unknown unless specified |
| 1466 |
ec := codes.Unknown |
| 1467 |
if tc.code != codes.OK { |
| 1468 |
ec = tc.code |
| 1469 |
} |
| 1470 |
opts := []ServerReactorOption{ |
| 1471 |
WithErrorInjection(tc.funcName, ec, errMsg), |
| 1472 |
} |
| 1473 |
_, _, server, cleanup := newFake(ctx, t, opts...) |
| 1474 |
defer cleanup() |
| 1475 |
|
| 1476 |
// We used reflection here to blindly look up the function by name and pass |
| 1477 |
// context and a typed nil, as all the functions under test will have such |
| 1478 |
// a function signature. |
| 1479 |
f := reflect.ValueOf(&server.GServer).MethodByName(tc.funcName) |
| 1480 |
if !f.IsValid() { |
| 1481 |
t.Fatalf("Method %v Not Found", tc.funcName) |
| 1482 |
} |
| 1483 |
// If param is provided, use the param, otherwise create a typed nil that matches the parameter type. |
| 1484 |
var req reflect.Value |
| 1485 |
if tc.param != nil { |
| 1486 |
req = reflect.ValueOf(tc.param) |
| 1487 |
} else { |
| 1488 |
req = reflect.New(f.Type().In(1).Elem()) |
| 1489 |
} |
| 1490 |
ret := reflect.ValueOf(&server.GServer).MethodByName(tc.funcName).Call([]reflect.Value{reflect.ValueOf(ctx), req}) |
| 1491 |
|
| 1492 |
got := ret[1].Interface().(error) |
| 1493 |
if got == nil || status.Code(got) != ec || !strings.Contains(got.Error(), errMsg) { |
| 1494 |
t.Errorf("Got error does not contain the right key %v", got) |
| 1495 |
} |
| 1496 |
} |
| 1497 |
} |
| 1498 |
|
| 1499 |
func TestPublishResponse(t *testing.T) { |
| 1500 |
ctx := context.Background() |
| 1501 |
_, _, srv, cleanup := newFake(ctx, t) |
| 1502 |
defer cleanup() |
| 1503 |
|
| 1504 |
// By default, autoPublishResponse is true so this should succeed immediately. |
| 1505 |
got := srv.Publish("projects/p/topics/t", []byte("msg1"), nil) |
| 1506 |
if want := "m0"; got != want { |
| 1507 |
t.Fatalf("srv.Publish(): got %v, want %v", got, want) |
| 1508 |
} |
| 1509 |
|
| 1510 |
// After disabling autoPublishResponse, publish() operations |
| 1511 |
// will read from the channel instead of auto generating messages. |
| 1512 |
srv.SetAutoPublishResponse(false) |
| 1513 |
|
| 1514 |
srv.AddPublishResponse(&pb.PublishResponse{ |
| 1515 |
MessageIds: []string{"1"}, |
| 1516 |
}, nil) |
| 1517 |
got = srv.Publish("projects/p/topics/t", []byte("msg2"), nil) |
| 1518 |
if want := "1"; got != want { |
| 1519 |
t.Fatalf("srv.Publish(): got %v, want %v", got, want) |
| 1520 |
} |
| 1521 |
|
| 1522 |
srv.AddPublishResponse(&pb.PublishResponse{ |
| 1523 |
MessageIds: []string{"2"}, |
| 1524 |
}, nil) |
| 1525 |
got = srv.Publish("projects/p/topics/t", []byte("msg3"), nil) |
| 1526 |
if want := "2"; got != want { |
| 1527 |
t.Fatalf("srv.Publish(): got %v, want %v", got, want) |
| 1528 |
} |
| 1529 |
|
| 1530 |
go func() { |
| 1531 |
got = srv.Publish("projects/p/topics/t", []byte("msg4"), nil) |
| 1532 |
if want := "3"; got != want { |
| 1533 |
fmt.Printf("srv.Publish(): got %v, want %v", got, want) |
| 1534 |
} |
| 1535 |
}() |
| 1536 |
time.Sleep(5 * time.Second) |
| 1537 |
srv.AddPublishResponse(&pb.PublishResponse{ |
| 1538 |
MessageIds: []string{"3"}, |
| 1539 |
}, nil) |
| 1540 |
} |
| 1541 |
|
| 1542 |
func TestTopicRetentionAdmin(t *testing.T) { |
| 1543 |
ctx := context.Background() |
| 1544 |
pclient, sclient, _, cleanup := newFake(ctx, t) |
| 1545 |
defer cleanup() |
| 1546 |
|
| 1547 |
initialDur := durationpb.New(10 * time.Hour) |
| 1548 |
top := mustCreateTopic(ctx, t, pclient, &pb.Topic{ |
| 1549 |
Name: "projects/P/topics/T", |
| 1550 |
MessageRetentionDuration: initialDur, |
| 1551 |
}) |
| 1552 |
got := top.MessageRetentionDuration |
| 1553 |
want := initialDur |
| 1554 |
if diff := testutil.Diff(got, want); diff != "" { |
| 1555 |
t.Errorf("top.MessageRetentionDuration mismatch: %s", diff) |
| 1556 |
} |
| 1557 |
|
| 1558 |
updateTopic := &pb.Topic{ |
| 1559 |
Name: "projects/P/topics/T", |
| 1560 |
MessageRetentionDuration: durationpb.New(5 * time.Hour), |
| 1561 |
} |
| 1562 |
top2 := mustUpdateTopic(ctx, t, pclient, &pb.UpdateTopicRequest{ |
| 1563 |
Topic: updateTopic, |
| 1564 |
UpdateMask: &field_mask.FieldMask{Paths: []string{"message_retention_duration"}}, |
| 1565 |
}) |
| 1566 |
got = top2.MessageRetentionDuration |
| 1567 |
want = updateTopic.MessageRetentionDuration |
| 1568 |
if diff := testutil.Diff(got, want); diff != "" { |
| 1569 |
t.Errorf("top2.MessageRetentionDuration mismatch: %s", diff) |
| 1570 |
} |
| 1571 |
|
| 1572 |
sub := mustCreateSubscription(ctx, t, sclient, &pb.Subscription{ |
| 1573 |
AckDeadlineSeconds: minAckDeadlineSecs, |
| 1574 |
Name: "projects/P/subscriptions/S", |
| 1575 |
Topic: top2.Name, |
| 1576 |
}) |
| 1577 |
|
| 1578 |
got = sub.TopicMessageRetentionDuration |
| 1579 |
want = top2.MessageRetentionDuration |
| 1580 |
if diff := testutil.Diff(got, want); diff != "" { |
| 1581 |
t.Errorf("sub.TopicMessageRetentionDuration mismatch: %s", diff) |
| 1582 |
} |
| 1583 |
} |
| 1584 |
|
| 1585 |
func TestStreaming_SubscriptionProperties(t *testing.T) { |
| 1586 |
ctx := context.Background() |
| 1587 |
pc, sc, s, cleanup := newFake(ctx, t) |
| 1588 |
defer cleanup() |
| 1589 |
|
| 1590 |
top := mustCreateTopic(ctx, t, pc, &pb.Topic{ |
| 1591 |
Name: "projects/P/topics/T", |
| 1592 |
}) |
| 1593 |
|
| 1594 |
sub := mustCreateSubscription(ctx, t, sc, &pb.Subscription{ |
| 1595 |
AckDeadlineSeconds: 10, |
| 1596 |
Name: "projects/P/subscriptions/S", |
| 1597 |
Topic: top.Name, |
| 1598 |
EnableMessageOrdering: true, |
| 1599 |
EnableExactlyOnceDelivery: true, |
| 1600 |
}) |
| 1601 |
|
| 1602 |
spc := mustStartStreamingPull(ctx, t, sc, sub) |
| 1603 |
|
| 1604 |
s.Publish("projects/P/topics/T", []byte("hello"), nil) |
| 1605 |
|
| 1606 |
res, err := spc.Recv() |
| 1607 |
if err != nil { |
| 1608 |
t.Fatalf("spc.Recv() got err: %v", err) |
| 1609 |
} |
| 1610 |
sp := res.GetSubscriptionProperties() |
| 1611 |
if !sp.GetExactlyOnceDeliveryEnabled() { |
| 1612 |
t.Fatalf("expected exactly once delivery to be enabled in StreamingPullResponse") |
| 1613 |
} |
| 1614 |
if !sp.GetMessageOrderingEnabled() { |
| 1615 |
t.Fatalf("expected message ordering to be enabled in StreamingPullResponse") |
| 1616 |
} |
| 1617 |
|
| 1618 |
// Close the stream. |
| 1619 |
if err := spc.CloseSend(); err != nil { |
| 1620 |
t.Fatal(err) |
| 1621 |
} |
| 1622 |
res, err = spc.Recv() |
| 1623 |
if err != io.EOF { |
| 1624 |
t.Fatalf("Recv returned <%v> instead of EOF; res = %v", err, res) |
| 1625 |
} |
| 1626 |
} |
| 1627 |
|
| 1628 |
// Test switching between the various subscription types: push to endpoint, bigquery, cloud storage, and pull. |
| 1629 |
func TestSubscriptionPushPull(t *testing.T) { |
| 1630 |
ctx := context.Background() |
| 1631 |
pclient, sclient, _, cleanup := newFake(ctx, t) |
| 1632 |
defer cleanup() |
| 1633 |
|
| 1634 |
top := mustCreateTopic(ctx, t, pclient, &pb.Topic{ |
| 1635 |
Name: "projects/P/topics/T", |
| 1636 |
}) |
| 1637 |
|
| 1638 |
// Create a push subscription. |
| 1639 |
pc := &pb.PushConfig{ |
| 1640 |
PushEndpoint: "some-endpoint", |
| 1641 |
Wrapper: &pb.PushConfig_PubsubWrapper_{ |
| 1642 |
PubsubWrapper: &pb.PushConfig_PubsubWrapper{}, |
| 1643 |
}, |
| 1644 |
} |
| 1645 |
got := mustCreateSubscription(ctx, t, sclient, &pb.Subscription{ |
| 1646 |
AckDeadlineSeconds: minAckDeadlineSecs, |
| 1647 |
Name: "projects/P/subscriptions/S", |
| 1648 |
Topic: top.Name, |
| 1649 |
PushConfig: pc, |
| 1650 |
}) |
| 1651 |
|
| 1652 |
if diff := testutil.Diff(got.PushConfig, pc); diff != "" { |
| 1653 |
t.Errorf("sub.PushConfig mismatch: %s", diff) |
| 1654 |
} |
| 1655 |
|
| 1656 |
// Update the subscription to write to BigQuery instead. |
| 1657 |
updateSub := got |
| 1658 |
updateSub.PushConfig = &pb.PushConfig{} |
| 1659 |
bqc := &pb.BigQueryConfig{ |
| 1660 |
Table: "some-table", |
| 1661 |
} |
| 1662 |
updateSub.BigqueryConfig = bqc |
| 1663 |
got = mustUpdateSubscription(ctx, t, sclient, &pb.UpdateSubscriptionRequest{ |
| 1664 |
Subscription: updateSub, |
| 1665 |
UpdateMask: &field_mask.FieldMask{Paths: []string{"push_config", "bigquery_config"}}, |
| 1666 |
}) |
| 1667 |
if diff := testutil.Diff(got.PushConfig, new(pb.PushConfig)); diff != "" { |
| 1668 |
t.Errorf("sub.PushConfig should be zero value\n%s", diff) |
| 1669 |
} |
| 1670 |
want := bqc |
| 1671 |
want.State = pb.BigQueryConfig_ACTIVE |
| 1672 |
if diff := testutil.Diff(got.BigqueryConfig, want); diff != "" { |
| 1673 |
t.Errorf("sub.BigQueryConfig mismatch: %s", diff) |
| 1674 |
} |
| 1675 |
|
| 1676 |
// Switch back to a pull subscription. |
| 1677 |
updateSub.BigqueryConfig = nil |
| 1678 |
got = mustUpdateSubscription(ctx, t, sclient, &pb.UpdateSubscriptionRequest{ |
| 1679 |
Subscription: updateSub, |
| 1680 |
UpdateMask: &field_mask.FieldMask{Paths: []string{"bigquery_config"}}, |
| 1681 |
}) |
| 1682 |
if diff := testutil.Diff(got.PushConfig, new(pb.PushConfig)); diff != "" { |
| 1683 |
t.Errorf("sub.PushConfig should be zero value\n%s", diff) |
| 1684 |
} |
| 1685 |
if got.BigqueryConfig != nil { |
| 1686 |
t.Errorf("sub.BigqueryConfig should be nil, got %s", got.BigqueryConfig) |
| 1687 |
} |
| 1688 |
|
| 1689 |
// Update the subscription to write to Cloud Storage. |
| 1690 |
csc := &pb.CloudStorageConfig{ |
| 1691 |
Bucket: "fake-bucket", |
| 1692 |
} |
| 1693 |
updateSub.CloudStorageConfig = csc |
| 1694 |
got = mustUpdateSubscription(ctx, t, sclient, &pb.UpdateSubscriptionRequest{ |
| 1695 |
Subscription: updateSub, |
| 1696 |
UpdateMask: &field_mask.FieldMask{Paths: []string{"cloud_storage_config"}}, |
| 1697 |
}) |
| 1698 |
want2 := csc |
| 1699 |
want2.State = pb.CloudStorageConfig_ACTIVE |
| 1700 |
if diff := testutil.Diff(got.CloudStorageConfig, want2); diff != "" { |
| 1701 |
t.Errorf("sub.CloudStorageConfig mismatch: %s", diff) |
| 1702 |
} |
| 1703 |
} |
| 1704 |
|
| 1705 |
func TestSubscriptionMessageOrdering(t *testing.T) { |
| 1706 |
ctx := context.Background() |
| 1707 |
|
| 1708 |
s := NewServer() |
| 1709 |
defer s.Close() |
| 1710 |
|
| 1711 |
top, err := s.GServer.CreateTopic(ctx, &pb.Topic{Name: "projects/p/topics/t"}) |
| 1712 |
if err != nil { |
| 1713 |
t.Errorf("Failed to init pubsub topic: %v", err) |
| 1714 |
} |
| 1715 |
sub, err := s.GServer.CreateSubscription(ctx, &pb.Subscription{ |
| 1716 |
Name: "projects/p/subscriptions/s", |
| 1717 |
Topic: top.Name, |
| 1718 |
AckDeadlineSeconds: 30, |
| 1719 |
EnableMessageOrdering: true, |
| 1720 |
}) |
| 1721 |
if err != nil { |
| 1722 |
t.Errorf("Failed to init pubsub subscription: %v", err) |
| 1723 |
} |
| 1724 |
|
| 1725 |
const orderingKey = "ordering-key" |
| 1726 |
var ids []string |
| 1727 |
for i := 0; i < 1000; i++ { |
| 1728 |
ids = append(ids, s.PublishOrdered("projects/p/topics/t", []byte("hello"), nil, orderingKey)) |
| 1729 |
} |
| 1730 |
for len(ids) > 0 { |
| 1731 |
pull, err := s.GServer.Pull(ctx, &pb.PullRequest{Subscription: sub.Name}) |
| 1732 |
if err != nil { |
| 1733 |
t.Errorf("Failed to pull from server: %v", err) |
| 1734 |
} |
| 1735 |
for i, msg := range pull.ReceivedMessages { |
| 1736 |
if msg.Message.MessageId != ids[i] { |
| 1737 |
t.Errorf("want %s, got %s", ids[i], msg.AckId) |
| 1738 |
} |
| 1739 |
s.GServer.Acknowledge(ctx, &pb.AcknowledgeRequest{Subscription: sub.Name, AckIds: []string{msg.AckId}}) |
| 1740 |
} |
| 1741 |
ids = ids[len(pull.ReceivedMessages):] |
| 1742 |
} |
| 1743 |
} |
| 1744 |
|
| 1745 |
func TestSubscriptionRetention(t *testing.T) { |
| 1746 |
// Check that subscriptions with undelivered messages past the |
| 1747 |
// retention deadline do not trigger a panic. |
| 1748 |
|
| 1749 |
ctx := context.Background() |
| 1750 |
s := NewServer() |
| 1751 |
defer s.Close() |
| 1752 |
|
| 1753 |
start := time.Now() |
| 1754 |
s.SetTimeNowFunc(func() time.Time { return start }) |
| 1755 |
|
| 1756 |
const topicName = "projects/p/topics/t" |
| 1757 |
top, err := s.GServer.CreateTopic(ctx, &pb.Topic{Name: topicName}) |
| 1758 |
if err != nil { |
| 1759 |
t.Fatal(err) |
| 1760 |
} |
| 1761 |
if _, err := s.GServer.CreateSubscription(ctx, &pb.Subscription{ |
| 1762 |
Name: "projects/p/subscriptions/s", |
| 1763 |
Topic: top.Name, |
| 1764 |
AckDeadlineSeconds: 30, |
| 1765 |
EnableMessageOrdering: true, |
| 1766 |
}); err != nil { |
| 1767 |
t.Fatal(err) |
| 1768 |
} |
| 1769 |
s.Publish(topicName, []byte("payload"), nil) |
| 1770 |
|
| 1771 |
s.SetTimeNowFunc(func() time.Time { return start.Add(retentionDuration + 1) }) |
| 1772 |
time.Sleep(1 * time.Second) |
| 1773 |
} |