Skip to content

Commit a899900

Browse files
committed
test: adapt flow to new streamable promise behavior
1 parent 70ddfd6 commit a899900

4 files changed

Lines changed: 125 additions & 74 deletions

File tree

tests/cbjs/tests/bucketmanager.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ describe.shuffle('bucket manager', async () => {
8282
historyRetentionCollectionDefault: undefined,
8383
historyRetentionBytes: undefined,
8484
historyRetentionDuration: undefined,
85+
numVBuckets: 1024,
8586
};
8687
if (!serverSupportsFeatures(ServerFeatures.StorageBackend)) {
8788
expected.storageBackend = undefined;
@@ -264,6 +265,7 @@ describe.shuffle('bucket manager', async () => {
264265
historyRetentionCollectionDefault: undefined,
265266
historyRetentionBytes: undefined,
266267
historyRetentionDuration: undefined,
268+
numVBuckets: 1024,
267269
};
268270

269271
if (!serverSupportsFeatures(ServerFeatures.StorageBackend)) {
@@ -305,6 +307,7 @@ describe.shuffle('bucket manager', async () => {
305307
historyRetentionCollectionDefault: true,
306308
historyRetentionBytes: 2147483648,
307309
historyRetentionDuration: 13000,
310+
numVBuckets: 1024,
308311
};
309312

310313
expect(res).toEqual(expected);
@@ -347,6 +350,7 @@ describe.shuffle('bucket manager', async () => {
347350
historyRetentionCollectionDefault: false,
348351
historyRetentionBytes: 0,
349352
historyRetentionDuration: 14000,
353+
numVBuckets: 1024,
350354
};
351355

352356
expect(res).toEqual(expected);

tests/cbjs/tests/scan.spec.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import { describe, vi } from 'vitest';
1818

1919
import {
20+
Cas,
2021
CollectionNotFoundError,
2122
InvalidArgumentError,
2223
MutationState,
@@ -29,6 +30,7 @@ import {
2930
import { ServerFeatures } from '@cbjsdev/http-client';
3031
import { couchbaseFixture, createCouchbaseTest } from '@cbjsdev/vitest';
3132

33+
import { defer } from '../utils/defer.js';
3234
import { serverSupportsFeatures } from '../utils/serverFeature.js';
3335

3436
describe
@@ -178,24 +180,28 @@ describe
178180
});
179181

180182
test('should be able to stream the results ', async ({ expect, data }) => {
183+
const results: Array<{ id: string; cas: Cas; content: { id: string } }> = [];
184+
181185
const onResult = vi.fn((result: ScanResult) => {
182186
expect(result.id).toBeTypeOf('string');
183187
expect(result.cas).toBeNonZeroCAS();
184188
expect(result.content).toEqual({ id: result.id });
189+
results.push(result as never);
185190
});
186191

187192
const scanType = new PrefixScan('doc::');
188193
const resultsStream = data.collection.scan(scanType, {
189194
consistentWith: data.mutationState,
190195
});
191196

197+
const deferredResults = defer<void>();
198+
192199
void resultsStream.on('result', onResult);
200+
void resultsStream.on('end', deferredResults.resolve);
193201

194-
const results = await resultsStream;
202+
await deferredResults.promise;
195203

196204
expect(onResult).toHaveBeenCalledTimes(100);
197-
198-
expect(results).toBeInstanceOf(Array);
199205
expect(results).toHaveLength(100);
200206

201207
results.forEach((result) => {
@@ -222,14 +228,14 @@ describe
222228
}
223229
});
224230

231+
const deferredResult = defer<void>();
232+
225233
void resultsStream.on('result', onResult);
234+
void resultsStream.on('end', deferredResult.resolve);
226235

227-
const results = await resultsStream;
236+
await deferredResult.promise;
228237

229238
expect(onResult).toHaveBeenCalledTimes(5);
230-
231-
expect(results).toBeInstanceOf(Array);
232-
expect(results).toHaveLength(5);
233239
});
234240
});
235241

tests/cbjs/utils/defer.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2023-Present Jonathan MASSUCHETTI <jonathan.massuchetti@dappit.fr>.
3+
* Copyright (c) 2013-Present Couchbase Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
export type Deferred<T> = {
18+
promise: Promise<T>;
19+
resolve: (value: T | PromiseLike<T>) => void;
20+
reject: (reason?: unknown) => void;
21+
};
22+
23+
export function defer<T = void>(): Deferred<T> {
24+
const deferred: Deferred<T> = {
25+
promise: null as never,
26+
resolve: null as never,
27+
reject: null as never,
28+
};
29+
30+
deferred.promise = new Promise<T>((resolve, reject) => {
31+
deferred.resolve = resolve;
32+
deferred.reject = reject;
33+
});
34+
35+
return deferred;
36+
}

tests/http-client/tests/search/getSearchIndexDocumentsByIds.spec.ts

Lines changed: 72 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -15,83 +15,88 @@
1515
*/
1616
import { describe } from 'vitest';
1717

18-
import { getSearchIndexDocumentsByIds } from '@cbjsdev/http-client';
18+
import { getSearchIndexDocumentsByIds, ServerFeatures } from '@cbjsdev/http-client';
1919
import { getRandomId, waitFor } from '@cbjsdev/shared';
2020
import { createCouchbaseTest } from '@cbjsdev/vitest';
2121

22+
import { serverSupportsFeatures } from '../../utils/serverFeature.js';
2223
import getIndexDefinition from './getIndexDefinition.js';
2324

24-
describe('getSearchIndexDocumentsByIds', { timeout: 100_000 }, async () => {
25-
const test = await createCouchbaseTest();
25+
describe.runIf(serverSupportsFeatures(ServerFeatures.ScopeSearchIndexManagement))(
26+
'getSearchIndexDocumentsByIds',
27+
{ timeout: 100_000 },
28+
async () => {
29+
const test = await createCouchbaseTest();
2630

27-
test(
28-
'should return all the documents',
29-
async ({
30-
expect,
31-
useScopedSearchIndex,
32-
useDocumentKey,
33-
useScope,
34-
useCollection,
35-
serverTestContext,
36-
apiConfig,
37-
}) => {
38-
const scopeName = await useScope();
39-
const collectionName = await useCollection({ scopeName });
31+
test(
32+
'should return all the documents',
33+
async ({
34+
expect,
35+
useScopedSearchIndex,
36+
useDocumentKey,
37+
useScope,
38+
useCollection,
39+
serverTestContext,
40+
apiConfig,
41+
}) => {
42+
const scopeName = await useScope();
43+
const collectionName = await useCollection({ scopeName });
4044

41-
const testDocKey = useDocumentKey();
42-
const testDocBody = {
43-
title: 'Couchbase',
44-
tagline: 'No Equal - The Cloud database for modern applications',
45-
description:
46-
'Couchbase is a NoSQL, distributed database focused on performances at scale.',
47-
};
45+
const testDocKey = useDocumentKey();
46+
const testDocBody = {
47+
title: 'Couchbase',
48+
tagline: 'No Equal - The Cloud database for modern applications',
49+
description:
50+
'Couchbase is a NoSQL, distributed database focused on performances at scale.',
51+
};
4852

49-
const testDocKey2 = useDocumentKey();
50-
const testDocBody2 = {
51-
title: 'Magma',
52-
description: 'The latest storage engine from Couchbase.',
53-
};
53+
const testDocKey2 = useDocumentKey();
54+
const testDocBody2 = {
55+
title: 'Magma',
56+
description: 'The latest storage engine from Couchbase.',
57+
};
5458

55-
const collection = serverTestContext.bucket
56-
.scope(scopeName)
57-
.collection(collectionName);
58-
await collection.insert(testDocKey, testDocBody);
59-
await collection.insert(testDocKey2, testDocBody2);
59+
const collection = serverTestContext.bucket
60+
.scope(scopeName)
61+
.collection(collectionName);
62+
await collection.insert(testDocKey, testDocBody);
63+
await collection.insert(testDocKey2, testDocBody2);
6064

61-
const searchIndexName = `searchIndex` + getRandomId();
62-
const def = getIndexDefinition(searchIndexName, {
63-
bucket: serverTestContext.bucket.name,
64-
scope: scopeName,
65-
collection: collectionName,
66-
});
65+
const searchIndexName = `searchIndex` + getRandomId();
66+
const def = getIndexDefinition(searchIndexName, {
67+
bucket: serverTestContext.bucket.name,
68+
scope: scopeName,
69+
collection: collectionName,
70+
});
6771

68-
const indexName = await useScopedSearchIndex(
69-
{
70-
bucketName: serverTestContext.bucket.name,
71-
scopeName,
72-
collectionName,
73-
...def,
74-
},
75-
{ waitSearchIndexTimeout: 100_000, awaitMutations: true }
76-
);
72+
const indexName = await useScopedSearchIndex(
73+
{
74+
bucketName: serverTestContext.bucket.name,
75+
scopeName,
76+
collectionName,
77+
...def,
78+
},
79+
{ waitSearchIndexTimeout: 100_000, awaitMutations: true }
80+
);
7781

78-
await waitFor(
79-
async () => {
80-
const docs = await getSearchIndexDocumentsByIds(
81-
apiConfig,
82-
{
83-
bucket: serverTestContext.bucket.name,
84-
scope: scopeName,
85-
index: indexName,
86-
},
87-
[testDocKey, testDocKey2]
88-
);
82+
await waitFor(
83+
async () => {
84+
const docs = await getSearchIndexDocumentsByIds(
85+
apiConfig,
86+
{
87+
bucket: serverTestContext.bucket.name,
88+
scope: scopeName,
89+
index: indexName,
90+
},
91+
[testDocKey, testDocKey2]
92+
);
8993

90-
expect(docs.hits).toHaveLength(2);
91-
},
92-
{ timeout: 60_000, retryInterval: 2_500 }
93-
);
94-
},
95-
{ timeout: 100_000 }
96-
);
97-
});
94+
expect(docs.hits).toHaveLength(2);
95+
},
96+
{ timeout: 60_000, retryInterval: 2_500 }
97+
);
98+
},
99+
{ timeout: 100_000 }
100+
);
101+
}
102+
);

0 commit comments

Comments
 (0)