feat(GODT-2552): Add functional test.
This commit is contained in:
committed by
Romain Le Jeune
parent
67b5e7f96a
commit
d88bee68c6
72
tests/ctx_heartbeat_test.go
Normal file
72
tests/ctx_heartbeat_test.go
Normal file
@ -0,0 +1,72 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"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() bool {
|
||||
if hb.bridge == nil {
|
||||
return false
|
||||
}
|
||||
return hb.bridge.IsTelemetryAvailable()
|
||||
}
|
||||
|
||||
func (hb *heartbeatRecorder) SendHeartbeat(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) rejectSend() {
|
||||
hb.reject = true
|
||||
}
|
||||
Reference in New Issue
Block a user