84 lines
1.9 KiB
Go
84 lines
1.9 KiB
Go
package peer
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestPeerState_ClientInit_initWithIncorrectTraceID(t *testing.T) {
|
|
h := NewPeerStateTestHarness()
|
|
h.ConfigClientInit(t)
|
|
|
|
// Should have sent the first init packet.
|
|
assertEqual(t, len(h.Sent), 1)
|
|
init := assertType[packetInit](t, h.Sent[0].Packet)
|
|
|
|
init.TraceID = newTraceID()
|
|
h.OnInit(controlMsg[packetInit]{Packet: init})
|
|
|
|
assertType[*stateClientInit](t, h.State)
|
|
}
|
|
|
|
func TestPeerState_ClientInit_init(t *testing.T) {
|
|
h := NewPeerStateTestHarness()
|
|
h.ConfigClientInit(t)
|
|
|
|
// Should have sent the first init packet.
|
|
assertEqual(t, len(h.Sent), 1)
|
|
init := assertType[packetInit](t, h.Sent[0].Packet)
|
|
h.OnInit(controlMsg[packetInit]{Packet: init})
|
|
|
|
assertType[*stateClient](t, h.State)
|
|
}
|
|
|
|
func TestPeerState_ClientInit_onPing(t *testing.T) {
|
|
h := NewPeerStateTestHarness()
|
|
h.ConfigClientInit(t)
|
|
|
|
// Should have sent the first init packet.
|
|
assertEqual(t, len(h.Sent), 1)
|
|
h.Sent = h.Sent[:0]
|
|
|
|
for range 3 {
|
|
h.OnPingTimer()
|
|
}
|
|
|
|
assertEqual(t, len(h.Sent), 3)
|
|
|
|
for i := range h.Sent {
|
|
assertType[packetInit](t, h.Sent[i].Packet)
|
|
}
|
|
}
|
|
|
|
func TestPeerState_ClientInit_onPingTimeout(t *testing.T) {
|
|
h := NewPeerStateTestHarness()
|
|
h.ConfigClientInit(t)
|
|
|
|
state := assertType[*stateClientInit](t, h.State)
|
|
state.startedAt = time.Now().Add(-2 * timeoutInterval)
|
|
|
|
h.OnPingTimer()
|
|
|
|
// Should have moved into the client state due to timeout.
|
|
assertType[*stateClient](t, h.State)
|
|
}
|
|
|
|
func TestPeerState_ClientInit_onPeerUpdate(t *testing.T) {
|
|
h := NewPeerStateTestHarness()
|
|
h.ConfigClientInit(t)
|
|
|
|
h.PeerUpdate(nil)
|
|
|
|
// Should have moved into the client state due to timeout.
|
|
assertType[*stateDisconnected](t, h.State)
|
|
}
|
|
|
|
func TestPeerState_ClientInit_ignoreMessage(t *testing.T) {
|
|
h := NewPeerStateTestHarness()
|
|
h.ConfigClientInit(t)
|
|
h.OnProbe(controlMsg[packetProbe]{})
|
|
|
|
// Shouldn't do anything.
|
|
assertType[*stateClientInit](t, h.State)
|
|
}
|