| 1 |
// Copyright 2019 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 natspubsub |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"context" |
| 20 |
"fmt" |
| 21 |
"os" |
| 22 |
"testing" |
| 23 |
|
| 24 |
"gocloud.dev/gcerrors" |
| 25 |
"gocloud.dev/pubsub" |
| 26 |
"gocloud.dev/pubsub/driver" |
| 27 |
"gocloud.dev/pubsub/drivertest" |
| 28 |
|
| 29 |
"github.com/google/go-cmp/cmp" |
| 30 |
"github.com/nats-io/nats-server/v2/server" |
| 31 |
gnatsd "github.com/nats-io/nats-server/v2/test" |
| 32 |
"github.com/nats-io/nats.go" |
| 33 |
) |
| 34 |
|
| 35 |
const ( |
| 36 |
testPort = 11222 |
| 37 |
benchPort = 9222 |
| 38 |
) |
| 39 |
|
| 40 |
type harness struct { |
| 41 |
s *server.Server |
| 42 |
nc *nats.Conn |
| 43 |
useV2 bool |
| 44 |
} |
| 45 |
|
| 46 |
func newHarness(ctx context.Context, t *testing.T) (drivertest.Harness, error) { |
| 47 |
opts := gnatsd.DefaultTestOptions |
| 48 |
opts.Port = testPort |
| 49 |
s := gnatsd.RunServer(&opts) |
| 50 |
nc, err := nats.Connect(fmt.Sprintf("nats://127.0.0.1:%d", testPort)) |
| 51 |
if err != nil { |
| 52 |
return nil, err |
| 53 |
} |
| 54 |
return &harness{s: s, nc: nc, useV2: false}, nil |
| 55 |
} |
| 56 |
|
| 57 |
func newHarnessV2(ctx context.Context, t *testing.T) (drivertest.Harness, error) { |
| 58 |
opts := gnatsd.DefaultTestOptions |
| 59 |
opts.Port = testPort |
| 60 |
s := gnatsd.RunServer(&opts) |
| 61 |
nc, err := nats.Connect(fmt.Sprintf("nats://127.0.0.1:%d", testPort)) |
| 62 |
if err != nil { |
| 63 |
return nil, err |
| 64 |
} |
| 65 |
return &harness{s: s, nc: nc, useV2: true}, nil |
| 66 |
} |
| 67 |
|
| 68 |
func (h *harness) CreateTopic(ctx context.Context, testName string) (driver.Topic, func(), error) { |
| 69 |
cleanup := func() {} |
| 70 |
dt, err := openTopic(h.nc, testName, h.useV2) |
| 71 |
if err != nil { |
| 72 |
return nil, nil, err |
| 73 |
} |
| 74 |
return dt, cleanup, nil |
| 75 |
} |
| 76 |
|
| 77 |
func (h *harness) MakeNonexistentTopic(ctx context.Context) (driver.Topic, error) { |
| 78 |
// A nil *topic behaves like a nonexistent topic. |
| 79 |
return (*topic)(nil), nil |
| 80 |
} |
| 81 |
|
| 82 |
func (h *harness) CreateSubscription(ctx context.Context, dt driver.Topic, testName string) (driver.Subscription, func(), error) { |
| 83 |
ds, err := openSubscription(h.nc, testName, nil, h.useV2) |
| 84 |
if err != nil { |
| 85 |
return nil, nil, err |
| 86 |
} |
| 87 |
cleanup := func() { |
| 88 |
var sub *nats.Subscription |
| 89 |
if ds.As(&sub) { |
| 90 |
sub.Unsubscribe() |
| 91 |
} |
| 92 |
} |
| 93 |
return ds, cleanup, nil |
| 94 |
} |
| 95 |
|
| 96 |
func (h *harness) CreateQueueSubscription(ctx context.Context, dt driver.Topic, testName string) (driver.Subscription, func(), error) { |
| 97 |
ds, err := openSubscription(h.nc, testName, &SubscriptionOptions{Queue: testName}, h.useV2) |
| 98 |
if err != nil { |
| 99 |
return nil, nil, err |
| 100 |
} |
| 101 |
cleanup := func() { |
| 102 |
var sub *nats.Subscription |
| 103 |
if ds.As(&sub) { |
| 104 |
sub.Unsubscribe() |
| 105 |
} |
| 106 |
} |
| 107 |
return ds, cleanup, nil |
| 108 |
} |
| 109 |
|
| 110 |
func (h *harness) MakeNonexistentSubscription(ctx context.Context) (driver.Subscription, func(), error) { |
| 111 |
return (*subscription)(nil), func() {}, nil |
| 112 |
} |
| 113 |
|
| 114 |
func (h *harness) Close() { |
| 115 |
h.nc.Close() |
| 116 |
h.s.Shutdown() |
| 117 |
} |
| 118 |
|
| 119 |
func (h *harness) MaxBatchSizes() (int, int) { return 0, 0 } |
| 120 |
|
| 121 |
func (*harness) SupportsMultipleSubscriptions() bool { return true } |
| 122 |
|
| 123 |
type natsAsTest struct { |
| 124 |
useV2 bool |
| 125 |
} |
| 126 |
|
| 127 |
func (natsAsTest) Name() string { |
| 128 |
return "nats test" |
| 129 |
} |
| 130 |
|
| 131 |
func (natsAsTest) TopicCheck(topic *pubsub.Topic) error { |
| 132 |
var c2 nats.Conn |
| 133 |
if topic.As(&c2) { |
| 134 |
return fmt.Errorf("cast succeeded for %T, want failure", &c2) |
| 135 |
} |
| 136 |
var c3 *nats.Conn |
| 137 |
if !topic.As(&c3) { |
| 138 |
return fmt.Errorf("cast failed for %T", &c3) |
| 139 |
} |
| 140 |
return nil |
| 141 |
} |
| 142 |
|
| 143 |
func (natsAsTest) SubscriptionCheck(sub *pubsub.Subscription) error { |
| 144 |
var c2 nats.Subscription |
| 145 |
if sub.As(&c2) { |
| 146 |
return fmt.Errorf("cast succeeded for %T, want failure", &c2) |
| 147 |
} |
| 148 |
var c3 *nats.Subscription |
| 149 |
if !sub.As(&c3) { |
| 150 |
return fmt.Errorf("cast failed for %T", &c3) |
| 151 |
} |
| 152 |
return nil |
| 153 |
} |
| 154 |
|
| 155 |
func (natsAsTest) TopicErrorCheck(t *pubsub.Topic, err error) error { |
| 156 |
var dummy string |
| 157 |
if t.ErrorAs(err, &dummy) { |
| 158 |
return fmt.Errorf("cast succeeded for %T, want failure", &dummy) |
| 159 |
} |
| 160 |
return nil |
| 161 |
} |
| 162 |
|
| 163 |
func (natsAsTest) SubscriptionErrorCheck(s *pubsub.Subscription, err error) error { |
| 164 |
var dummy string |
| 165 |
if s.ErrorAs(err, &dummy) { |
| 166 |
return fmt.Errorf("cast succeeded for %T, want failure", &dummy) |
| 167 |
} |
| 168 |
return nil |
| 169 |
} |
| 170 |
|
| 171 |
func (natsAsTest) MessageCheck(m *pubsub.Message) error { |
| 172 |
var pm nats.Msg |
| 173 |
if m.As(&pm) { |
| 174 |
return fmt.Errorf("cast succeeded for %T, want failure", &pm) |
| 175 |
} |
| 176 |
var ppm *nats.Msg |
| 177 |
if !m.As(&ppm) { |
| 178 |
return fmt.Errorf("cast failed for %T", &ppm) |
| 179 |
} |
| 180 |
return nil |
| 181 |
} |
| 182 |
|
| 183 |
func (n natsAsTest) BeforeSend(as func(any) bool) error { |
| 184 |
if !n.useV2 { |
| 185 |
return nil |
| 186 |
} |
| 187 |
var pm nats.Msg |
| 188 |
if as(&pm) { |
| 189 |
return fmt.Errorf("cast succeeded for %T, want failure", &pm) |
| 190 |
} |
| 191 |
|
| 192 |
var ppm *nats.Msg |
| 193 |
if !as(&ppm) { |
| 194 |
return fmt.Errorf("cast failed for %T", &ppm) |
| 195 |
} |
| 196 |
return nil |
| 197 |
} |
| 198 |
|
| 199 |
func (natsAsTest) AfterSend(as func(any) bool) error { |
| 200 |
return nil |
| 201 |
} |
| 202 |
|
| 203 |
func TestConformance(t *testing.T) { |
| 204 |
asTests := []drivertest.AsTest{natsAsTest{}} |
| 205 |
drivertest.RunConformanceTests(t, newHarness, asTests) |
| 206 |
} |
| 207 |
|
| 208 |
func TestConformanceV2(t *testing.T) { |
| 209 |
asTests := []drivertest.AsTest{natsAsTest{useV2: true}} |
| 210 |
drivertest.RunConformanceTests(t, newHarnessV2, asTests) |
| 211 |
} |
| 212 |
|
| 213 |
// These are natspubsub specific to increase coverage. |
| 214 |
|
| 215 |
// If we only send a body we should be able to get that from a direct NATS subscriber. |
| 216 |
func TestInteropWithDirectNATS(t *testing.T) { |
| 217 |
ctx := context.Background() |
| 218 |
dh, err := newHarness(ctx, t) |
| 219 |
if err != nil { |
| 220 |
t.Fatal(err) |
| 221 |
} |
| 222 |
defer dh.Close() |
| 223 |
conn := dh.(*harness).nc |
| 224 |
|
| 225 |
const topic = "foo" |
| 226 |
body := []byte("hello") |
| 227 |
|
| 228 |
// Send a message using Go CDK and receive it using NATS directly. |
| 229 |
pt, err := OpenTopic(conn, topic, nil) |
| 230 |
if err != nil { |
| 231 |
t.Fatal(err) |
| 232 |
} |
| 233 |
defer pt.Shutdown(ctx) |
| 234 |
nsub, _ := conn.SubscribeSync(topic) |
| 235 |
if err = pt.Send(ctx, &pubsub.Message{Body: body}); err != nil { |
| 236 |
t.Fatal(err) |
| 237 |
} |
| 238 |
m, err := nsub.NextMsgWithContext(ctx) |
| 239 |
if err != nil { |
| 240 |
t.Fatal(err.Error()) |
| 241 |
} |
| 242 |
if !bytes.Equal(m.Data, body) { |
| 243 |
t.Fatalf("Data did not match. %q vs %q\n", m.Data, body) |
| 244 |
} |
| 245 |
|
| 246 |
// Send a message using NATS directly and receive it using Go CDK. |
| 247 |
ps, err := OpenSubscription(conn, topic, nil) |
| 248 |
if err != nil { |
| 249 |
t.Fatal(err) |
| 250 |
} |
| 251 |
defer ps.Shutdown(ctx) |
| 252 |
if err := conn.Publish(topic, body); err != nil { |
| 253 |
t.Fatal(err) |
| 254 |
} |
| 255 |
msg, err := ps.Receive(ctx) |
| 256 |
if err != nil { |
| 257 |
t.Fatal(err) |
| 258 |
} |
| 259 |
defer msg.Ack() |
| 260 |
if !bytes.Equal(msg.Body, body) { |
| 261 |
t.Fatalf("Data did not match. %q vs %q\n", m.Data, body) |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
// These are natspubsub specific to increase coverage. |
| 266 |
|
| 267 |
// If we only send a body we should be able to get that from a direct NATS subscriber. |
| 268 |
func TestInteropWithDirectNATSV2(t *testing.T) { |
| 269 |
ctx := context.Background() |
| 270 |
dh, err := newHarnessV2(ctx, t) |
| 271 |
if err != nil { |
| 272 |
t.Fatal(err) |
| 273 |
} |
| 274 |
defer dh.Close() |
| 275 |
conn := dh.(*harness).nc |
| 276 |
|
| 277 |
const topic = "foo" |
| 278 |
// In version V2 we can use metadata which will be natively used in the nats message. |
| 279 |
md := map[string]string{"a": "1", "b": "2", "c": "3"} |
| 280 |
body := []byte("hello") |
| 281 |
|
| 282 |
// Send a message using Go CDK and receive it using NATS directly. |
| 283 |
pt, err := OpenTopicV2(conn, topic, nil) |
| 284 |
if err != nil { |
| 285 |
t.Fatal(err) |
| 286 |
} |
| 287 |
defer pt.Shutdown(ctx) |
| 288 |
nsub, _ := conn.SubscribeSync(topic) |
| 289 |
if err = pt.Send(ctx, &pubsub.Message{Body: body, Metadata: md}); err != nil { |
| 290 |
t.Fatal(err) |
| 291 |
} |
| 292 |
m, err := nsub.NextMsgWithContext(ctx) |
| 293 |
if err != nil { |
| 294 |
t.Fatal(err.Error()) |
| 295 |
} |
| 296 |
if !bytes.Equal(m.Data, body) { |
| 297 |
t.Fatalf("Data did not match. %q vs %q\n", m.Data, body) |
| 298 |
} |
| 299 |
for k, v := range md { |
| 300 |
if m.Header.Get(k) != v { |
| 301 |
t.Fatalf("Metadata %q did not match. %q vs %q\n", k, m.Header.Get(k), v) |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
// Send a message using NATS directly and receive it using Go CDK. |
| 306 |
ps, err := OpenSubscriptionV2(conn, topic, nil) |
| 307 |
if err != nil { |
| 308 |
t.Fatal(err) |
| 309 |
} |
| 310 |
defer ps.Shutdown(ctx) |
| 311 |
if err := conn.Publish(topic, body); err != nil { |
| 312 |
t.Fatal(err) |
| 313 |
} |
| 314 |
msg, err := ps.Receive(ctx) |
| 315 |
if err != nil { |
| 316 |
t.Fatal(err) |
| 317 |
} |
| 318 |
defer msg.Ack() |
| 319 |
if !bytes.Equal(msg.Body, body) { |
| 320 |
t.Fatalf("Data did not match. %q vs %q\n", m.Data, body) |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
func TestErrorCode(t *testing.T) { |
| 325 |
ctx := context.Background() |
| 326 |
dh, err := newHarness(ctx, t) |
| 327 |
if err != nil { |
| 328 |
t.Fatal(err) |
| 329 |
} |
| 330 |
defer dh.Close() |
| 331 |
h := dh.(*harness) |
| 332 |
|
| 333 |
// Topics |
| 334 |
dt, err := openTopic(h.nc, "bar", false) |
| 335 |
if err != nil { |
| 336 |
t.Fatal(err) |
| 337 |
} |
| 338 |
|
| 339 |
if gce := dt.ErrorCode(nil); gce != gcerrors.OK { |
| 340 |
t.Fatalf("Expected %v, got %v", gcerrors.OK, gce) |
| 341 |
} |
| 342 |
if gce := dt.ErrorCode(context.Canceled); gce != gcerrors.Canceled { |
| 343 |
t.Fatalf("Expected %v, got %v", gcerrors.Canceled, gce) |
| 344 |
} |
| 345 |
if gce := dt.ErrorCode(nats.ErrBadSubject); gce != gcerrors.FailedPrecondition { |
| 346 |
t.Fatalf("Expected %v, got %v", gcerrors.FailedPrecondition, gce) |
| 347 |
} |
| 348 |
if gce := dt.ErrorCode(nats.ErrAuthorization); gce != gcerrors.PermissionDenied { |
| 349 |
t.Fatalf("Expected %v, got %v", gcerrors.PermissionDenied, gce) |
| 350 |
} |
| 351 |
if gce := dt.ErrorCode(nats.ErrMaxPayload); gce != gcerrors.ResourceExhausted { |
| 352 |
t.Fatalf("Expected %v, got %v", gcerrors.ResourceExhausted, gce) |
| 353 |
} |
| 354 |
if gce := dt.ErrorCode(nats.ErrReconnectBufExceeded); gce != gcerrors.ResourceExhausted { |
| 355 |
t.Fatalf("Expected %v, got %v", gcerrors.ResourceExhausted, gce) |
| 356 |
} |
| 357 |
|
| 358 |
// Subscriptions |
| 359 |
ds, err := openSubscription(h.nc, "bar", nil, false) |
| 360 |
if err != nil { |
| 361 |
t.Fatal(err) |
| 362 |
} |
| 363 |
if gce := ds.ErrorCode(nil); gce != gcerrors.OK { |
| 364 |
t.Fatalf("Expected %v, got %v", gcerrors.OK, gce) |
| 365 |
} |
| 366 |
if gce := ds.ErrorCode(context.Canceled); gce != gcerrors.Canceled { |
| 367 |
t.Fatalf("Expected %v, got %v", gcerrors.Canceled, gce) |
| 368 |
} |
| 369 |
if gce := ds.ErrorCode(nats.ErrBadSubject); gce != gcerrors.FailedPrecondition { |
| 370 |
t.Fatalf("Expected %v, got %v", gcerrors.FailedPrecondition, gce) |
| 371 |
} |
| 372 |
if gce := ds.ErrorCode(nats.ErrBadSubscription); gce != gcerrors.NotFound { |
| 373 |
t.Fatalf("Expected %v, got %v", gcerrors.NotFound, gce) |
| 374 |
} |
| 375 |
if gce := ds.ErrorCode(nats.ErrTypeSubscription); gce != gcerrors.FailedPrecondition { |
| 376 |
t.Fatalf("Expected %v, got %v", gcerrors.FailedPrecondition, gce) |
| 377 |
} |
| 378 |
if gce := ds.ErrorCode(nats.ErrAuthorization); gce != gcerrors.PermissionDenied { |
| 379 |
t.Fatalf("Expected %v, got %v", gcerrors.PermissionDenied, gce) |
| 380 |
} |
| 381 |
if gce := ds.ErrorCode(nats.ErrMaxMessages); gce != gcerrors.ResourceExhausted { |
| 382 |
t.Fatalf("Expected %v, got %v", gcerrors.ResourceExhausted, gce) |
| 383 |
} |
| 384 |
if gce := ds.ErrorCode(nats.ErrSlowConsumer); gce != gcerrors.ResourceExhausted { |
| 385 |
t.Fatalf("Expected %v, got %v", gcerrors.ResourceExhausted, gce) |
| 386 |
} |
| 387 |
if gce := ds.ErrorCode(nats.ErrTimeout); gce != gcerrors.DeadlineExceeded { |
| 388 |
t.Fatalf("Expected %v, got %v", gcerrors.DeadlineExceeded, gce) |
| 389 |
} |
| 390 |
|
| 391 |
// Queue Subscription |
| 392 |
qs, err := openSubscription(h.nc, "bar", &SubscriptionOptions{Queue: t.Name()}, false) |
| 393 |
if err != nil { |
| 394 |
t.Fatal(err) |
| 395 |
} |
| 396 |
if gce := qs.ErrorCode(nil); gce != gcerrors.OK { |
| 397 |
t.Fatalf("Expected %v, got %v", gcerrors.OK, gce) |
| 398 |
} |
| 399 |
if gce := qs.ErrorCode(context.Canceled); gce != gcerrors.Canceled { |
| 400 |
t.Fatalf("Expected %v, got %v", gcerrors.Canceled, gce) |
| 401 |
} |
| 402 |
if gce := qs.ErrorCode(nats.ErrBadSubject); gce != gcerrors.FailedPrecondition { |
| 403 |
t.Fatalf("Expected %v, got %v", gcerrors.FailedPrecondition, gce) |
| 404 |
} |
| 405 |
if gce := qs.ErrorCode(nats.ErrBadSubscription); gce != gcerrors.NotFound { |
| 406 |
t.Fatalf("Expected %v, got %v", gcerrors.NotFound, gce) |
| 407 |
} |
| 408 |
if gce := qs.ErrorCode(nats.ErrTypeSubscription); gce != gcerrors.FailedPrecondition { |
| 409 |
t.Fatalf("Expected %v, got %v", gcerrors.FailedPrecondition, gce) |
| 410 |
} |
| 411 |
if gce := qs.ErrorCode(nats.ErrAuthorization); gce != gcerrors.PermissionDenied { |
| 412 |
t.Fatalf("Expected %v, got %v", gcerrors.PermissionDenied, gce) |
| 413 |
} |
| 414 |
if gce := qs.ErrorCode(nats.ErrMaxMessages); gce != gcerrors.ResourceExhausted { |
| 415 |
t.Fatalf("Expected %v, got %v", gcerrors.ResourceExhausted, gce) |
| 416 |
} |
| 417 |
if gce := qs.ErrorCode(nats.ErrSlowConsumer); gce != gcerrors.ResourceExhausted { |
| 418 |
t.Fatalf("Expected %v, got %v", gcerrors.ResourceExhausted, gce) |
| 419 |
} |
| 420 |
if gce := qs.ErrorCode(nats.ErrTimeout); gce != gcerrors.DeadlineExceeded { |
| 421 |
t.Fatalf("Expected %v, got %v", gcerrors.DeadlineExceeded, gce) |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
func BenchmarkNatsQueuePubSub(b *testing.B) { |
| 426 |
ctx := context.Background() |
| 427 |
|
| 428 |
opts := gnatsd.DefaultTestOptions |
| 429 |
opts.Port = benchPort |
| 430 |
s := gnatsd.RunServer(&opts) |
| 431 |
defer s.Shutdown() |
| 432 |
|
| 433 |
nc, err := nats.Connect(fmt.Sprintf("nats://127.0.0.1:%d", benchPort)) |
| 434 |
if err != nil { |
| 435 |
b.Fatal(err) |
| 436 |
} |
| 437 |
defer nc.Close() |
| 438 |
|
| 439 |
for _, tc := range []struct { |
| 440 |
name string |
| 441 |
h *harness |
| 442 |
}{ |
| 443 |
{name: "V1", h: &harness{s: s, nc: nc, useV2: false}}, |
| 444 |
{name: "V2", h: &harness{s: s, nc: nc, useV2: true}}, |
| 445 |
} { |
| 446 |
b.Run(tc.name, func(b *testing.B) { |
| 447 |
dt, cleanup, err := tc.h.CreateTopic(ctx, b.Name()) |
| 448 |
if err != nil { |
| 449 |
b.Fatal(err) |
| 450 |
} |
| 451 |
defer cleanup() |
| 452 |
|
| 453 |
qs, cleanup, err := tc.h.CreateQueueSubscription(ctx, dt, b.Name()) |
| 454 |
if err != nil { |
| 455 |
b.Fatal(err) |
| 456 |
} |
| 457 |
defer cleanup() |
| 458 |
|
| 459 |
topic := pubsub.NewTopic(dt, nil) |
| 460 |
defer topic.Shutdown(ctx) |
| 461 |
queueSub := pubsub.NewSubscription(qs, recvBatcherOpts, nil) |
| 462 |
defer queueSub.Shutdown(ctx) |
| 463 |
|
| 464 |
drivertest.RunBenchmarks(b, topic, queueSub) |
| 465 |
}) |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
func BenchmarkNatsPubSub(b *testing.B) { |
| 470 |
ctx := context.Background() |
| 471 |
|
| 472 |
opts := gnatsd.DefaultTestOptions |
| 473 |
opts.Port = benchPort |
| 474 |
s := gnatsd.RunServer(&opts) |
| 475 |
defer s.Shutdown() |
| 476 |
|
| 477 |
nc, err := nats.Connect(fmt.Sprintf("nats://127.0.0.1:%d", benchPort)) |
| 478 |
if err != nil { |
| 479 |
b.Fatal(err) |
| 480 |
} |
| 481 |
defer nc.Close() |
| 482 |
|
| 483 |
for _, tc := range []struct { |
| 484 |
name string |
| 485 |
h *harness |
| 486 |
}{ |
| 487 |
{name: "V1", h: &harness{s: s, nc: nc, useV2: false}}, |
| 488 |
{name: "V2", h: &harness{s: s, nc: nc, useV2: true}}, |
| 489 |
} { |
| 490 |
b.Run(tc.name, func(b *testing.B) { |
| 491 |
dt, cleanup, err := tc.h.CreateTopic(ctx, b.Name()) |
| 492 |
if err != nil { |
| 493 |
b.Fatal(err) |
| 494 |
} |
| 495 |
defer cleanup() |
| 496 |
ds, cleanup, err := tc.h.CreateSubscription(ctx, dt, b.Name()) |
| 497 |
if err != nil { |
| 498 |
b.Fatal(err) |
| 499 |
} |
| 500 |
defer cleanup() |
| 501 |
|
| 502 |
topic := pubsub.NewTopic(dt, nil) |
| 503 |
defer topic.Shutdown(ctx) |
| 504 |
sub := pubsub.NewSubscription(ds, recvBatcherOpts, nil) |
| 505 |
defer sub.Shutdown(ctx) |
| 506 |
|
| 507 |
drivertest.RunBenchmarks(b, topic, sub) |
| 508 |
}) |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
func fakeConnectionStringInEnv() func() { |
| 513 |
oldEnvVal := os.Getenv("NATS_SERVER_URL") |
| 514 |
os.Setenv("NATS_SERVER_URL", fmt.Sprintf("nats://localhost:%d", testPort)) |
| 515 |
return func() { |
| 516 |
os.Setenv("NATS_SERVER_URL", oldEnvVal) |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
func TestOpenTopicFromURL(t *testing.T) { |
| 521 |
ctx := context.Background() |
| 522 |
dh, err := newHarness(ctx, t) |
| 523 |
if err != nil { |
| 524 |
t.Fatal(err) |
| 525 |
} |
| 526 |
defer dh.Close() |
| 527 |
|
| 528 |
cleanup := fakeConnectionStringInEnv() |
| 529 |
defer cleanup() |
| 530 |
|
| 531 |
tests := []struct { |
| 532 |
URL string |
| 533 |
WantErr bool |
| 534 |
}{ |
| 535 |
// OK. |
| 536 |
{"nats://mytopic", false}, |
| 537 |
// Invalid parameter. |
| 538 |
{"nats://mytopic?param=value", true}, |
| 539 |
} |
| 540 |
|
| 541 |
for _, test := range tests { |
| 542 |
topic, err := pubsub.OpenTopic(ctx, test.URL) |
| 543 |
if (err != nil) != test.WantErr { |
| 544 |
t.Errorf("%s: got error %v, want error %v", test.URL, err, test.WantErr) |
| 545 |
} |
| 546 |
if topic != nil { |
| 547 |
topic.Shutdown(ctx) |
| 548 |
} |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
func TestOpenSubscriptionFromURL(t *testing.T) { |
| 553 |
ctx := context.Background() |
| 554 |
dh, err := newHarness(ctx, t) |
| 555 |
if err != nil { |
| 556 |
t.Fatal(err) |
| 557 |
} |
| 558 |
defer dh.Close() |
| 559 |
|
| 560 |
cleanup := fakeConnectionStringInEnv() |
| 561 |
defer cleanup() |
| 562 |
|
| 563 |
tests := []struct { |
| 564 |
URL string |
| 565 |
WantErr bool |
| 566 |
}{ |
| 567 |
// OK. |
| 568 |
{"nats://mytopic", false}, |
| 569 |
// Invalid parameter. |
| 570 |
{"nats://mytopic?param=value", true}, |
| 571 |
// Queue URL Parameter for QueueSubscription. |
| 572 |
{"nats://mytopic?queue=queue1", false}, |
| 573 |
// Multiple values for Queue URL Parameter for QueueSubscription. |
| 574 |
{"nats://mytopic?queue=queue1&queue=queue2", true}, |
| 575 |
// NATSV2 URL should be acceptable without values. |
| 576 |
{"nats://mytopic?natsv2", false}, |
| 577 |
// NATSV2 URL should be acceptable with boolean parsable values. |
| 578 |
{"nats://mytopic?natsv2=true", false}, |
| 579 |
{"nats://mytopic?natsv2=false", false}, |
| 580 |
// NATSV2 URL should throw error with non-boolean parsable values. |
| 581 |
{"nats://mytopic?natsv2=foo", true}, |
| 582 |
} |
| 583 |
|
| 584 |
for _, test := range tests { |
| 585 |
sub, err := pubsub.OpenSubscription(ctx, test.URL) |
| 586 |
if (err != nil) != test.WantErr { |
| 587 |
t.Errorf("%s: got error %v, want error %v", test.URL, err, test.WantErr) |
| 588 |
} |
| 589 |
if sub != nil { |
| 590 |
sub.Shutdown(ctx) |
| 591 |
} |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
func TestCodec(t *testing.T) { |
| 596 |
const sub = "foo" |
| 597 |
for _, dm := range []*driver.Message{ |
| 598 |
{Metadata: nil, Body: nil}, |
| 599 |
{Metadata: map[string]string{"a": "1"}, Body: nil}, |
| 600 |
{Metadata: nil, Body: []byte("hello")}, |
| 601 |
{Metadata: map[string]string{"a": "1"}, Body: []byte("hello")}, |
| 602 |
{ |
| 603 |
Metadata: map[string]string{"a": "1"}, Body: []byte("hello"), |
| 604 |
AckID: "foo", AsFunc: func(any) bool { return true }, |
| 605 |
}, |
| 606 |
} { |
| 607 |
t.Run("V1", func(t *testing.T) { |
| 608 |
bytes, err := encodeMessage(dm) |
| 609 |
if err != nil { |
| 610 |
t.Fatal(err) |
| 611 |
} |
| 612 |
var got driver.Message |
| 613 |
if err := decodeMessage(bytes, &got); err != nil { |
| 614 |
t.Fatal(err) |
| 615 |
} |
| 616 |
want := *dm |
| 617 |
want.AckID = nil |
| 618 |
want.AsFunc = nil |
| 619 |
if diff := cmp.Diff(got, want); diff != "" { |
| 620 |
t.Errorf("%+v:\n%s", want, diff) |
| 621 |
} |
| 622 |
}) |
| 623 |
t.Run("V2", func(t *testing.T) { |
| 624 |
nm := encodeMessageV2(dm, sub) |
| 625 |
got, err := decodeMessageV2(nm) |
| 626 |
if err != nil { |
| 627 |
t.Fatal(err) |
| 628 |
} |
| 629 |
|
| 630 |
want := *dm |
| 631 |
want.AckID = nil |
| 632 |
want.AsFunc = nil |
| 633 |
// AsFunc needs to be cleared as it cannot be comparable using Diff. |
| 634 |
got.AsFunc = nil |
| 635 |
if diff := cmp.Diff(*got, want); diff != "" { |
| 636 |
t.Errorf("%+v:\n%s", want, diff) |
| 637 |
} |
| 638 |
}) |
| 639 |
|
| 640 |
} |
| 641 |
} |