Browse Source

Fix module reboot in updater and reachview mode

develop
William Petit 5 months ago
parent
commit
8c6c0e12b2

+ 56
- 0
emlid/reachview/reboot.go View File

@@ -0,0 +1,56 @@
1
+package reachview
2
+
3
+import (
4
+	"context"
5
+	"sync"
6
+
7
+	"forge.cadoles.com/Pyxis/golang-socketio"
8
+	"github.com/pkg/errors"
9
+)
10
+
11
+const (
12
+	eventReboot = "reboot"
13
+)
14
+
15
+// Reboot asks the ReachRS module to reboot
16
+func (c *Client) Reboot(ctx context.Context, waitDisconnect bool) error {
17
+
18
+	var err error
19
+	var wg sync.WaitGroup
20
+
21
+	if waitDisconnect {
22
+
23
+		var once sync.Once
24
+
25
+		done := func() {
26
+			c.Off(gosocketio.OnDisconnection)
27
+			wg.Done()
28
+		}
29
+
30
+		wg.Add(1)
31
+
32
+		go func() {
33
+			<-ctx.Done()
34
+			err = ctx.Err()
35
+			once.Do(done)
36
+		}()
37
+
38
+		err = c.On(gosocketio.OnDisconnection, func(h *gosocketio.Channel) {
39
+			once.Do(done)
40
+		})
41
+		if err != nil {
42
+			return errors.Wrapf(err, "error while binding to '%s' event", gosocketio.OnDisconnection)
43
+		}
44
+	}
45
+
46
+	if err = c.Emit(eventReboot, nil); err != nil {
47
+		return err
48
+	}
49
+
50
+	if waitDisconnect {
51
+		wg.Wait()
52
+	}
53
+
54
+	return err
55
+
56
+}

+ 33
- 0
emlid/reachview/reboot_test.go View File

@@ -0,0 +1,33 @@
1
+package reachview
2
+
3
+import (
4
+	"context"
5
+	"testing"
6
+	"time"
7
+
8
+	"forge.cadoles.com/Pyxis/orion/emlid"
9
+)
10
+
11
+func TestClientReboot(t *testing.T) {
12
+
13
+	if !*runRebootTest {
14
+		t.Skip("To run this test, use: go test -reboot-test")
15
+	}
16
+
17
+	client := NewClient(
18
+		emlid.WithStandardLogger(),
19
+		emlid.WithEndpoint(*reachHost, 80),
20
+	)
21
+	if err := client.Connect(); err != nil {
22
+		t.Fatal(err)
23
+	}
24
+
25
+	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
26
+	defer cancel()
27
+	if err := client.Reboot(ctx, true); err != nil {
28
+		t.Error(err)
29
+	}
30
+
31
+	defer client.Close()
32
+
33
+}

+ 5
- 0
emlid/reachview/util_test.go View File

@@ -7,6 +7,11 @@ var runReachViewIntegrationTests = flag.Bool(
7 7
 	"Run the 'ReachView' integration tests (in addition to the unit tests)",
8 8
 )
9 9
 
10
+var runRebootTest = flag.Bool(
11
+	"reboot-test", false,
12
+	"Run the updater 'Reboot' test (in addition to the unit tests)",
13
+)
14
+
10 15
 var reachHost = flag.String(
11 16
 	"reach-host", "192.168.42.1",
12 17
 	"The Reach module host to use in integration tests",

emlid/reboot_now.go → emlid/updater/reboot_now.go View File

@@ -1,4 +1,4 @@
1
-package emlid
1
+package updater
2 2
 
3 3
 import (
4 4
 	"context"

emlid/reboot_now_test.go → emlid/updater/reboot_now_test.go View File

@@ -1,9 +1,11 @@
1
-package emlid
1
+package updater
2 2
 
3 3
 import (
4 4
 	"context"
5 5
 	"testing"
6 6
 	"time"
7
+
8
+	"forge.cadoles.com/Pyxis/orion/emlid"
7 9
 )
8 10
 
9 11
 func TestClientRebootNow(t *testing.T) {
@@ -13,8 +15,8 @@ func TestClientRebootNow(t *testing.T) {
13 15
 	}
14 16
 
15 17
 	client := NewClient(
16
-		WithStandardLogger(),
17
-		WithEndpoint(*reachHost, 80),
18
+		emlid.WithStandardLogger(),
19
+		emlid.WithEndpoint(*reachHost, 80),
18 20
 	)
19 21
 	if err := client.Connect(); err != nil {
20 22
 		t.Fatal(err)

+ 5
- 0
emlid/updater/util_test.go View File

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

+ 0
- 5
emlid/util_test.go View File

@@ -7,11 +7,6 @@ var runDiscoveryIntegrationTests = flag.Bool(
7 7
 	"Run the 'Discovery' integration tests (in addition to the unit tests)",
8 8
 )
9 9
 
10
-var runRebootTest = flag.Bool(
11
-	"reboot-test", false,
12
-	"Run the updater 'Reboot' test (in addition to the unit tests)",
13
-)
14
-
15 10
 var reachHost = flag.String(
16 11
 	"reach-host", "192.168.42.1",
17 12
 	"The Reach module host to use in integration tests",

+ 1
- 1
go.mod View File

@@ -4,7 +4,7 @@ require (
4 4
 	forge.cadoles.com/Pyxis/golang-socketio v0.0.0-20180919100209-bb857ced6b95
5 5
 	github.com/caarlos0/env v3.4.0+incompatible
6 6
 	github.com/cenkalti/backoff v2.0.0+incompatible // indirect
7
-	github.com/davecgh/go-spew v1.1.1 // indirect
7
+	github.com/davecgh/go-spew v1.1.1
8 8
 	github.com/go-chi/chi v3.3.3+incompatible
9 9
 	github.com/gorilla/websocket v1.4.0 // indirect
10 10
 	github.com/grandcat/zeroconf v0.0.0-20180329153754-df75bb3ccae1

Loading…
Cancel
Save