GODT-1409 Wait in GetEvents during message preparation for live API.

This commit is contained in:
Jakub
2021-11-19 09:40:57 +01:00
parent 1d9855a190
commit 59075f2e26
6 changed files with 65 additions and 0 deletions

View File

@ -177,3 +177,6 @@ func (ctx *TestContext) SetLastError(err error) {
func (ctx *TestContext) GetLastError() error {
return ctx.lastError
}
func (ctx *TestContext) MessagePreparationStarted() { ctx.pmapiController.LockEvents() }
func (ctx *TestContext) MessagePreparationFinished() { ctx.pmapiController.UnlockEvents() }

View File

@ -42,6 +42,8 @@ type PMAPIController interface {
WasCalled(method, path string, expectedRequest []byte) bool
WasCalledRegex(methodRegex, pathRegex string, expectedRequest []byte) (bool, error)
GetCalls(method, path string) [][]byte
LockEvents()
UnlockEvents()
}
func newPMAPIController(app string, listener listener.Listener) (PMAPIController, pmapi.Manager) {

View File

@ -197,3 +197,9 @@ func (ctl *Controller) GetAuthClient(username string) pmapi.Client {
return nil
}
// LockEvents doesn't needs to be implemented for fakeAPI.
func (ctl *Controller) LockEvents() {}
// UnlockEvents doesn't needs to be implemented for fakeAPI.
func (ctl *Controller) UnlockEvents() {}

26
test/liveapi/events.go Normal file
View File

@ -0,0 +1,26 @@
// Copyright (c) 2021 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.Bridge.
//
// ProtonMail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonMail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
package liveapi
func (ctl *Controller) LockEvents() {
persistentClients.eventsPaused.Add(1)
}
func (ctl *Controller) UnlockEvents() {
persistentClients.eventsPaused.Done()
}

View File

@ -22,6 +22,7 @@ import (
"fmt"
"math/rand"
"os"
"sync"
"github.com/ProtonMail/go-srp"
"github.com/ProtonMail/proton-bridge/internal/constants"
@ -47,6 +48,8 @@ var persistentClients = struct {
manager pmapi.Manager
byName map[string]clientAuthGetter
saltByName map[string]string
eventsPaused sync.WaitGroup
}{}
type persistentClient struct {
@ -69,6 +72,17 @@ func (pc *persistentClient) AuthSalt(_ context.Context) (string, error) {
return persistentClients.saltByName[pc.username], nil
}
// GetEvent needs to wait for preparation to finish. Otherwise messages will be
// in wrong order and test will fail.
func (pc *persistentClient) GetEvent(ctx context.Context, eventID string) (*pmapi.Event, error) {
persistentClients.eventsPaused.Wait()
normalClient, ok := persistentClients.byName[pc.username].(pmapi.Client)
if !ok {
return nil, errors.New("cannot convert to normal client")
}
return normalClient.GetEvent(ctx, eventID)
}
func SetupPersistentClients() {
app := os.Getenv("TEST_APP")

View File

@ -75,6 +75,13 @@ func thereAreMessagesInMailboxesForUser(mailboxNames, bddUserID string, messages
}
func thereAreMessagesInMailboxesForAddressOfUser(mailboxNames, bddAddressID, bddUserID string, messages *godog.Table) error {
// It is needed to prevent event processing before syncing these message
// otherwise the seqID and UID will be in reverse order. The
// synchronization add newest message first, the eventloop adds the oldest
// message first.
ctx.MessagePreparationStarted()
defer ctx.MessagePreparationFinished()
account := ctx.GetTestAccountWithAddress(bddUserID, bddAddressID)
if account == nil {
return godog.ErrPending
@ -263,6 +270,13 @@ func processMailboxStructureDataTable(structure *godog.Table, callback func(stri
}
func thereAreSomeMessagesInMailboxesForAddressOfUser(numberOfMessages int, mailboxNames, bddAddressID, bddUserID string) error {
// It is needed to prevent event processing before syncing these message
// otherwise the seqID and UID will be in reverse order. The
// synchronization add newest message first, the eventloop adds the oldest
// message first.
ctx.MessagePreparationStarted()
defer ctx.MessagePreparationFinished()
account := ctx.GetTestAccountWithAddress(bddUserID, bddAddressID)
if account == nil {
return godog.ErrPending