-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathheartbeat_test.go
91 lines (76 loc) · 2.59 KB
/
heartbeat_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) 2025 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 (
"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.GetRecordedHeartbeat(), 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) bridgeNeedsToSendExplicitHeartbeat() error {
return s.t.heartbeat.SetLastHeartbeatSent(time.Now().Add(-24 * time.Hour))
}
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.Values.Rollout = have.Values.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())
}