github.com/metaworking/channeld@v0.7.3/pkg/fsm/fsm_test.go (about)

     1  package fsm
     2  
     3  import (
     4  	"io/ioutil"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func loadServerFSM(t *testing.T) FiniteStateMachine {
    11  	bytes, err := ioutil.ReadFile("../../config/server_conn_fsm_test.json")
    12  	if err != nil {
    13  		t.Error(err)
    14  	}
    15  	serverFSM, err := Load(bytes)
    16  	if err != nil {
    17  		t.Error(err)
    18  	}
    19  	return *serverFSM
    20  }
    21  
    22  func TestLoad(t *testing.T) {
    23  	serverFSM := loadServerFSM(t)
    24  	assert.Equal(t, 3, len(serverFSM.States))
    25  	assert.Equal(t, 3, len(serverFSM.Transitions))
    26  	assert.Equal(t, &serverFSM.States[0], serverFSM.CurrentState())
    27  }
    28  
    29  func TestChange(t *testing.T) {
    30  	serverFSM := loadServerFSM(t)
    31  	assert.NoError(t, serverFSM.ChangeState("OPEN"))
    32  	assert.Error(t, serverFSM.ChangeState("BLAH"))
    33  }
    34  
    35  func TestTransitionAndMsgAllowence(t *testing.T) {
    36  	serverFSM := loadServerFSM(t)
    37  	assert.Equal(t, "INIT", serverFSM.CurrentState().Name)
    38  	assert.True(t, serverFSM.IsAllowed(1))
    39  	assert.False(t, serverFSM.IsAllowed(2))
    40  	assert.False(t, serverFSM.IsAllowed(9))
    41  	assert.False(t, serverFSM.IsAllowed(21))
    42  
    43  	serverFSM.OnReceived(1)
    44  	assert.Equal(t, "OPEN", serverFSM.CurrentState().Name)
    45  	assert.False(t, serverFSM.IsAllowed(1))
    46  	assert.True(t, serverFSM.IsAllowed(2))
    47  	assert.False(t, serverFSM.IsAllowed(9))
    48  	assert.True(t, serverFSM.IsAllowed(20))
    49  	assert.False(t, serverFSM.IsAllowed(21))
    50  
    51  	serverFSM.OnReceived(20)
    52  	assert.Equal(t, "HANDOVER", serverFSM.CurrentState().Name)
    53  	assert.False(t, serverFSM.IsAllowed(1))
    54  	assert.False(t, serverFSM.IsAllowed(2))
    55  	assert.True(t, serverFSM.IsAllowed(21))
    56  	assert.False(t, serverFSM.IsAllowed(100))
    57  
    58  	serverFSM.OnReceived(1)
    59  	assert.Equal(t, "HANDOVER", serverFSM.CurrentState().Name)
    60  
    61  	serverFSM.OnReceived(21)
    62  	assert.Equal(t, "HANDOVER", serverFSM.CurrentState().Name)
    63  
    64  	serverFSM.OnReceived(22)
    65  	assert.Equal(t, "OPEN", serverFSM.CurrentState().Name)
    66  }