Skip to content

clean up and consolidate tests, move to the blocking fsm state channel #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/robbyt/go-supervisor
go 1.24.3

require (
github.com/robbyt/go-fsm v1.3.0
github.com/robbyt/go-fsm v1.4.0
github.com/stretchr/testify v1.10.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robbyt/go-fsm v1.3.0 h1:3Zy6l+FvgO5W+If4qcaKYBAusBKWHBSrRyfenY0uio0=
github.com/robbyt/go-fsm v1.3.0/go.mod h1:6a4xGQG+3RywGY/EVDXGIYoElXmKqX6y/d/uPSlbGZY=
github.com/robbyt/go-fsm v1.4.0 h1:HpZRTBiTDSsjQGtXSJPUk+5tDC/Vg95rmOt0501t1tc=
github.com/robbyt/go-fsm v1.4.0/go.mod h1:6a4xGQG+3RywGY/EVDXGIYoElXmKqX6y/d/uPSlbGZY=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
Expand Down
42 changes: 15 additions & 27 deletions internal/finitestate/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package finitestate
import (
"context"
"log/slog"
"time"

"github.com/robbyt/go-fsm"
)
Expand All @@ -21,35 +22,22 @@ const (
// TypicalTransitions is a set of standard transitions for a finite state machine.
var TypicalTransitions = fsm.TypicalTransitions

// Machine defines the interface for the finite state machine that tracks
// the HTTP server's lifecycle states. This abstraction allows for different
// FSM implementations and simplifies testing.
type Machine interface {
// Transition attempts to transition the state machine to the specified state.
Transition(state string) error

// TransitionBool attempts to transition the state machine to the specified state.
TransitionBool(state string) bool

// TransitionIfCurrentState attempts to transition the state machine to the specified state
TransitionIfCurrentState(currentState, newState string) error

// SetState sets the state of the state machine to the specified state.
SetState(state string) error

// GetState returns the current state of the state machine.
GetState() string

// GetStateChan returns a channel that emits the state machine's state whenever it changes.
// The channel is closed when the provided context is canceled.
GetStateChan(ctx context.Context) <-chan string
// Machine is a wrapper around go-fsm.Machine that provides additional functionality.
type Machine struct {
*fsm.Machine
}

// GetStateChanBuffer returns a channel with a configurable buffer size that emits the state machine's state whenever it changes.
// The channel is closed when the provided context is canceled.
GetStateChanBuffer(ctx context.Context, bufferSize int) <-chan string
// GetStateChan returns a channel that emits the state whenever it changes.
// The channel is closed when the provided context is canceled.
func (s *Machine) GetStateChanWithTimeout(ctx context.Context) <-chan string {
return s.GetStateChanWithOptions(ctx, fsm.WithSyncTimeout(5*time.Second))
}

// New creates a new finite state machine with the specified logger using "standard" state transitions.
func New(handler slog.Handler) (*fsm.Machine, error) {
return fsm.New(handler, StatusNew, TypicalTransitions)
func New(handler slog.Handler) (*Machine, error) {
f, err := fsm.New(handler, StatusNew, TypicalTransitions)
if err != nil {
return nil, err
}
return &Machine{Machine: f}, nil
}
141 changes: 137 additions & 4 deletions internal/finitestate/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ func TestNew(t *testing.T) {
func TestMachineInterface(t *testing.T) {
t.Parallel()

setup := func() Machine {
setup := func() *Machine {
handler := slog.NewTextHandler(os.Stdout, nil)
machine, err := New(handler)
m, err := New(handler)
require.NoError(t, err)
return machine
return m
}

t.Run("Transition changes state", func(t *testing.T) {
Expand Down Expand Up @@ -136,7 +136,7 @@ func TestMachineInterface(t *testing.T) {
assert.Equal(t, StatusBooting, machine.GetState())

// Set up context with timeout
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
defer cancel()

// Set up the channel to receive state updates
Expand Down Expand Up @@ -198,3 +198,136 @@ func TestTypicalTransitions(t *testing.T) {
assert.Equal(t, fsm.StatusUnknown, StatusUnknown)
})
}

func TestGetStateChanWithTimeout(t *testing.T) {
t.Parallel()

setup := func() *Machine {
handler := slog.NewTextHandler(os.Stdout, nil)
m, err := New(handler)
require.NoError(t, err)
return m
}

t.Run("provides state updates with timeout", func(t *testing.T) {
machine := setup()

// Set up context
ctx, cancel := context.WithCancel(t.Context())
defer cancel()

// Get state channel with timeout
stateChan := machine.GetStateChanWithTimeout(ctx)
require.NotNil(t, stateChan)

// Should receive initial state
assert.Eventually(t, func() bool {
select {
case state := <-stateChan:
return state == StatusNew
default:
return false
}
}, 1*time.Second, 10*time.Millisecond, "Should receive initial state")

// Transition to Booting and verify state update
err := machine.Transition(StatusBooting)
require.NoError(t, err)

assert.Eventually(t, func() bool {
select {
case state := <-stateChan:
return state == StatusBooting
default:
return false
}
}, 1*time.Second, 10*time.Millisecond, "Should receive Booting state")

// Transition to Running and verify state update
err = machine.Transition(StatusRunning)
require.NoError(t, err)

assert.Eventually(t, func() bool {
select {
case state := <-stateChan:
return state == StatusRunning
default:
return false
}
}, 1*time.Second, 10*time.Millisecond, "Should receive Running state")

// Cancel context and verify channel closes
cancel()

assert.Eventually(t, func() bool {
_, open := <-stateChan
return !open
}, 1*time.Second, 10*time.Millisecond, "Channel should close when context is canceled")
})

t.Run("channel closes when context is canceled", func(t *testing.T) {
machine := setup()

// Set up context with timeout
ctx, cancel := context.WithTimeout(t.Context(), 100*time.Millisecond)
defer cancel()

// Get state channel with timeout
stateChan := machine.GetStateChanWithTimeout(ctx)
require.NotNil(t, stateChan)

// Wait for context to timeout
assert.Eventually(t, func() bool {
_, open := <-stateChan
return !open
}, 200*time.Millisecond, 10*time.Millisecond, "Channel should close when context times out")
})

t.Run("multiple channels can be created", func(t *testing.T) {
machine := setup()

// Set up context
ctx, cancel := context.WithCancel(t.Context())
defer cancel()

// Get multiple state channels
stateChan1 := machine.GetStateChanWithTimeout(ctx)
stateChan2 := machine.GetStateChanWithTimeout(ctx)
require.NotNil(t, stateChan1)
require.NotNil(t, stateChan2)
assert.NotEqual(t, stateChan1, stateChan2, "Should create different channel instances")

// Drain any initial states from both channels
select {
case <-stateChan1:
default:
}
select {
case <-stateChan2:
default:
}

// Transition and verify both channels receive updates
err := machine.Transition(StatusBooting)
require.NoError(t, err)

// Both channels should receive the state update
assert.Eventually(t, func() bool {
select {
case state := <-stateChan1:
return state == StatusBooting
default:
return false
}
}, 1*time.Second, 10*time.Millisecond, "First channel should receive state update")

assert.Eventually(t, func() bool {
select {
case state := <-stateChan2:
return state == StatusBooting
default:
return false
}
}, 1*time.Second, 10*time.Millisecond, "Second channel should receive state update")
})
}
Loading