...

Source file src/go.mongodb.org/mongo-driver/mongo/integration/retryable_writes_spec_test.go

Documentation: go.mongodb.org/mongo-driver/mongo/integration

     1  // Copyright (C) MongoDB, Inc. 2017-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  package integration
     8  
     9  import (
    10  	"io/ioutil"
    11  	"path"
    12  	"testing"
    13  
    14  	"go.mongodb.org/mongo-driver/bson"
    15  	"go.mongodb.org/mongo-driver/internal/assert"
    16  	"go.mongodb.org/mongo-driver/mongo/integration/mtest"
    17  )
    18  
    19  const retryableWritesTestDir = "../../testdata/retryable-writes/legacy"
    20  
    21  type retryableWritesTestFile struct {
    22  	RunOn []mtest.RunOnBlock    `bson:"runOn"`
    23  	Data  []bson.Raw            `bson:"data"`
    24  	Tests []retryableWritesTest `bson:"tests"`
    25  }
    26  
    27  type retryableWritesTest struct {
    28  	Description         string           `bson:"description"`
    29  	ClientOptions       bson.Raw         `bson:"clientOptions"`
    30  	UseMultipleMongoses bool             `bson:"useMultipleMongoses"`
    31  	FailPoint           *mtest.FailPoint `bson:"failPoint"`
    32  	Operation           crudOperation    `bson:"operation"`
    33  	Outcome             crudOutcome      `bson:"outcome"`
    34  }
    35  
    36  func TestRetryableWritesSpec(t *testing.T) {
    37  	for _, file := range jsonFilesInDir(t, retryableWritesTestDir) {
    38  		t.Run(file, func(t *testing.T) {
    39  			runRetryableWritesFile(t, path.Join(retryableWritesTestDir, file))
    40  		})
    41  	}
    42  }
    43  
    44  func runRetryableWritesFile(t *testing.T, filePath string) {
    45  	content, err := ioutil.ReadFile(filePath)
    46  	assert.Nil(t, err, "ReadFile error for %v: %v", filePath, err)
    47  
    48  	var testFile retryableWritesTestFile
    49  	err = bson.UnmarshalExtJSONWithRegistry(specTestRegistry, content, false, &testFile)
    50  	assert.Nil(t, err, "UnmarshalExtJSONWithRegistry error: %v", err)
    51  
    52  	mt := mtest.New(t, mtest.NewOptions().RunOn(testFile.RunOn...).CreateClient(false))
    53  
    54  	for _, test := range testFile.Tests {
    55  		runRetryableWritesTest(mt, test, testFile)
    56  	}
    57  }
    58  
    59  func runRetryableWritesTest(mt *mtest.T, test retryableWritesTest, testFile retryableWritesTestFile) {
    60  	// Use a low heartbeat frequency so the Client will quickly recover when using failpoints that cause SDAM state
    61  	// changes.
    62  	testClientOpts := createClientOptions(mt, test.ClientOptions)
    63  	testClientOpts.SetHeartbeatInterval(defaultHeartbeatInterval)
    64  	opts := mtest.NewOptions().ClientOptions(testClientOpts)
    65  	if mtest.ClusterTopologyKind() == mtest.Sharded && !test.UseMultipleMongoses {
    66  		// pin to a single mongos
    67  		opts = opts.ClientType(mtest.Pinned)
    68  	}
    69  
    70  	mt.RunOpts(test.Description, opts, func(mt *mtest.T) {
    71  		// setup - insert test data and set fail point
    72  		insertDocuments(mt, mt.Coll, testFile.Data)
    73  		if test.FailPoint != nil {
    74  			mt.SetFailPoint(*test.FailPoint)
    75  		}
    76  
    77  		// run operation and verify outcome/error
    78  		runCrudOperation(mt, test.Description, test.Operation, test.Outcome)
    79  	})
    80  }
    81  

View as plain text