Skip to content

Commit dc94758

Browse files
authored
🐛 [pagination] Fix panic in contructor (#192)
<!-- Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. SPDX-License-Identifier: Apache-2.0 --> ### Description Fix panic in contructor ### Test Coverage <!-- Please put an `x` in the correct box e.g. `[x]` to indicate the testing coverage of this change. --> - [x] This change is covered by existing or additional automated tests. - [ ] Manual testing has been performed (and evidence provided) as automated testing was not feasible. - [ ] Additional tests are not required for this change (e.g. documentation update).
1 parent 1a110e5 commit dc94758

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

changes/20230121234948.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:bug:`[pagination]` Fix panic in contructor

utils/collection/pagination/pagination_test.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,119 @@ func TestPaginator(t *testing.T) {
161161
}
162162
}
163163

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+
164277
func TestPaginator_stop(t *testing.T) {
165278
tests := []struct {
166279
paginator func(context.Context, IStaticPageStream) (IGenericPaginator, error)

utils/collection/pagination/stream.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ func NewStaticPageStreamPaginator(ctx context.Context, runOutTimeOut, backoff ti
191191
parent, err := newAbstractStreamPaginator(ctx, runOutTimeOut, backoff, func(fCtx context.Context) (IStaticPage, error) {
192192
return fetchFirstPageFunc(fCtx)
193193
}, fetchNextPageFunc, fetchFutureFunc)
194+
if err != nil {
195+
return
196+
}
194197
paginator = &StaticPageStreamPaginator{
195198
AbstractStreamPaginator: *parent,
196199
}

0 commit comments

Comments
 (0)