mirror of
https://github.com/netbirdio/netbird.git
synced 2026-08-26 17:41:30 +02:00
On mobile the client kept dialing management, signal, relay and peer connections while the device had no usable network at all (airplane mode), burning battery for attempts that cannot succeed. Stopping the engine is not an option: tearing it down destroys the TUN device, and traffic can leak outside the tunnel until it is rebuilt. Add client/netstate, a small gate the platform feeds from its own connectivity callbacks. Every reconnection loop waits on it instead of retrying blindly, and resets its backoff when the network returns so recovery is immediate. The state is injected through functional options and consumers hold a *State that may be nil, so every platform that does not report availability behaves exactly as before. The relay quick-reconnect rechecks availability after its 1.5s wait: the disconnect that triggers it is usually the first symptom of the network going away, so the flag typically arrives while it sleeps. Report the suspension to the UI as well. peer.Listener grows OnStateChanged with a typed ClientState, re-exported across the gomobile boundary as integer constants, and the notifier maps Connecting to a new NoNetwork state while the OS reports no network, so mobile clients can show "no network available" instead of a misleading "connecting". Finally, exit the client retry loop cleanly when its context is cancelled. backoff.WithContext surfaces the bare context error, which callers could not distinguish from a real failure — on Android that turned an engine restart into an unrecoverable error.
118 lines
2.5 KiB
Go
118 lines
2.5 KiB
Go
package peer
|
|
|
|
import (
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
type mocListener struct {
|
|
lastState ClientState
|
|
wg sync.WaitGroup
|
|
peersWg sync.WaitGroup
|
|
peers int
|
|
}
|
|
|
|
func (l *mocListener) OnConnected() {
|
|
l.lastState = ClientStateConnected
|
|
l.wg.Done()
|
|
}
|
|
func (l *mocListener) OnDisconnected() {
|
|
l.lastState = ClientStateDisconnected
|
|
l.wg.Done()
|
|
}
|
|
func (l *mocListener) OnConnecting() {
|
|
l.lastState = ClientStateConnecting
|
|
l.wg.Done()
|
|
}
|
|
func (l *mocListener) OnDisconnecting() {
|
|
l.lastState = ClientStateDisconnecting
|
|
l.wg.Done()
|
|
}
|
|
|
|
func (l *mocListener) OnStateChanged(state ClientState) {
|
|
|
|
}
|
|
func (l *mocListener) OnAddressChanged(host, addr string) {
|
|
|
|
}
|
|
func (l *mocListener) OnPeersListChanged(size int) {
|
|
l.peers = size
|
|
l.peersWg.Done()
|
|
}
|
|
|
|
func (l *mocListener) setWaiter() {
|
|
l.wg.Add(1)
|
|
}
|
|
|
|
func (l *mocListener) wait() {
|
|
l.wg.Wait()
|
|
}
|
|
|
|
func (l *mocListener) setPeersWaiter() {
|
|
l.peersWg.Add(1)
|
|
}
|
|
|
|
func (l *mocListener) waitPeers() {
|
|
l.peersWg.Wait()
|
|
}
|
|
|
|
func Test_notifier_serverState(t *testing.T) {
|
|
|
|
type scenario struct {
|
|
name string
|
|
expected ClientState
|
|
mgmState bool
|
|
signalState bool
|
|
}
|
|
scenarios := []scenario{
|
|
{"connected", ClientStateConnected, true, true},
|
|
{"mgm down", ClientStateConnecting, false, true},
|
|
{"signal down", ClientStateConnecting, true, false},
|
|
{"disconnected", ClientStateDisconnected, false, false},
|
|
}
|
|
|
|
for _, tt := range scenarios {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
n := newNotifier()
|
|
n.updateServerStates(tt.mgmState, tt.signalState)
|
|
if n.lastNotification != tt.expected {
|
|
t.Errorf("invalid serverstate: %d, expected: %d", n.lastNotification, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func Test_notifier_SetListener(t *testing.T) {
|
|
listener := &mocListener{}
|
|
listener.setWaiter()
|
|
listener.setPeersWaiter()
|
|
|
|
n := newNotifier()
|
|
n.lastNotification = ClientStateConnecting
|
|
n.setListener(listener)
|
|
listener.wait()
|
|
listener.waitPeers()
|
|
if listener.lastState != n.lastNotification {
|
|
t.Errorf("invalid state: %d, expected: %d", listener.lastState, n.lastNotification)
|
|
}
|
|
}
|
|
|
|
func Test_notifier_RemoveListener(t *testing.T) {
|
|
listener := &mocListener{}
|
|
listener.setWaiter()
|
|
listener.setPeersWaiter()
|
|
n := newNotifier()
|
|
n.lastNotification = ClientStateConnecting
|
|
n.setListener(listener)
|
|
// setListener replays cached state on a goroutine; wait for both the state
|
|
// and peers callbacks to finish so we don't race on listener.peers.
|
|
listener.wait()
|
|
listener.waitPeers()
|
|
n.removeListener()
|
|
n.peerListChanged(1)
|
|
|
|
if listener.peers != 0 {
|
|
t.Errorf("invalid state: %d", listener.peers)
|
|
}
|
|
}
|