...

Source file src/go.mongodb.org/mongo-driver/x/mongo/driver/topology/fsm_test.go

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

     1  // Copyright (C) MongoDB, Inc. 2023-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 topology
     8  
     9  import (
    10  	"fmt"
    11  	"testing"
    12  
    13  	"go.mongodb.org/mongo-driver/internal/assert"
    14  	"go.mongodb.org/mongo-driver/mongo/description"
    15  )
    16  
    17  func TestFSMSessionTimeout(t *testing.T) {
    18  	t.Parallel()
    19  
    20  	int64ToPtr := func(i64 int64) *int64 { return &i64 }
    21  
    22  	tests := []struct {
    23  		name string
    24  		f    *fsm
    25  		s    description.Server
    26  		want *int64
    27  	}{
    28  		{
    29  			name: "empty",
    30  			f:    &fsm{},
    31  			s:    description.Server{},
    32  			want: nil,
    33  		},
    34  		{
    35  			name: "no session support on data-bearing server with session support on fsm",
    36  			f: &fsm{
    37  				Topology: description.Topology{
    38  					SessionTimeoutMinutesPtr: int64ToPtr(1),
    39  				},
    40  			},
    41  			s: description.Server{
    42  				Kind: description.RSPrimary,
    43  			},
    44  			want: nil,
    45  		},
    46  		{
    47  			name: "lower timeout on data-bearing server with session support on fsm",
    48  			f: &fsm{
    49  				Topology: description.Topology{
    50  					SessionTimeoutMinutesPtr: int64ToPtr(2),
    51  				},
    52  			},
    53  			s: description.Server{
    54  				Kind:                     description.RSPrimary,
    55  				SessionTimeoutMinutesPtr: int64ToPtr(1),
    56  			},
    57  			want: int64ToPtr(1),
    58  		},
    59  		{
    60  			name: "session support on data-bearing server with no session support on fsm with no servers",
    61  			f:    &fsm{Topology: description.Topology{}},
    62  			s: description.Server{
    63  				Kind:                     description.RSPrimary,
    64  				SessionTimeoutMinutesPtr: int64ToPtr(1),
    65  			},
    66  			want: int64ToPtr(1),
    67  		},
    68  		{
    69  			name: "session support on data-bearing server with no session support on fsm and lower servers",
    70  			f: &fsm{Topology: description.Topology{
    71  				Servers: []description.Server{
    72  					{
    73  						Kind:                     description.RSPrimary,
    74  						SessionTimeoutMinutesPtr: int64ToPtr(1),
    75  					},
    76  				},
    77  			}},
    78  			s: description.Server{
    79  				Kind:                     description.RSPrimary,
    80  				SessionTimeoutMinutesPtr: int64ToPtr(2),
    81  			},
    82  			want: int64ToPtr(1),
    83  		},
    84  		{
    85  			name: "session support on data-bearing server with no session support on fsm and higher servers",
    86  			f: &fsm{Topology: description.Topology{
    87  				Servers: []description.Server{
    88  					{
    89  						Kind:                     description.RSPrimary,
    90  						SessionTimeoutMinutesPtr: int64ToPtr(3),
    91  					},
    92  				},
    93  			}},
    94  			s: description.Server{
    95  				Kind:                     description.RSPrimary,
    96  				SessionTimeoutMinutesPtr: int64ToPtr(2),
    97  			},
    98  			want: int64ToPtr(2),
    99  		},
   100  	}
   101  
   102  	for _, test := range tests {
   103  		test := test // capture the range variable
   104  
   105  		t.Run(test.name, func(t *testing.T) {
   106  			t.Parallel()
   107  
   108  			got := selectFSMSessionTimeout(test.f, test.s)
   109  			gotStr := "nil"
   110  			wantStr := "nil"
   111  			if got != nil {
   112  				gotStr = fmt.Sprintf("%v", *got)
   113  			}
   114  			if test.want != nil {
   115  				wantStr = fmt.Sprintf("%v", *test.want)
   116  			}
   117  			assert.Equal(t, test.want, got, "minFSMServersTimeout() = %v (%v), wanted %v (%v).", got, gotStr, test.want, wantStr)
   118  		})
   119  	}
   120  }
   121  

View as plain text