diff options
author | Chocobozzz <me@florianbigard.com> | 2023-07-31 14:34:36 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-08-11 15:02:33 +0200 |
commit | 3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch) | |
tree | e4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/tests/api/live/live-socket-messages.ts | |
parent | 04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff) | |
download | PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip |
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge
conflicts, but it's a major step forward:
* Server can be faster at startup because imports() are async and we can
easily lazy import big modules
* Angular doesn't seem to support ES import (with .js extension), so we
had to correctly organize peertube into a monorepo:
* Use yarn workspace feature
* Use typescript reference projects for dependencies
* Shared projects have been moved into "packages", each one is now a
node module (with a dedicated package.json/tsconfig.json)
* server/tools have been moved into apps/ and is now a dedicated app
bundled and published on NPM so users don't have to build peertube
cli tools manually
* server/tests have been moved into packages/ so we don't compile
them every time we want to run the server
* Use isolatedModule option:
* Had to move from const enum to const
(https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums)
* Had to explictely specify "type" imports when used in decorators
* Prefer tsx (that uses esbuild under the hood) instead of ts-node to
load typescript files (tests with mocha or scripts):
* To reduce test complexity as esbuild doesn't support decorator
metadata, we only test server files that do not import server
models
* We still build tests files into js files for a faster CI
* Remove unmaintained peertube CLI import script
* Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'server/tests/api/live/live-socket-messages.ts')
-rw-r--r-- | server/tests/api/live/live-socket-messages.ts | 186 |
1 files changed, 0 insertions, 186 deletions
diff --git a/server/tests/api/live/live-socket-messages.ts b/server/tests/api/live/live-socket-messages.ts deleted file mode 100644 index 0cccd1594..000000000 --- a/server/tests/api/live/live-socket-messages.ts +++ /dev/null | |||
@@ -1,186 +0,0 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | ||
2 | |||
3 | import { expect } from 'chai' | ||
4 | import { wait } from '@shared/core-utils' | ||
5 | import { LiveVideoEventPayload, VideoPrivacy, VideoState } from '@shared/models' | ||
6 | import { | ||
7 | cleanupTests, | ||
8 | createMultipleServers, | ||
9 | doubleFollow, | ||
10 | PeerTubeServer, | ||
11 | setAccessTokensToServers, | ||
12 | setDefaultVideoChannel, | ||
13 | stopFfmpeg, | ||
14 | waitJobs, | ||
15 | waitUntilLivePublishedOnAllServers | ||
16 | } from '@shared/server-commands' | ||
17 | |||
18 | describe('Test live socket messages', function () { | ||
19 | let servers: PeerTubeServer[] = [] | ||
20 | |||
21 | before(async function () { | ||
22 | this.timeout(120000) | ||
23 | |||
24 | servers = await createMultipleServers(2) | ||
25 | |||
26 | // Get the access tokens | ||
27 | await setAccessTokensToServers(servers) | ||
28 | await setDefaultVideoChannel(servers) | ||
29 | |||
30 | await servers[0].config.updateCustomSubConfig({ | ||
31 | newConfig: { | ||
32 | live: { | ||
33 | enabled: true, | ||
34 | allowReplay: true, | ||
35 | transcoding: { | ||
36 | enabled: false | ||
37 | } | ||
38 | } | ||
39 | } | ||
40 | }) | ||
41 | |||
42 | // Server 1 and server 2 follow each other | ||
43 | await doubleFollow(servers[0], servers[1]) | ||
44 | }) | ||
45 | |||
46 | describe('Live socket messages', function () { | ||
47 | |||
48 | async function createLiveWrapper () { | ||
49 | const liveAttributes = { | ||
50 | name: 'live video', | ||
51 | channelId: servers[0].store.channel.id, | ||
52 | privacy: VideoPrivacy.PUBLIC | ||
53 | } | ||
54 | |||
55 | const { uuid } = await servers[0].live.create({ fields: liveAttributes }) | ||
56 | return uuid | ||
57 | } | ||
58 | |||
59 | it('Should correctly send a message when the live starts and ends', async function () { | ||
60 | this.timeout(60000) | ||
61 | |||
62 | const localStateChanges: VideoState[] = [] | ||
63 | const remoteStateChanges: VideoState[] = [] | ||
64 | |||
65 | const liveVideoUUID = await createLiveWrapper() | ||
66 | await waitJobs(servers) | ||
67 | |||
68 | { | ||
69 | const videoId = await servers[0].videos.getId({ uuid: liveVideoUUID }) | ||
70 | |||
71 | const localSocket = servers[0].socketIO.getLiveNotificationSocket() | ||
72 | localSocket.on('state-change', data => localStateChanges.push(data.state)) | ||
73 | localSocket.emit('subscribe', { videoId }) | ||
74 | } | ||
75 | |||
76 | { | ||
77 | const videoId = await servers[1].videos.getId({ uuid: liveVideoUUID }) | ||
78 | |||
79 | const remoteSocket = servers[1].socketIO.getLiveNotificationSocket() | ||
80 | remoteSocket.on('state-change', data => remoteStateChanges.push(data.state)) | ||
81 | remoteSocket.emit('subscribe', { videoId }) | ||
82 | } | ||
83 | |||
84 | const ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID }) | ||
85 | |||
86 | await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID) | ||
87 | await waitJobs(servers) | ||
88 | |||
89 | for (const stateChanges of [ localStateChanges, remoteStateChanges ]) { | ||
90 | expect(stateChanges).to.have.length.at.least(1) | ||
91 | expect(stateChanges[stateChanges.length - 1]).to.equal(VideoState.PUBLISHED) | ||
92 | } | ||
93 | |||
94 | await stopFfmpeg(ffmpegCommand) | ||
95 | |||
96 | for (const server of servers) { | ||
97 | await server.live.waitUntilEnded({ videoId: liveVideoUUID }) | ||
98 | } | ||
99 | await waitJobs(servers) | ||
100 | |||
101 | for (const stateChanges of [ localStateChanges, remoteStateChanges ]) { | ||
102 | expect(stateChanges).to.have.length.at.least(2) | ||
103 | expect(stateChanges[stateChanges.length - 1]).to.equal(VideoState.LIVE_ENDED) | ||
104 | } | ||
105 | }) | ||
106 | |||
107 | it('Should correctly send views change notification', async function () { | ||
108 | this.timeout(60000) | ||
109 | |||
110 | let localLastVideoViews = 0 | ||
111 | let remoteLastVideoViews = 0 | ||
112 | |||
113 | const liveVideoUUID = await createLiveWrapper() | ||
114 | await waitJobs(servers) | ||
115 | |||
116 | { | ||
117 | const videoId = await servers[0].videos.getId({ uuid: liveVideoUUID }) | ||
118 | |||
119 | const localSocket = servers[0].socketIO.getLiveNotificationSocket() | ||
120 | localSocket.on('views-change', (data: LiveVideoEventPayload) => { localLastVideoViews = data.viewers }) | ||
121 | localSocket.emit('subscribe', { videoId }) | ||
122 | } | ||
123 | |||
124 | { | ||
125 | const videoId = await servers[1].videos.getId({ uuid: liveVideoUUID }) | ||
126 | |||
127 | const remoteSocket = servers[1].socketIO.getLiveNotificationSocket() | ||
128 | remoteSocket.on('views-change', (data: LiveVideoEventPayload) => { remoteLastVideoViews = data.viewers }) | ||
129 | remoteSocket.emit('subscribe', { videoId }) | ||
130 | } | ||
131 | |||
132 | const ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID }) | ||
133 | |||
134 | await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID) | ||
135 | await waitJobs(servers) | ||
136 | |||
137 | expect(localLastVideoViews).to.equal(0) | ||
138 | expect(remoteLastVideoViews).to.equal(0) | ||
139 | |||
140 | await servers[0].views.simulateView({ id: liveVideoUUID }) | ||
141 | await servers[1].views.simulateView({ id: liveVideoUUID }) | ||
142 | |||
143 | await waitJobs(servers) | ||
144 | |||
145 | expect(localLastVideoViews).to.equal(2) | ||
146 | expect(remoteLastVideoViews).to.equal(2) | ||
147 | |||
148 | await stopFfmpeg(ffmpegCommand) | ||
149 | }) | ||
150 | |||
151 | it('Should not receive a notification after unsubscribe', async function () { | ||
152 | this.timeout(120000) | ||
153 | |||
154 | const stateChanges: VideoState[] = [] | ||
155 | |||
156 | const liveVideoUUID = await createLiveWrapper() | ||
157 | await waitJobs(servers) | ||
158 | |||
159 | const videoId = await servers[0].videos.getId({ uuid: liveVideoUUID }) | ||
160 | |||
161 | const socket = servers[0].socketIO.getLiveNotificationSocket() | ||
162 | socket.on('state-change', data => stateChanges.push(data.state)) | ||
163 | socket.emit('subscribe', { videoId }) | ||
164 | |||
165 | const command = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID }) | ||
166 | |||
167 | await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID) | ||
168 | await waitJobs(servers) | ||
169 | |||
170 | // Notifier waits before sending a notification | ||
171 | await wait(10000) | ||
172 | |||
173 | expect(stateChanges).to.have.lengthOf(1) | ||
174 | socket.emit('unsubscribe', { videoId }) | ||
175 | |||
176 | await stopFfmpeg(command) | ||
177 | await waitJobs(servers) | ||
178 | |||
179 | expect(stateChanges).to.have.lengthOf(1) | ||
180 | }) | ||
181 | }) | ||
182 | |||
183 | after(async function () { | ||
184 | await cleanupTests(servers) | ||
185 | }) | ||
186 | }) | ||