...

Source file src/go.mongodb.org/mongo-driver/x/mongo/driver/session/server_session_test.go

Documentation: go.mongodb.org/mongo-driver/x/mongo/driver/session

     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 session
     8  
     9  import (
    10  	"testing"
    11  	"time"
    12  
    13  	"go.mongodb.org/mongo-driver/internal/assert"
    14  	"go.mongodb.org/mongo-driver/mongo/description"
    15  )
    16  
    17  func TestServerSession(t *testing.T) {
    18  	int64ToPtr := func(i64 int64) *int64 { return &i64 }
    19  
    20  	t.Run("Expired", func(t *testing.T) {
    21  		t.Run("non-lb mode", func(t *testing.T) {
    22  			sess, err := newServerSession()
    23  			assert.Nil(t, err, "newServerSession error: %v", err)
    24  
    25  			// The session should be expired if timeoutMinutes is 0 or if its last used time is too old.
    26  			assert.True(t, sess.expired(topologyDescription{}), "expected session to be expired when timeoutMinutes=0")
    27  			sess.LastUsed = time.Now().Add(-30 * time.Minute)
    28  			topoDesc := topologyDescription{timeoutMinutes: int64ToPtr(30)}
    29  			assert.True(t, sess.expired(topoDesc), "expected session to be expired when timeoutMinutes=30")
    30  		})
    31  		t.Run("lb mode", func(t *testing.T) {
    32  			sess, err := newServerSession()
    33  			assert.Nil(t, err, "newServerSession error: %v", err)
    34  
    35  			// The session should never be considered expired.
    36  			topoDesc := topologyDescription{kind: description.LoadBalanced}
    37  			assert.False(t, sess.expired(topoDesc), "session reported that it was expired in LB mode with timeoutMinutes=0")
    38  
    39  			sess.LastUsed = time.Now().Add(-30 * time.Minute)
    40  			topoDesc.timeoutMinutes = int64ToPtr(10)
    41  			assert.False(t, sess.expired(topoDesc), "session reported that it was expired in LB mode with timeoutMinutes=10")
    42  		})
    43  	})
    44  }
    45  

View as plain text