]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - shared/extra-utils/mock-servers/mock-instances-index.ts
Fix admin edition disabling feature
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / mock-servers / mock-instances-index.ts
index 2604eda0363fe22800e8dea34a8120c4da875dd0..43c2e9f6ec6466a9d6e7f47b278d444ad8aa5517 100644 (file)
@@ -1,10 +1,15 @@
-import * as express from 'express'
+import express from 'express'
+import { Server } from 'http'
+import { randomInt } from '@shared/core-utils'
+import { terminateServer } from './utils'
 
 export class MockInstancesIndex {
+  private server: Server
+
   private readonly indexInstances: { host: string, createdAt: string }[] = []
 
   initialize () {
-    return new Promise<void>(res => {
+    return new Promise<number>(res => {
       const app = express()
 
       app.use('/', (req: express.Request, res: express.Response, next: express.NextFunction) => {
@@ -28,11 +33,16 @@ export class MockInstancesIndex {
         })
       })
 
-      app.listen(42101, () => res())
+      const port = 42000 + randomInt(1, 1000)
+      this.server = app.listen(port, () => res(port))
     })
   }
 
   addInstance (host: string) {
     this.indexInstances.push({ host, createdAt: new Date().toISOString() })
   }
+
+  terminate () {
+    return terminateServer(this.server)
+  }
 }