fix lint errors
Some checks failed
Machine Tests / test (push) Waiting to run
Lint / golangci-lint (push) Has been cancelled

This commit is contained in:
Matthew Rich 2024-04-04 13:06:45 -07:00
parent 944d73f34a
commit 8b92e9e143
6 changed files with 30 additions and 10 deletions

7
go.mod
View File

@ -1,3 +1,10 @@
module gitea.rosskeen.house/rosskeen.house/machine module gitea.rosskeen.house/rosskeen.house/machine
go 1.21.1 go 1.21.1
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

9
go.sum Normal file
View File

@ -0,0 +1,9 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -14,7 +14,7 @@ type Stater interface {
AddTransition(trigger string, source State, dest State) AddTransition(trigger string, source State, dest State)
AddSubscription(transition string, subscription Subscriber) error AddSubscription(transition string, subscription Subscriber) error
AddModel(m Modeler) AddModel(m Modeler)
Trigger(transition string) Trigger(transition string) error
CurrentState() State CurrentState() State
} }
@ -50,8 +50,8 @@ func (d *Definition) AddModel(m Modeler) {
d.model = m d.model = m
} }
func (d *Definition) Trigger(transition string) { func (d *Definition) Trigger(transition string) error {
d.triggers[transition].Run(d.model) return d.triggers[transition].Run(d.model)
} }
func (d *Definition) CurrentState() State { func (d *Definition) CurrentState() State {
@ -61,6 +61,7 @@ func (d *Definition) CurrentState() State {
func (d *Definition) AddSubscription(transition string, subscription Subscriber) error { func (d *Definition) AddSubscription(transition string, subscription Subscriber) error {
if t,ok := d.triggers[transition]; ok { if t,ok := d.triggers[transition]; ok {
t.Subscribe(subscription) t.Subscribe(subscription)
return nil
} }
return errors.New("Transition does not exist") return errors.New("Transition does not exist")
} }

View File

@ -5,6 +5,7 @@ package machine
import( import(
"log" "log"
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func setupStater(initial State) Stater { func setupStater(initial State) Stater {
@ -41,7 +42,8 @@ func TestMachineAddTransition(t *testing.T) {
s.AddTransition("connect", "disconnected", "start_connection") s.AddTransition("connect", "disconnected", "start_connection")
s.AddModel(setupModel("disconnected")) s.AddModel(setupModel("disconnected"))
// worker gets a trigger message // worker gets a trigger message
s.Trigger("connect") assert.Nil(t, s.Trigger("connect"))
// machine generates transition event mesages // machine generates transition event mesages
if s.CurrentState() != "start_connection" { if s.CurrentState() != "start_connection" {
t.Errorf("State transition failed for: connect - %s", s.CurrentState()) t.Errorf("State transition failed for: connect - %s", s.CurrentState())
@ -53,7 +55,7 @@ func TestMachineAddSubscription(t *testing.T) {
s := setupStater("disconnected") s := setupStater("disconnected")
s.AddStates("disconnected", "start_connection", "connected") s.AddStates("disconnected", "start_connection", "connected")
s.AddTransition("connect", "disconnected", "start_connection") s.AddTransition("connect", "disconnected", "start_connection")
s.AddSubscription("connect", x) assert.Nil(t, s.AddSubscription("connect", x))
s.AddModel(setupModel("disconnected")) s.AddModel(setupModel("disconnected"))
s.Trigger("connect") s.Trigger("connect")
exitMessage := <- *x.(*EventChannel) exitMessage := <- *x.(*EventChannel)

View File

@ -3,7 +3,7 @@
package machine package machine
type Modeler interface { type Modeler interface {
Trigger(transition Transitioner) Trigger(transition Transitioner) error
ChangeState(target State) State ChangeState(target State) State
InspectState() State InspectState() State
} }
@ -16,8 +16,8 @@ func NewModel(initial State) Modeler {
return &Model{ state: initial } return &Model{ state: initial }
} }
func (m *Model) Trigger(transition Transitioner) { func (m *Model) Trigger(transition Transitioner) error {
transition.Run(m) return transition.Run(m)
} }
func (m *Model) ChangeState(target State) State { func (m *Model) ChangeState(target State) State {

View File

@ -5,6 +5,7 @@ package machine
import ( import (
"log" "log"
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func setupTransition() Transitioner { func setupTransition() Transitioner {
@ -30,7 +31,7 @@ func TestNewTransition(t *testing.T) {
func TestTransitionExecution(t *testing.T) { func TestTransitionExecution(t *testing.T) {
s := setupTransition() s := setupTransition()
m := setupModel("closed") m := setupModel("closed")
s.Run(m) assert.Nil(t, s.Run(m))
state := m.InspectState() state := m.InspectState()
if state != "open" { if state != "open" {
t.Errorf("Failed to transition state: %s", state) t.Errorf("Failed to transition state: %s", state)
@ -42,7 +43,7 @@ func TestTransitionSubscribe(t *testing.T) {
s := setupTransition() s := setupTransition()
s.Subscribe(c) s.Subscribe(c)
m := setupModel("closed") m := setupModel("closed")
s.Run(m) assert.Nil(t, s.Run(m))
exitEvent := <- *c.(*EventChannel) exitEvent := <- *c.(*EventChannel)
enterEvent := <- *c.(*EventChannel) enterEvent := <- *c.(*EventChannel)
if exitEvent.on != EXITSTATEEVENT { if exitEvent.on != EXITSTATEEVENT {