Use in memory database for messenger tests

This commit is contained in:
Andrea Maria Piana 2020-10-08 16:05:49 +02:00
parent 25d44a9d3c
commit 5cea355541
7 changed files with 23 additions and 75 deletions

View File

@ -3,8 +3,6 @@ package protocol
import (
"context"
"crypto/ecdsa"
"io/ioutil"
"os"
"testing"
"github.com/google/uuid"
@ -28,9 +26,8 @@ type MessengerContactUpdateSuite struct {
privateKey *ecdsa.PrivateKey // private key for the main instance of Messenger
// If one wants to send messages between different instances of Messenger,
// a single waku service should be shared.
shh types.Waku
tmpFiles []*os.File // files to clean up
logger *zap.Logger
shh types.Waku
logger *zap.Logger
}
func (s *MessengerContactUpdateSuite) SetupTest() {
@ -52,13 +49,10 @@ func (s *MessengerContactUpdateSuite) TearDownTest() {
}
func (s *MessengerContactUpdateSuite) newMessengerWithKey(shh types.Waku, privateKey *ecdsa.PrivateKey) *Messenger {
tmpFile, err := ioutil.TempFile("", "")
s.Require().NoError(err)
options := []Option{
WithCustomLogger(s.logger),
WithMessagesPersistenceEnabled(),
WithDatabaseConfig(tmpFile.Name(), "some-key"),
WithDatabaseConfig(":memory:", "some-key"),
WithDatasync(),
}
m, err := NewMessenger(
@ -72,8 +66,6 @@ func (s *MessengerContactUpdateSuite) newMessengerWithKey(shh types.Waku, privat
err = m.Init()
s.Require().NoError(err)
s.tmpFiles = append(s.tmpFiles, tmpFile)
return m
}

View File

@ -3,8 +3,6 @@ package protocol
import (
"context"
"crypto/ecdsa"
"io/ioutil"
"os"
"testing"
"github.com/google/uuid"
@ -15,6 +13,7 @@ import (
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/waku"
)
@ -32,8 +31,7 @@ type MessengerEmojiSuite struct {
// a single Waku service should be shared.
shh types.Waku
tmpFiles []*os.File // files to clean up
logger *zap.Logger
logger *zap.Logger
}
func (s *MessengerEmojiSuite) SetupTest() {
@ -55,13 +53,10 @@ func (s *MessengerEmojiSuite) TearDownTest() {
}
func (s *MessengerEmojiSuite) newMessengerWithKey(shh types.Waku, privateKey *ecdsa.PrivateKey) *Messenger {
tmpFile, err := ioutil.TempFile("", "")
s.Require().NoError(err)
options := []Option{
WithCustomLogger(s.logger),
WithMessagesPersistenceEnabled(),
WithDatabaseConfig(tmpFile.Name(), "some-key"),
WithDatabaseConfig(sqlite.InMemoryPath, "some-key"),
WithDatasync(),
}
installationID := uuid.New().String()
@ -76,8 +71,6 @@ func (s *MessengerEmojiSuite) newMessengerWithKey(shh types.Waku, privateKey *ec
err = m.Init()
s.Require().NoError(err)
s.tmpFiles = append(s.tmpFiles, tmpFile)
return m
}

View File

@ -4,8 +4,6 @@ import (
"context"
"crypto/ecdsa"
"errors"
"io/ioutil"
"os"
"testing"
"github.com/google/uuid"
@ -16,6 +14,7 @@ import (
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/encryption/multidevice"
"github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/waku"
)
@ -35,8 +34,7 @@ type MessengerInstallationSuite struct {
// a single Waku service should be shared.
shh types.Waku
tmpFiles []*os.File // files to clean up
logger *zap.Logger
logger *zap.Logger
}
func (s *MessengerInstallationSuite) SetupTest() {
@ -59,13 +57,10 @@ func (s *MessengerInstallationSuite) TearDownTest() {
}
func (s *MessengerInstallationSuite) newMessengerWithKey(shh types.Waku, privateKey *ecdsa.PrivateKey) *Messenger {
tmpFile, err := ioutil.TempFile("", "")
s.Require().NoError(err)
options := []Option{
WithCustomLogger(s.logger),
WithMessagesPersistenceEnabled(),
WithDatabaseConfig(tmpFile.Name(), "some-key"),
WithDatabaseConfig(sqlite.InMemoryPath, "some-key"),
WithDatasync(),
}
installationID := uuid.New().String()
@ -80,8 +75,6 @@ func (s *MessengerInstallationSuite) newMessengerWithKey(shh types.Waku, private
err = m.Init()
s.Require().NoError(err)
s.tmpFiles = append(s.tmpFiles, tmpFile)
return m
}

View File

@ -2,8 +2,6 @@ package protocol
import (
"crypto/ecdsa"
"io/ioutil"
"os"
"testing"
"github.com/google/uuid"
@ -13,6 +11,7 @@ import (
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/waku"
)
@ -30,8 +29,7 @@ type MessengerMuteSuite struct {
// a single Waku service should be shared.
shh types.Waku
tmpFiles []*os.File // files to clean up
logger *zap.Logger
logger *zap.Logger
}
func (s *MessengerMuteSuite) SetupTest() {
@ -53,13 +51,10 @@ func (s *MessengerMuteSuite) TearDownTest() {
}
func (s *MessengerMuteSuite) newMessengerWithKey(shh types.Waku, privateKey *ecdsa.PrivateKey) *Messenger {
tmpFile, err := ioutil.TempFile("", "")
s.Require().NoError(err)
options := []Option{
WithCustomLogger(s.logger),
WithMessagesPersistenceEnabled(),
WithDatabaseConfig(tmpFile.Name(), "some-key"),
WithDatabaseConfig(sqlite.InMemoryPath, "some-key"),
WithDatasync(),
}
installationID := uuid.New().String()
@ -73,9 +68,6 @@ func (s *MessengerMuteSuite) newMessengerWithKey(shh types.Waku, privateKey *ecd
err = m.Init()
s.Require().NoError(err)
s.tmpFiles = append(s.tmpFiles, tmpFile)
return m
}

View File

@ -6,9 +6,7 @@ import (
"encoding/hex"
"encoding/json"
"errors"
"io/ioutil"
"math/big"
"os"
"strconv"
"strings"
"testing"
@ -63,9 +61,8 @@ type MessengerSuite struct {
privateKey *ecdsa.PrivateKey // private key for the main instance of Messenger
// If one wants to send messages between different instances of Messenger,
// a single Whisper service should be shared.
shh types.Waku
tmpFiles []*os.File // files to clean up
logger *zap.Logger
shh types.Waku
logger *zap.Logger
}
type testNode struct {
@ -111,13 +108,10 @@ func (s *MessengerSuite) SetupTest() {
}
func (s *MessengerSuite) newMessengerWithKey(shh types.Waku, privateKey *ecdsa.PrivateKey) *Messenger {
tmpFile, err := ioutil.TempFile("", "")
s.Require().NoError(err)
options := []Option{
WithCustomLogger(s.logger),
WithMessagesPersistenceEnabled(),
WithDatabaseConfig(tmpFile.Name(), "some-key"),
WithDatabaseConfig(":memory:", "some-key"),
}
if s.enableDataSync {
options = append(options, WithDatasync())
@ -133,8 +127,6 @@ func (s *MessengerSuite) newMessengerWithKey(shh types.Waku, privateKey *ecdsa.P
err = m.Init()
s.Require().NoError(err)
s.tmpFiles = append(s.tmpFiles, tmpFile)
return m
}
@ -146,9 +138,6 @@ func (s *MessengerSuite) newMessenger(shh types.Waku) *Messenger {
func (s *MessengerSuite) TearDownTest() {
s.Require().NoError(s.m.Shutdown())
for _, f := range s.tmpFiles {
_ = os.Remove(f.Name())
}
_ = s.logger.Sync()
}

View File

@ -5,8 +5,6 @@ import (
"crypto/ecdsa"
"encoding/hex"
"errors"
"io/ioutil"
"os"
"testing"
"github.com/google/uuid"
@ -20,6 +18,7 @@ import (
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/pushnotificationclient"
"github.com/status-im/status-go/protocol/pushnotificationserver"
"github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/waku"
)
@ -40,9 +39,8 @@ type MessengerPushNotificationSuite struct {
privateKey *ecdsa.PrivateKey // private key for the main instance of Messenger
// If one wants to send messages between different instances of Messenger,
// a single Waku service should be shared.
shh types.Waku
tmpFiles []*os.File // files to clean up
logger *zap.Logger
shh types.Waku
logger *zap.Logger
}
func (s *MessengerPushNotificationSuite) SetupTest() {
@ -61,9 +59,6 @@ func (s *MessengerPushNotificationSuite) SetupTest() {
func (s *MessengerPushNotificationSuite) TearDownTest() {
s.Require().NoError(s.m.Shutdown())
for _, f := range s.tmpFiles {
_ = os.Remove(f.Name())
}
_ = s.logger.Sync()
}
@ -83,13 +78,10 @@ func (s *MessengerPushNotificationSuite) newMessengerWithOptions(shh types.Waku,
}
func (s *MessengerPushNotificationSuite) newMessengerWithKey(shh types.Waku, privateKey *ecdsa.PrivateKey) *Messenger {
tmpFile, err := ioutil.TempFile("", "")
s.Require().NoError(err)
options := []Option{
WithCustomLogger(s.logger),
WithMessagesPersistenceEnabled(),
WithDatabaseConfig(tmpFile.Name(), ""),
WithDatabaseConfig(sqlite.InMemoryPath, ""),
WithDatasync(),
WithPushNotifications(),
}
@ -105,9 +97,6 @@ func (s *MessengerPushNotificationSuite) newMessenger(shh types.Waku) *Messenger
func (s *MessengerPushNotificationSuite) newPushNotificationServer(shh types.Waku, privateKey *ecdsa.PrivateKey) *Messenger {
tmpFile, err := ioutil.TempFile("", "")
s.Require().NoError(err)
serverConfig := &pushnotificationserver.Config{
Enabled: true,
Logger: s.logger,
@ -117,7 +106,7 @@ func (s *MessengerPushNotificationSuite) newPushNotificationServer(shh types.Wak
options := []Option{
WithCustomLogger(s.logger),
WithMessagesPersistenceEnabled(),
WithDatabaseConfig(tmpFile.Name(), "some-key"),
WithDatabaseConfig(sqlite.InMemoryPath, "some-key"),
WithPushNotificationServerConfig(serverConfig),
WithDatasync(),
}

View File

@ -25,7 +25,7 @@ const defaultKdfIterationsNumber = 64000 // nolint: deadcode,varcheck,unused
// https://notes.status.im/i8Y_l7ccTiOYq09HVgoFwA
const reducedKdfIterationsNumber = 3200
const inMemoryPath = ":memory:"
const InMemoryPath = ":memory:"
// MigrationConfig is a struct that allows to define bindata migrations.
type MigrationConfig struct {
@ -42,7 +42,7 @@ func Open(path, key string) (*sql.DB, error) {
// OpenInMemory opens an in memory SQLite database.
// Number of KDF iterations is reduced to 0.
func OpenInMemory() (*sql.DB, error) {
return open(inMemoryPath, "", 0)
return open(InMemoryPath, "", 0)
}
// OpenWithIter allows to open a new database with a custom number of kdf iterations.
@ -52,7 +52,7 @@ func OpenWithIter(path, key string, kdfIter int) (*sql.DB, error) {
}
func open(path string, key string, kdfIter int) (*sql.DB, error) {
if path != inMemoryPath {
if path != InMemoryPath {
_, err := os.OpenFile(path, os.O_CREATE, 0600)
if err != nil {
return nil, err