@@ -11,8 +11,7 @@ import { loggerMock, MockedLogger } from '../../logging/logger.mock';
1111import { SavedObjectsFindOptions } from '../types' ;
1212import { SavedObjectsFindResult } from '../service' ;
1313
14- import type { FindWithPointInTime } from './find_with_point_in_time' ;
15- import { findWithPointInTime } from './find_with_point_in_time' ;
14+ import { createPointInTimeFinder } from './point_in_time_finder' ;
1615
1716const mockHits = [
1817 {
@@ -39,18 +38,55 @@ const mockHits = [
3938 } ,
4039] ;
4140
42- describe ( 'findWithPointInTime ()' , ( ) => {
41+ describe ( 'createPointInTimeFinder ()' , ( ) => {
4342 let logger : MockedLogger ;
4443 let savedObjectsClient : ReturnType < typeof savedObjectsClientMock . create > ;
45- let finder : FindWithPointInTime ;
4644
4745 beforeEach ( ( ) => {
4846 logger = loggerMock . create ( ) ;
4947 savedObjectsClient = savedObjectsClientMock . create ( ) ;
50- finder = findWithPointInTime ( { savedObjectsClient, logger } ) ;
5148 } ) ;
5249
5350 describe ( '#find' , ( ) => {
51+ test ( 'throws if a PIT is already open' , async ( ) => {
52+ savedObjectsClient . openPointInTimeForType . mockResolvedValueOnce ( {
53+ id : 'abc123' ,
54+ } ) ;
55+ savedObjectsClient . find . mockResolvedValueOnce ( {
56+ total : 2 ,
57+ saved_objects : mockHits ,
58+ pit_id : 'abc123' ,
59+ per_page : 1 ,
60+ page : 0 ,
61+ } ) ;
62+ savedObjectsClient . find . mockResolvedValueOnce ( {
63+ total : 2 ,
64+ saved_objects : mockHits ,
65+ pit_id : 'abc123' ,
66+ per_page : 1 ,
67+ page : 1 ,
68+ } ) ;
69+
70+ const findOptions : SavedObjectsFindOptions = {
71+ type : [ 'visualization' ] ,
72+ search : 'foo*' ,
73+ perPage : 1 ,
74+ } ;
75+
76+ const finder = createPointInTimeFinder ( { findOptions, logger, savedObjectsClient } ) ;
77+ await finder . find ( ) . next ( ) ;
78+
79+ expect ( savedObjectsClient . find ) . toHaveBeenCalledTimes ( 1 ) ;
80+ savedObjectsClient . find . mockClear ( ) ;
81+
82+ expect ( async ( ) => {
83+ await finder . find ( ) . next ( ) ;
84+ } ) . rejects . toThrowErrorMatchingInlineSnapshot (
85+ `"Point In Time has already been opened for this finder instance. Please call \`close()\` before calling \`find()\` again."`
86+ ) ;
87+ expect ( savedObjectsClient . find ) . toHaveBeenCalledTimes ( 0 ) ;
88+ } ) ;
89+
5490 test ( 'works with a single page of results' , async ( ) => {
5591 savedObjectsClient . openPointInTimeForType . mockResolvedValueOnce ( {
5692 id : 'abc123' ,
@@ -63,13 +99,14 @@ describe('findWithPointInTime()', () => {
6399 page : 0 ,
64100 } ) ;
65101
66- const options : SavedObjectsFindOptions = {
102+ const findOptions : SavedObjectsFindOptions = {
67103 type : [ 'visualization' ] ,
68104 search : 'foo*' ,
69105 } ;
70106
107+ const finder = createPointInTimeFinder ( { findOptions, logger, savedObjectsClient } ) ;
71108 const hits : SavedObjectsFindResult [ ] = [ ] ;
72- for await ( const result of finder . find ( options ) ) {
109+ for await ( const result of finder . find ( ) ) {
73110 hits . push ( ...result . saved_objects ) ;
74111 }
75112
@@ -113,14 +150,15 @@ describe('findWithPointInTime()', () => {
113150 page : 0 ,
114151 } ) ;
115152
116- const options : SavedObjectsFindOptions = {
153+ const findOptions : SavedObjectsFindOptions = {
117154 type : [ 'visualization' ] ,
118155 search : 'foo*' ,
119156 perPage : 1 ,
120157 } ;
121158
159+ const finder = createPointInTimeFinder ( { findOptions, logger, savedObjectsClient } ) ;
122160 const hits : SavedObjectsFindResult [ ] = [ ] ;
123- for await ( const result of finder . find ( options ) ) {
161+ for await ( const result of finder . find ( ) ) {
124162 hits . push ( ...result . saved_objects ) ;
125163 }
126164
@@ -154,14 +192,15 @@ describe('findWithPointInTime()', () => {
154192 page : 0 ,
155193 } ) ;
156194
157- const options : SavedObjectsFindOptions = {
195+ const findOptions : SavedObjectsFindOptions = {
158196 type : [ 'visualization' ] ,
159197 search : 'foo*' ,
160198 perPage : 2 ,
161199 } ;
162200
201+ const finder = createPointInTimeFinder ( { findOptions, logger, savedObjectsClient } ) ;
163202 const hits : SavedObjectsFindResult [ ] = [ ] ;
164- for await ( const result of finder . find ( options ) ) {
203+ for await ( const result of finder . find ( ) ) {
165204 hits . push ( ...result . saved_objects ) ;
166205 await finder . close ( ) ;
167206 }
@@ -195,14 +234,15 @@ describe('findWithPointInTime()', () => {
195234 page : 0 ,
196235 } ) ;
197236
198- const options : SavedObjectsFindOptions = {
237+ const findOptions : SavedObjectsFindOptions = {
199238 type : [ 'visualization' ] ,
200239 search : 'foo*' ,
201240 perPage : 1 ,
202241 } ;
203242
243+ const finder = createPointInTimeFinder ( { findOptions, logger, savedObjectsClient } ) ;
204244 const hits : SavedObjectsFindResult [ ] = [ ] ;
205- for await ( const result of finder . find ( options ) ) {
245+ for await ( const result of finder . find ( ) ) {
206246 hits . push ( ...result . saved_objects ) ;
207247 await finder . close ( ) ;
208248 }
@@ -217,15 +257,16 @@ describe('findWithPointInTime()', () => {
217257 } ) ;
218258 savedObjectsClient . find . mockRejectedValueOnce ( new Error ( 'oops' ) ) ;
219259
220- const options : SavedObjectsFindOptions = {
260+ const findOptions : SavedObjectsFindOptions = {
221261 type : [ 'visualization' ] ,
222262 search : 'foo*' ,
223263 perPage : 2 ,
224264 } ;
225265
266+ const finder = createPointInTimeFinder ( { findOptions, logger, savedObjectsClient } ) ;
226267 const hits : SavedObjectsFindResult [ ] = [ ] ;
227268 try {
228- for await ( const result of finder . find ( options ) ) {
269+ for await ( const result of finder . find ( ) ) {
229270 hits . push ( ...result . saved_objects ) ;
230271 }
231272 } catch ( e ) {
@@ -234,5 +275,47 @@ describe('findWithPointInTime()', () => {
234275
235276 expect ( savedObjectsClient . closePointInTime ) . toHaveBeenCalledWith ( 'test' ) ;
236277 } ) ;
278+
279+ test ( 'finder can be reused after closing' , async ( ) => {
280+ savedObjectsClient . openPointInTimeForType . mockResolvedValueOnce ( {
281+ id : 'abc123' ,
282+ } ) ;
283+ savedObjectsClient . find . mockResolvedValueOnce ( {
284+ total : 2 ,
285+ saved_objects : mockHits ,
286+ pit_id : 'abc123' ,
287+ per_page : 1 ,
288+ page : 0 ,
289+ } ) ;
290+ savedObjectsClient . find . mockResolvedValueOnce ( {
291+ total : 2 ,
292+ saved_objects : mockHits ,
293+ pit_id : 'abc123' ,
294+ per_page : 1 ,
295+ page : 1 ,
296+ } ) ;
297+
298+ const findOptions : SavedObjectsFindOptions = {
299+ type : [ 'visualization' ] ,
300+ search : 'foo*' ,
301+ perPage : 1 ,
302+ } ;
303+
304+ const finder = createPointInTimeFinder ( { findOptions, logger, savedObjectsClient } ) ;
305+
306+ const findA = finder . find ( ) ;
307+ await findA . next ( ) ;
308+ await finder . close ( ) ;
309+
310+ const findB = finder . find ( ) ;
311+ await findB . next ( ) ;
312+ await finder . close ( ) ;
313+
314+ expect ( ( await findA . next ( ) ) . done ) . toBe ( true ) ;
315+ expect ( ( await findB . next ( ) ) . done ) . toBe ( true ) ;
316+ expect ( savedObjectsClient . openPointInTimeForType ) . toHaveBeenCalledTimes ( 2 ) ;
317+ expect ( savedObjectsClient . find ) . toHaveBeenCalledTimes ( 2 ) ;
318+ expect ( savedObjectsClient . closePointInTime ) . toHaveBeenCalledTimes ( 2 ) ;
319+ } ) ;
237320 } ) ;
238321} ) ;
0 commit comments