forked from Silverfish/proton-bridge
feat(GODT-2552): Add functional test.
This commit is contained in:
committed by
Romain Le Jeune
parent
67b5e7f96a
commit
d88bee68c6
70
tests/heartbeat_test.go
Normal file
70
tests/heartbeat_test.go
Normal file
@ -0,0 +1,70 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/ProtonMail/proton-bridge/v3/internal/telemetry"
|
||||
"github.com/cucumber/godog"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (s *scenario) bridgeEventuallySendsTheFollowingHeartbeat(text *godog.DocString) error {
|
||||
return eventually(func() error {
|
||||
err := s.bridgeSendsTheFollowingHeartbeat(text)
|
||||
logrus.WithError(err).Trace("Matching eventually")
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func (s *scenario) bridgeSendsTheFollowingHeartbeat(text *godog.DocString) error {
|
||||
var wantHeartbeat telemetry.HeartbeatData
|
||||
err := json.Unmarshal([]byte(text.Content), &wantHeartbeat)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return matchHeartbeat(s.t.heartbeat.heartbeat, wantHeartbeat)
|
||||
}
|
||||
|
||||
func (s *scenario) bridgeNeedsToSendHeartbeat() error {
|
||||
last := s.t.heartbeat.GetLastHeartbeatSent()
|
||||
if !isAnotherDay(last, time.Now()) {
|
||||
return fmt.Errorf("heartbeat already sent at %s", last)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *scenario) bridgeDoNotNeedToSendHeartbeat() error {
|
||||
last := s.t.heartbeat.GetLastHeartbeatSent()
|
||||
if isAnotherDay(last, time.Now()) {
|
||||
return fmt.Errorf("heartbeat needs to be sent - last %s", last)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *scenario) heartbeatIsNotwhitelisted() error {
|
||||
s.t.heartbeat.rejectSend()
|
||||
return nil
|
||||
}
|
||||
|
||||
func matchHeartbeat(have, want telemetry.HeartbeatData) error {
|
||||
if have == (telemetry.HeartbeatData{}) {
|
||||
return errors.New("no heartbeat send (yet)")
|
||||
}
|
||||
|
||||
// Ignore rollout number
|
||||
want.Dimensions.Rollout = have.Dimensions.Rollout
|
||||
|
||||
if have != want {
|
||||
return fmt.Errorf("missing heartbeat: have %#v, want %#v", have, want)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func isAnotherDay(last, now time.Time) bool {
|
||||
return now.Year() > last.Year() || (now.Year() == last.Year() && now.YearDay() > last.YearDay())
|
||||
}
|
||||
Reference in New Issue
Block a user