github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+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  			maxOpenConns := 1
    77  			queryTimeout := 5
    78  
    79  			settings := model.SqlSettings{
    80  				DriverName:               &driverName,
    81  				DataSource:               &dataSource,
    82  				MaxIdleConns:             &maxIdleConns,
    83  				MaxOpenConns:             &maxOpenConns,
    84  				QueryTimeout:             &queryTimeout,
    85  				DataSourceReplicas:       testCase.DataSourceReplicas,
    86  				DataSourceSearchReplicas: testCase.DataSourceSearchReplicas,
    87  			}
    88  			supplier := sqlstore.NewSqlSupplier(settings, nil)
    89  
    90  			replicas := make(map[*gorp.DbMap]bool)
    91  			for i := 0; i < 5; i++ {
    92  				replicas[supplier.GetReplica()] = true
    93  			}
    94  
    95  			searchReplicas := make(map[*gorp.DbMap]bool)
    96  			for i := 0; i < 5; i++ {
    97  				searchReplicas[supplier.GetSearchReplica()] = true
    98  			}
    99  
   100  			if len(testCase.DataSourceReplicas) > 0 {
   101  				// If replicas were defined, ensure none are the master.
   102  				assert.Len(t, replicas, len(testCase.DataSourceReplicas))
   103  
   104  				for replica := range replicas {
   105  					assert.NotEqual(t, supplier.GetMaster(), replica)
   106  				}
   107  
   108  			} else if assert.Len(t, replicas, 1) {
   109  				// Otherwise ensure the replicas contains only the master.
   110  				for replica := range replicas {
   111  					assert.Equal(t, supplier.GetMaster(), replica)
   112  				}
   113  			}
   114  
   115  			if len(testCase.DataSourceSearchReplicas) > 0 {
   116  				// If search replicas were defined, ensure none are the master nor the replicas.
   117  				assert.Len(t, searchReplicas, len(testCase.DataSourceSearchReplicas))
   118  
   119  				for searchReplica := range searchReplicas {
   120  					assert.NotEqual(t, supplier.GetMaster(), searchReplica)
   121  					for replica := range replicas {
   122  						assert.NotEqual(t, searchReplica, replica)
   123  					}
   124  				}
   125  
   126  			} else if len(testCase.DataSourceReplicas) > 0 {
   127  				// If no search replicas were defined, but replicas were, ensure they are equal.
   128  				assert.Equal(t, replicas, searchReplicas)
   129  
   130  			} else if assert.Len(t, searchReplicas, 1) {
   131  				// Otherwise ensure the search replicas contains the master.
   132  				for searchReplica := range searchReplicas {
   133  					assert.Equal(t, supplier.GetMaster(), searchReplica)
   134  				}
   135  			}
   136  		})
   137  	}
   138  }
   139  
   140  func TestGetAllConns(t *testing.T) {
   141  	t.Parallel()
   142  	testCases := []struct {
   143  		Description              string
   144  		DataSourceReplicas       []string
   145  		DataSourceSearchReplicas []string
   146  		ExpectedNumConnections   int
   147  	}{
   148  		{
   149  			"no replicas",
   150  			[]string{},
   151  			[]string{},
   152  			1,
   153  		},
   154  		{
   155  			"one source replica",
   156  			[]string{":memory:"},
   157  			[]string{},
   158  			2,
   159  		},
   160  		{
   161  			"multiple source replicas",
   162  			[]string{":memory:", ":memory:", ":memory:"},
   163  			[]string{},
   164  			4,
   165  		},
   166  		{
   167  			"one source search replica",
   168  			[]string{},
   169  			[]string{":memory:"},
   170  			1,
   171  		},
   172  		{
   173  			"multiple source search replicas",
   174  			[]string{},
   175  			[]string{":memory:", ":memory:", ":memory:"},
   176  			1,
   177  		},
   178  		{
   179  			"one source replica, one source search replica",
   180  			[]string{":memory:"},
   181  			[]string{":memory:"},
   182  			2,
   183  		},
   184  		{
   185  			"one source replica, multiple source search replicas",
   186  			[]string{":memory:"},
   187  			[]string{":memory:", ":memory:", ":memory:"},
   188  			2,
   189  		},
   190  		{
   191  			"multiple source replica, one source search replica",
   192  			[]string{":memory:", ":memory:", ":memory:"},
   193  			[]string{":memory:"},
   194  			4,
   195  		},
   196  		{
   197  			"multiple source replica, multiple source search replicas",
   198  			[]string{":memory:", ":memory:", ":memory:"},
   199  			[]string{":memory:", ":memory:", ":memory:"},
   200  			4,
   201  		},
   202  	}
   203  
   204  	for _, testCase := range testCases {
   205  		testCase := testCase
   206  		t.Run(testCase.Description, func(t *testing.T) {
   207  			t.Parallel()
   208  
   209  			driverName := model.DATABASE_DRIVER_SQLITE
   210  			dataSource := ":memory:"
   211  			maxIdleConns := 1
   212  			maxOpenConns := 1
   213  			queryTimeout := 5
   214  
   215  			settings := model.SqlSettings{
   216  				DriverName:               &driverName,
   217  				DataSource:               &dataSource,
   218  				MaxIdleConns:             &maxIdleConns,
   219  				MaxOpenConns:             &maxOpenConns,
   220  				QueryTimeout:             &queryTimeout,
   221  				DataSourceReplicas:       testCase.DataSourceReplicas,
   222  				DataSourceSearchReplicas: testCase.DataSourceSearchReplicas,
   223  			}
   224  			supplier := sqlstore.NewSqlSupplier(settings, nil)
   225  
   226  			assert.Equal(t, testCase.ExpectedNumConnections, len(supplier.GetAllConns()))
   227  		})
   228  	}
   229  }