@@ -85,7 +85,7 @@ func TestNewNtp_Timeout(t *testing.T) {
8585 }
8686}
8787
88- func TestOffset_Cache (t * testing.T ) {
88+ func TestOffset_Interval (t * testing.T ) {
8989 testCases := []struct {
9090 name string
9191 interval uint
@@ -113,12 +113,11 @@ func TestOffset_Cache(t *testing.T) {
113113 expectedUpdatedAt : mustParse ("2006-01-02 15:04:05" , "2022-09-28 16:02:45" ), // same as now
114114 },
115115 {
116- name : "request before interval should use cached value " ,
116+ name : "request before interval should return interval error " ,
117117 now : nowMock ("2022-09-28 16:02:45" ),
118118 updatedAt : mustParse ("2006-01-02 15:04:05" , "2022-09-28 15:52:45" ),
119119 pool : []string {"one" , "two" },
120- offset : 20 * time .Millisecond ,
121- expectedOffset : 20 * time .Millisecond ,
120+ expectedError : ErrNotInInterval ,
122121 expectedUpdatedAt : mustParse ("2006-01-02 15:04:05" , "2022-09-28 15:52:45" ),
123122 },
124123 {
@@ -131,17 +130,25 @@ func TestOffset_Cache(t *testing.T) {
131130 expectedOffset : 30 * time .Millisecond ,
132131 expectedUpdatedAt : mustParse ("2006-01-02 15:04:05" , "2022-09-28 16:02:45" ), // same as now
133132 },
133+ {
134+ name : "request with query error should update updatedAt" ,
135+ now : nowMock ("2022-09-28 16:02:45" ),
136+ updatedAt : mustParse ("2006-01-02 15:04:05" , "2022-09-28 15:42:45" ),
137+ pool : []string {"one" },
138+ ntpResponses : []ntpResp {{nil , errors .New ("this is an error" )}},
139+ expectedError : ErrGettingNtpOffset ,
140+ expectedUpdatedAt : mustParse ("2006-01-02 15:04:05" , "2022-09-28 16:02:45" ), // same as now
141+ },
134142 }
135143 for _ , testCase := range testCases {
136144 t .Run (testCase .name , func (t * testing.T ) {
137145 ntpMonitor := NewNtp (testCase .pool , testCase .timeout , testCase .interval )
138146 ntpMonitor .ntpQuery = ntpQueryMock (testCase .ntpResponses ... )
139147 ntpMonitor .now = testCase .now
140148 ntpMonitor .updatedAt = testCase .updatedAt
141- ntpMonitor .offset = testCase .offset
142149 offset , err := ntpMonitor .Offset ()
143150 assert .Equal (t , testCase .expectedOffset , offset )
144- assert .Equal (t , testCase .expectedError , err )
151+ assert .ErrorIs (t , err , testCase .expectedError )
145152 assert .Equal (t , testCase .expectedUpdatedAt , ntpMonitor .updatedAt )
146153 })
147154 }
0 commit comments