2021-10-21 13:14:14 +00:00
|
|
|
package persistence
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"testing"
|
2021-11-05 14:27:30 +00:00
|
|
|
"time"
|
2021-10-21 13:14:14 +00:00
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3" // Blank import to register the sqlite3 driver
|
2021-11-05 14:27:30 +00:00
|
|
|
"github.com/status-im/go-waku/tests"
|
2021-10-21 13:14:14 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/pb"
|
2022-05-06 19:29:31 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/utils"
|
2021-10-21 13:14:14 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewMock() *sql.DB {
|
|
|
|
db, err := sql.Open("sqlite3", ":memory:")
|
|
|
|
if err != nil {
|
2022-05-06 19:29:31 +00:00
|
|
|
utils.Logger().Fatalf("an error '%s' was not expected when opening a stub database connection", err)
|
2021-10-21 13:14:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
2022-02-23 15:01:53 +00:00
|
|
|
func createIndex(digest []byte, receiverTime int64) *pb.Index {
|
2021-11-05 14:27:30 +00:00
|
|
|
return &pb.Index{
|
|
|
|
Digest: digest,
|
|
|
|
ReceiverTime: receiverTime,
|
|
|
|
SenderTime: 1.0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 13:14:14 +00:00
|
|
|
func TestDbStore(t *testing.T) {
|
|
|
|
db := NewMock()
|
|
|
|
option := WithDB(db)
|
2022-05-06 19:29:31 +00:00
|
|
|
store, err := NewDBStore(utils.Logger(), option)
|
2021-10-21 13:14:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
res, err := store.GetAll()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, res)
|
|
|
|
|
|
|
|
err = store.Put(
|
2021-11-05 14:27:30 +00:00
|
|
|
createIndex([]byte("digest"), 1),
|
2021-10-21 13:14:14 +00:00
|
|
|
"test",
|
2021-11-05 14:27:30 +00:00
|
|
|
tests.CreateWakuMessage("test", 1),
|
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()
|
2022-05-06 19:29:31 +00:00
|
|
|
store, err := NewDBStore(utils.Logger(), WithDB(db), WithRetentionPolicy(5, 20*time.Second))
|
2021-11-05 14:27:30 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
insertTime := time.Now()
|
|
|
|
|
2022-02-23 15:01:53 +00:00
|
|
|
_ = store.Put(createIndex([]byte{1}, insertTime.Add(-70*time.Second).UnixNano()), "test", tests.CreateWakuMessage("test", 1))
|
|
|
|
_ = store.Put(createIndex([]byte{2}, insertTime.Add(-60*time.Second).UnixNano()), "test", tests.CreateWakuMessage("test", 2))
|
|
|
|
_ = store.Put(createIndex([]byte{3}, insertTime.Add(-50*time.Second).UnixNano()), "test", tests.CreateWakuMessage("test", 3))
|
|
|
|
_ = store.Put(createIndex([]byte{4}, insertTime.Add(-40*time.Second).UnixNano()), "test", tests.CreateWakuMessage("test", 4))
|
|
|
|
_ = store.Put(createIndex([]byte{5}, insertTime.Add(-30*time.Second).UnixNano()), "test", tests.CreateWakuMessage("test", 5))
|
2021-11-05 14:27:30 +00:00
|
|
|
|
|
|
|
dbResults, err := store.GetAll()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, dbResults, 5)
|
|
|
|
|
2022-02-23 15:01:53 +00:00
|
|
|
_ = store.Put(createIndex([]byte{6}, insertTime.Add(-20*time.Second).UnixNano()), "test", tests.CreateWakuMessage("test", 6))
|
|
|
|
_ = store.Put(createIndex([]byte{7}, insertTime.Add(-10*time.Second).UnixNano()), "test", tests.CreateWakuMessage("test", 7))
|
2021-11-05 14:27:30 +00:00
|
|
|
|
|
|
|
// This step simulates starting go-waku again from scratch
|
|
|
|
|
2022-05-06 19:29:31 +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)
|
|
|
|
|
|
|
|
dbResults, err = store.GetAll()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, dbResults, 3)
|
|
|
|
require.Equal(t, []byte{5}, dbResults[0].ID)
|
|
|
|
require.Equal(t, []byte{6}, dbResults[1].ID)
|
|
|
|
require.Equal(t, []byte{7}, dbResults[2].ID)
|
|
|
|
}
|