@@ -140,71 +140,52 @@ func getTestContext() (*ExampleFSM, *CachedObserver) {
140140 return exampleContext , cachedObserver
141141}
142142
143- // TestExampleFSMFlow tests different flows that the example FSM can go through .
144- func TestExampleFSMFlow (t * testing.T ) {
143+ // TestObserverAsyncWait tests the observer's WaitForStateAsync function .
144+ func TestObserverAsyncWait (t * testing.T ) {
145145 testCases := []struct {
146- name string
147- expectedStateFlow []StateType
148- expectedEventFlow []EventType
149- storeError error
150- serviceError error
146+ name string
147+ waitTime time.Duration
148+ blockTime time.Duration
149+ expectTimeout bool
151150 }{
152151 {
153- name : "success" ,
154- expectedStateFlow : []StateType {
155- InitFSM ,
156- StuffSentOut ,
157- StuffSuccess ,
158- },
159- expectedEventFlow : []EventType {
160- OnRequestStuff ,
161- OnStuffSentOut ,
162- OnStuffSuccess ,
163- },
164- },
165- {
166- name : "failure on store" ,
167- expectedStateFlow : []StateType {
168- InitFSM ,
169- StuffFailed ,
170- },
171- expectedEventFlow : []EventType {
172- OnRequestStuff ,
173- OnError ,
174- },
175- storeError : errStore ,
152+ name : "success" ,
153+ waitTime : time .Second ,
154+ blockTime : time .Millisecond ,
155+ expectTimeout : false ,
176156 },
177157 {
178- name : "failure on service" ,
179- expectedStateFlow : []StateType {
180- InitFSM ,
181- StuffSentOut ,
182- StuffFailed ,
183- },
184- expectedEventFlow : []EventType {
185- OnRequestStuff ,
186- OnStuffSentOut ,
187- OnError ,
188- },
189- serviceError : errService ,
158+ name : "timeout" ,
159+ waitTime : time .Millisecond ,
160+ blockTime : time .Second ,
161+ expectTimeout : true ,
190162 },
191163 }
192164
193165 for _ , tc := range testCases {
194166 tc := tc
195167
196168 t .Run (tc .name , func (t * testing.T ) {
197- exampleContext , cachedObserver := getTestContext ()
198-
199- if tc .storeError != nil {
200- exampleContext .store .(* mockStore ).
201- storeErr = tc .storeError
169+ service := & mockService {
170+ respondChan : make (chan bool ),
202171 }
203172
204- if tc .serviceError != nil {
205- exampleContext .service .(* mockService ).
206- respondErr = tc .serviceError
207- }
173+ store := & mockStore {}
174+
175+ exampleContext := NewExampleFSMContext (service , store )
176+ cachedObserver := NewCachedObserver (100 )
177+ exampleContext .RegisterObserver (cachedObserver )
178+
179+ t0 := time .Now ()
180+ timeoutCtx , cancel := context .WithTimeout (
181+ context .Background (), tc .waitTime ,
182+ )
183+ defer cancel ()
184+
185+ // Wait for the final state.
186+ errChan := cachedObserver .WaitForStateAsync (
187+ timeoutCtx , StuffSuccess , true ,
188+ )
208189
209190 go func () {
210191 err := exampleContext .SendEvent (
@@ -213,32 +194,26 @@ func TestExampleFSMFlow(t *testing.T) {
213194 )
214195
215196 require .NoError (t , err )
216- }()
217197
218- // Wait for the final state.
219- err := cachedObserver .WaitForState (
220- context .Background (),
221- time .Second ,
222- tc .expectedStateFlow [len (
223- tc .expectedStateFlow ,
224- )- 1 ],
225- )
226- require .NoError (t , err )
198+ time .Sleep (tc .blockTime )
199+ service .respondChan <- true
200+ }()
227201
228- allNotifications := cachedObserver .
229- GetCachedNotifications ()
202+ timeout := false
203+ select {
204+ case <- timeoutCtx .Done ():
205+ timeout = true
230206
231- for index , notification := range allNotifications {
232- require .Equal (
233- t ,
234- tc .expectedStateFlow [index ],
235- notification .NextState ,
236- )
237- require .Equal (
238- t ,
239- tc .expectedEventFlow [index ],
240- notification .Event ,
241- )
207+ case <- errChan :
208+ }
209+ require .Equal (t , tc .expectTimeout , timeout )
210+
211+ t1 := time .Now ()
212+ diff := t1 .Sub (t0 )
213+ if tc .expectTimeout {
214+ require .Less (t , diff , tc .blockTime )
215+ } else {
216+ require .Less (t , diff , tc .waitTime )
242217 }
243218 })
244219 }
0 commit comments