Skip to content

Commit

Permalink
refactor: added a helper method to wait for locks
Browse files Browse the repository at this point in the history
  • Loading branch information
ekraffmiller committed Dec 18, 2023
1 parent 22ff4c0 commit a50347b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
10 changes: 6 additions & 4 deletions test/integration/datasets/DatasetsRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
createDatasetViaApi,
createPrivateUrlViaApi,
publishDatasetViaApi,
deaccessionDatasetViaApi
deaccessionDatasetViaApi
// , waitForNoLocks
} from '../../testHelpers/datasets/datasetHelper';
import { ReadError } from '../../../src/core/domain/repositories/ReadError';
import { DatasetNotNumberedVersion, DatasetLockType } from '../../../src/datasets';
Expand Down Expand Up @@ -110,13 +111,14 @@ describe('DatasetsRepository', () => {
assert.fail('Error while publishing test Dataset');
});

// await waitForNoLocks(createdDatasetId,2)
let locks = await sut.getDatasetLocks(createdDatasetId);
const maxTries = 10;
let tries = 0;
while (locks.length > 0 && tries < maxTries) {
await new Promise(resolve => setTimeout(resolve, 1000));
locks = await sut.getDatasetLocks(createdDatasetId);
tries++
await new Promise(resolve => setTimeout(resolve, 1000));
locks = await sut.getDatasetLocks(createdDatasetId);
tries++
}
if (tries >= maxTries && locks.length > 0) {
assert.fail('Error while waiting for locks to be released');
Expand Down
19 changes: 19 additions & 0 deletions test/testHelpers/datasets/datasetHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,22 @@ export const createPrivateUrlViaApi = async (datasetId: number): Promise<AxiosRe
DATAVERSE_API_REQUEST_HEADERS,
);
};
export const getLocksViaApi = async (persistentId: string): Promise<any> => {
return axios.get(`${TestConstants.TEST_API_URL}/datasets/:persistentId/locks?persistentId=${persistentId}`).then((response) => {
return response.data.data;
})
}

export const waitForNoLocks = async (persistentId: string, maxRetries = 20, delay = 1000): Promise<void> => {
for (let retry = 0; retry < maxRetries; retry++) {
await getLocksViaApi(persistentId)
.then(
response => {
if (Object.keys(response).length === 1) {
return;
}})
await new Promise(resolve => setTimeout(resolve, delay));
}
throw new Error('Max retries reached.');
};

0 comments on commit a50347b

Please sign in to comment.