From 22cc65347b0f2549b3f406b7bd079fd8d547ef71 Mon Sep 17 00:00:00 2001
From: Jono Wenger <jono@elixxir.io>
Date: Fri, 2 Apr 2021 22:24:15 +0000
Subject: [PATCH] XX-3169 / Net Time

---
 netTime/timeNow.go      | 20 ++++++++++++++++++++
 netTime/timeNow_test.go | 40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+)
 create mode 100644 netTime/timeNow.go
 create mode 100644 netTime/timeNow_test.go

diff --git a/netTime/timeNow.go b/netTime/timeNow.go
new file mode 100644
index 0000000..a024cc7
--- /dev/null
+++ b/netTime/timeNow.go
@@ -0,0 +1,20 @@
+///////////////////////////////////////////////////////////////////////////////
+// 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
diff --git a/netTime/timeNow_test.go b/netTime/timeNow_test.go
new file mode 100644
index 0000000..6c64856
--- /dev/null
+++ b/netTime/timeNow_test.go
@@ -0,0 +1,40 @@
+///////////////////////////////////////////////////////////////////////////////
+// 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)
+	}
+}
-- 
GitLab