2021-10-21 13:14:14 +00:00
|
|
|
package persistence
|
|
|
|
|
|
|
|
import (
|
2023-03-09 15:48:25 +00:00
|
|
|
"context"
|
2021-10-21 13:14:14 +00:00
|
|
|
"database/sql"
|
|
|
|
"testing"
|
2021-11-05 14:27:30 +00:00
|
|
|
"time"
|
2021-10-21 13:14:14 +00:00
|
|
|
|
2023-01-04 17:58:14 +00:00
|
|
|
"github.com/golang-migrate/migrate/v4/database/sqlite3"
|
2021-10-21 13:14:14 +00:00
|
|
|
_ "github.com/mattn/go-sqlite3" // Blank import to register the sqlite3 driver
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/tests"
|
2023-01-04 17:58:14 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/persistence/sqlite/migrations"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol"
|
2022-12-09 03:08:04 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/timesource"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/utils"
|
2022-05-27 13:25:06 +00:00
|
|
|
"go.uber.org/zap"
|
2021-10-21 13:14:14 +00:00
|
|
|
)
|
|
|
|
|
2023-01-04 17:58:14 +00:00
|
|
|
func Migrate(db *sql.DB) error {
|
|
|
|
migrationDriver, err := sqlite3.WithInstance(db, &sqlite3.Config{
|
|
|
|
MigrationsTable: "gowaku_" + sqlite3.DefaultMigrationsTable,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return migrations.Migrate(db, migrationDriver)
|
|
|
|
}
|
|
|
|
|
2021-10-21 13:14:14 +00:00
|
|
|
func NewMock() *sql.DB {
|
|
|
|
db, err := sql.Open("sqlite3", ":memory:")
|
|
|
|
if err != nil {
|
2022-05-27 13:25:06 +00:00
|
|
|
utils.Logger().Fatal("opening a stub database connection", zap.Error(err))
|
2021-10-21 13:14:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDbStore(t *testing.T) {
|
|
|
|
db := NewMock()
|
2023-01-04 17:58:14 +00:00
|
|
|
store, err := NewDBStore(utils.Logger(), WithDB(db), WithMigrations(Migrate))
|
2021-10-21 13:14:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-03-09 15:48:25 +00:00
|
|
|
err = store.Start(context.Background(), timesource.NewDefaultClock())
|
2022-12-09 03:08:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-10-21 13:14:14 +00:00
|
|
|
res, err := store.GetAll()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, res)
|
|
|
|
|
2023-04-19 20:54:33 +00:00
|
|
|
err = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test", utils.GetUnixEpoch()), utils.GetUnixEpoch(), "test"))
|
2021-10-21 13:14:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
res, err = store.GetAll()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, res)
|
|
|
|
}
|
2021-11-05 14:27:30 +00:00
|
|
|
|
|
|
|
func TestStoreRetention(t *testing.T) {
|
|
|
|
db := NewMock()
|
2023-01-04 17:58:14 +00:00
|
|
|
store, err := NewDBStore(utils.Logger(), WithDB(db), WithMigrations(Migrate), WithRetentionPolicy(5, 20*time.Second))
|
2021-11-05 14:27:30 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-03-09 15:48:25 +00:00
|
|
|
err = store.Start(context.Background(), timesource.NewDefaultClock())
|
2022-12-09 03:08:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-11-05 14:27:30 +00:00
|
|
|
insertTime := time.Now()
|
2023-04-19 20:54:33 +00:00
|
|
|
//////////////////////////////////
|
2022-05-30 18:48:22 +00:00
|
|
|
_ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test1", insertTime.Add(-70*time.Second).UnixNano()), insertTime.Add(-70*time.Second).UnixNano(), "test"))
|
|
|
|
_ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test2", insertTime.Add(-60*time.Second).UnixNano()), insertTime.Add(-60*time.Second).UnixNano(), "test"))
|
|
|
|
_ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test3", insertTime.Add(-50*time.Second).UnixNano()), insertTime.Add(-50*time.Second).UnixNano(), "test"))
|
|
|
|
_ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test4", insertTime.Add(-40*time.Second).UnixNano()), insertTime.Add(-40*time.Second).UnixNano(), "test"))
|
|
|
|
_ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test5", insertTime.Add(-30*time.Second).UnixNano()), insertTime.Add(-30*time.Second).UnixNano(), "test"))
|
2021-11-05 14:27:30 +00:00
|
|
|
|
|
|
|
dbResults, err := store.GetAll()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, dbResults, 5)
|
|
|
|
|
2022-05-30 18:48:22 +00:00
|
|
|
_ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test6", insertTime.Add(-20*time.Second).UnixNano()), insertTime.Add(-20*time.Second).UnixNano(), "test"))
|
|
|
|
_ = store.Put(protocol.NewEnvelope(tests.CreateWakuMessage("test7", insertTime.Add(-10*time.Second).UnixNano()), insertTime.Add(-10*time.Second).UnixNano(), "test"))
|
2021-11-05 14:27:30 +00:00
|
|
|
|
|
|
|
// This step simulates starting go-waku again from scratch
|
|
|
|
|
2022-05-30 15:55:30 +00:00
|
|
|
store, err = NewDBStore(utils.Logger(), WithDB(db), WithRetentionPolicy(5, 40*time.Second))
|
2021-11-05 14:27:30 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-03-09 15:48:25 +00:00
|
|
|
err = store.Start(context.Background(), timesource.NewDefaultClock())
|
2022-12-09 03:08:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-11-05 14:27:30 +00:00
|
|
|
dbResults, err = store.GetAll()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, dbResults, 3)
|
2022-05-30 18:48:22 +00:00
|
|
|
require.Equal(t, "test5", dbResults[0].Message.ContentTopic)
|
|
|
|
require.Equal(t, "test6", dbResults[1].Message.ContentTopic)
|
|
|
|
require.Equal(t, "test7", dbResults[2].Message.ContentTopic)
|
2021-11-05 14:27:30 +00:00
|
|
|
}
|