Allow ConnectToWifiNetwork() to wait for disconnection

This commit is contained in:
wpetit 2018-09-20 10:59:39 +02:00
parent 52bac70174
commit f24f697a3d
2 changed files with 61 additions and 1 deletions

View File

@ -138,11 +138,35 @@ func (u *Updater) RemoveWifiNetwork(ssid string) (bool, error) {
}
// ConnectToWifiNetwork asks the ReachRS module to connect to the given WiFi network
func (u *Updater) ConnectToWifiNetwork(ssid string) error {
func (u *Updater) ConnectToWifiNetwork(ssid string, waitDisconnect bool) error {
var err error
var wg sync.WaitGroup
if waitDisconnect {
wg.Add(1)
err = u.conn.On(gosocketio.OnDisconnection, func(h *gosocketio.Channel) {
u.conn.Off(gosocketio.OnDisconnection)
wg.Done()
})
if err != nil {
return errors.Wrapf(err, "error while binding to '%s' event", gosocketio.OnDisconnection)
}
}
u.logf("sending '%s' event", eventConnectToNetwork)
if err := u.conn.Emit(eventConnectToNetwork, ssid); err != nil {
return errors.Wrapf(err, "error while emitting '%s' event", eventConnectToNetwork)
}
u.logf("'%s' event sent", eventConnectToNetwork)
if waitDisconnect {
wg.Wait()
}
return nil
}

View File

@ -4,6 +4,7 @@ import (
"fmt"
"math/rand"
"testing"
"time"
)
func TestClientSavedWiFiNetworks(t *testing.T) {
@ -83,3 +84,38 @@ func TestClientCRUDWiFiNetwork(t *testing.T) {
defer client.Close()
}
func TestClientWifiNetworkConnect(t *testing.T) {
if !*runUpdaterIntegrationTests {
t.Skip("To run this test, use: go test -updater-integration")
}
client := NewUpdaterClient(
WithStandardLogger(),
WithEndpoint(*reachHost, 80),
)
if err := client.Connect(); err != nil {
t.Fatal(err)
}
ssid := fmt.Sprintf("wifi_test_%d", rand.Uint32())
done, err := client.AddWifiNetwork(ssid, SecurityOpen, "")
if err != nil {
t.Error(err)
}
if g, e := done, true; g != e {
t.Errorf("AddWifiNetwork() -> done: got '%v', expected '%v'", g, e)
}
if err := client.ConnectToWifiNetwork(ssid, true); err != nil {
t.Error(err)
}
time.Sleep(5 * time.Second)
defer client.Close()
}