From 1bf205347e3bb1c635a53c0cc0650710951da8e4 Mon Sep 17 00:00:00 2001 From: William Petit Date: Wed, 19 Sep 2018 15:35:56 +0200 Subject: [PATCH] Add TimeSyncStatus() API --- reach/time_sync.go | 47 +++++++++++++++++++++++++++++++++++++++++ reach/time_sync_test.go | 31 +++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 reach/time_sync.go create mode 100644 reach/time_sync_test.go diff --git a/reach/time_sync.go b/reach/time_sync.go new file mode 100644 index 0000000..37f0378 --- /dev/null +++ b/reach/time_sync.go @@ -0,0 +1,47 @@ +package reach + +import ( + "sync" + + "forge.cadoles.com/Pyxis/golang-socketio" + "github.com/pkg/errors" +) + +const ( + eventGetTimeSyncStatus = "get time sync status" + eventTimeSyncResults = "time sync status" +) + +type timeSyncStatus struct { + Status bool `json:"status"` +} + +// TimeSyncStatus returns the ReachRS module time sync status +func (c *Client) TimeSyncStatus() (bool, error) { + + var err error + var synced bool + var wg sync.WaitGroup + + wg.Add(1) + + err = c.conn.On(eventTimeSyncResults, func(h *gosocketio.Channel, st *timeSyncStatus) { + synced = st.Status + c.conn.Off(eventTimeSyncResults) + wg.Done() + }) + if err != nil { + return false, errors.Wrapf(err, "error while binding to '%s' event", eventTimeSyncResults) + } + + c.logf("sending '%s' event", eventGetTimeSyncStatus) + if err = c.conn.Emit(eventGetTimeSyncStatus, nil); err != nil { + return false, errors.Wrapf(err, "error while emitting '%s' event", eventGetTimeSyncStatus) + } + c.logf("'%s' event sent", eventGetTimeSyncStatus) + + wg.Wait() + + return synced, err + +} diff --git a/reach/time_sync_test.go b/reach/time_sync_test.go new file mode 100644 index 0000000..25afa17 --- /dev/null +++ b/reach/time_sync_test.go @@ -0,0 +1,31 @@ +package reach + +import ( + "log" + "testing" +) + +func TestClientTimeSync(t *testing.T) { + + if !*runIntegrationTests { + t.Skip("To run this test, use: go test -integration") + } + + client := NewClient( + WithStandardLogger(), + WithEndpoint(*reachHost, 80), + ) + if err := client.Connect(); err != nil { + t.Fatal(err) + } + + synced, err := client.TimeSyncStatus() + if err != nil { + t.Error(err) + } + + log.Printf("time sync result: %v", synced) + + defer client.Close() + +}