From 5cea35554123c7e3a997a86ba395739538466e67 Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Thu, 8 Oct 2020 16:05:49 +0200 Subject: [PATCH] Use in memory database for messenger tests --- protocol/messenger_contact_update_test.go | 14 +++----------- protocol/messenger_emoji_test.go | 13 +++---------- protocol/messenger_installations_test.go | 13 +++---------- protocol/messenger_mute_test.go | 14 +++----------- protocol/messenger_test.go | 17 +++-------------- protocol/push_notification_test.go | 21 +++++---------------- protocol/sqlite/db.go | 6 +++--- 7 files changed, 23 insertions(+), 75 deletions(-) diff --git a/protocol/messenger_contact_update_test.go b/protocol/messenger_contact_update_test.go index b54f8360d..8e847e7c9 100644 --- a/protocol/messenger_contact_update_test.go +++ b/protocol/messenger_contact_update_test.go @@ -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 } diff --git a/protocol/messenger_emoji_test.go b/protocol/messenger_emoji_test.go index 10c8cd266..3d04edbf8 100644 --- a/protocol/messenger_emoji_test.go +++ b/protocol/messenger_emoji_test.go @@ -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 } diff --git a/protocol/messenger_installations_test.go b/protocol/messenger_installations_test.go index 92c2bccf5..a294da4f3 100644 --- a/protocol/messenger_installations_test.go +++ b/protocol/messenger_installations_test.go @@ -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 } diff --git a/protocol/messenger_mute_test.go b/protocol/messenger_mute_test.go index 3fb6635f3..883bf3822 100644 --- a/protocol/messenger_mute_test.go +++ b/protocol/messenger_mute_test.go @@ -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 } diff --git a/protocol/messenger_test.go b/protocol/messenger_test.go index c3b9dfaac..507f5279d 100644 --- a/protocol/messenger_test.go +++ b/protocol/messenger_test.go @@ -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() } diff --git a/protocol/push_notification_test.go b/protocol/push_notification_test.go index f334ed9b4..975900706 100644 --- a/protocol/push_notification_test.go +++ b/protocol/push_notification_test.go @@ -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(), } diff --git a/protocol/sqlite/db.go b/protocol/sqlite/db.go index 43ec2626d..41b2073fb 100644 --- a/protocol/sqlite/db.go +++ b/protocol/sqlite/db.go @@ -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