Skip to content
Snippets Groups Projects
Commit 3536eaee authored by Oliver Herms's avatar Oliver Herms
Browse files

Cleanup

parent 22966a23
Branches
Tags
No related merge requests found
package server
import (
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestConnectStateManualStop(t *testing.T) {
fsm := &FSM2{
eventCh: make(chan int),
connectRetryCounter: 100,
connectRetryTimer: time.NewTimer(time.Second * 120),
}
fsm.startConnectRetryTimer()
fsm.state = newConnectState(fsm)
var wg sync.WaitGroup
var nextState state
var reason string
wg.Add(1)
go func() {
nextState, reason = fsm.state.run()
wg.Done()
}()
fsm.eventCh <- ManualStop
wg.Wait()
assert.IsType(t, &idleState{}, nextState, "Unexpected state returned")
assert.Equalf(t, 0, fsm.connectRetryCounter, "Unexpected resetConnectRetryCounter: %d", fsm.connectRetryCounter)
}
func TestConnectStateConnectRetryTimer(t *testing.T) {
fsm := &FSM2{
eventCh: make(chan int),
connectRetryTimer: time.NewTimer(time.Second * 120),
}
fsm.startConnectRetryTimer()
fsm.state = newConnectState(fsm)
var wg sync.WaitGroup
var nextState state
var reason string
wg.Add(1)
go func() {
fsm.connectRetryTimer = time.NewTimer(time.Duration(0))
nextState, reason = fsm.state.run()
wg.Done()
}()
wg.Wait()
assert.IsType(t, &connectState{}, nextState, "Unexpected state returned")
}
func TestConnectStateConEstablished(t *testing.T) {
fsm := &FSM2{
eventCh: make(chan int),
connectRetryTimer: time.NewTimer(time.Second * 120),
}
fsm.startConnectRetryTimer()
fsm.state = newConnectState(fsm)
var wg sync.WaitGroup
var nextState state
var reason string
wg.Add(1)
go func() {
fsm.connectRetryTimer = time.NewTimer(time.Duration(0))
nextState, reason = fsm.state.run()
wg.Done()
}()
wg.Wait()
assert.IsType(t, &connectState{}, nextState, "Unexpected state returned")
}
package server
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestNewIdleState(t *testing.T) {
tests := []struct {
name string
fsm *FSM2
expected *idleState
}{
{
name: "Test #1",
fsm: &FSM2{},
expected: &idleState{
fsm: &FSM2{},
},
},
}
for _, test := range tests {
res := newIdleState(test.fsm)
assert.Equalf(t, test.expected, res, "Test: %s", test.name)
}
}
func TestStart(t *testing.T) {
tests := []struct {
name string
state *idleState
expected *idleState
}{
{
name: "Test #1",
state: &idleState{
fsm: &FSM2{
connectRetryCounter: 5,
connectRetryTimer: time.NewTimer(time.Second * 20),
},
newStateReason: "Foo Bar",
},
expected: &idleState{
fsm: &FSM2{
connectRetryCounter: 0,
connectRetryTimer: time.NewTimer(time.Second * 20),
},
newStateReason: "Foo Bar",
},
},
}
for _, test := range tests {
if !test.expected.fsm.connectRetryTimer.Stop() {
<-test.expected.fsm.connectRetryTimer.C
}
test.state.start()
assert.Equalf(t, test.expected, test.state, "Test: %s", test.name)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment