Finite-state machine in go
Go
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
conf
.gitignore
.travis.yml
LICENSE
README.md
fsm.go
fsm_test.go
reader.go

README.md

fsm

Finite-state machine in go

Sample

Config

{
  "fsm": [
    {
      "name": "fsm1",
      "sets": {
        "status": ["status1", "status2", "status3"],
        "events": ["event1", "event2"],
        "transactions": [
          {"id": 1, "current": "status1", "event": "event1", "target": "status2"},
          {"id": 2, "current": "status2", "event": "event2", "target": "status3"}
        ],
        "transactions_group": [
          {"name": "group1", "transaction_ids": [1]},
          {"name": "group2", "transaction_ids": [2]}
        ]
      }
    },
    {
      "name": "fsm2",
      "sets": {
        "status": ["status1", "status2", "status3"],
        "events": ["event1", "event2"],
        "transactions": [
          {"id": 1, "current": "status1", "event": "event1", "target": "status2"},
          {"id": 2, "current": "status2", "event": "event2", "target": "status3"}
        ],
        "transactions_group": [
          {"name": "group1", "transaction_ids": [1, 2]}
        ]
      }
    },
    {
      "name": "fsm3",
      "sets": {
        "status": ["status1", "status2", "status3"],
        "events": ["event1", "event2"],
        "transactions": [
          {"id": 1, "current": "status1", "event": "event1", "target": "status2"},
          {"id": 2, "current": "status2", "event": "event2", "target": "status3"}
        ],
        "transactions_group": []
      }
    }
  ]
}

TEST

fsm_test.go

func TestFSM(t *testing.T) {
    Convey("failed get target status", t, func() {
        Convey("when system not in conf", func() {
            Convey("will return system not found", func() {
                _, err := GetTargetStatus("fsm", "status1", "event1", "group1")
                So(err, ShouldNotBeNil)
                So(err.Error(), ShouldEqual, "system not found")
            })
        })
        Convey("when status not in system", func() {
            Convey("will return transaction not found", func() {
                _, err := GetTargetStatus("fsm1", "status2", "event1", "group1")
                So(err, ShouldNotBeNil)
                So(err.Error(), ShouldEqual, "transaction not found")
            })
        })
        Convey("when event not in system", func() {
            Convey("will return transaction not found", func() {
                _, err := GetTargetStatus("fsm1", "status1", "event", "group1")
                So(err, ShouldNotBeNil)
                So(err.Error(), ShouldEqual, "transaction not found")
            })
        })
        Convey("when group not in system", func() {
            Convey("will return transaction not found", func() {
                _, err := GetTargetStatus("fsm1", "status1", "event1", "group3")
                So(err, ShouldNotBeNil)
                So(err.Error(), ShouldEqual, "transaction not found")
            })
        })
    })
    Convey("success get target status", t, func() {
        Convey("get test fsm with groups", func() {
            Convey("will return transaction", func() {
                tran, err := GetTargetStatus("fsm1", "status1", "event1", "group1")
                So(err, ShouldBeNil)
                So(tran.TargetStatus, ShouldEqual, "status2")
                tran, err = GetTargetStatus("fsm1", "status2", "event2", "group2", "group3")
                So(err, ShouldBeNil)
                So(tran.TargetStatus, ShouldEqual, "status3")
                tran, err = GetTargetStatus("fsm2", "status1", "event1", "group1")
                So(err, ShouldBeNil)
                So(tran.TargetStatus, ShouldEqual, "status2")
                tran, err = GetTargetStatus("fsm2", "status2", "event2", "group1")
                So(err, ShouldBeNil)
                So(tran.TargetStatus, ShouldEqual, "status3")
            })
        })
        Convey("get test fsm with default groups", func() {
            Convey("will return transaction", func() {
                tran, err := GetTargetStatus("fsm1", "status2", "event2")
                So(err, ShouldBeNil)
                So(tran.TargetStatus, ShouldEqual, "status3")
                tran, err = GetTargetStatus("fsm3", "status1", "event1")
                So(err, ShouldBeNil)
                So(tran.TargetStatus, ShouldEqual, "status2")
                tran, err = GetTargetStatus("fsm3", "status2", "event2")
                So(err, ShouldBeNil)
                So(tran.TargetStatus, ShouldEqual, "status3")
            })
        })
    })
}