github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+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/mlog"
    12  	"github.com/mattermost/mattermost-server/model"
    13  	"github.com/mattermost/mattermost-server/store"
    14  	"github.com/mattermost/mattermost-server/store/storetest"
    15  	"github.com/mattermost/mattermost-server/utils"
    16  )
    17  
    18  var storeTypes = []*struct {
    19  	Name      string
    20  	Func      func() (*storetest.RunningContainer, *model.SqlSettings, error)
    21  	Container *storetest.RunningContainer
    22  	Store     store.Store
    23  }{
    24  	{
    25  		Name: "MySQL",
    26  		Func: storetest.NewMySQLContainer,
    27  	},
    28  	{
    29  		Name: "PostgreSQL",
    30  		Func: storetest.NewPostgreSQLContainer,
    31  	},
    32  }
    33  
    34  func StoreTest(t *testing.T, f func(*testing.T, store.Store)) {
    35  	defer func() {
    36  		if err := recover(); err != nil {
    37  			tearDownStores()
    38  			panic(err)
    39  		}
    40  	}()
    41  	for _, st := range storeTypes {
    42  		st := st
    43  		t.Run(st.Name, func(t *testing.T) { f(t, st.Store) })
    44  	}
    45  }
    46  
    47  func initStores() {
    48  	defer func() {
    49  		if err := recover(); err != nil {
    50  			tearDownStores()
    51  			panic(err)
    52  		}
    53  	}()
    54  	var wg sync.WaitGroup
    55  	errCh := make(chan error, len(storeTypes))
    56  	wg.Add(len(storeTypes))
    57  	for _, st := range storeTypes {
    58  		st := st
    59  		go func() {
    60  			defer wg.Done()
    61  			container, settings, err := st.Func()
    62  			if err != nil {
    63  				errCh <- err
    64  				return
    65  			}
    66  			st.Container = container
    67  			st.Store = store.NewLayeredStore(NewSqlSupplier(*settings, nil), nil, nil)
    68  			st.Store.MarkSystemRanUnitTests()
    69  		}()
    70  	}
    71  	wg.Wait()
    72  	select {
    73  	case err := <-errCh:
    74  		panic(err)
    75  	default:
    76  	}
    77  }
    78  
    79  var tearDownStoresOnce sync.Once
    80  
    81  func tearDownStores() {
    82  	tearDownStoresOnce.Do(func() {
    83  		var wg sync.WaitGroup
    84  		wg.Add(len(storeTypes))
    85  		for _, st := range storeTypes {
    86  			st := st
    87  			go func() {
    88  				if st.Store != nil {
    89  					st.Store.Close()
    90  				}
    91  				if st.Container != nil {
    92  					st.Container.Stop()
    93  				}
    94  				wg.Done()
    95  			}()
    96  		}
    97  		wg.Wait()
    98  	})
    99  }
   100  
   101  func TestMain(m *testing.M) {
   102  	// Setup a global logger to catch tests logging outside of app context
   103  	// The global logger will be stomped by apps initalizing but that's fine for testing. Ideally this won't happen.
   104  	mlog.InitGlobalLogger(mlog.NewLogger(&mlog.LoggerConfiguration{
   105  		EnableConsole: true,
   106  		ConsoleJson:   true,
   107  		ConsoleLevel:  "error",
   108  		EnableFile:    false,
   109  	}))
   110  
   111  	utils.TranslationsPreInit()
   112  
   113  	status := 0
   114  
   115  	initStores()
   116  	defer func() {
   117  		tearDownStores()
   118  		os.Exit(status)
   119  	}()
   120  
   121  	status = m.Run()
   122  }