gatus/storage/storage_test.go

90 lines
2.0 KiB
Go
Raw Normal View History

2021-05-19 06:55:03 +02:00
package storage
import (
2021-07-16 04:07:30 +02:00
"fmt"
2021-05-19 06:55:03 +02:00
"testing"
"time"
2021-07-16 04:07:30 +02:00
"github.com/TwinProduction/gatus/storage/store/database"
2021-05-19 06:55:03 +02:00
)
func TestInitialize(t *testing.T) {
2021-07-16 04:07:30 +02:00
type Scenario struct {
Name string
Cfg *Config
ExpectedErr error
2021-05-19 06:55:03 +02:00
}
2021-07-16 04:07:30 +02:00
scenarios := []Scenario{
{
Name: "nil",
Cfg: nil,
ExpectedErr: nil,
},
{
Name: "blank",
Cfg: &Config{},
ExpectedErr: nil,
},
{
Name: "inmemory-no-file",
Cfg: &Config{Type: TypeInMemory},
ExpectedErr: nil,
},
{
Name: "inmemory-with-file",
Cfg: &Config{Type: TypeInMemory, File: t.TempDir() + "/TestInitialize_inmemory-with-file.db"},
ExpectedErr: nil,
},
{
Name: "sqlite-no-file",
Cfg: &Config{Type: TypeSQLite},
ExpectedErr: database.ErrFilePathNotSpecified,
},
{
Name: "sqlite-with-file",
Cfg: &Config{Type: TypeSQLite, File: t.TempDir() + "/TestInitialize_sqlite-with-file.db"},
ExpectedErr: nil,
},
2021-05-19 06:55:03 +02:00
}
2021-07-16 04:07:30 +02:00
for _, scenario := range scenarios {
t.Run(scenario.Name, func(t *testing.T) {
err := Initialize(scenario.Cfg)
if err != scenario.ExpectedErr {
t.Errorf("expected %v, got %v", scenario.ExpectedErr, err)
}
if err != nil {
return
}
if cancelFunc == nil {
t.Error("cancelFunc shouldn't have been nil")
}
if ctx == nil {
t.Error("ctx shouldn't have been nil")
}
if provider == nil {
fmt.Println("wtf?")
}
provider.Close()
// Try to initialize it again
err = Initialize(scenario.Cfg)
if err != scenario.ExpectedErr {
t.Errorf("expected %v, got %v", scenario.ExpectedErr, err)
return
}
provider.Close()
provider = nil
})
2021-05-19 06:55:03 +02:00
}
}
func TestAutoSave(t *testing.T) {
2021-07-16 04:07:30 +02:00
file := t.TempDir() + "/TestAutoSave.db"
2021-05-19 06:55:03 +02:00
if err := Initialize(&Config{File: file}); err != nil {
t.Fatal("shouldn't have returned an error")
}
2021-07-16 04:07:30 +02:00
go autoSaveStore(ctx, provider, 3*time.Millisecond)
2021-05-19 06:55:03 +02:00
time.Sleep(15 * time.Millisecond)
cancelFunc()
2021-07-16 04:07:30 +02:00
time.Sleep(10 * time.Millisecond)
2021-05-19 06:55:03 +02:00
}