@@ -27,6 +27,8 @@ func TestApiController_PingHandler(t *testing.T) {
2727 downtimeSaveErr error
2828 calculateDowntimeErr error
2929 downtimeDuration time.Duration
30+ requestTimestamp time.Time
31+ lastPingTimestamp time.Time
3032 }{
3133 {
3234 name : "Returns 200 if downtime calculation fails" ,
@@ -37,26 +39,32 @@ func TestApiController_PingHandler(t *testing.T) {
3739 downtimeSaveCallCount : 0 ,
3840 downtimeDuration : time .Duration (0 ),
3941 calculateDowntimeErr : fmt .Errorf ("ERROR" ),
42+ requestTimestamp : time .Now (),
43+ lastPingTimestamp : time .Now ().Add (- 5 * time .Second ),
4044 },
4145 {
42- name : "Returns 200 if donwtime save fails" ,
46+ name : "Returns 200 if downtime save fails" ,
4347 statusCode : 200 ,
4448 pingSaveCallCount : 1 ,
4549 pingSaveErr : nil ,
4650 downtimeSaveErr : fmt .Errorf ("ERROR" ),
4751 downtimeSaveCallCount : 1 ,
48- downtimeDuration : time .Duration (time .Second * 11 ),
52+ downtimeDuration : time .Duration (time .Second * 19 ),
4953 calculateDowntimeErr : nil ,
54+ requestTimestamp : time .Now (),
55+ lastPingTimestamp : time .Now ().Add (- 19 * time .Second ),
5056 },
5157 {
52- name : "Saves downtime if downtime duration more than 5 seconds" ,
58+ name : "Saves downtime if downtime duration more than 18 seconds" ,
5359 statusCode : 200 ,
5460 pingSaveCallCount : 1 ,
5561 pingSaveErr : nil ,
5662 downtimeSaveErr : nil ,
5763 downtimeSaveCallCount : 1 ,
58- downtimeDuration : time .Duration (time .Second * 11 ),
64+ downtimeDuration : time .Duration (time .Second * 19 ),
5965 calculateDowntimeErr : nil ,
66+ requestTimestamp : time .Now (),
67+ lastPingTimestamp : time .Now ().Add (- 19 * time .Second ),
6068 },
6169 {
6270 name : "Returns 500 if saving ping fails" ,
@@ -67,6 +75,8 @@ func TestApiController_PingHandler(t *testing.T) {
6775 downtimeSaveCallCount : 0 ,
6876 downtimeDuration : time .Duration (time .Second * 8 ),
6977 calculateDowntimeErr : nil ,
78+ requestTimestamp : time .Now (),
79+ lastPingTimestamp : time .Now ().Add (- 5 * time .Second ),
7080 },
7181 {
7282 name : "Returns 200 and does not save downtime if downtime duration less than 5 + 5 seconds" ,
@@ -77,13 +87,14 @@ func TestApiController_PingHandler(t *testing.T) {
7787 downtimeSaveCallCount : 0 ,
7888 downtimeDuration : time .Duration (time .Second * 8 ),
7989 calculateDowntimeErr : nil ,
90+ requestTimestamp : time .Now (),
91+ lastPingTimestamp : time .Now ().Add (- 5 * time .Second ),
8092 },
8193 }
8294
8395 for _ , test := range tests {
8496 t .Run (test .name , func (t * testing.T ) {
8597
86- timestamp := time .Now ()
8798 // create mock controller
8899 nodeRepoMock := mocks.NodeRepository {}
89100 recordRepoMock := mocks.RecordRepository {}
@@ -92,10 +103,10 @@ func TestApiController_PingHandler(t *testing.T) {
92103 pingRepoMock := mocks.PingRepository {}
93104 pingRepoMock .On ("Save" , & models.Ping {
94105 NodeId : "1" ,
95- Timestamp : timestamp ,
106+ Timestamp : test . requestTimestamp ,
96107 }).Return (test .pingSaveErr )
97108 pingRepoMock .On ("CalculateDowntime" , mock .Anything , mock .Anything ).Return (
98- time . Now () , test .downtimeDuration , test .calculateDowntimeErr )
109+ test . lastPingTimestamp , test .downtimeDuration , test .calculateDowntimeErr )
99110
100111 downtimeRepoMock := mocks.DowntimeRepository {}
101112 downtimeRepoMock .On ("Save" , mock .Anything ).Return (test .downtimeSaveErr )
@@ -113,7 +124,7 @@ func TestApiController_PingHandler(t *testing.T) {
113124 req , _ := http .NewRequest ("POST" , "/api/v1/node" , bytes .NewReader (nil ))
114125 c := & auth.RequestContext {
115126 NodeId : "1" ,
116- Timestamp : timestamp ,
127+ Timestamp : test . requestTimestamp ,
117128 }
118129 ctx := context .WithValue (req .Context (), auth .RequestContextKey , c )
119130 req = req .WithContext (ctx )
0 commit comments