github.com/mad-app/mattermost-server@v5.11.1+incompatible/store/sqlstore/store_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package sqlstore
     5  
     6  import (
     7  	"sync"
     8  	"testing"
     9  
    10  	"github.com/mattermost/mattermost-server/model"
    11  	"github.com/mattermost/mattermost-server/store"
    12  	"github.com/mattermost/mattermost-server/store/storetest"
    13  )
    14  
    15  type storeType struct {
    16  	Name        string
    17  	SqlSettings *model.SqlSettings
    18  	SqlSupplier *SqlSupplier
    19  	Store       store.Store
    20  }
    21  
    22  var storeTypes []*storeType
    23  
    24  func StoreTest(t *testing.T, f func(*testing.T, store.Store)) {
    25  	defer func() {
    26  		if err := recover(); err != nil {
    27  			tearDownStores()
    28  			panic(err)
    29  		}
    30  	}()
    31  	for _, st := range storeTypes {
    32  		st := st
    33  		t.Run(st.Name, func(t *testing.T) { f(t, st.Store) })
    34  	}
    35  }
    36  
    37  func StoreTestWithSqlSupplier(t *testing.T, f func(*testing.T, store.Store, storetest.SqlSupplier)) {
    38  	defer func() {
    39  		if err := recover(); err != nil {
    40  			tearDownStores()
    41  			panic(err)
    42  		}
    43  	}()
    44  	for _, st := range storeTypes {
    45  		st := st
    46  		t.Run(st.Name, func(t *testing.T) { f(t, st.Store, st.SqlSupplier) })
    47  	}
    48  }
    49  
    50  func initStores() {
    51  	storeTypes = append(storeTypes, &storeType{
    52  		Name:        "MySQL",
    53  		SqlSettings: storetest.MakeSqlSettings(model.DATABASE_DRIVER_MYSQL),
    54  	})
    55  	storeTypes = append(storeTypes, &storeType{
    56  		Name:        "PostgreSQL",
    57  		SqlSettings: storetest.MakeSqlSettings(model.DATABASE_DRIVER_POSTGRES),
    58  	})
    59  
    60  	defer func() {
    61  		if err := recover(); err != nil {
    62  			tearDownStores()
    63  			panic(err)
    64  		}
    65  	}()
    66  	var wg sync.WaitGroup
    67  	for _, st := range storeTypes {
    68  		st := st
    69  		wg.Add(1)
    70  		go func() {
    71  			defer wg.Done()
    72  			st.SqlSupplier = NewSqlSupplier(*st.SqlSettings, nil)
    73  			st.Store = store.NewLayeredStore(st.SqlSupplier, nil, nil)
    74  			st.Store.DropAllTables()
    75  			st.Store.MarkSystemRanUnitTests()
    76  		}()
    77  	}
    78  	wg.Wait()
    79  }
    80  
    81  var tearDownStoresOnce sync.Once
    82  
    83  func tearDownStores() {
    84  	tearDownStoresOnce.Do(func() {
    85  		var wg sync.WaitGroup
    86  		wg.Add(len(storeTypes))
    87  		for _, st := range storeTypes {
    88  			st := st
    89  			go func() {
    90  				if st.Store != nil {
    91  					st.Store.Close()
    92  				}
    93  				wg.Done()
    94  			}()
    95  		}
    96  		wg.Wait()
    97  	})
    98  }