Skip to content

Commit

Permalink
test: assert clientSession is not nil
Browse files Browse the repository at this point in the history
  • Loading branch information
kumarlokesh committed Nov 5, 2023
1 parent d52c9e1 commit d762b10
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions mongo/integration/unified_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,15 +447,19 @@ func executeGridFSOperation(mt *mtest.T, bucket *gridfs.Bucket, op *operation) e
}

func executeTestRunnerOperation(mt *mtest.T, testCase *testCase, op *operation, sess mongo.Session) error {
if sess == nil {
return fmt.Errorf("received unexpected nil session")
}

var clientSession *session.Client
if sess != nil {
xsess, ok := sess.(mongo.XSession)
if !ok {
return fmt.Errorf("expected session type %T to implement mongo.XSession", sess)
}
clientSession = xsess.ClientSession()
xsess, ok := sess.(mongo.XSession)
if !ok {
return fmt.Errorf("expected session type %T to implement mongo.XSession", sess)
}

clientSession = xsess.ClientSession()
assert.NotNil(mt, clientSession, "expected valid session, got nil")

switch op.Name {
case "targetedFailPoint":
fpDoc := op.Arguments.Lookup("failPoint")
Expand All @@ -480,23 +484,29 @@ func executeTestRunnerOperation(mt *mtest.T, testCase *testCase, op *operation,
mt.TrackFailPoint(fp.ConfigureFailPoint)
case "configureFailPoint":
fp, err := op.Arguments.LookupErr("failPoint")
assert.Nil(mt, err, "failPoint not found in arguments")
if err != nil {
return fmt.Errorf("unable to find 'failPoint' in arguments: %v", err)
}
mt.SetFailPointFromDocument(fp.Document())
case "assertSessionTransactionState":
stateVal, err := op.Arguments.LookupErr("state")
assert.Nil(mt, err, "state not found in arguments")
if err != nil {
return fmt.Errorf("unable to find 'state' in arguments: %v", err)
}
expectedState, ok := stateVal.StringValueOK()
assert.True(mt, ok, "state argument is not a string")
if !ok {
return fmt.Errorf("expected 'state' argument to be string")
}

assert.NotNil(mt, clientSession, "expected valid session, got nil")
actualState := clientSession.TransactionState.String()

// actualState should match expectedState, but "in progress" is the same as
// "in_progress".
stateMatch := actualState == expectedState ||
actualState == "in progress" && expectedState == "in_progress"
assert.True(mt, stateMatch, "expected transaction state %v, got %v",
expectedState, actualState)
if !stateMatch {
return fmt.Errorf("expected transaction state %v, got %v", expectedState, actualState)
}
case "assertSessionPinned":
if clientSession.PinnedServer == nil {
return errors.New("expected pinned server, got nil")
Expand Down

0 comments on commit d762b10

Please sign in to comment.