Skip to content
Snippets Groups Projects
Commit 22cc6534 authored by Jono Wenger's avatar Jono Wenger
Browse files

XX-3169 / Net Time

parent ed23858b
Branches
Tags
No related merge requests found
///////////////////////////////////////////////////////////////////////////////
// Copyright © 2020 xx network SEZC //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file //
///////////////////////////////////////////////////////////////////////////////
// Package netTime provides a custom time function that should provide the
// current accurate time used by the network from a custom time service.
package netTime
import (
"time"
)
type NowFunc func() time.Time
// Now returns the current accurate time. The function must be set an accurate
// time service that returns the current time with an accuracy of +/- 300 ms.
var Now NowFunc = time.Now
///////////////////////////////////////////////////////////////////////////////
// Copyright © 2020 xx network SEZC //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file //
///////////////////////////////////////////////////////////////////////////////
package netTime
import (
"testing"
"time"
)
// Happy path: tests that Now() returns time.Now() if it is unset.
func TestNow(t *testing.T) {
expectedTime := time.Now().Round(time.Millisecond)
receivedTime := Now().Round(time.Millisecond)
if !expectedTime.Equal(receivedTime) {
t.Errorf("Returned incorrect time.\nexpected: %s\nreceived: %s",
expectedTime, receivedTime)
}
}
// Happy path: tests that setting Now works.
func TestNow_Set(t *testing.T) {
expectedTime := time.Now()
testNow := func() time.Time {
return expectedTime
}
Now = testNow
now := Now()
if !Now().Equal(expectedTime) {
t.Errorf("Returned incorrect time.\nexpected: %s\nreceived: %s",
expectedTime, now)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment