mirror of
https://github.com/laurent22/joplin.git
synced 2025-01-26 18:58:21 +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:
parent
4f2d316db4
commit
1dbeff1908
@ -2,7 +2,7 @@ import { PaginationOrderDir } from '@joplin/lib/models/utils/types';
|
|||||||
import Api, { RequestMethod } from '@joplin/lib/services/rest/Api';
|
import Api, { RequestMethod } from '@joplin/lib/services/rest/Api';
|
||||||
import shim from '@joplin/lib/shim';
|
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 Folder = require('@joplin/lib/models/Folder');
|
||||||
const Resource = require('@joplin/lib/models/Resource');
|
const Resource = require('@joplin/lib/models/Resource');
|
||||||
const Note = require('@joplin/lib/models/Note');
|
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 ResourceService = require('@joplin/lib/services/ResourceService').default;
|
||||||
const SearchEngine = require('@joplin/lib/services/searchengine/SearchEngine');
|
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) => {
|
const createFolderForPagination = async (num: number, time: number) => {
|
||||||
await Folder.save({
|
await Folder.save({
|
||||||
title: `folder${num}`,
|
title: `folder${num}`,
|
||||||
|
@ -203,9 +203,28 @@ function sleep(n: number) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function msleep(ms: 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) => {
|
return new Promise((resolve) => {
|
||||||
shim.setTimeout(() => {
|
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);
|
}, ms);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user