Fix module reboot in updater and reachview mode

This commit is contained in:
2019-02-26 20:39:03 +01:00
parent e63d7b6f97
commit 8c6c0e12b2
8 changed files with 106 additions and 10 deletions

View File

@ -0,0 +1,56 @@
package updater
import (
"context"
"sync"
"forge.cadoles.com/Pyxis/golang-socketio"
"github.com/pkg/errors"
)
const (
eventReboot = "reboot now"
)
// RebootNow asks the ReachRS module to reboot now
func (c *Client) RebootNow(ctx context.Context, waitDisconnect bool) error {
var err error
var wg sync.WaitGroup
if waitDisconnect {
var once sync.Once
done := func() {
c.Off(gosocketio.OnDisconnection)
wg.Done()
}
wg.Add(1)
go func() {
<-ctx.Done()
err = ctx.Err()
once.Do(done)
}()
err = c.On(gosocketio.OnDisconnection, func(h *gosocketio.Channel) {
once.Do(done)
})
if err != nil {
return errors.Wrapf(err, "error while binding to '%s' event", gosocketio.OnDisconnection)
}
}
if err = c.Emit(eventReboot, nil); err != nil {
return err
}
if waitDisconnect {
wg.Wait()
}
return err
}

View File

@ -0,0 +1,33 @@
package updater
import (
"context"
"testing"
"time"
"forge.cadoles.com/Pyxis/orion/emlid"
)
func TestClientRebootNow(t *testing.T) {
if !*runRebootTest {
t.Skip("To run this test, use: go test -reboot-test")
}
client := NewClient(
emlid.WithStandardLogger(),
emlid.WithEndpoint(*reachHost, 80),
)
if err := client.Connect(); err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := client.RebootNow(ctx, true); err != nil {
t.Error(err)
}
defer client.Close()
}

View File

@ -12,6 +12,11 @@ var runJoinNetworkTest = flag.Bool(
"Run the updater 'JoinWiFiNetwork' test (in addition to the unit tests)",
)
var runRebootTest = flag.Bool(
"reboot-test", false,
"Run the updater 'Reboot' test (in addition to the unit tests)",
)
var reachHost = flag.String(
"reach-host", "192.168.42.1",
"The Reach module host to use in integration tests",