@@ -221,10 +221,10 @@ func TestStateMachineTerminateCleanup(t *testing.T) {
221221 env := & dummyEnv {}
222222 startingState := & dummyStateStart {}
223223
224- adapters := & dummyAdapters {}
224+ adapters := newDaemonAdapters ()
225225
226226 stateMachine := NewStateMachine [dummyEvents , * dummyEnv ](
227- adapters , startingState , env ,
227+ adapters , startingState , env , fn . None [ DaemonEvent ](),
228228 )
229229 stateMachine .Start ()
230230 defer stateMachine .Stop ()
@@ -244,6 +244,53 @@ func TestStateMachineTerminateCleanup(t *testing.T) {
244244 env .AssertExpectations (t )
245245}
246246
247+ // TestStateMachineOnInitDaemonEvent tests that the state machine will properly
248+ // execute any init-level daemon events passed into it.
249+ func TestStateMachineOnInitDaemonEvent (t * testing.T ) {
250+ // First, we'll create our state machine given the env, and our
251+ // starting state.
252+ env := & dummyEnv {}
253+ startingState := & dummyStateStart {}
254+
255+ adapters := newDaemonAdapters ()
256+
257+ // We'll make an init event that'll send to a peer, then transition us
258+ // to our terminal state.
259+ initEvent := & SendMsgEvent [dummyEvents ]{
260+ TargetPeer : * pub1 ,
261+ PostSendEvent : fn .Some (dummyEvents (& goToFin {})),
262+ }
263+
264+ stateMachine := NewStateMachine [dummyEvents , * dummyEnv ](
265+ adapters , startingState , env , fn.Some [DaemonEvent ](initEvent ),
266+ )
267+
268+ // Before we start up the state machine, we'll assert that the send
269+ // message adapter is called on start up.
270+ adapters .On ("SendMessages" , * pub1 , mock .Anything ).Return (nil )
271+ env .On ("CleanUp" ).Return (nil )
272+
273+ stateMachine .Start ()
274+ defer stateMachine .Stop ()
275+
276+ // As we're triggering internal events, we'll also subscribe to the set
277+ // of new states so we can assert as we go.
278+ stateSub := stateMachine .RegisterStateEvents ()
279+ defer stateMachine .RemoveStateSub (stateSub )
280+
281+ // Assert that we go from the starting state to the final state. The
282+ // state machine should now also be on the final terminal state.
283+ expectedStates := []State [dummyEvents , * dummyEnv ]{
284+ & dummyStateStart {}, & dummyStateFin {},
285+ }
286+ assertStateTransitions (t , stateSub , expectedStates )
287+
288+ // We'll now assert that after the daemon was started, the send message
289+ // adapter was called above as specified in the init event.
290+ adapters .AssertExpectations (t )
291+ env .AssertExpectations (t )
292+ }
293+
247294// TestStateMachineInternalEvents tests that the state machine is able to add
248295// new internal events to the event queue for further processing during a state
249296// transition.
@@ -255,10 +302,10 @@ func TestStateMachineInternalEvents(t *testing.T) {
255302 env := & dummyEnv {}
256303 startingState := & dummyStateStart {}
257304
258- adapters := & dummyAdapters {}
305+ adapters := newDaemonAdapters ()
259306
260307 stateMachine := NewStateMachine [dummyEvents , * dummyEnv ](
261- adapters , startingState , env ,
308+ adapters , startingState , env , fn . None [ DaemonEvent ](),
262309 )
263310 stateMachine .Start ()
264311 defer stateMachine .Stop ()
@@ -306,10 +353,10 @@ func TestStateMachineDaemonEvents(t *testing.T) {
306353 canSend : & boolTrigger ,
307354 }
308355
309- adapters := & dummyAdapters {}
356+ adapters := newDaemonAdapters ()
310357
311358 stateMachine := NewStateMachine [dummyEvents , * dummyEnv ](
312- adapters , startingState , env ,
359+ adapters , startingState , env , fn . None [ DaemonEvent ](),
313360 )
314361 stateMachine .Start ()
315362 defer stateMachine .Stop ()
0 commit comments