aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/utils
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-06-13 10:06:50 +0200
committerChocobozzz <me@florianbigard.com>2018-06-13 10:22:53 +0200
commit3cd0734fd9b0ff21aaef02317a874e8f1c06e027 (patch)
tree9e8622d269919addd35b462141ab5f22236aa6f4 /server/tests/utils
parent2186386cca113506791583cb07d6ccacba7af4e0 (diff)
downloadPeerTube-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.ts1
-rw-r--r--server/tests/utils/server/follows.ts3
-rw-r--r--server/tests/utils/server/jobs.ts41
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'
5import * as request from 'supertest' 5import * as request from 'supertest'
6import * as WebTorrent from 'webtorrent' 6import * as WebTorrent from 'webtorrent'
7import { readFileBufferPromise } from '../../../helpers/core-utils' 7import { readFileBufferPromise } from '../../../helpers/core-utils'
8import { ServerInfo } from '..'
8 9
9const expect = chai.expect 10const expect = chai.expect
10let webtorrent = new WebTorrent() 11let 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 @@
1import * as request from 'supertest' 1import * as request from 'supertest'
2import { wait } from '../miscs/miscs' 2import { wait } from '../miscs/miscs'
3import { ServerInfo } from './servers' 3import { ServerInfo } from './servers'
4import { waitJobs } from './jobs'
4 5
5function getFollowersListPaginationAndSort (url: string, start: number, count: number, sort: string) { 6function 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 @@
1import * as request from 'supertest' 1import * as request from 'supertest'
2import { JobState } from '../../../../shared/models' 2import { JobState } from '../../../../shared/models'
3import { ServerInfo, wait } from '../index'
3 4
4function getJobsList (url: string, accessToken: string, state: JobState) { 5function 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
30async 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
31export { 71export {
32 getJobsList, 72 getJobsList,
73 waitJobs,
33 getJobsListPaginationAndSort 74 getJobsListPaginationAndSort
34} 75}