@@ -161,6 +161,119 @@ func TestPaginator(t *testing.T) {
161
161
}
162
162
}
163
163
164
+ // TestPaginator_InitialisationError tests whether errors are correctly handled if API returns some error
165
+ func TestPaginator_InitialisationError (t * testing.T ) {
166
+ tests := []struct {
167
+ paginator func (context.Context , IStaticPageStream ) (IGenericPaginator , error )
168
+ name string
169
+ expectInitialisationError bool
170
+ }{
171
+ {
172
+ paginator : func (ctx context.Context , collection IStaticPageStream ) (IGenericPaginator , error ) {
173
+ return NewAbstractPaginator (ctx , collection , func (fCtx context.Context , current IStaticPage ) (IStaticPage , error ) {
174
+ return nil , commonerrors .ErrUnexpected
175
+ })
176
+ },
177
+ name : "Abstract paginator" ,
178
+ expectInitialisationError : false ,
179
+ },
180
+ {
181
+ paginator : func (ctx context.Context , collection IStaticPageStream ) (IGenericPaginator , error ) {
182
+ return NewStaticPagePaginator (ctx , func (context.Context ) (IStaticPage , error ) {
183
+ return nil , commonerrors .ErrUnexpected
184
+ }, func (fCtx context.Context , current IStaticPage ) (IStaticPage , error ) {
185
+ return nil , commonerrors .ErrUnexpected
186
+ })
187
+ },
188
+ name : "Static page paginator" ,
189
+ expectInitialisationError : true ,
190
+ },
191
+ {
192
+ paginator : func (ctx context.Context , collection IStaticPageStream ) (IGenericPaginator , error ) {
193
+ return NewCollectionPaginator (ctx , func (context.Context ) (IPage , error ) {
194
+ return nil , commonerrors .ErrUnexpected
195
+ })
196
+ },
197
+ name : "paginator over a collection of dynamic pages" ,
198
+ expectInitialisationError : true ,
199
+ },
200
+ {
201
+ paginator : func (ctx context.Context , collection IStaticPageStream ) (IGenericPaginator , error ) {
202
+ return NewStaticPageStreamPaginator (ctx , time .Second , 10 * time .Millisecond , func (context.Context ) (IStaticPageStream , error ) {
203
+ return nil , commonerrors .ErrUnexpected
204
+ }, func (fCtx context.Context , current IStaticPage ) (IStaticPage , error ) {
205
+ return nil , commonerrors .ErrUnexpected
206
+ }, func (fCtx context.Context , current IStaticPageStream ) (IStaticPageStream , error ) {
207
+ return nil , commonerrors .ErrUnexpected
208
+ })
209
+ },
210
+ name : "stream paginator over a collection of static pages" ,
211
+ expectInitialisationError : true ,
212
+ },
213
+ {
214
+ paginator : func (ctx context.Context , collection IStaticPageStream ) (IGenericPaginator , error ) {
215
+ return NewStreamPaginator (ctx , time .Second , 10 * time .Millisecond , func (context.Context ) (IStream , error ) {
216
+ return nil , commonerrors .ErrUnexpected
217
+ })
218
+ },
219
+ name : "stream paginator over a collection of dynamic pages" ,
220
+ expectInitialisationError : true ,
221
+ },
222
+ {
223
+ paginator : func (ctx context.Context , collection IStaticPageStream ) (IGenericPaginator , error ) {
224
+ paginator , err := NewStaticPageStreamPaginator (ctx , time .Second , 10 * time .Millisecond , func (context.Context ) (IStaticPageStream , error ) {
225
+ return nil , commonerrors .ErrUnexpected
226
+ }, func (fCtx context.Context , current IStaticPage ) (IStaticPage , error ) {
227
+ return nil , commonerrors .ErrUnexpected
228
+ }, func (fCtx context.Context , current IStaticPageStream ) (IStaticPageStream , error ) {
229
+ return nil , commonerrors .ErrUnexpected
230
+ })
231
+ if paginator != nil {
232
+ // Indicate the stream will run out.
233
+ err = paginator .DryUp ()
234
+ }
235
+ return paginator , err
236
+ },
237
+ name : "stream paginator over a running dry stream of static pages" ,
238
+ expectInitialisationError : true ,
239
+ },
240
+ {
241
+ paginator : func (ctx context.Context , collection IStaticPageStream ) (IGenericPaginator , error ) {
242
+ paginator , err := NewStreamPaginator (ctx , 50 * time .Millisecond , 10 * time .Millisecond , func (context.Context ) (IStream , error ) {
243
+ return nil , commonerrors .ErrUnexpected
244
+ })
245
+ if paginator != nil {
246
+ // Indicate the stream will run out.
247
+ err = paginator .DryUp ()
248
+ }
249
+ return paginator , err
250
+ },
251
+ name : "stream paginator over a running dry stream of dynamic pages" ,
252
+ expectInitialisationError : true ,
253
+ },
254
+ }
255
+
256
+ for te := range tests {
257
+ test := tests [te ]
258
+ for i := 0 ; i < 50 ; i ++ {
259
+ var mockPages IStream
260
+ t .Run (fmt .Sprintf ("%v-#%v" , test .name , i ), func (t * testing.T ) {
261
+ paginator , err := test .paginator (context .TODO (), mockPages )
262
+ if test .expectInitialisationError {
263
+ assert .Error (t , err )
264
+ assert .Nil (t , paginator )
265
+ } else {
266
+ assert .NoError (t , err )
267
+ assert .NotNil (t , paginator )
268
+ assert .False (t , paginator .HasNext ())
269
+ require .NoError (t , paginator .Close ())
270
+ }
271
+
272
+ })
273
+ }
274
+ }
275
+ }
276
+
164
277
func TestPaginator_stop (t * testing.T ) {
165
278
tests := []struct {
166
279
paginator func (context.Context , IStaticPageStream ) (IGenericPaginator , error )
0 commit comments