2021-05-19 06:55:03 +02:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
2021-07-16 04:07:30 +02:00
|
|
|
|
2021-10-08 03:28:04 +02:00
|
|
|
"github.com/TwiN/gatus/v3/storage/store/sql"
|
2021-05-19 06:55:03 +02:00
|
|
|
)
|
|
|
|
|
2021-07-19 05:24:09 +02:00
|
|
|
func TestGet(t *testing.T) {
|
|
|
|
store := Get()
|
|
|
|
if store == nil {
|
|
|
|
t.Error("store should've been automatically initialized")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
{
|
2021-08-07 18:11:35 +02:00
|
|
|
Name: "memory-no-file",
|
|
|
|
Cfg: &Config{Type: TypeMemory},
|
2021-07-16 04:07:30 +02:00
|
|
|
ExpectedErr: nil,
|
|
|
|
},
|
|
|
|
{
|
2021-08-07 18:11:35 +02:00
|
|
|
Name: "memory-with-file",
|
|
|
|
Cfg: &Config{Type: TypeMemory, File: t.TempDir() + "/TestInitialize_memory-with-file.db"},
|
2021-07-16 04:07:30 +02:00
|
|
|
ExpectedErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "sqlite-no-file",
|
|
|
|
Cfg: &Config{Type: TypeSQLite},
|
2021-09-11 00:00:04 +02:00
|
|
|
ExpectedErr: sql.ErrFilePathNotSpecified,
|
2021-07-16 04:07:30 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
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 {
|
2021-07-19 05:24:09 +02:00
|
|
|
t.Fatal("provider shouldn't have been nit")
|
2021-07-16 04:07:30 +02:00
|
|
|
}
|
|
|
|
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()
|
|
|
|
})
|
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-17 03:32:53 +02:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2021-05-19 06:55:03 +02:00
|
|
|
}
|