diff options
author | Ben Morrison <ben@gbmor.dev> | 2019-06-09 02:58:45 -0400 |
---|---|---|
committer | Ben Morrison <ben@gbmor.dev> | 2019-06-09 02:58:45 -0400 |
commit | e10fc64f9855262c4a9511025dffcc1d603c7206 (patch) | |
tree | 75cd201bdb02c2d3a077b07a128a93b3322b8bd3 /svc/init_test.go | |
parent | e280417e804cb0f3723639b14a7af12e6bdebd09 (diff) | |
download | getwtxt-e10fc64f9855262c4a9511025dffcc1d603c7206.tar.gz |
using sync.Once for DB init in tests, cleaned up test config init
Diffstat (limited to 'svc/init_test.go')
-rw-r--r-- | svc/init_test.go | 50 |
1 files changed, 18 insertions, 32 deletions
diff --git a/svc/init_test.go b/svc/init_test.go index 9c99a3b..cac2fe6 100644 --- a/svc/init_test.go +++ b/svc/init_test.go @@ -9,23 +9,35 @@ import ( "sync" "time" - "github.com/fsnotify/fsnotify" "github.com/spf13/viper" ) -var testport = fmt.Sprintf(":%v", confObj.Port) -var hasInit = false - -var initTestOnce sync.Once +var ( + testport string + initTestOnce sync.Once + initDBOnce sync.Once +) func initTestConf() { initTestOnce.Do(func() { + testConfig() tmpls = testTemplates() + + confObj.Mu.RLock() + testport = fmt.Sprintf(":%v", confObj.Port) + confObj.Mu.RUnlock() + logToNull() }) } +func initTestDB() { + initDBOnce.Do(func() { + initDatabase() + }) +} + func logToNull() { hush, err := os.Open("/dev/null") if err != nil { @@ -44,27 +56,12 @@ func testConfig() { viper.SetConfigType("yml") viper.AddConfigPath("..") - log.Printf("Loading configuration ...\n") - if err := viper.ReadInConfig(); err != nil { - log.Printf("%v\n", err.Error()) - log.Printf("Using defaults ...\n") - } else { - viper.WatchConfig() - viper.OnConfigChange(func(e fsnotify.Event) { - log.Printf("Config file change detected. Reloading...\n") - rebindConfig() - }) - } - viper.SetDefault("ListenPort", 9001) - viper.SetDefault("LogFile", "getwtxt.log") viper.SetDefault("DatabasePath", "getwtxt.db") viper.SetDefault("AssetsDirectory", "assets") viper.SetDefault("DatabaseType", "leveldb") - viper.SetDefault("StdoutLogging", false) viper.SetDefault("ReCacheInterval", "1h") viper.SetDefault("DatabasePushInterval", "5m") - viper.SetDefault("Instance.SiteName", "getwtxt") viper.SetDefault("Instance.OwnerName", "Anonymous Microblogger") viper.SetDefault("Instance.Email", "nobody@knows") @@ -74,25 +71,14 @@ func testConfig() { confObj.Mu.Lock() confObj.Port = viper.GetInt("ListenPort") - confObj.LogFile = viper.GetString("LogFile") + confObj.AssetsDir = "../" + viper.GetString("AssetsDirectory") confObj.DBType = strings.ToLower(viper.GetString("DatabaseType")) - confObj.DBPath = viper.GetString("DatabasePath") log.Printf("Using %v database: %v\n", confObj.DBType, confObj.DBPath) - confObj.AssetsDir = "../" + viper.GetString("AssetsDirectory") - - confObj.StdoutLogging = viper.GetBool("StdoutLogging") - if confObj.StdoutLogging { - log.Printf("Logging to stdout\n") - } else { - log.Printf("Logging to %v\n", confObj.LogFile) - } - confObj.CacheInterval = viper.GetDuration("StatusFetchInterval") log.Printf("User status fetch interval: %v\n", confObj.CacheInterval) - confObj.DBInterval = viper.GetDuration("DatabasePushInterval") log.Printf("Database push interval: %v\n", confObj.DBInterval) |