@@ -211,6 +211,52 @@ func (d *dummyAdapters) RegisterSpendNtfn(outpoint *wire.OutPoint,
211211 }, err
212212}
213213
214+ // TestStateMachineOnInitDaemonEvent tests that the state machine will properly
215+ // execute any init-level daemon events passed into it.
216+ func TestStateMachineOnInitDaemonEvent (t * testing.T ) {
217+ // First, we'll create our state machine given the env, and our
218+ // starting state.
219+ env := & dummyEnv {}
220+ startingState := & dummyStateStart {}
221+
222+ adapters := newDaemonAdapters ()
223+
224+ // We'll make an init event that'll send to a peer, then transition us
225+ // to our terminal state.
226+ initEvent := & SendMsgEvent [dummyEvents ]{
227+ TargetPeer : * pub1 ,
228+ PostSendEvent : fn .Some (dummyEvents (& goToFin {})),
229+ }
230+
231+ stateMachine := NewStateMachine [dummyEvents , * dummyEnv ](
232+ adapters , startingState , env , fn.Some [DaemonEvent ](initEvent ),
233+ )
234+
235+ // Before we start up the state machine, we'll assert that the send
236+ // message adapter is called on start up.
237+ adapters .On ("SendMessages" , * pub1 , mock .Anything ).Return (nil )
238+
239+ stateMachine .Start ()
240+ defer stateMachine .Stop ()
241+
242+ // As we're triggering internal events, we'll also subscribe to the set
243+ // of new states so we can assert as we go.
244+ stateSub := stateMachine .RegisterStateEvents ()
245+ defer stateMachine .RemoveStateSub (stateSub )
246+
247+ // Assert that we go from the starting state to the final state. The
248+ // state machine should now also be on the final terminal state.
249+ expectedStates := []State [dummyEvents , * dummyEnv ]{
250+ & dummyStateStart {}, & dummyStateFin {},
251+ }
252+ assertStateTransitions (t , stateSub , expectedStates )
253+
254+ // We'll now assert that after the daemon was started, the send message
255+ // adapter was called above as specified in the init event.
256+ adapters .AssertExpectations (t )
257+ env .AssertExpectations (t )
258+ }
259+
214260// TestStateMachineInternalEvents tests that the state machine is able to add
215261// new internal events to the event queue for further processing during a state
216262// transition.
@@ -222,10 +268,10 @@ func TestStateMachineInternalEvents(t *testing.T) {
222268 env := & dummyEnv {}
223269 startingState := & dummyStateStart {}
224270
225- adapters := & dummyAdapters {}
271+ adapters := newDaemonAdapters ()
226272
227273 stateMachine := NewStateMachine [dummyEvents , * dummyEnv ](
228- adapters , startingState , env ,
274+ adapters , startingState , env , fn . None [ DaemonEvent ](),
229275 )
230276 stateMachine .Start ()
231277 defer stateMachine .Stop ()
@@ -269,10 +315,10 @@ func TestStateMachineDaemonEvents(t *testing.T) {
269315 canSend : & boolTrigger ,
270316 }
271317
272- adapters := & dummyAdapters {}
318+ adapters := newDaemonAdapters ()
273319
274320 stateMachine := NewStateMachine [dummyEvents , * dummyEnv ](
275- adapters , startingState , env ,
321+ adapters , startingState , env , fn . None [ DaemonEvent ](),
276322 )
277323 stateMachine .Start ()
278324 defer stateMachine .Stop ()
0 commit comments