Fix module reboot in updater and reachview mode
This commit is contained in:
parent
e63d7b6f97
commit
8c6c0e12b2
|
@ -0,0 +1,56 @@
|
|||
package reachview
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"forge.cadoles.com/Pyxis/golang-socketio"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
eventReboot = "reboot"
|
||||
)
|
||||
|
||||
// Reboot asks the ReachRS module to reboot
|
||||
func (c *Client) Reboot(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
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package reachview
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"forge.cadoles.com/Pyxis/orion/emlid"
|
||||
)
|
||||
|
||||
func TestClientReboot(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.Reboot(ctx, true); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
defer client.Close()
|
||||
|
||||
}
|
|
@ -7,6 +7,11 @@ var runReachViewIntegrationTests = flag.Bool(
|
|||
"Run the 'ReachView' integration tests (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",
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package emlid
|
||||
package updater
|
||||
|
||||
import (
|
||||
"context"
|
|
@ -1,9 +1,11 @@
|
|||
package emlid
|
||||
package updater
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"forge.cadoles.com/Pyxis/orion/emlid"
|
||||
)
|
||||
|
||||
func TestClientRebootNow(t *testing.T) {
|
||||
|
@ -13,8 +15,8 @@ func TestClientRebootNow(t *testing.T) {
|
|||
}
|
||||
|
||||
client := NewClient(
|
||||
WithStandardLogger(),
|
||||
WithEndpoint(*reachHost, 80),
|
||||
emlid.WithStandardLogger(),
|
||||
emlid.WithEndpoint(*reachHost, 80),
|
||||
)
|
||||
if err := client.Connect(); err != nil {
|
||||
t.Fatal(err)
|
|
@ -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",
|
||||
|
|
|
@ -7,11 +7,6 @@ var runDiscoveryIntegrationTests = flag.Bool(
|
|||
"Run the 'Discovery' integration tests (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",
|
||||
|
|
2
go.mod
2
go.mod
|
@ -4,7 +4,7 @@ require (
|
|||
forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919100209-bb857ced6b95
|
||||
github.com/caarlos0/env v3.4.0+incompatible
|
||||
github.com/cenkalti/backoff v2.0.0+incompatible // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1
|
||||
github.com/go-chi/chi v3.3.3+incompatible
|
||||
github.com/gorilla/websocket v1.4.0 // indirect
|
||||
github.com/grandcat/zeroconf v0.0.0-20180329153754-df75bb3ccae1
|
||||
|
|
Reference in New Issue