1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
|
// Copyright (C) MongoDB, Inc. 2024-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package integration
import (
"context"
"errors"
"os"
"testing"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/event"
"go.mongodb.org/mongo-driver/internal/assert"
"go.mongodb.org/mongo-driver/internal/eventtest"
"go.mongodb.org/mongo-driver/internal/require"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
"go.mongodb.org/mongo-driver/x/mongo/driver"
)
// Test automatic "maxTimeMS" appending and connection closing behavior when
// CSOT is disabled and enabled.
func TestCSOT_maxTimeMS(t *testing.T) {
// Skip CSOT tests when SKIP_CSOT_TESTS=true. In Evergreen, we typically set
// that environment variable on Windows and macOS because the CSOT spec
// tests are unreliable on those hosts.
if os.Getenv("SKIP_CSOT_TESTS") == "true" {
t.Skip("Skipping CSOT test because SKIP_CSOT_TESTS=true")
}
mt := mtest.New(t, mtest.NewOptions().CreateClient(false))
testCases := []struct {
desc string
commandName string
operation func(ctx context.Context, coll *mongo.Collection) error
topologies []mtest.TopologyKind
// sendsMaxTimeMSWithTimeoutMS specifies whether the driver
// automatically adds "maxTimeMS" to the command-under-test when
// "timeoutMS" is set but no context deadline is provided.
sendsMaxTimeMSWithTimeoutMS bool
// sendsMaxTimeMSWithContextDeadline specifies whether the driver
// automatically adds "maxTimeMS" to the command-under-test when
// "timeoutMS" is set and a context deadline is provided.
sendsMaxTimeMSWithContextDeadline bool
// preventsConnClosureWithTimeoutMS specifies whether the driver
// attempts to prevent closing connections when "timeoutMS" is set for
// the command-under-test.
preventsConnClosureWithTimeoutMS bool
}{
{
desc: "FindOne",
commandName: "find",
operation: func(ctx context.Context, coll *mongo.Collection) error {
return coll.FindOne(ctx, bson.D{}).Err()
},
sendsMaxTimeMSWithTimeoutMS: true,
sendsMaxTimeMSWithContextDeadline: true,
preventsConnClosureWithTimeoutMS: true,
},
{
desc: "Find",
commandName: "find",
operation: func(ctx context.Context, coll *mongo.Collection) error {
_, err := coll.Find(ctx, bson.D{})
return err
},
sendsMaxTimeMSWithTimeoutMS: true,
sendsMaxTimeMSWithContextDeadline: false,
preventsConnClosureWithTimeoutMS: true,
},
{
desc: "FindOneAndDelete",
commandName: "findAndModify",
operation: func(ctx context.Context, coll *mongo.Collection) error {
return coll.FindOneAndDelete(ctx, bson.D{}).Err()
},
sendsMaxTimeMSWithTimeoutMS: true,
sendsMaxTimeMSWithContextDeadline: true,
preventsConnClosureWithTimeoutMS: true,
},
{
desc: "FindOneAndUpdate",
commandName: "findAndModify",
operation: func(ctx context.Context, coll *mongo.Collection) error {
return coll.FindOneAndUpdate(ctx, bson.D{}, bson.M{"$set": bson.M{"key": "value"}}).Err()
},
sendsMaxTimeMSWithTimeoutMS: true,
sendsMaxTimeMSWithContextDeadline: true,
preventsConnClosureWithTimeoutMS: true,
},
{
desc: "FindOneAndReplace",
commandName: "findAndModify",
operation: func(ctx context.Context, coll *mongo.Collection) error {
return coll.FindOneAndReplace(ctx, bson.D{}, bson.D{}).Err()
},
sendsMaxTimeMSWithTimeoutMS: true,
sendsMaxTimeMSWithContextDeadline: true,
preventsConnClosureWithTimeoutMS: true,
},
{
desc: "InsertOne",
commandName: "insert",
operation: func(ctx context.Context, coll *mongo.Collection) error {
_, err := coll.InsertOne(ctx, bson.D{})
return err
},
sendsMaxTimeMSWithTimeoutMS: true,
sendsMaxTimeMSWithContextDeadline: true,
preventsConnClosureWithTimeoutMS: true,
},
{
desc: "InsertMany",
commandName: "insert",
operation: func(ctx context.Context, coll *mongo.Collection) error {
_, err := coll.InsertMany(ctx, []interface{}{bson.D{}})
return err
},
sendsMaxTimeMSWithTimeoutMS: true,
sendsMaxTimeMSWithContextDeadline: true,
preventsConnClosureWithTimeoutMS: true,
},
{
desc: "UpdateOne",
commandName: "update",
operation: func(ctx context.Context, coll *mongo.Collection) error {
_, err := coll.UpdateOne(ctx, bson.D{}, bson.M{"$set": bson.M{"key": "value"}})
return err
},
sendsMaxTimeMSWithTimeoutMS: true,
sendsMaxTimeMSWithContextDeadline: true,
preventsConnClosureWithTimeoutMS: true,
},
{
desc: "UpdateMany",
commandName: "update",
operation: func(ctx context.Context, coll *mongo.Collection) error {
_, err := coll.UpdateMany(ctx, bson.D{}, bson.M{"$set": bson.M{"key": "value"}})
return err
},
sendsMaxTimeMSWithTimeoutMS: true,
sendsMaxTimeMSWithContextDeadline: true,
preventsConnClosureWithTimeoutMS: true,
},
{
desc: "ReplaceOne",
commandName: "update",
operation: func(ctx context.Context, coll *mongo.Collection) error {
_, err := coll.ReplaceOne(ctx, bson.D{}, bson.D{})
return err
},
sendsMaxTimeMSWithTimeoutMS: true,
sendsMaxTimeMSWithContextDeadline: true,
preventsConnClosureWithTimeoutMS: true,
},
{
desc: "DeleteOne",
commandName: "delete",
operation: func(ctx context.Context, coll *mongo.Collection) error {
_, err := coll.DeleteOne(ctx, bson.D{})
return err
},
sendsMaxTimeMSWithTimeoutMS: true,
sendsMaxTimeMSWithContextDeadline: true,
preventsConnClosureWithTimeoutMS: true,
},
{
desc: "DeleteMany",
commandName: "delete",
operation: func(ctx context.Context, coll *mongo.Collection) error {
_, err := coll.DeleteMany(ctx, bson.D{})
return err
},
sendsMaxTimeMSWithTimeoutMS: true,
sendsMaxTimeMSWithContextDeadline: true,
preventsConnClosureWithTimeoutMS: true,
},
{
desc: "Distinct",
commandName: "distinct",
operation: func(ctx context.Context, coll *mongo.Collection) error {
_, err := coll.Distinct(ctx, "name", bson.D{})
return err
},
sendsMaxTimeMSWithTimeoutMS: true,
sendsMaxTimeMSWithContextDeadline: true,
preventsConnClosureWithTimeoutMS: true,
},
{
desc: "Aggregate",
commandName: "aggregate",
operation: func(ctx context.Context, coll *mongo.Collection) error {
_, err := coll.Aggregate(ctx, mongo.Pipeline{})
return err
},
sendsMaxTimeMSWithTimeoutMS: true,
sendsMaxTimeMSWithContextDeadline: false,
preventsConnClosureWithTimeoutMS: true,
},
{
desc: "Watch",
commandName: "aggregate",
operation: func(ctx context.Context, coll *mongo.Collection) error {
cs, err := coll.Watch(ctx, mongo.Pipeline{})
if cs != nil {
cs.Close(context.Background())
}
return err
},
sendsMaxTimeMSWithTimeoutMS: true,
sendsMaxTimeMSWithContextDeadline: true,
preventsConnClosureWithTimeoutMS: true,
// Change Streams aren't supported on standalone topologies.
topologies: []mtest.TopologyKind{
mtest.ReplicaSet,
mtest.Sharded,
},
},
{
desc: "Cursor getMore",
commandName: "getMore",
operation: func(ctx context.Context, coll *mongo.Collection) error {
cursor, err := coll.Find(ctx, bson.D{}, options.Find().SetBatchSize(1))
if err != nil {
return err
}
var res []bson.D
return cursor.All(ctx, &res)
},
sendsMaxTimeMSWithTimeoutMS: false,
sendsMaxTimeMSWithContextDeadline: false,
preventsConnClosureWithTimeoutMS: false,
},
}
// insertTwoDocuments inserts two documents in the test collection.
insertTwoDocuments := func(mt *mtest.T) {
mt.Helper()
_, err := mt.Coll.InsertMany(context.Background(), []interface{}{bson.D{}, bson.D{}})
require.NoError(mt, err, "InsertMany error")
}
// getStartedEvent returns the first command started event that matches the
// specified command name.
getStartedEvent := func(mt *mtest.T, command string) *event.CommandStartedEvent {
for {
evt := mt.GetStartedEvent()
if evt == nil {
break
}
_, err := evt.Command.LookupErr(command)
if errors.Is(err, bsoncore.ErrElementNotFound) {
continue
}
return evt
}
mt.Errorf("could not find command started event for command %q", command)
mt.FailNow()
return nil
}
// getMaxTimeMS asserts that "maxTimeMS" is set on the command document for
// the given command name and returns the value.
getMaxTimeMS := func(mt *mtest.T, command string) int64 {
mt.Helper()
evt := getStartedEvent(mt, command)
maxTimeVal := evt.Command.Lookup("maxTimeMS")
require.Greater(mt,
len(maxTimeVal.Value),
0,
"expected maxTimeMS BSON value to be non-empty")
require.Equal(mt,
maxTimeVal.Type,
bson.TypeInt64,
"expected maxTimeMS BSON value to be type Int64")
assert.Greater(mt,
maxTimeVal.Int64(),
int64(0),
"expected maxTimeMS value to be greater than 0")
return maxTimeVal.Int64()
}
// assertMaxTimeMSIsSet asserts that "maxTimeMS" is not set on the given
// command document.
assertMaxTimeMSNotSet := func(mt *mtest.T, command string) {
mt.Helper()
evt := getStartedEvent(mt, command)
_, err := evt.Command.LookupErr("maxTimeMS")
assert.ErrorIs(mt,
err,
bsoncore.ErrElementNotFound,
"expected maxTimeMS BSON value to be missing, but is present")
}
for _, tc := range testCases {
mt.RunOpts(tc.desc, mtest.NewOptions().Topologies(tc.topologies...), func(mt *mtest.T) {
mt.Run("maxTimeMS", func(mt *mtest.T) {
mt.Run("timeoutMS not set", func(mt *mtest.T) {
// Insert some documents so the collection isn't empty.
insertTwoDocuments(mt)
err := tc.operation(context.Background(), mt.Coll)
require.NoError(mt, err)
assertMaxTimeMSNotSet(mt, tc.commandName)
})
csotOpts := mtest.NewOptions().ClientOptions(options.Client().SetTimeout(10 * time.Second))
mt.RunOpts("timeoutMS and context.Background", csotOpts, func(mt *mtest.T) {
// Insert some documents so the collection isn't empty.
insertTwoDocuments(mt)
err := tc.operation(context.Background(), mt.Coll)
require.NoError(mt, err)
if !tc.sendsMaxTimeMSWithTimeoutMS {
assertMaxTimeMSNotSet(mt, tc.commandName)
return
}
maxTimeMS := getMaxTimeMS(mt, tc.commandName)
assert.Greater(mt, maxTimeMS, int64(0), "expected maxTimeMS to be greater than 0")
})
mt.RunOpts("timeoutMS and Context with deadline", csotOpts, func(mt *mtest.T) {
// Insert some documents so the collection isn't empty.
insertTwoDocuments(mt)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err := tc.operation(ctx, mt.Coll)
require.NoError(mt, err)
if !tc.sendsMaxTimeMSWithContextDeadline {
assertMaxTimeMSNotSet(mt, tc.commandName)
return
}
maxTimeMS := getMaxTimeMS(mt, tc.commandName)
assert.Greater(mt, maxTimeMS, int64(0), "expected maxTimeMS to be greater than 0")
})
})
if tc.preventsConnClosureWithTimeoutMS {
opts := mtest.NewOptions().
// Blocking failpoints don't work on pre-4.2 and sharded clusters.
Topologies(mtest.Single, mtest.ReplicaSet).
MinServerVersion("4.2")
mt.RunOpts("prevents connection closure with timeoutMS", opts, func(mt *mtest.T) {
// Insert some documents so the collection isn't empty.
insertTwoDocuments(mt)
mt.SetFailPoint(mtest.FailPoint{
ConfigureFailPoint: "failCommand",
Mode: "alwaysOn",
Data: mtest.FailPointData{
FailCommands: []string{tc.commandName},
BlockConnection: true,
BlockTimeMS: 500,
},
})
tpm := eventtest.NewTestPoolMonitor()
mt.ResetClient(options.Client().
SetPoolMonitor(tpm.PoolMonitor))
// Run 5 operations that time out with CSOT disabled, then
// assert that at least 1 connection was closed during those
// timeouts.
for i := 0; i < 5; i++ {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Millisecond)
err := tc.operation(ctx, mt.Coll)
cancel()
if !mongo.IsTimeout(err) {
t.Errorf("CSOT-disabled operation %d returned a non-timeout error: %v", i, err)
}
}
closedEvents := tpm.Events(func(pe *event.PoolEvent) bool {
return pe.Type == event.ConnectionClosed
})
assert.Greater(mt,
len(closedEvents),
0,
"expected more than 0 connection closed events")
tpm = eventtest.NewTestPoolMonitor()
mt.ResetClient(options.Client().
SetPoolMonitor(tpm.PoolMonitor).
SetTimeout(10 * time.Second))
// Run 5 operations that time out with CSOT enabled, then
// assert that no connections were closed.
for i := 0; i < 5; i++ {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Millisecond)
err := tc.operation(ctx, mt.Coll)
cancel()
if !mongo.IsTimeout(err) {
t.Errorf("CSOT-enabled operation %d returned a non-timeout error: %v", i, err)
}
}
closedEvents = tpm.Events(func(pe *event.PoolEvent) bool {
return pe.Type == event.ConnectionClosed
})
assert.Len(mt, closedEvents, 0, "expected no connection closed event")
})
}
})
}
mt.Run("maxTimeMS is omitted for values greater than 2147483647ms", func(mt *mtest.T) {
// Set a client-level timeoutMS value.
mt.ResetClient(options.Client().SetTimeout(10 * time.Second))
ctx, cancel := context.WithTimeout(context.Background(), (2147483647+1000)*time.Millisecond)
defer cancel()
_, err := mt.Coll.InsertOne(ctx, bson.D{})
require.NoError(t, err)
evt := mt.GetStartedEvent()
_, err = evt.Command.LookupErr("maxTimeMS")
assert.ErrorIs(mt,
err,
bsoncore.ErrElementNotFound,
"expected maxTimeMS BSON value to be missing, but is present")
})
// Deprecated MaxTime option tests.
mt.Run("Find uses MaxTime option when no other timeouts are set", func(mt *mtest.T) {
// Insert some documents so the collection isn't empty.
insertTwoDocuments(mt)
// Set a 5-second MaxTime value.
opts := options.Find().SetMaxTime(5 * time.Second)
cursor, err := mt.Coll.Find(context.Background(), bson.D{}, opts)
require.NoError(mt, err, "Find error")
err = cursor.Close(context.Background())
require.NoError(mt, err, "Cursor.Close error")
// Assert that maxTimeMS is set and that it's equal to the MaxTime
// value.
maxTimeMS := getMaxTimeMS(mt, "find")
assert.Equal(mt,
int64(5_000),
maxTimeMS,
"expected maxTimeMS to be equal to the MaxTime value")
})
mt.Run("Find ignores MaxTime option when timeoutMS is set", func(mt *mtest.T) {
// Insert some documents so the collection isn't empty.
insertTwoDocuments(mt)
// Set a 10-second client-level timeoutMS value .
mt.ResetClient(options.Client().SetTimeout(10 * time.Second))
// Set a 5-second MaxTime value.
opts := options.Find().SetMaxTime(5 * time.Second)
cursor, err := mt.Coll.Find(context.Background(), bson.D{}, opts)
require.NoError(mt, err, "Find error")
err = cursor.Close(context.Background())
require.NoError(mt, err, "Cursor.Close error")
// Assert that maxTimeMS is set and that it's greater than the
// MaxTime value.
maxTimeMS := getMaxTimeMS(mt, "find")
assert.Greater(mt,
maxTimeMS,
int64(5_000),
"expected maxTimeMS to be greater than the MaxTime value")
})
// TODO(GODRIVER-2944): Remove this test once the "timeoutMode" option is
// supported.
mt.Run("Find uses MaxTime option when timeoutMS and Context with deadline are set", func(mt *mtest.T) {
// Insert some documents so the collection isn't empty.
insertTwoDocuments(mt)
// Set a 10-second client-level timeoutMS value .
mt.ResetClient(options.Client().SetTimeout(10 * time.Second))
// Set a 10-second operation-level Context timeout.
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Set a 5-second MaxTime value.
opts := options.Find().SetMaxTime(5 * time.Second)
cursor, err := mt.Coll.Find(ctx, bson.D{}, opts)
require.NoError(mt, err, "Find error")
err = cursor.Close(context.Background())
require.NoError(mt, err, "Cursor.Close error")
// Assert that maxTimeMS is set and that it's equal to the MaxTime
// value.
maxTimeMS := getMaxTimeMS(mt, "find")
assert.Equal(mt,
int64(5_000),
maxTimeMS,
"expected maxTimeMS to be equal to the MaxTime value")
})
mt.Run("Aggregate uses MaxTime option when no other timeouts are set", func(mt *mtest.T) {
// Insert some documents so the collection isn't empty.
insertTwoDocuments(mt)
// Set a 5-second MaxTime value.
opts := options.Aggregate().SetMaxTime(5 * time.Second)
cursor, err := mt.Coll.Aggregate(context.Background(), bson.D{}, opts)
require.NoError(mt, err, "Aggregate error")
err = cursor.Close(context.Background())
require.NoError(mt, err, "Cursor.Close error")
// Assert that maxTimeMS is set and that it's equal to the MaxTime
// value.
maxTimeMS := getMaxTimeMS(mt, "aggregate")
assert.Equal(mt,
int64(5_000),
maxTimeMS,
"expected maxTimeMS to be equal to the MaxTime value")
})
mt.Run("Aggregate ignores MaxTime option when timeoutMS is set", func(mt *mtest.T) {
// Insert some documents so the collection isn't empty.
insertTwoDocuments(mt)
// Set a 10-second client-level timeoutMS value .
mt.ResetClient(options.Client().SetTimeout(10 * time.Second))
// Set a 5-second MaxTime value.
opts := options.Aggregate().SetMaxTime(5 * time.Second)
cursor, err := mt.Coll.Aggregate(context.Background(), bson.D{}, opts)
require.NoError(mt, err, "Aggregate error")
err = cursor.Close(context.Background())
require.NoError(mt, err, "Cursor.Close error")
// Assert that maxTimeMS is set and that it's greater than the
// MaxTime value.
maxTimeMS := getMaxTimeMS(mt, "aggregate")
assert.Greater(mt,
maxTimeMS,
int64(5_000),
"expected maxTimeMS to be greater than the MaxTime value")
})
// TODO(GODRIVER-2944): Remove this test once the "timeoutMode" option is
// supported.
mt.Run("Aggregate uses MaxTime option when timeoutMS and Context with deadline are set", func(mt *mtest.T) {
// Insert some documents so the collection isn't empty.
insertTwoDocuments(mt)
// Set a 10-second client-level timeoutMS value .
mt.ResetClient(options.Client().SetTimeout(10 * time.Second))
// Set a 10-second operation-level Context timeout.
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Set a 5-second MaxTime value.
opts := options.Aggregate().SetMaxTime(5 * time.Second)
cursor, err := mt.Coll.Aggregate(ctx, bson.D{}, opts)
require.NoError(mt, err, "Aggregate error")
err = cursor.Close(context.Background())
require.NoError(mt, err, "Cursor.Close error")
// Assert that maxTimeMS is set and that it's equal to the MaxTime
// value.
maxTimeMS := getMaxTimeMS(mt, "aggregate")
assert.Equal(mt,
int64(5_000),
maxTimeMS,
"expected maxTimeMS to be equal to the MaxTime value")
})
}
func TestCSOT_errors(t *testing.T) {
// Skip CSOT tests when SKIP_CSOT_TESTS=true. In Evergreen, we typically set
// that environment variable on Windows and macOS because the CSOT spec
// tests are unreliable on those hosts.
if os.Getenv("SKIP_CSOT_TESTS") == "true" {
t.Skip("Skipping CSOT test because SKIP_CSOT_TESTS=true")
}
mt := mtest.New(t, mtest.NewOptions().
CreateClient(false).
// Blocking failpoints don't work on pre-4.2 and sharded clusters.
Topologies(mtest.Single, mtest.ReplicaSet).
MinServerVersion("4.2").
// Enable CSOT.
ClientOptions(options.Client().SetTimeout(10*time.Second)))
// Test that, when CSOT is enabled, the error returned when the database
// returns a MaxTimeMSExceeded error (error code 50) wraps
// "context.DeadlineExceeded".
mt.Run("MaxTimeMSExceeded wraps context.DeadlineExceeded", func(mt *mtest.T) {
_, err := mt.Coll.InsertOne(context.Background(), bson.D{})
require.NoError(mt, err, "InsertOne error")
mt.SetFailPoint(mtest.FailPoint{
ConfigureFailPoint: "failCommand",
Mode: mtest.FailPointMode{
Times: 1,
},
Data: mtest.FailPointData{
FailCommands: []string{"find"},
ErrorCode: 50, // MaxTimeMSExceeded
},
})
err = mt.Coll.FindOne(context.Background(), bson.D{}).Err()
assert.True(mt,
errors.Is(err, context.DeadlineExceeded),
"expected error %[1]T(%[1]q) to wrap context.DeadlineExceeded",
err)
assert.True(mt,
mongo.IsTimeout(err),
"expected error %[1]T(%[1]q) to be a timeout error",
err)
})
// Test that, when CSOT is enabled, the error returned when a context
// deadline is exceeded during a network operation wraps
// "context.DeadlineExceeded".
mt.Run("Context timeout wraps context.DeadlineExceeded", func(mt *mtest.T) {
_, err := mt.Coll.InsertOne(context.Background(), bson.D{})
require.NoError(mt, err, "InsertOne error")
mt.SetFailPoint(mtest.FailPoint{
ConfigureFailPoint: "failCommand",
Mode: mtest.FailPointMode{
Times: 1,
},
Data: mtest.FailPointData{
FailCommands: []string{"find"},
BlockConnection: true,
BlockTimeMS: 500,
},
})
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Millisecond)
defer cancel()
err = mt.Coll.FindOne(ctx, bson.D{}).Err()
assert.False(mt,
errors.Is(err, driver.ErrDeadlineWouldBeExceeded),
"expected error %[1]T(%[1]q) to not wrap driver.ErrDeadlineWouldBeExceeded",
err)
assert.True(mt,
errors.Is(err, context.DeadlineExceeded),
"expected error %[1]T(%[1]q) to wrap context.DeadlineExceeded",
err)
assert.True(mt,
mongo.IsTimeout(err),
"expected error %[1]T(%[1]q) to be a timeout error",
err)
})
mt.Run("timeoutMS timeout wraps context.DeadlineExceeded", func(mt *mtest.T) {
_, err := mt.Coll.InsertOne(context.Background(), bson.D{})
require.NoError(mt, err, "InsertOne error")
mt.SetFailPoint(mtest.FailPoint{
ConfigureFailPoint: "failCommand",
Mode: mtest.FailPointMode{
Times: 1,
},
Data: mtest.FailPointData{
FailCommands: []string{"find"},
BlockConnection: true,
BlockTimeMS: 100,
},
})
// Set timeoutMS=10 to run the FindOne, then unset it so the mtest
// cleanup operations pass successfully (e.g. unsetting failpoints).
mt.ResetClient(options.Client().SetTimeout(10 * time.Millisecond))
defer mt.ResetClient(options.Client())
err = mt.Coll.FindOne(context.Background(), bson.D{}).Err()
assert.False(mt,
errors.Is(err, driver.ErrDeadlineWouldBeExceeded),
"expected error %[1]T(%[1]q) to not wrap driver.ErrDeadlineWouldBeExceeded",
err)
assert.True(mt,
errors.Is(err, context.DeadlineExceeded),
"expected error %[1]T(%[1]q) to wrap context.DeadlineExceeded",
err)
assert.True(mt,
mongo.IsTimeout(err),
"expected error %[1]T(%[1]q) to be a timeout error",
err)
})
}
|