From 3cd0734fd9b0ff21aaef02317a874e8f1c06e027 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 13 Jun 2018 10:06:50 +0200 Subject: Improve tests when waiting pending jobs --- server/tests/utils/miscs/miscs.ts | 1 + server/tests/utils/server/follows.ts | 3 ++- server/tests/utils/server/jobs.ts | 41 ++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) (limited to 'server/tests/utils') diff --git a/server/tests/utils/miscs/miscs.ts b/server/tests/utils/miscs/miscs.ts index 5e46004a7..7ac60a983 100644 --- a/server/tests/utils/miscs/miscs.ts +++ b/server/tests/utils/miscs/miscs.ts @@ -5,6 +5,7 @@ import { isAbsolute, join } from 'path' import * as request from 'supertest' import * as WebTorrent from 'webtorrent' import { readFileBufferPromise } from '../../../helpers/core-utils' +import { ServerInfo } from '..' const expect = chai.expect let webtorrent = new WebTorrent() diff --git a/server/tests/utils/server/follows.ts b/server/tests/utils/server/follows.ts index 82e89175c..d21fb5e58 100644 --- a/server/tests/utils/server/follows.ts +++ b/server/tests/utils/server/follows.ts @@ -1,6 +1,7 @@ import * as request from 'supertest' import { wait } from '../miscs/miscs' import { ServerInfo } from './servers' +import { waitJobs } from './jobs' function getFollowersListPaginationAndSort (url: string, start: number, count: number, sort: string) { const path = '/api/v1/server/followers' @@ -61,7 +62,7 @@ async function doubleFollow (server1: ServerInfo, server2: ServerInfo) { ]) // Wait request propagation - await wait(10000) + await waitJobs([ server1, server2 ]) return true } diff --git a/server/tests/utils/server/jobs.ts b/server/tests/utils/server/jobs.ts index 4053dd40b..375e76f93 100644 --- a/server/tests/utils/server/jobs.ts +++ b/server/tests/utils/server/jobs.ts @@ -1,5 +1,6 @@ import * as request from 'supertest' import { JobState } from '../../../../shared/models' +import { ServerInfo, wait } from '../index' function getJobsList (url: string, accessToken: string, state: JobState) { const path = '/api/v1/jobs/' + state @@ -26,9 +27,49 @@ function getJobsListPaginationAndSort (url: string, accessToken: string, state: .expect('Content-Type', /json/) } +async function waitJobs (serversArg: ServerInfo[] | ServerInfo) { + let servers: ServerInfo[] + + if (Array.isArray(serversArg) === false) servers = [ serversArg as ServerInfo ] + else servers = serversArg as ServerInfo[] + + const states: JobState[] = [ 'inactive', 'active', 'delayed' ] + const tasks: Promise[] = [] + let pendingRequests: boolean + + do { + pendingRequests = false + + // Check if each server has pending request + for (const server of servers) { + for (const state of states) { + const p = getJobsListPaginationAndSort(server.url, server.accessToken, state, 0, 10, '-createdAt') + .then(res => { + if (res.body.total > 0) pendingRequests = true + }) + tasks.push(p) + } + } + + await Promise.all(tasks) + + // Retry, in case of new jobs were created + if (pendingRequests === false) { + await wait(1000) + + await Promise.all(tasks) + } + + if (pendingRequests) { + await wait(1000) + } + } while (pendingRequests) +} + // --------------------------------------------------------------------------- export { getJobsList, + waitJobs, getJobsListPaginationAndSort } -- cgit v1.2.3