diff options
author | Chocobozzz <me@florianbigard.com> | 2018-06-13 10:06:50 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-06-13 10:22:53 +0200 |
commit | 3cd0734fd9b0ff21aaef02317a874e8f1c06e027 (patch) | |
tree | 9e8622d269919addd35b462141ab5f22236aa6f4 /server/tests/utils | |
parent | 2186386cca113506791583cb07d6ccacba7af4e0 (diff) | |
download | PeerTube-3cd0734fd9b0ff21aaef02317a874e8f1c06e027.tar.gz PeerTube-3cd0734fd9b0ff21aaef02317a874e8f1c06e027.tar.zst PeerTube-3cd0734fd9b0ff21aaef02317a874e8f1c06e027.zip |
Improve tests when waiting pending jobs
Diffstat (limited to 'server/tests/utils')
-rw-r--r-- | server/tests/utils/miscs/miscs.ts | 1 | ||||
-rw-r--r-- | server/tests/utils/server/follows.ts | 3 | ||||
-rw-r--r-- | server/tests/utils/server/jobs.ts | 41 |
3 files changed, 44 insertions, 1 deletions
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' | |||
5 | import * as request from 'supertest' | 5 | import * as request from 'supertest' |
6 | import * as WebTorrent from 'webtorrent' | 6 | import * as WebTorrent from 'webtorrent' |
7 | import { readFileBufferPromise } from '../../../helpers/core-utils' | 7 | import { readFileBufferPromise } from '../../../helpers/core-utils' |
8 | import { ServerInfo } from '..' | ||
8 | 9 | ||
9 | const expect = chai.expect | 10 | const expect = chai.expect |
10 | let webtorrent = new WebTorrent() | 11 | 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 @@ | |||
1 | import * as request from 'supertest' | 1 | import * as request from 'supertest' |
2 | import { wait } from '../miscs/miscs' | 2 | import { wait } from '../miscs/miscs' |
3 | import { ServerInfo } from './servers' | 3 | import { ServerInfo } from './servers' |
4 | import { waitJobs } from './jobs' | ||
4 | 5 | ||
5 | function getFollowersListPaginationAndSort (url: string, start: number, count: number, sort: string) { | 6 | function getFollowersListPaginationAndSort (url: string, start: number, count: number, sort: string) { |
6 | const path = '/api/v1/server/followers' | 7 | const path = '/api/v1/server/followers' |
@@ -61,7 +62,7 @@ async function doubleFollow (server1: ServerInfo, server2: ServerInfo) { | |||
61 | ]) | 62 | ]) |
62 | 63 | ||
63 | // Wait request propagation | 64 | // Wait request propagation |
64 | await wait(10000) | 65 | await waitJobs([ server1, server2 ]) |
65 | 66 | ||
66 | return true | 67 | return true |
67 | } | 68 | } |
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 @@ | |||
1 | import * as request from 'supertest' | 1 | import * as request from 'supertest' |
2 | import { JobState } from '../../../../shared/models' | 2 | import { JobState } from '../../../../shared/models' |
3 | import { ServerInfo, wait } from '../index' | ||
3 | 4 | ||
4 | function getJobsList (url: string, accessToken: string, state: JobState) { | 5 | function getJobsList (url: string, accessToken: string, state: JobState) { |
5 | const path = '/api/v1/jobs/' + state | 6 | const path = '/api/v1/jobs/' + state |
@@ -26,9 +27,49 @@ function getJobsListPaginationAndSort (url: string, accessToken: string, state: | |||
26 | .expect('Content-Type', /json/) | 27 | .expect('Content-Type', /json/) |
27 | } | 28 | } |
28 | 29 | ||
30 | async function waitJobs (serversArg: ServerInfo[] | ServerInfo) { | ||
31 | let servers: ServerInfo[] | ||
32 | |||
33 | if (Array.isArray(serversArg) === false) servers = [ serversArg as ServerInfo ] | ||
34 | else servers = serversArg as ServerInfo[] | ||
35 | |||
36 | const states: JobState[] = [ 'inactive', 'active', 'delayed' ] | ||
37 | const tasks: Promise<any>[] = [] | ||
38 | let pendingRequests: boolean | ||
39 | |||
40 | do { | ||
41 | pendingRequests = false | ||
42 | |||
43 | // Check if each server has pending request | ||
44 | for (const server of servers) { | ||
45 | for (const state of states) { | ||
46 | const p = getJobsListPaginationAndSort(server.url, server.accessToken, state, 0, 10, '-createdAt') | ||
47 | .then(res => { | ||
48 | if (res.body.total > 0) pendingRequests = true | ||
49 | }) | ||
50 | tasks.push(p) | ||
51 | } | ||
52 | } | ||
53 | |||
54 | await Promise.all(tasks) | ||
55 | |||
56 | // Retry, in case of new jobs were created | ||
57 | if (pendingRequests === false) { | ||
58 | await wait(1000) | ||
59 | |||
60 | await Promise.all(tasks) | ||
61 | } | ||
62 | |||
63 | if (pendingRequests) { | ||
64 | await wait(1000) | ||
65 | } | ||
66 | } while (pendingRequests) | ||
67 | } | ||
68 | |||
29 | // --------------------------------------------------------------------------- | 69 | // --------------------------------------------------------------------------- |
30 | 70 | ||
31 | export { | 71 | export { |
32 | getJobsList, | 72 | getJobsList, |
73 | waitJobs, | ||
33 | getJobsListPaginationAndSort | 74 | getJobsListPaginationAndSort |
34 | } | 75 | } |