status-go/t/benchmarks/mailserver_test.go

118 lines
3.1 KiB
Go
Raw Normal View History

package benchmarks
import (
"context"
"fmt"
"os"
"testing"
"time"
"github.com/ethereum/go-ethereum/node"
"github.com/status-im/status-go/params"
"github.com/status-im/status-go/services/shhext"
whisper "github.com/status-im/whisper/whisperv6"
"github.com/stretchr/testify/require"
)
const (
mailServerPass = "status-offline-inbox"
)
// TestConcurrentMailserverPeers runs `ccyPeers` tests in parallel
// that require messages from a MailServer.
//
// It can be used to test the maximum number of concurrent MailServer peers.
//
// Messages stored by the MailServer must be generated separately.
// Take a look at TestSendMessages test.
func TestConcurrentMailserverPeers(t *testing.T) {
// Request for messages from mail server
for i := 0; i < *ccyPeers; i++ {
t.Run(fmt.Sprintf("Peer #%d", i), testMailserverPeer)
}
}
func testMailserverPeer(t *testing.T) {
t.Parallel()
shhService := createWhisperService()
shhAPI := whisper.NewPublicWhisperAPI(shhService)
config := params.ShhextConfig{
BackupDisabledDataDir: os.TempDir(),
InstallationID: "1",
}
mailService := shhext.New(shhService, nil, nil, config)
shhextAPI := shhext.NewPublicAPI(mailService)
// create node with services
n, err := createNode()
require.NoError(t, err)
err = n.Register(func(_ *node.ServiceContext) (node.Service, error) {
return shhService, nil
})
require.NoError(t, err)
// register mail service as well
err = n.Register(func(_ *node.ServiceContext) (node.Service, error) {
return mailService, nil
})
require.NoError(t, err)
// start node
require.NoError(t, n.Start())
defer func() { require.NoError(t, n.Stop()) }()
// add mail server as a peer
require.NoError(t, addPeerWithConfirmation(n.Server(), peerEnode))
// sym key to decrypt messages
msgSymKeyID, err := shhService.AddSymKeyFromPassword(*msgPass)
require.NoError(t, err)
// prepare new filter for messages from mail server
filterID, err := shhAPI.NewMessageFilter(whisper.Criteria{
SymKeyID: msgSymKeyID,
Topics: []whisper.TopicType{topic},
AllowP2P: true,
})
require.NoError(t, err)
messages, err := shhAPI.GetFilterMessages(filterID)
require.NoError(t, err)
require.Len(t, messages, 0)
// request messages from mail server
symKeyID, err := shhService.AddSymKeyFromPassword(mailServerPass)
require.NoError(t, err)
ok, err := shhAPI.MarkTrustedPeer(context.TODO(), *peerURL)
require.NoError(t, err)
require.True(t, ok)
mailserver pagination (#1039) * mailserver sends envelopes in descending order * add limit value in mailserver request payload * mailserver sends messages up to the limit specified in the request * update Archive method to return key and error * processRequest returns the next page cursor * add cursor to mailserver request * add limit and cursor to request payload * fix request limit encoding * wait for request completed event in TrackerSuite/TestRequestCompleted * add cursor to mailserver response * fix cursor position in payload * add e2e test for mail server pagination * validate mail server response size * remove old limitReached var * fix lint warnings * add whisper patch * fix tests after rebase * check all return values to avoid lint warnings * check that all messages have been retrieved after 2 paginated requests * fix lint warnings * rename geth patch * merge mailserver patches into one * add last envelope hash to mailserver response and EventEnvelopeAvailable event * update whisper patch * add docs to MailServerResponse * update whisper patch * fix tests and lint warnings * send mailserver response data on EventMailServerRequestCompleted signal * update tracker tests * optimise pagination test waiting for mailserver to archive only before requesting * rollback mailserver interface changes * refactoring and docs changes * fix payload size check to determine if a limit is specified * add more docs to the processRequest method * add constants for request payload field lengths * add const noLimits to specify that limit=0 means no limits
2018-07-02 07:38:10 +00:00
requestID, err := shhextAPI.RequestMessages(context.TODO(), shhext.MessagesRequest{
MailServerPeer: *peerURL,
SymKeyID: symKeyID,
Topic: topic,
})
require.NoError(t, err)
mailserver pagination (#1039) * mailserver sends envelopes in descending order * add limit value in mailserver request payload * mailserver sends messages up to the limit specified in the request * update Archive method to return key and error * processRequest returns the next page cursor * add cursor to mailserver request * add limit and cursor to request payload * fix request limit encoding * wait for request completed event in TrackerSuite/TestRequestCompleted * add cursor to mailserver response * fix cursor position in payload * add e2e test for mail server pagination * validate mail server response size * remove old limitReached var * fix lint warnings * add whisper patch * fix tests after rebase * check all return values to avoid lint warnings * check that all messages have been retrieved after 2 paginated requests * fix lint warnings * rename geth patch * merge mailserver patches into one * add last envelope hash to mailserver response and EventEnvelopeAvailable event * update whisper patch * add docs to MailServerResponse * update whisper patch * fix tests and lint warnings * send mailserver response data on EventMailServerRequestCompleted signal * update tracker tests * optimise pagination test waiting for mailserver to archive only before requesting * rollback mailserver interface changes * refactoring and docs changes * fix payload size check to determine if a limit is specified * add more docs to the processRequest method * add constants for request payload field lengths * add const noLimits to specify that limit=0 means no limits
2018-07-02 07:38:10 +00:00
require.NotNil(t, requestID)
// wait for all messages
require.NoError(t, waitForMessages(t, *msgCount, shhAPI, filterID))
}
func waitForMessages(t *testing.T, messagesCount int64, shhAPI *whisper.PublicWhisperAPI, filterID string) error {
received := int64(0)
for range time.After(time.Second) {
messages, err := shhAPI.GetFilterMessages(filterID)
if err != nil {
return err
}
received += int64(len(messages))
fmt.Printf("Received %d messages so far\n", received)
if received >= messagesCount {
return nil
}
}
return nil
}