github.com/RajatVaryani/mattermost-server@v5.11.1+incompatible/store/sqlstore/supplier_test.go (about)

     1  package sqlstore_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/mattermost/gorp"
     7  	_ "github.com/mattn/go-sqlite3"
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"github.com/mattermost/mattermost-server/model"
    11  	"github.com/mattermost/mattermost-server/store/sqlstore"
    12  )
    13  
    14  func TestGetReplica(t *testing.T) {
    15  	t.Parallel()
    16  	testCases := []struct {
    17  		Description              string
    18  		DataSourceReplicas       []string
    19  		DataSourceSearchReplicas []string
    20  	}{
    21  		{
    22  			"no replicas",
    23  			[]string{},
    24  			[]string{},
    25  		},
    26  		{
    27  			"one source replica",
    28  			[]string{":memory:"},
    29  			[]string{},
    30  		},
    31  		{
    32  			"multiple source replicas",
    33  			[]string{":memory:", ":memory:", ":memory:"},
    34  			[]string{},
    35  		},
    36  		{
    37  			"one source search replica",
    38  			[]string{},
    39  			[]string{":memory:"},
    40  		},
    41  		{
    42  			"multiple source search replicas",
    43  			[]string{},
    44  			[]string{":memory:", ":memory:", ":memory:"},
    45  		},
    46  		{
    47  			"one source replica, one source search replica",
    48  			[]string{":memory:"},
    49  			[]string{":memory:"},
    50  		},
    51  		{
    52  			"one source replica, multiple source search replicas",
    53  			[]string{":memory:"},
    54  			[]string{":memory:", ":memory:", ":memory:"},
    55  		},
    56  		{
    57  			"multiple source replica, one source search replica",
    58  			[]string{":memory:", ":memory:", ":memory:"},
    59  			[]string{":memory:"},
    60  		},
    61  		{
    62  			"multiple source replica, multiple source search replicas",
    63  			[]string{":memory:", ":memory:", ":memory:"},
    64  			[]string{":memory:", ":memory:", ":memory:"},
    65  		},
    66  	}
    67  
    68  	for _, testCase := range testCases {
    69  		testCase := testCase
    70  		t.Run(testCase.Description, func(t *testing.T) {
    71  			t.Parallel()
    72  
    73  			driverName := model.DATABASE_DRIVER_SQLITE
    74  			dataSource := ":memory:"
    75  			maxIdleConns := 1
    76  			connMaxLifetimeMilliseconds := 3600000
    77  			maxOpenConns := 1
    78  			queryTimeout := 5
    79  
    80  			settings := model.SqlSettings{
    81  				DriverName:                  &driverName,
    82  				DataSource:                  &dataSource,
    83  				MaxIdleConns:                &maxIdleConns,
    84  				ConnMaxLifetimeMilliseconds: &connMaxLifetimeMilliseconds,
    85  				MaxOpenConns:                &maxOpenConns,
    86  				QueryTimeout:                &queryTimeout,
    87  				DataSourceReplicas:          testCase.DataSourceReplicas,
    88  				DataSourceSearchReplicas:    testCase.DataSourceSearchReplicas,
    89  			}
    90  			supplier := sqlstore.NewSqlSupplier(settings, nil)
    91  
    92  			replicas := make(map[*gorp.DbMap]bool)
    93  			for i := 0; i < 5; i++ {
    94  				replicas[supplier.GetReplica()] = true
    95  			}
    96  
    97  			searchReplicas := make(map[*gorp.DbMap]bool)
    98  			for i := 0; i < 5; i++ {
    99  				searchReplicas[supplier.GetSearchReplica()] = true
   100  			}
   101  
   102  			if len(testCase.DataSourceReplicas) > 0 {
   103  				// If replicas were defined, ensure none are the master.
   104  				assert.Len(t, replicas, len(testCase.DataSourceReplicas))
   105  
   106  				for replica := range replicas {
   107  					assert.NotEqual(t, supplier.GetMaster(), replica)
   108  				}
   109  
   110  			} else if assert.Len(t, replicas, 1) {
   111  				// Otherwise ensure the replicas contains only the master.
   112  				for replica := range replicas {
   113  					assert.Equal(t, supplier.GetMaster(), replica)
   114  				}
   115  			}
   116  
   117  			if len(testCase.DataSourceSearchReplicas) > 0 {
   118  				// If search replicas were defined, ensure none are the master nor the replicas.
   119  				assert.Len(t, searchReplicas, len(testCase.DataSourceSearchReplicas))
   120  
   121  				for searchReplica := range searchReplicas {
   122  					assert.NotEqual(t, supplier.GetMaster(), searchReplica)
   123  					for replica := range replicas {
   124  						assert.NotEqual(t, searchReplica, replica)
   125  					}
   126  				}
   127  
   128  			} else if len(testCase.DataSourceReplicas) > 0 {
   129  				// If no search replicas were defined, but replicas were, ensure they are equal.
   130  				assert.Equal(t, replicas, searchReplicas)
   131  
   132  			} else if assert.Len(t, searchReplicas, 1) {
   133  				// Otherwise ensure the search replicas contains the master.
   134  				for searchReplica := range searchReplicas {
   135  					assert.Equal(t, supplier.GetMaster(), searchReplica)
   136  				}
   137  			}
   138  		})
   139  	}
   140  }
   141  
   142  func TestGetAllConns(t *testing.T) {
   143  	t.Parallel()
   144  	testCases := []struct {
   145  		Description              string
   146  		DataSourceReplicas       []string
   147  		DataSourceSearchReplicas []string
   148  		ExpectedNumConnections   int
   149  	}{
   150  		{
   151  			"no replicas",
   152  			[]string{},
   153  			[]string{},
   154  			1,
   155  		},
   156  		{
   157  			"one source replica",
   158  			[]string{":memory:"},
   159  			[]string{},
   160  			2,
   161  		},
   162  		{
   163  			"multiple source replicas",
   164  			[]string{":memory:", ":memory:", ":memory:"},
   165  			[]string{},
   166  			4,
   167  		},
   168  		{
   169  			"one source search replica",
   170  			[]string{},
   171  			[]string{":memory:"},
   172  			1,
   173  		},
   174  		{
   175  			"multiple source search replicas",
   176  			[]string{},
   177  			[]string{":memory:", ":memory:", ":memory:"},
   178  			1,
   179  		},
   180  		{
   181  			"one source replica, one source search replica",
   182  			[]string{":memory:"},
   183  			[]string{":memory:"},
   184  			2,
   185  		},
   186  		{
   187  			"one source replica, multiple source search replicas",
   188  			[]string{":memory:"},
   189  			[]string{":memory:", ":memory:", ":memory:"},
   190  			2,
   191  		},
   192  		{
   193  			"multiple source replica, one source search replica",
   194  			[]string{":memory:", ":memory:", ":memory:"},
   195  			[]string{":memory:"},
   196  			4,
   197  		},
   198  		{
   199  			"multiple source replica, multiple source search replicas",
   200  			[]string{":memory:", ":memory:", ":memory:"},
   201  			[]string{":memory:", ":memory:", ":memory:"},
   202  			4,
   203  		},
   204  	}
   205  
   206  	for _, testCase := range testCases {
   207  		testCase := testCase
   208  		t.Run(testCase.Description, func(t *testing.T) {
   209  			t.Parallel()
   210  
   211  			driverName := model.DATABASE_DRIVER_SQLITE
   212  			dataSource := ":memory:"
   213  			maxIdleConns := 1
   214  			connMaxLifetimeMilliseconds := 3600000
   215  			maxOpenConns := 1
   216  			queryTimeout := 5
   217  
   218  			settings := model.SqlSettings{
   219  				DriverName:                  &driverName,
   220  				DataSource:                  &dataSource,
   221  				MaxIdleConns:                &maxIdleConns,
   222  				ConnMaxLifetimeMilliseconds: &connMaxLifetimeMilliseconds,
   223  				MaxOpenConns:                &maxOpenConns,
   224  				QueryTimeout:                &queryTimeout,
   225  				DataSourceReplicas:          testCase.DataSourceReplicas,
   226  				DataSourceSearchReplicas:    testCase.DataSourceSearchReplicas,
   227  			}
   228  			supplier := sqlstore.NewSqlSupplier(settings, nil)
   229  
   230  			assert.Equal(t, testCase.ExpectedNumConnections, len(supplier.GetAllConns()))
   231  		})
   232  	}
   233  }