status-go/mailserver/mailserver_db_postgres_test.go
Samuel Hawksby-Robinson 07e46714f0
Anon Metrics Broadcast (#2198)
* Protobufs and adapters

* Added basic anon metric service and config init

* Added fibonacci interval incrementer

* Added basic Client.Start func and integrated interval incrementer

* Added new processed field to app metrics table

* Added id column to app metrics table

* Added migration clean up

* Added appmetrics GetUnprocessed and SetToProcessedByIDs and tests

There was a wierd bug where metrics in the db that did not explicitly insert a  value would be NULL, so could not be found by . In addition I've added a new primary id field to the app_metrics table so that updates could be done against very specific metric rows.

* Updated adaptors and db to handle proto_id

I need a way to distinguish individual metric items from each other so that I can ignore the ones that have been seen before.

* Moved incrementer into dedicated file

* Resolve incrementer test fail

* Finalised the main loop functionality

* Implemented delete loop framework

* Updated adaptors file name

* Added delete loop delay and quit, and tweak on RawMessage gen

* Completed delete loop logic

* Added DBLock to prevent deletion during mainLoop

* Added postgres DB connection, integrated into anonmetrics.Server

* Removed proto_id from SQL migration and model

* Integrated postgres with Server and updated adaptors

* Function name update

* Added sample config files for client and server

* Fixes and testing for low level e2e

* make generate

* Fix lint

* Fix for receiving an anonMetricBatch not in server mode

* Postgres test fixes

* Tidy up, make vendor and make generate

* delinting

* Fixing database tests

* Attempted fix of does:  cannot open `does' (No such file or directory)
not:   cannot open `not' (No such file or directory)
exist: cannot open `exist' (No such file or directory) error on sql resource loas

* Moved all anon metric postgres migration logic and sources into a the protocol/anonmetrics package or sub packages. I don't know if this will fix the does:  cannot open `does' (No such file or directory)
not:   cannot open `not' (No such file or directory)
exist: cannot open `exist' (No such file or directory) error that happens in Jenkins but this could work

* Lint for the lint god

* Why doesn't the linter list all its problems at once?

* test tweaks

* Fix for wakuV2 change

* DB reset change

* Fix for postgres db migrations fails

* More robust implementation of postgres test setup and teardown

* Added block for anon metrics functionality

* Version Bump to 0.84.0

* Added test to check anon metrics broadcast is deactivated

* Protobufs and adapters

* Added basic anon metric service and config init

* Added new processed field to app metrics table

* Added id column to app metrics table

* Added migration clean up

* Added appmetrics GetUnprocessed and SetToProcessedByIDs and tests

There was a wierd bug where metrics in the db that did not explicitly insert a  value would be NULL, so could not be found by . In addition I've added a new primary id field to the app_metrics table so that updates could be done against very specific metric rows.

* Updated adaptors and db to handle proto_id

I need a way to distinguish individual metric items from each other so that I can ignore the ones that have been seen before.

* Added postgres DB connection, integrated into anonmetrics.Server

* Removed proto_id from SQL migration and model

* Integrated postgres with Server and updated adaptors

* Added sample config files for client and server

* Fix lint

* Fix for receiving an anonMetricBatch not in server mode

* Postgres test fixes

* Tidy up, make vendor and make generate

* Moved all anon metric postgres migration logic and sources into a the protocol/anonmetrics package or sub packages. I don't know if this will fix the does:  cannot open `does' (No such file or directory)
not:   cannot open `not' (No such file or directory)
exist: cannot open `exist' (No such file or directory) error that happens in Jenkins but this could work
2021-09-01 13:02:18 +01:00

130 lines
3.5 KiB
Go

// In order to run these tests, you must run a PostgreSQL database.
//
// Using Docker:
// docker run -e POSTGRES_HOST_AUTH_METHOD=trust -d -p 5432:5432 postgres:9.6-alpine
//
package mailserver
import (
"testing"
"time"
"github.com/stretchr/testify/suite"
"github.com/ethereum/go-ethereum/rlp"
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/postgres"
waku "github.com/status-im/status-go/waku/common"
)
func TestMailServerPostgresDBSuite(t *testing.T) {
suite.Run(t, new(MailServerPostgresDBSuite))
}
type MailServerPostgresDBSuite struct {
suite.Suite
}
func (s *MailServerPostgresDBSuite) SetupSuite() {
// ResetDefaultTestPostgresDB Required to completely reset the Postgres DB
err := postgres.ResetDefaultTestPostgresDB()
s.NoError(err)
}
func (s *MailServerPostgresDBSuite) TestPostgresDB_BuildIteratorWithBloomFilter() {
topic := []byte{0xaa, 0xbb, 0xcc, 0xdd}
db, err := NewPostgresDB(postgres.DefaultTestURI)
s.NoError(err)
defer db.Close()
envelope, err := newTestEnvelope(topic)
s.NoError(err)
err = db.SaveEnvelope(envelope)
s.NoError(err)
iter, err := db.BuildIterator(CursorQuery{
start: NewDBKey(uint32(time.Now().Add(-time.Hour).Unix()), types.BytesToTopic(topic), types.Hash{}).Bytes(),
end: NewDBKey(uint32(time.Now().Add(time.Second).Unix()), types.BytesToTopic(topic), types.Hash{}).Bytes(),
bloom: types.TopicToBloom(types.BytesToTopic(topic)),
limit: 10,
})
s.NoError(err)
hasNext := iter.Next()
s.True(hasNext)
rawValue, err := iter.GetEnvelopeByBloomFilter(nil)
s.NoError(err)
s.NotEmpty(rawValue)
var receivedEnvelope waku.Envelope
err = rlp.DecodeBytes(rawValue, &receivedEnvelope)
s.NoError(err)
s.EqualValues(waku.BytesToTopic(topic), receivedEnvelope.Topic)
err = iter.Release()
s.NoError(err)
s.NoError(iter.Error())
}
func (s *MailServerPostgresDBSuite) TestPostgresDB_BuildIteratorWithTopic() {
topic := []byte{0x01, 0x02, 0x03, 0x04}
db, err := NewPostgresDB(postgres.DefaultTestURI)
s.NoError(err)
defer db.Close()
envelope, err := newTestEnvelope(topic)
s.NoError(err)
err = db.SaveEnvelope(envelope)
s.NoError(err)
iter, err := db.BuildIterator(CursorQuery{
start: NewDBKey(uint32(time.Now().Add(-time.Hour).Unix()), types.BytesToTopic(topic), types.Hash{}).Bytes(),
end: NewDBKey(uint32(time.Now().Add(time.Second).Unix()), types.BytesToTopic(topic), types.Hash{}).Bytes(),
topics: [][]byte{topic},
limit: 10,
})
s.NoError(err)
hasNext := iter.Next()
s.True(hasNext)
rawValue, err := iter.GetEnvelopeByBloomFilter(nil)
s.NoError(err)
s.NotEmpty(rawValue)
var receivedEnvelope waku.Envelope
err = rlp.DecodeBytes(rawValue, &receivedEnvelope)
s.NoError(err)
s.EqualValues(waku.BytesToTopic(topic), receivedEnvelope.Topic)
err = iter.Release()
s.NoError(err)
s.NoError(iter.Error())
}
func newTestEnvelope(topic []byte) (types.Envelope, error) {
privateKey, err := crypto.GenerateKey()
if err != nil {
return nil, err
}
params := waku.MessageParams{
TTL: 10,
PoW: 2.0,
Payload: []byte("hello world"),
WorkTime: 1,
Topic: waku.BytesToTopic(topic),
Dst: &privateKey.PublicKey,
}
message, err := waku.NewSentMessage(&params)
if err != nil {
return nil, err
}
now := time.Now()
envelope, err := message.Wrap(&params, now)
if err != nil {
return nil, err
}
return gethbridge.NewWakuEnvelope(envelope), nil
}