From 055ee0000d75ffb4b05b61d554556c91fc942d1c Mon Sep 17 00:00:00 2001 From: William Petit Date: Wed, 19 Sep 2018 15:46:15 +0200 Subject: [PATCH] Add ReceiverUpradeAvailable() API --- reach/receiver_upgrade.go | 47 ++++++++++++++++++++++++++++++++++ reach/receiver_upgrade_test.go | 31 ++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 reach/receiver_upgrade.go create mode 100644 reach/receiver_upgrade_test.go diff --git a/reach/receiver_upgrade.go b/reach/receiver_upgrade.go new file mode 100644 index 0000000..26fd400 --- /dev/null +++ b/reach/receiver_upgrade.go @@ -0,0 +1,47 @@ +package reach + +import ( + "sync" + + "forge.cadoles.com/Pyxis/golang-socketio" + "github.com/pkg/errors" +) + +const ( + eventIsReceiverUpgradeAvailable = "is receiver upgrade available" + eventReceiverUpgradeAvailable = "receiver upgrade available" +) + +type receiverUpgreAvailable struct { + Available bool `json:"available"` + Running bool `json:"running"` +} + +// ReceiverUpgradeAvailable checks if an upgrade is avaialable/running for the ReachRS module +func (c *Client) ReceiverUpgradeAvailable() (available bool, running bool, err error) { + + var wg sync.WaitGroup + + wg.Add(1) + + err = c.conn.On(eventReceiverUpgradeAvailable, func(h *gosocketio.Channel, up *receiverUpgreAvailable) { + available = up.Available + running = up.Running + c.conn.Off(eventReceiverUpgradeAvailable) + wg.Done() + }) + if err != nil { + return false, false, errors.Wrapf(err, "error while binding to '%s' event", eventReceiverUpgradeAvailable) + } + + c.logf("sending '%s' event", eventIsReceiverUpgradeAvailable) + if err = c.conn.Emit(eventIsReceiverUpgradeAvailable, nil); err != nil { + return false, false, errors.Wrapf(err, "error while emitting '%s' event", eventIsReceiverUpgradeAvailable) + } + c.logf("'%s' event sent", eventIsReceiverUpgradeAvailable) + + wg.Wait() + + return available, running, err + +} diff --git a/reach/receiver_upgrade_test.go b/reach/receiver_upgrade_test.go new file mode 100644 index 0000000..3560ef1 --- /dev/null +++ b/reach/receiver_upgrade_test.go @@ -0,0 +1,31 @@ +package reach + +import ( + "log" + "testing" +) + +func TestClientReceiverUpgradeAvailable(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) + } + + available, running, err := client.ReceiverUpgradeAvailable() + if err != nil { + t.Error(err) + } + + log.Printf("receiver upgrade result: available: %v, running: %v", available, running) + + defer client.Close() + +}