forked from Silverfish/proton-bridge
Ensure that the heartbeat background task is stopped before we close the users as it accesses data within these instances. Additionally, we also make sure that when telemetry is disabled, we stop the background task. Finally, `HeartbeatManager` now specifies what the desired interval is so we can better configure the test cases.
95 lines
2.3 KiB
Go
95 lines
2.3 KiB
Go
// Copyright (c) 2023 Proton AG
|
|
//
|
|
// This file is part of Proton Mail Bridge.
|
|
//
|
|
// Proton Mail 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.
|
|
//
|
|
// Proton Mail 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 Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
package tests
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/ProtonMail/proton-bridge/v3/internal/bridge"
|
|
"github.com/ProtonMail/proton-bridge/v3/internal/telemetry"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
type heartbeatRecorder struct {
|
|
heartbeat telemetry.HeartbeatData
|
|
bridge *bridge.Bridge
|
|
reject bool
|
|
assert *assert.Assertions
|
|
}
|
|
|
|
func newHeartbeatRecorder(tb testing.TB) *heartbeatRecorder {
|
|
return &heartbeatRecorder{
|
|
heartbeat: telemetry.HeartbeatData{},
|
|
bridge: nil,
|
|
reject: false,
|
|
assert: assert.New(tb),
|
|
}
|
|
}
|
|
|
|
func (hb *heartbeatRecorder) setBridge(bridge *bridge.Bridge) {
|
|
hb.bridge = bridge
|
|
}
|
|
|
|
func (hb *heartbeatRecorder) GetLastHeartbeatSent() time.Time {
|
|
if hb.bridge == nil {
|
|
return time.Now()
|
|
}
|
|
return hb.bridge.GetLastHeartbeatSent()
|
|
}
|
|
|
|
func (hb *heartbeatRecorder) IsTelemetryAvailable(ctx context.Context) bool {
|
|
if hb.bridge == nil {
|
|
return false
|
|
}
|
|
return hb.bridge.IsTelemetryAvailable(ctx)
|
|
}
|
|
|
|
func (hb *heartbeatRecorder) SendHeartbeat(_ context.Context, metrics *telemetry.HeartbeatData) bool {
|
|
if hb.bridge == nil {
|
|
return false
|
|
}
|
|
|
|
if len(hb.bridge.GetUserIDs()) == 0 {
|
|
return false
|
|
}
|
|
|
|
if hb.reject {
|
|
return false
|
|
}
|
|
hb.heartbeat = *metrics
|
|
return true
|
|
}
|
|
|
|
func (hb *heartbeatRecorder) SetLastHeartbeatSent(timestamp time.Time) error {
|
|
if hb.bridge == nil {
|
|
return errors.New("no bridge initialized")
|
|
}
|
|
return hb.bridge.SetLastHeartbeatSent(timestamp)
|
|
}
|
|
|
|
func (hb *heartbeatRecorder) GetHeartbeatPeriodicInterval() time.Duration {
|
|
return 200 * time.Millisecond
|
|
}
|
|
|
|
func (hb *heartbeatRecorder) rejectSend() {
|
|
hb.reject = true
|
|
}
|