1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-23 18:53:36 +02:00

Tests: Fixed potential timing issue in tests

Might fix this: https://travis-ci.org/github/laurent22/joplin/jobs/752585346#L1352
This commit is contained in:
Laurent Cozic 2021-01-03 16:27:51 +00:00
parent 4f2d316db4
commit 1dbeff1908
2 changed files with 21 additions and 10 deletions

View File

@ -2,7 +2,7 @@ import { PaginationOrderDir } from '@joplin/lib/models/utils/types';
import Api, { RequestMethod } from '@joplin/lib/services/rest/Api';
import shim from '@joplin/lib/shim';
const { setupDatabaseAndSynchronizer, switchClient, checkThrowAsync, db } = require('./test-utils.js');
const { setupDatabaseAndSynchronizer, switchClient, checkThrowAsync, db, msleep } = require('./test-utils.js');
const Folder = require('@joplin/lib/models/Folder');
const Resource = require('@joplin/lib/models/Resource');
const Note = require('@joplin/lib/models/Note');
@ -11,14 +11,6 @@ const NoteTag = require('@joplin/lib/models/NoteTag');
const ResourceService = require('@joplin/lib/services/ResourceService').default;
const SearchEngine = require('@joplin/lib/services/searchengine/SearchEngine');
async function msleep(ms: number) {
return new Promise((resolve) => {
shim.setTimeout(() => {
resolve();
}, ms);
});
}
const createFolderForPagination = async (num: number, time: number) => {
await Folder.save({
title: `folder${num}`,

View File

@ -203,9 +203,28 @@ function sleep(n: number) {
}
function msleep(ms: number) {
// It seems setTimeout can sometimes last less time than the provided
// interval:
//
// https://stackoverflow.com/a/50912029/561309
//
// This can cause issues in tests where we expect the actual duration to be
// the same as the provided interval or more, but not less. So the code
// below check that the elapsed time is no less than the provided interval,
// and if it is, it waits a bit longer.
const startTime = Date.now();
return new Promise((resolve) => {
shim.setTimeout(() => {
resolve(null);
if (Date.now() - startTime < ms) {
const iid = setInterval(() => {
if (Date.now() - startTime >= ms) {
clearInterval(iid);
resolve(null);
}
}, 2);
} else {
resolve(null);
}
}, ms);
});
}