2018-05-04 08:23:38 +00:00
|
|
|
package timesource
|
|
|
|
|
|
|
|
import (
|
2024-10-24 14:35:01 +00:00
|
|
|
"context"
|
2018-05-04 08:23:38 +00:00
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/beevik/ntp"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-05-22 15:38:38 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-05-04 08:23:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// clockCompareDelta declares time required between multiple calls to time.Now
|
2018-05-22 10:26:03 +00:00
|
|
|
clockCompareDelta = 100 * time.Microsecond
|
2018-05-04 08:23:38 +00:00
|
|
|
)
|
|
|
|
|
2018-05-15 14:10:51 +00:00
|
|
|
// we don't user real servers for tests, but logic depends on
|
|
|
|
// actual number of involved NTP servers.
|
2018-05-15 15:59:25 +00:00
|
|
|
var mockedServers = []string{"ntp1", "ntp2", "ntp3", "ntp4"}
|
2018-05-15 14:10:51 +00:00
|
|
|
|
2018-05-04 08:23:38 +00:00
|
|
|
type testCase struct {
|
2018-05-09 05:10:48 +00:00
|
|
|
description string
|
2018-05-15 14:10:51 +00:00
|
|
|
servers []string
|
2018-05-09 05:10:48 +00:00
|
|
|
allowedFailures int
|
|
|
|
responses []queryResponse
|
|
|
|
expected time.Duration
|
|
|
|
expectError bool
|
2018-05-04 08:23:38 +00:00
|
|
|
|
|
|
|
// actual attempts are mutable
|
|
|
|
mu sync.Mutex
|
|
|
|
actualAttempts int
|
|
|
|
}
|
|
|
|
|
2018-05-09 05:10:48 +00:00
|
|
|
func (tc *testCase) query(string, ntp.QueryOptions) (*ntp.Response, error) {
|
2018-05-04 08:23:38 +00:00
|
|
|
tc.mu.Lock()
|
|
|
|
defer func() {
|
|
|
|
tc.actualAttempts++
|
|
|
|
tc.mu.Unlock()
|
|
|
|
}()
|
2018-06-07 14:31:36 +00:00
|
|
|
response := &ntp.Response{
|
|
|
|
ClockOffset: tc.responses[tc.actualAttempts].Offset,
|
|
|
|
Stratum: 1,
|
|
|
|
}
|
2018-05-04 08:23:38 +00:00
|
|
|
return response, tc.responses[tc.actualAttempts].Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestCases() []*testCase {
|
|
|
|
return []*testCase{
|
|
|
|
{
|
|
|
|
description: "SameResponse",
|
2018-05-15 14:10:51 +00:00
|
|
|
servers: mockedServers,
|
2018-05-04 08:23:38 +00:00
|
|
|
responses: []queryResponse{
|
|
|
|
{Offset: 10 * time.Second},
|
|
|
|
{Offset: 10 * time.Second},
|
|
|
|
{Offset: 10 * time.Second},
|
2018-05-15 15:59:25 +00:00
|
|
|
{Offset: 10 * time.Second},
|
2018-05-04 08:23:38 +00:00
|
|
|
},
|
|
|
|
expected: 10 * time.Second,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Median",
|
2018-05-15 14:10:51 +00:00
|
|
|
servers: mockedServers,
|
2018-05-04 08:23:38 +00:00
|
|
|
responses: []queryResponse{
|
|
|
|
{Offset: 10 * time.Second},
|
|
|
|
{Offset: 20 * time.Second},
|
2018-05-15 15:59:25 +00:00
|
|
|
{Offset: 20 * time.Second},
|
2018-05-04 08:23:38 +00:00
|
|
|
{Offset: 30 * time.Second},
|
|
|
|
},
|
|
|
|
expected: 20 * time.Second,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "EvenMedian",
|
2018-05-15 14:10:51 +00:00
|
|
|
servers: mockedServers[:2],
|
2018-05-04 08:23:38 +00:00
|
|
|
responses: []queryResponse{
|
|
|
|
{Offset: 10 * time.Second},
|
|
|
|
{Offset: 20 * time.Second},
|
|
|
|
},
|
|
|
|
expected: 15 * time.Second,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Error",
|
2018-05-15 14:10:51 +00:00
|
|
|
servers: mockedServers,
|
2018-05-04 08:23:38 +00:00
|
|
|
responses: []queryResponse{
|
|
|
|
{Offset: 10 * time.Second},
|
|
|
|
{Error: errors.New("test")},
|
|
|
|
{Offset: 30 * time.Second},
|
2018-05-15 15:59:25 +00:00
|
|
|
{Offset: 30 * time.Second},
|
2018-05-04 08:23:38 +00:00
|
|
|
},
|
|
|
|
expected: time.Duration(0),
|
|
|
|
expectError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "MultiError",
|
2018-05-15 14:10:51 +00:00
|
|
|
servers: mockedServers,
|
2018-05-04 08:23:38 +00:00
|
|
|
responses: []queryResponse{
|
|
|
|
{Error: errors.New("test 1")},
|
|
|
|
{Error: errors.New("test 2")},
|
|
|
|
{Error: errors.New("test 3")},
|
2018-05-15 15:59:25 +00:00
|
|
|
{Error: errors.New("test 3")},
|
2018-05-04 08:23:38 +00:00
|
|
|
},
|
|
|
|
expected: time.Duration(0),
|
|
|
|
expectError: true,
|
|
|
|
},
|
2018-05-09 05:10:48 +00:00
|
|
|
{
|
|
|
|
description: "TolerableError",
|
2018-05-15 14:10:51 +00:00
|
|
|
servers: mockedServers,
|
2018-05-09 05:10:48 +00:00
|
|
|
allowedFailures: 1,
|
|
|
|
responses: []queryResponse{
|
|
|
|
{Offset: 10 * time.Second},
|
|
|
|
{Error: errors.New("test")},
|
2018-05-15 15:59:25 +00:00
|
|
|
{Offset: 20 * time.Second},
|
2018-05-09 05:10:48 +00:00
|
|
|
{Offset: 30 * time.Second},
|
|
|
|
},
|
|
|
|
expected: 20 * time.Second,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "NonTolerableError",
|
2018-05-15 14:10:51 +00:00
|
|
|
servers: mockedServers,
|
2018-05-09 05:10:48 +00:00
|
|
|
allowedFailures: 1,
|
|
|
|
responses: []queryResponse{
|
|
|
|
{Offset: 10 * time.Second},
|
|
|
|
{Error: errors.New("test")},
|
|
|
|
{Error: errors.New("test")},
|
2018-05-15 15:59:25 +00:00
|
|
|
{Error: errors.New("test")},
|
2018-05-09 05:10:48 +00:00
|
|
|
},
|
|
|
|
expected: time.Duration(0),
|
|
|
|
expectError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "AllFailed",
|
2018-05-15 14:10:51 +00:00
|
|
|
servers: mockedServers,
|
2018-05-15 15:59:25 +00:00
|
|
|
allowedFailures: 4,
|
2018-05-09 05:10:48 +00:00
|
|
|
responses: []queryResponse{
|
|
|
|
{Error: errors.New("test")},
|
|
|
|
{Error: errors.New("test")},
|
|
|
|
{Error: errors.New("test")},
|
2018-05-15 15:59:25 +00:00
|
|
|
{Error: errors.New("test")},
|
2018-05-09 05:10:48 +00:00
|
|
|
},
|
|
|
|
expected: time.Duration(0),
|
|
|
|
expectError: true,
|
|
|
|
},
|
2018-05-15 15:59:25 +00:00
|
|
|
{
|
|
|
|
description: "HalfTolerable",
|
|
|
|
servers: mockedServers,
|
|
|
|
allowedFailures: 2,
|
|
|
|
responses: []queryResponse{
|
|
|
|
{Offset: 10 * time.Second},
|
|
|
|
{Offset: 20 * time.Second},
|
|
|
|
{Error: errors.New("test")},
|
|
|
|
{Error: errors.New("test")},
|
|
|
|
},
|
|
|
|
expected: 15 * time.Second,
|
|
|
|
},
|
2018-05-04 08:23:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestComputeOffset(t *testing.T) {
|
|
|
|
for _, tc := range newTestCases() {
|
|
|
|
t.Run(tc.description, func(t *testing.T) {
|
2018-05-15 14:10:51 +00:00
|
|
|
offset, err := computeOffset(tc.query, tc.servers, tc.allowedFailures)
|
2018-05-04 08:23:38 +00:00
|
|
|
if tc.expectError {
|
|
|
|
assert.Error(t, err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, tc.expected, offset)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNTPTimeSource(t *testing.T) {
|
|
|
|
for _, tc := range newTestCases() {
|
|
|
|
t.Run(tc.description, func(t *testing.T) {
|
|
|
|
source := &NTPTimeSource{
|
2018-05-15 14:10:51 +00:00
|
|
|
servers: tc.servers,
|
2018-05-09 05:10:48 +00:00
|
|
|
allowedFailures: tc.allowedFailures,
|
|
|
|
timeQuery: tc.query,
|
2024-08-23 19:30:58 +00:00
|
|
|
now: time.Now,
|
2018-05-04 08:23:38 +00:00
|
|
|
}
|
|
|
|
assert.WithinDuration(t, time.Now(), source.Now(), clockCompareDelta)
|
2018-05-22 15:38:38 +00:00
|
|
|
err := source.updateOffset()
|
|
|
|
if tc.expectError {
|
|
|
|
assert.Equal(t, errUpdateOffset, err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2018-05-04 08:23:38 +00:00
|
|
|
assert.WithinDuration(t, time.Now().Add(tc.expected), source.Now(), clockCompareDelta)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-05-22 15:38:38 +00:00
|
|
|
|
|
|
|
func TestRunningPeriodically(t *testing.T) {
|
|
|
|
var hits int
|
|
|
|
var mu sync.RWMutex
|
|
|
|
periods := make([]time.Duration, 0)
|
|
|
|
|
|
|
|
tc := newTestCases()[0]
|
|
|
|
fastHits := 3
|
|
|
|
slowHits := 1
|
|
|
|
|
|
|
|
t.Run(tc.description, func(t *testing.T) {
|
|
|
|
source := &NTPTimeSource{
|
|
|
|
servers: tc.servers,
|
|
|
|
allowedFailures: tc.allowedFailures,
|
|
|
|
timeQuery: tc.query,
|
|
|
|
fastNTPSyncPeriod: time.Duration(fastHits*10) * time.Millisecond,
|
|
|
|
slowNTPSyncPeriod: time.Duration(slowHits*10) * time.Millisecond,
|
2024-08-23 19:30:58 +00:00
|
|
|
now: time.Now,
|
2018-05-22 15:38:38 +00:00
|
|
|
}
|
|
|
|
lastCall := time.Now()
|
|
|
|
// we're simulating a calls to updateOffset, testing ntp calls happens
|
|
|
|
// on NTPTimeSource specified periods (fastNTPSyncPeriod & slowNTPSyncPeriod)
|
2023-11-13 19:06:32 +00:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
2024-10-24 14:35:01 +00:00
|
|
|
source.runPeriodically(context.TODO(), func() error {
|
2018-05-22 15:38:38 +00:00
|
|
|
mu.Lock()
|
|
|
|
periods = append(periods, time.Since(lastCall))
|
|
|
|
mu.Unlock()
|
|
|
|
hits++
|
|
|
|
if hits < 3 {
|
|
|
|
return errUpdateOffset
|
|
|
|
}
|
|
|
|
if hits == 6 {
|
2023-11-13 19:06:32 +00:00
|
|
|
wg.Done()
|
2018-05-22 15:38:38 +00:00
|
|
|
}
|
|
|
|
return nil
|
2023-11-13 19:06:32 +00:00
|
|
|
}, false)
|
2018-05-22 15:38:38 +00:00
|
|
|
|
2023-11-13 19:06:32 +00:00
|
|
|
wg.Wait()
|
2018-05-22 15:38:38 +00:00
|
|
|
|
|
|
|
mu.Lock()
|
|
|
|
require.Len(t, periods, 6)
|
|
|
|
defer mu.Unlock()
|
|
|
|
prev := 0
|
|
|
|
for _, period := range periods[1:3] {
|
|
|
|
p := int(period.Seconds() * 100)
|
|
|
|
require.True(t, fastHits <= (p-prev))
|
|
|
|
prev = p
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, period := range periods[3:] {
|
|
|
|
p := int(period.Seconds() * 100)
|
|
|
|
require.True(t, slowHits <= (p-prev))
|
|
|
|
prev = p
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2023-07-18 13:35:06 +00:00
|
|
|
|
|
|
|
func TestGetCurrentTimeInMillis(t *testing.T) {
|
|
|
|
invokeTimes := 3
|
|
|
|
numResponses := len(mockedServers) * invokeTimes
|
|
|
|
responseOffset := 10 * time.Second
|
|
|
|
tc := &testCase{
|
|
|
|
servers: mockedServers,
|
|
|
|
responses: make([]queryResponse, numResponses),
|
|
|
|
expected: responseOffset,
|
|
|
|
}
|
|
|
|
for i := range tc.responses {
|
|
|
|
tc.responses[i] = queryResponse{Offset: responseOffset}
|
|
|
|
}
|
|
|
|
|
2024-08-23 19:30:58 +00:00
|
|
|
ts := NTPTimeSource{
|
|
|
|
servers: tc.servers,
|
|
|
|
allowedFailures: tc.allowedFailures,
|
|
|
|
timeQuery: tc.query,
|
|
|
|
slowNTPSyncPeriod: SlowNTPSyncPeriod,
|
|
|
|
now: func() time.Time {
|
|
|
|
return time.Unix(1, 0)
|
|
|
|
},
|
2023-07-18 13:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expectedTime := uint64(11000)
|
2024-08-23 19:30:58 +00:00
|
|
|
n := ts.GetCurrentTimeInMillis()
|
2023-07-18 13:35:06 +00:00
|
|
|
require.Equal(t, expectedTime, n)
|
|
|
|
// test repeat invoke GetCurrentTimeInMillis
|
2024-08-23 19:30:58 +00:00
|
|
|
n = ts.GetCurrentTimeInMillis()
|
2023-07-18 13:35:06 +00:00
|
|
|
require.Equal(t, expectedTime, n)
|
2024-10-23 20:22:51 +00:00
|
|
|
ts.Stop()
|
2023-07-18 13:35:06 +00:00
|
|
|
|
|
|
|
// test invoke after stop
|
2024-08-23 19:30:58 +00:00
|
|
|
n = ts.GetCurrentTimeInMillis()
|
2023-07-18 13:35:06 +00:00
|
|
|
require.Equal(t, expectedTime, n)
|
2024-10-23 20:22:51 +00:00
|
|
|
ts.Stop()
|
2023-07-18 13:35:06 +00:00
|
|
|
}
|
2023-11-13 19:06:32 +00:00
|
|
|
|
|
|
|
func TestGetCurrentTimeOffline(t *testing.T) {
|
|
|
|
// covers https://github.com/status-im/status-desktop/issues/12691
|
2024-08-23 19:30:58 +00:00
|
|
|
ts := &NTPTimeSource{
|
|
|
|
servers: defaultServers,
|
|
|
|
allowedFailures: DefaultMaxAllowedFailures,
|
|
|
|
fastNTPSyncPeriod: 1 * time.Millisecond,
|
|
|
|
slowNTPSyncPeriod: 1 * time.Second,
|
|
|
|
timeQuery: func(string, ntp.QueryOptions) (*ntp.Response, error) {
|
|
|
|
return nil, errors.New("offline")
|
|
|
|
},
|
|
|
|
now: time.Now,
|
2023-11-13 19:06:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ensure there is no "panic: sync: negative WaitGroup counter"
|
|
|
|
// when GetCurrentTime() is invoked more than once when offline
|
2024-08-23 19:30:58 +00:00
|
|
|
_ = ts.GetCurrentTime()
|
|
|
|
_ = ts.GetCurrentTime()
|
2023-11-13 19:06:32 +00:00
|
|
|
}
|