示例#1
0
func Test_RunSimulation(t *testing.T) {
	var m sm.Machine
	err := json.Unmarshal([]byte(`{
		"name" : "Example",
		"states" : [ "idle", "working" ],
		"transitions" : {
			"idle" : { "working" : [ { "type" : "deterministic", "parameter" : 1 } ] },
			"working" : { "idle" : [ { "type" : "deterministic", "parameter" : 1 } ] }
		}
	}`), &m)
	assert.Nil(t, err)

	e := engine.NewEnvironment()
	e.AddMachine(hps.IMachine(&m))

	hits := counters.Hits{}
	working, _ := m.GetState("working")
	working.Entering(hits.StateHandler())

	limits := engine.NewLimits()
	limits.Time = 1.0
	e.Run(limits)

	assert.NotEqual(t, 0, hits.Value)
}
示例#2
0
func Test_DeterministicStateMachine(t *testing.T) {
	var m sm.Machine
	err := json.Unmarshal([]byte(`{
		"name" : "test",
		"machine" : "default",
		"states" : [ "ok", "fail" ],
		"transitions" : {
			"ok" : { "fail" : [ { "type" : "deterministic", "parameter" : 1 } ] },
			"fail" : { "ok" : [ { "type" : "deterministic", "parameter" : 1 } ] }
		}
	}`), &m)
	assert.Nil(t, err)

	ok, _ := m.GetState("ok")
	fail, _ := m.GetState("fail")

	assert.Equal(t, 2, len(m.States()), "Machine should have two states.")
	assert.Equal(t, "ok", m.State().Name(), "Initial state should be 'ok'.")
	assert.Equal(t, 1, len(ok.Transitions()), "Transition from 'ok' should be defined.")
	assert.Equal(t, "fail", ok.Transitions()[0].To(), "Transition from 'ok' to 'fail' should be defined.")
	assert.Equal(t, 1, len(fail.Transitions()), "Transition from 'fail' should be defined.")
	assert.Equal(t, "ok", fail.Transitions()[0].To(), "Transition from 'fail' to 'ok' should be defined.")

	time := 0.0

	event, _ := m.Event(time)
	assert.NotNil(t, event)

	smEvent, _ := event.(*sm.Event)
	assert.Equal(t, "ok", smEvent.From().Name())
	assert.Equal(t, "fail", smEvent.To().Name())

	time = event.Invoke()
	assert.Equal(t, "fail", m.State().Name())

	event, _ = m.Event(time)
	assert.NotNil(t, event)

	smEvent, _ = event.(*sm.Event)
	assert.Equal(t, "fail", smEvent.From().Name())
	assert.Equal(t, "ok", smEvent.To().Name())
}