@@ -228,10 +228,10 @@ func TestStateMachineTerminateCleanup(t *testing.T) {
228228 env := & dummyEnv {}
229229 startingState := & dummyStateStart {}
230230
231- adapters := & dummyAdapters {}
231+ adapters := newDaemonAdapters ()
232232
233233 stateMachine := NewStateMachine [dummyEvents , * dummyEnv ](
234- adapters , startingState , env ,
234+ adapters , startingState , env , fn . None [ DaemonEvent ](),
235235 )
236236 stateMachine .Start ()
237237 defer stateMachine .Stop ()
@@ -251,6 +251,53 @@ func TestStateMachineTerminateCleanup(t *testing.T) {
251251 env .AssertExpectations (t )
252252}
253253
254+ // TestStateMachineOnInitDaemonEvent tests that the state machine will properly
255+ // execute any init-level daemon events passed into it.
256+ func TestStateMachineOnInitDaemonEvent (t * testing.T ) {
257+ // First, we'll create our state machine given the env, and our
258+ // starting state.
259+ env := & dummyEnv {}
260+ startingState := & dummyStateStart {}
261+
262+ adapters := newDaemonAdapters ()
263+
264+ // We'll make an init event that'll send to a peer, then transition us
265+ // to our terminal state.
266+ initEvent := & SendMsgEvent [dummyEvents ]{
267+ TargetPeer : * pub1 ,
268+ PostSendEvent : fn .Some (dummyEvents (& goToFin {})),
269+ }
270+
271+ stateMachine := NewStateMachine [dummyEvents , * dummyEnv ](
272+ adapters , startingState , env , fn.Some [DaemonEvent ](initEvent ),
273+ )
274+
275+ // Before we start up the state machine, we'll assert that the send
276+ // message adapter is called on start up.
277+ adapters .On ("SendMessages" , * pub1 , mock .Anything ).Return (nil )
278+ env .On ("CleanUp" ).Return (nil )
279+
280+ stateMachine .Start ()
281+ defer stateMachine .Stop ()
282+
283+ // As we're triggering internal events, we'll also subscribe to the set
284+ // of new states so we can assert as we go.
285+ stateSub := stateMachine .RegisterStateEvents ()
286+ defer stateMachine .RemoveStateSub (stateSub )
287+
288+ // Assert that we go from the starting state to the final state. The
289+ // state machine should now also be on the final terminal state.
290+ expectedStates := []State [dummyEvents , * dummyEnv ]{
291+ & dummyStateStart {}, & dummyStateFin {},
292+ }
293+ assertStateTransitions (t , stateSub , expectedStates )
294+
295+ // We'll now assert that after the daemon was started, the send message
296+ // adapter was called above as specified in the init event.
297+ adapters .AssertExpectations (t )
298+ env .AssertExpectations (t )
299+ }
300+
254301// TestStateMachineInternalEvents tests that the state machine is able to add
255302// new internal events to the event queue for further processing during a state
256303// transition.
@@ -262,10 +309,10 @@ func TestStateMachineInternalEvents(t *testing.T) {
262309 env := & dummyEnv {}
263310 startingState := & dummyStateStart {}
264311
265- adapters := & dummyAdapters {}
312+ adapters := newDaemonAdapters ()
266313
267314 stateMachine := NewStateMachine [dummyEvents , * dummyEnv ](
268- adapters , startingState , env ,
315+ adapters , startingState , env , fn . None [ DaemonEvent ](),
269316 )
270317 stateMachine .Start ()
271318 defer stateMachine .Stop ()
@@ -313,10 +360,10 @@ func TestStateMachineDaemonEvents(t *testing.T) {
313360 canSend : & boolTrigger ,
314361 }
315362
316- adapters := & dummyAdapters {}
363+ adapters := newDaemonAdapters ()
317364
318365 stateMachine := NewStateMachine [dummyEvents , * dummyEnv ](
319- adapters , startingState , env ,
366+ adapters , startingState , env , fn . None [ DaemonEvent ](),
320367 )
321368 stateMachine .Start ()
322369 defer stateMachine .Stop ()
0 commit comments