2023-03-17 10:37:27 +01:00
|
|
|
package peer
|
|
|
|
|
|
|
|
import (
|
2023-04-03 16:59:13 +02:00
|
|
|
"sync"
|
2023-03-17 10:37:27 +01:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2023-04-03 16:59:13 +02:00
|
|
|
type mocListener struct {
|
|
|
|
lastState int
|
|
|
|
wg sync.WaitGroup
|
|
|
|
peers int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *mocListener) OnConnected() {
|
|
|
|
l.lastState = stateConnected
|
|
|
|
l.wg.Done()
|
|
|
|
}
|
|
|
|
func (l *mocListener) OnDisconnected() {
|
|
|
|
l.lastState = stateDisconnected
|
|
|
|
l.wg.Done()
|
|
|
|
}
|
|
|
|
func (l *mocListener) OnConnecting() {
|
|
|
|
l.lastState = stateConnecting
|
|
|
|
l.wg.Done()
|
|
|
|
}
|
|
|
|
func (l *mocListener) OnDisconnecting() {
|
|
|
|
l.lastState = stateDisconnecting
|
|
|
|
l.wg.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *mocListener) OnAddressChanged(host, addr string) {
|
|
|
|
|
|
|
|
}
|
|
|
|
func (l *mocListener) OnPeersListChanged(size int) {
|
|
|
|
l.peers = size
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *mocListener) setWaiter() {
|
|
|
|
l.wg.Add(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *mocListener) wait() {
|
|
|
|
l.wg.Wait()
|
|
|
|
}
|
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
func Test_notifier_serverState(t *testing.T) {
|
|
|
|
|
|
|
|
type scenario struct {
|
|
|
|
name string
|
|
|
|
expected bool
|
|
|
|
mgmState bool
|
|
|
|
signalState bool
|
|
|
|
}
|
|
|
|
scenarios := []scenario{
|
|
|
|
{"connected", true, true, true},
|
|
|
|
{"mgm down", false, false, true},
|
|
|
|
{"signal down", false, true, false},
|
|
|
|
{"disconnected", false, false, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range scenarios {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
n := newNotifier()
|
|
|
|
n.updateServerStates(tt.mgmState, tt.signalState)
|
|
|
|
if n.currentServerState != tt.expected {
|
|
|
|
t.Errorf("invalid serverstate: %t, expected: %t", n.currentServerState, tt.expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-04-03 16:59:13 +02:00
|
|
|
|
|
|
|
func Test_notifier_SetListener(t *testing.T) {
|
|
|
|
listener := &mocListener{}
|
|
|
|
listener.setWaiter()
|
|
|
|
|
|
|
|
n := newNotifier()
|
|
|
|
n.lastNotification = stateConnecting
|
|
|
|
n.setListener(listener)
|
|
|
|
listener.wait()
|
|
|
|
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()
|
|
|
|
n := newNotifier()
|
|
|
|
n.lastNotification = stateConnecting
|
|
|
|
n.setListener(listener)
|
|
|
|
n.removeListener()
|
|
|
|
n.peerListChanged(1)
|
|
|
|
|
|
|
|
if listener.peers != 0 {
|
|
|
|
t.Errorf("invalid state: %d", listener.peers)
|
|
|
|
}
|
|
|
|
}
|