From 51353d9a035fb6b81f903a8b5f391292841649fd Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 9 Nov 2021 10:11:20 +0100 Subject: Refactor video views Introduce viewers attribute for live videos Count views for live videos Reduce delay to see the viewer update for lives Add ability to configure video views buffer interval and view ip expiration --- server/tests/api/live/live-views.ts | 109 +++++++++++++++++++----------------- server/tests/api/server/jobs.ts | 2 +- 2 files changed, 59 insertions(+), 52 deletions(-) (limited to 'server/tests') diff --git a/server/tests/api/live/live-views.ts b/server/tests/api/live/live-views.ts index 5e3a79c64..9186af8e7 100644 --- a/server/tests/api/live/live-views.ts +++ b/server/tests/api/live/live-views.ts @@ -19,7 +19,7 @@ import { const expect = chai.expect -describe('Test live', function () { +describe('Live views', function () { let servers: PeerTubeServer[] = [] before(async function () { @@ -47,79 +47,86 @@ describe('Test live', function () { await doubleFollow(servers[0], servers[1]) }) - describe('Live views', function () { - let liveVideoId: string - let command: FfmpegCommand + let liveVideoId: string + let command: FfmpegCommand - async function countViews (expected: number) { - for (const server of servers) { - const video = await server.videos.get({ id: liveVideoId }) - expect(video.views).to.equal(expected) - } + async function countViewers (expectedViewers: number) { + for (const server of servers) { + const video = await server.videos.get({ id: liveVideoId }) + expect(video.viewers).to.equal(expectedViewers) } + } - before(async function () { - this.timeout(30000) + async function countViews (expectedViews: number) { + for (const server of servers) { + const video = await server.videos.get({ id: liveVideoId }) + expect(video.views).to.equal(expectedViews) + } + } - const liveAttributes = { - name: 'live video', - channelId: servers[0].store.channel.id, - privacy: VideoPrivacy.PUBLIC - } + before(async function () { + this.timeout(30000) - const live = await servers[0].live.create({ fields: liveAttributes }) - liveVideoId = live.uuid + const liveAttributes = { + name: 'live video', + channelId: servers[0].store.channel.id, + privacy: VideoPrivacy.PUBLIC + } - command = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoId }) - await waitUntilLivePublishedOnAllServers(servers, liveVideoId) - await waitJobs(servers) - }) + const live = await servers[0].live.create({ fields: liveAttributes }) + liveVideoId = live.uuid - it('Should display no views for a live', async function () { - await countViews(0) - }) + command = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoId }) + await waitUntilLivePublishedOnAllServers(servers, liveVideoId) + await waitJobs(servers) + }) - it('Should view a live twice and display 1 view', async function () { - this.timeout(30000) + it('Should display no views and viewers for a live', async function () { + await countViews(0) + await countViewers(0) + }) - await servers[0].videos.view({ id: liveVideoId }) - await servers[0].videos.view({ id: liveVideoId }) + it('Should view a live twice and display 1 view/viewer', async function () { + this.timeout(30000) - await wait(7000) + await servers[0].videos.view({ id: liveVideoId }) + await servers[0].videos.view({ id: liveVideoId }) - await waitJobs(servers) + await waitJobs(servers) + await countViewers(1) - await countViews(1) - }) + await wait(7000) + await countViews(1) + }) - it('Should wait and display 0 views', async function () { - this.timeout(30000) + it('Should wait and display 0 viewers while still have 1 view', async function () { + this.timeout(30000) - await wait(12000) - await waitJobs(servers) + await wait(12000) + await waitJobs(servers) - await countViews(0) - }) + await countViews(1) + await countViewers(0) + }) - it('Should view a live on a remote and on local and display 2 views', async function () { - this.timeout(30000) + it('Should view a live on a remote and on local and display 2 viewers and 3 views', async function () { + this.timeout(30000) - await servers[0].videos.view({ id: liveVideoId }) - await servers[1].videos.view({ id: liveVideoId }) - await servers[1].videos.view({ id: liveVideoId }) + await servers[0].videos.view({ id: liveVideoId }) + await servers[1].videos.view({ id: liveVideoId }) + await servers[1].videos.view({ id: liveVideoId }) + await waitJobs(servers) - await wait(7000) - await waitJobs(servers) + await countViewers(2) - await countViews(2) - }) + await wait(7000) + await waitJobs(servers) - after(async function () { - await stopFfmpeg(command) - }) + await countViews(3) }) after(async function () { + await stopFfmpeg(command) await cleanupTests(servers) }) }) diff --git a/server/tests/api/server/jobs.ts b/server/tests/api/server/jobs.ts index 8c4e01226..5d946f5e8 100644 --- a/server/tests/api/server/jobs.ts +++ b/server/tests/api/server/jobs.ts @@ -56,7 +56,7 @@ describe('Test jobs', function () { let job = body.data[0] // Skip repeat jobs - if (job.type === 'videos-views') job = body.data[1] + if (job.type === 'videos-views-stats') job = body.data[1] expect(job.state).to.equal('completed') expect(job.type.startsWith('activitypub-')).to.be.true -- cgit v1.2.3