2017-04-12 08:27:57 -04:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
2015-06-14 23:53:32 -08:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
2017-09-25 09:11:25 -05:00
|
|
|
package sqlstore
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2017-09-25 09:11:25 -05:00
|
|
|
import (
|
2017-10-09 10:16:14 -07:00
|
|
|
"sync"
|
2017-10-06 11:08:59 -07:00
|
|
|
"testing"
|
|
|
|
|
|
2017-10-09 10:16:14 -07:00
|
|
|
"github.com/mattermost/mattermost-server/model"
|
2017-09-25 09:11:25 -05:00
|
|
|
"github.com/mattermost/mattermost-server/store"
|
2017-10-09 10:16:14 -07:00
|
|
|
"github.com/mattermost/mattermost-server/store/storetest"
|
2017-09-25 09:11:25 -05:00
|
|
|
)
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2018-12-10 11:58:09 -05:00
|
|
|
type storeType struct {
|
2018-09-13 13:47:17 -04:00
|
|
|
Name string
|
2018-12-10 11:58:09 -05:00
|
|
|
SqlSettings *model.SqlSettings
|
2018-09-13 13:47:17 -04:00
|
|
|
SqlSupplier *SqlSupplier
|
|
|
|
|
Store store.Store
|
2017-10-09 10:16:14 -07:00
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2018-12-10 11:58:09 -05:00
|
|
|
var storeTypes []*storeType
|
|
|
|
|
|
2017-10-06 11:08:59 -07:00
|
|
|
func StoreTest(t *testing.T, f func(*testing.T, store.Store)) {
|
2017-10-09 10:16:14 -07:00
|
|
|
defer func() {
|
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
|
tearDownStores()
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
for _, st := range storeTypes {
|
|
|
|
|
st := st
|
|
|
|
|
t.Run(st.Name, func(t *testing.T) { f(t, st.Store) })
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-15 11:16:26 -07:00
|
|
|
|
2018-09-13 13:47:17 -04:00
|
|
|
func StoreTestWithSqlSupplier(t *testing.T, f func(*testing.T, store.Store, storetest.SqlSupplier)) {
|
|
|
|
|
defer func() {
|
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
|
tearDownStores()
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
for _, st := range storeTypes {
|
|
|
|
|
st := st
|
|
|
|
|
t.Run(st.Name, func(t *testing.T) { f(t, st.Store, st.SqlSupplier) })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-09 10:16:14 -07:00
|
|
|
func initStores() {
|
2018-12-10 11:58:09 -05:00
|
|
|
storeTypes = append(storeTypes, &storeType{
|
|
|
|
|
Name: "MySQL",
|
|
|
|
|
SqlSettings: storetest.MakeSqlSettings(model.DATABASE_DRIVER_MYSQL),
|
|
|
|
|
})
|
|
|
|
|
storeTypes = append(storeTypes, &storeType{
|
|
|
|
|
Name: "PostgreSQL",
|
|
|
|
|
SqlSettings: storetest.MakeSqlSettings(model.DATABASE_DRIVER_POSTGRES),
|
|
|
|
|
})
|
|
|
|
|
|
2017-10-09 10:16:14 -07:00
|
|
|
defer func() {
|
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
|
tearDownStores()
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
for _, st := range storeTypes {
|
|
|
|
|
st := st
|
2018-12-10 11:58:09 -05:00
|
|
|
wg.Add(1)
|
2017-10-09 10:16:14 -07:00
|
|
|
go func() {
|
|
|
|
|
defer wg.Done()
|
2018-12-10 11:58:09 -05:00
|
|
|
st.SqlSupplier = NewSqlSupplier(*st.SqlSettings, nil)
|
2018-09-13 13:47:17 -04:00
|
|
|
st.Store = store.NewLayeredStore(st.SqlSupplier, nil, nil)
|
2018-12-10 11:58:09 -05:00
|
|
|
st.Store.DropAllTables()
|
2017-10-09 10:16:14 -07:00
|
|
|
st.Store.MarkSystemRanUnitTests()
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
wg.Wait()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tearDownStoresOnce sync.Once
|
|
|
|
|
|
|
|
|
|
func tearDownStores() {
|
|
|
|
|
tearDownStoresOnce.Do(func() {
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
wg.Add(len(storeTypes))
|
|
|
|
|
for _, st := range storeTypes {
|
|
|
|
|
st := st
|
|
|
|
|
go func() {
|
2017-10-09 14:59:48 -07:00
|
|
|
if st.Store != nil {
|
|
|
|
|
st.Store.Close()
|
|
|
|
|
}
|
2017-10-09 10:16:14 -07:00
|
|
|
wg.Done()
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
wg.Wait()
|
|
|
|
|
})
|
|
|
|
|
}
|