github.com/coincircle/mattermost-server@v4.8.1-0.20180321182714-9d701c704416+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  	"os"
     8  	"sync"
     9  	"testing"
    10  
    11  	"github.com/mattermost/mattermost-server/model"
    12  	"github.com/mattermost/mattermost-server/store"
    13  	"github.com/mattermost/mattermost-server/store/storetest"
    14  	"github.com/mattermost/mattermost-server/utils"
    15  )
    16  
    17  var storeTypes = []*struct {
    18  	Name      string
    19  	Func      func() (*storetest.RunningContainer, *model.SqlSettings, error)
    20  	Container *storetest.RunningContainer
    21  	Store     store.Store
    22  }{
    23  	{
    24  		Name: "MySQL",
    25  		Func: storetest.NewMySQLContainer,
    26  	},
    27  	{
    28  		Name: "PostgreSQL",
    29  		Func: storetest.NewPostgreSQLContainer,
    30  	},
    31  }
    32  
    33  func StoreTest(t *testing.T, f func(*testing.T, store.Store)) {
    34  	defer func() {
    35  		if err := recover(); err != nil {
    36  			tearDownStores()
    37  			panic(err)
    38  		}
    39  	}()
    40  	for _, st := range storeTypes {
    41  		st := st
    42  		t.Run(st.Name, func(t *testing.T) { f(t, st.Store) })
    43  	}
    44  }
    45  
    46  func initStores() {
    47  	defer func() {
    48  		if err := recover(); err != nil {
    49  			tearDownStores()
    50  			panic(err)
    51  		}
    52  	}()
    53  	var wg sync.WaitGroup
    54  	errCh := make(chan error, len(storeTypes))
    55  	wg.Add(len(storeTypes))
    56  	for _, st := range storeTypes {
    57  		st := st
    58  		go func() {
    59  			defer wg.Done()
    60  			container, settings, err := st.Func()
    61  			if err != nil {
    62  				errCh <- err
    63  				return
    64  			}
    65  			st.Container = container
    66  			st.Store = store.NewLayeredStore(NewSqlSupplier(*settings, nil), nil, nil)
    67  			st.Store.MarkSystemRanUnitTests()
    68  		}()
    69  	}
    70  	wg.Wait()
    71  	select {
    72  	case err := <-errCh:
    73  		panic(err)
    74  	default:
    75  	}
    76  }
    77  
    78  var tearDownStoresOnce sync.Once
    79  
    80  func tearDownStores() {
    81  	tearDownStoresOnce.Do(func() {
    82  		var wg sync.WaitGroup
    83  		wg.Add(len(storeTypes))
    84  		for _, st := range storeTypes {
    85  			st := st
    86  			go func() {
    87  				if st.Store != nil {
    88  					st.Store.Close()
    89  				}
    90  				if st.Container != nil {
    91  					st.Container.Stop()
    92  				}
    93  				wg.Done()
    94  			}()
    95  		}
    96  		wg.Wait()
    97  	})
    98  }
    99  
   100  func TestMain(m *testing.M) {
   101  	utils.TranslationsPreInit()
   102  
   103  	status := 0
   104  
   105  	initStores()
   106  	defer func() {
   107  		tearDownStores()
   108  		os.Exit(status)
   109  	}()
   110  
   111  	status = m.Run()
   112  }