Add ReceiverUpradeAvailable() API

This commit is contained in:
wpetit 2018-09-19 15:46:15 +02:00
parent 1bf205347e
commit 055ee0000d
2 changed files with 78 additions and 0 deletions

47
reach/receiver_upgrade.go Normal file
View File

@ -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
}

View File

@ -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()
}