protofsm: add CustomPollInterval for mocking purposes

Adding this makes a state machine easier to unit test, as the caller can
specify a custom polling interval.
This commit is contained in:
Olaoluwa Osuntokun
2024-02-29 16:08:54 -06:00
parent 35ea05d5dc
commit d805c0fc3c

View File

@@ -178,6 +178,10 @@ type StateMachineCfg[Event any, Env Environment] struct {
// MsgMapper is an optional message mapper that can be used to map
// normal wire messages into FSM events.
MsgMapper fn.Option[MsgMapper[Event]]
// CustomPollInterval is an optional custom poll interval that can be
// used to set a quicker interval for tests.
CustomPollInterval fn.Option[time.Duration]
}
// NewStateMachine creates a new state machine given a set of daemon adapters,
@@ -373,7 +377,9 @@ func (s *StateMachine[Event, Env]) executeDaemonEvent( //nolint:funlen
go func() {
defer s.wg.Done()
predicateTicker := time.NewTicker(pollInterval)
predicateTicker := time.NewTicker(
s.cfg.CustomPollInterval.UnwrapOr(pollInterval),
)
defer predicateTicker.Stop()
log.Infof("FSM(%v): waiting for send predicate to "+