Repository: elastic/synthetics
Pull request #186 (2021-01-12 12:43:39)

test: run unit tests against browser service
This is part of browser service integration effort. This PR runs our tests against browser service to make sure the application logic works correctly. This effectively makes sure that the synthetics agent behaves the same in both running with or without browser service.



apmmachine: 
## :green_heart: Build Succeeded


<!-- BUILD BADGES-->
> _the below badges are clickable and redirect to their specific view in the CI or DOCS_
[![Pipeline View](https://img.shields.io/badge/pipeline-pipeline%20-green)](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-rum%2Felastic-synthetics%2FPR-186/detail/PR-186/1//pipeline) [![Test View](https://img.shields.io/badge/test-test-green)](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-rum%2Felastic-synthetics%2FPR-186/detail/PR-186/1//tests) [![Changes](https://img.shields.io/badge/changes-changes-green)](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-rum%2Felastic-synthetics%2FPR-186/detail/PR-186/1//changes) [![Artifacts](https://img.shields.io/badge/artifacts-artifacts-yellow)](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-rum%2Felastic-synthetics%2FPR-186/detail/PR-186/1//artifacts) [![preview](https://img.shields.io/badge/docs-preview-yellowgreen)](http://synthetics_186.docs-preview.app.elstc.co/diff)

<!-- BUILD SUMMARY-->
<details><summary>Expand to view the summary</summary>
<p>

#### Build stats

* Build Cause: `Pull request #186 opened`

* Start Time: 2021-01-12T12:43:47.610+0000

* Duration: 15 min 26 sec




#### Test stats :test_tube:

| Test         | Results                         |
| ------------ | :-----------------------------: |
| Failed       | 0  |
| Passed       | 47  |
| Skipped      | 0 |
| Total        | 47   |


</p>
</details>

<!-- TEST RESULTS IF ANY-->


<!-- STEPS ERRORS IF ANY -->








<!--COMMENT_GENERATED_WITH_ID_comment.id-->
vigneshshanmugam: @jahtalab Once you add the missing tests for `wsEndpoint` to the tests in `gatherer.test.ts` and `network.test.ts`, you can see the process hanging error that i mentioned in review. 
jahtalab: @vigneshshanmugam, the `wsEndpoint` is already added to `gatherer.test.ts` and `network.test.ts` and tests run successfully both on my machine as well as the CI. 

Do you see this issue locally on your machine? Also can you elaborate on the issue that you're experiencing?
vigneshshanmugam: @jahtalab Its for these tests, I can reproduce the process not being killed issue - 

1. https://github.com/elastic/synthetics/blob/b55ab0a3812cdb301f3eef58e85ebf4718c56ccf/__tests__/core/gatherer.test.ts#L39-L49

2. https://github.com/elastic/synthetics/blob/b55ab0a3812cdb301f3eef58e85ebf4718c56ccf/__tests__/plugins/network.test.ts#L55-L73

You can add `wsEndpoint` to those tests and then `WSENDPOINT=ws://localhost:9322 npx jest __tests__/plugins/network.test.ts` should reproduce the process issue. 
jahtalab: @vigneshshanmugam , the problem with tests hanging is not related to the how the tests are running against browser service, but it's related to browser service not handling `SIGTERM`

We still need to handle process events properly in the browser service and as part of the [Browser service integration](https://github.com/elastic/uptime-dev/issues/38), I've added a task for this.
jahtalab: jenkins retest this

Patch:
diff --git a/jest.config.js b/jest.config.js
index b0356e5..308afdd 100644
--- a/jest.config.js
+++ b/jest.config.js
@@ -30,5 +30,7 @@ module.exports = {
   coverageDirectory: 'coverage',
   coverageProvider: 'v8',
   testPathIgnorePatterns: ['dist'],
-  modulePathIgnorePatterns: ['/e2e/']
+  modulePathIgnorePatterns: ['/e2e/', '/utils/'],
+  globalSetup: './__tests__/utils/jest-global-setup.ts',
+  globalTeardown: './__tests__/utils/jest-global-teardown.ts',
 };
diff --git a/package.json b/package.json
index 3cd3cad..fecfc24 100644
--- a/package.json
+++ b/package.json
@@ -11,7 +11,9 @@
     "watch": "tsc -w",
     "lint": "eslint . --rulesdir utils/eslint-rules",
     "lint:fix": "npm run lint -- --fix",
-    "test": "jest",
+    "test": "npm run test:unit && npm run test:browser-service",
+    "test:unit": "jest",
+    "test:browser-service": "WSENDPOINT=ws://localhost:9322 npm run test:unit",
     "coverage": "jest --coverage",
     "docker": "./build-docker.sh"
   },
@@ -62,4 +64,4 @@
     "prettier": "^2.2.1",
     "ts-jest": "^26.4.4"
   }
-}
+}
\ No newline at end of file
diff --git a/src/browser-service.ts b/src/browser-service.ts
index 99470cc..91cd2f0 100644
--- a/src/browser-service.ts
+++ b/src/browser-service.ts
@@ -31,6 +31,12 @@ import { chromium } from 'playwright-chromium';
 const proxy = createProxyServer();
 const proxyServer = createServer();
 
+/**
+ * TODO: refactor browser service into a class
+ * to manage this state with init/dispose.
+ */
+let closeCallbacks = [];
+
 proxyServer.on('upgrade', async function (req, socket, head) {
   const browserServer = await chromium.launchServer({ headless: true });
   const wsEndpoint = browserServer.wsEndpoint();
@@ -39,9 +45,14 @@ proxyServer.on('upgrade', async function (req, socket, head) {
   console.log(`New browser: ${wsEndpoint}`);
   proxy.ws(req, socket, head, { target: origin });
   const closeBrowser = async () => {
+    const index = closeCallbacks.indexOf(closeBrowser);
+    if (index >= 0) {
+      closeCallbacks.splice(index, 1);
+    }
     await browserServer.close();
     console.log(`Socket closed: ${wsEndpoint}`);
   };
+  closeCallbacks.push(closeBrowser);
   socket.on('close', closeBrowser);
   socket.on('error', closeBrowser);
 });
@@ -50,3 +61,12 @@ const port = 9322;
 proxyServer.listen(port, () => {
   console.log(`Listening on port: ${port}`);
 });
+
+process.on('SIGTERM', async () => {
+  for (let i = 0; i < closeCallbacks.length; i++) {
+    const callback = closeCallbacks[i];
+    await callback();
+  }
+  closeCallbacks = [];
+  process.exit(0);
+});


Test patch:
diff --git a/__tests__/core/gatherer.test.ts b/__tests__/core/gatherer.test.ts
index b095dcc..adfc062 100644
--- a/__tests__/core/gatherer.test.ts
+++ b/__tests__/core/gatherer.test.ts
@@ -26,18 +26,19 @@
 import { Gatherer } from '../../src/core/gatherer';
 import { PluginManager } from '../../src/plugins';
 import { NetworkManager } from '../../src/plugins/network';
+import { wsEndpoint } from '../utils/test-config';
 
 jest.mock('../../src/plugins/network');
 
 describe('Gatherer', () => {
   it('boot and dispose driver', async () => {
-    const driver = await Gatherer.setupDriver(true);
+    const driver = await Gatherer.setupDriver(true, wsEndpoint);
     expect(typeof driver.page.goto).toBe('function');
     await Gatherer.dispose(driver);
   });
 
   it('begin recording based on flags', async () => {
-    const driver = await Gatherer.setupDriver(true);
+    const driver = await Gatherer.setupDriver(true, wsEndpoint);
     const pluginManager = await Gatherer.beginRecording(driver, {
       network: true,
     });
diff --git a/__tests__/core/runner.test.ts b/__tests__/core/runner.test.ts
index 6722490..9d6cbf8 100644
--- a/__tests__/core/runner.test.ts
+++ b/__tests__/core/runner.test.ts
@@ -28,8 +28,9 @@ import { Gatherer } from '../../src/core/gatherer';
 import Runner from '../../src/core/runner';
 import { step, journey } from '../../src/core';
 import { Journey } from '../../src/dsl';
-import { Server } from '../../utils/server';
+import { Server } from '../utils/server';
 import { generateTempPath } from '../../src/helpers';
+import { wsEndpoint } from '../utils/test-config';
 
 describe('runner', () => {
   let runner: Runner, server: Server;
@@ -104,7 +105,7 @@ describe('runner', () => {
         end: expect.any(Number),
       });
     });
-    const result = await runner.runJourney(journey, {});
+    const result = await runner.runJourney(journey, { wsEndpoint });
     expect(result.status).toBe('succeeded');
   });
 
@@ -115,7 +116,7 @@ describe('runner', () => {
     journey.addStep('step2', async () => {
       throw error;
     });
-    const result = await runner.runJourney(journey, {});
+    const result = await runner.runJourney(journey, { wsEndpoint });
     expect(result).toEqual({
       status: 'failed',
       error,
@@ -126,7 +127,7 @@ describe('runner', () => {
     const journey = new Journey({ name: 'with hooks' }, noop);
     journey.addHook('before', noop);
     journey.addHook('after', noop);
-    const result = await runner.runJourney(journey, {});
+    const result = await runner.runJourney(journey, { wsEndpoint });
     expect(result).toEqual({
       status: 'succeeded',
     });
@@ -139,7 +140,7 @@ describe('runner', () => {
     journey.addHook('after', () => {
       throw error;
     });
-    const result = await runner.runJourney(journey, {});
+    const result = await runner.runJourney(journey, { wsEndpoint });
     expect(result).toEqual({
       status: 'failed',
       error,
@@ -221,6 +222,7 @@ describe('runner', () => {
     const journey = new Journey({ name }, noop);
     runner.addJourney(journey);
     const result = await runner.run({
+      wsEndpoint,
       outfd: fs.openSync(dest, 'w'),
     });
     expect(result).toEqual({
@@ -232,6 +234,7 @@ describe('runner', () => {
     runner.addJourney(new Journey({ name: 'j1' }, noop));
     runner.addJourney(new Journey({ name: 'j2' }, noop));
     const result = await runner.run({
+      wsEndpoint,
       outfd: fs.openSync(dest, 'w'),
       journeyName: 'j2',
     });
@@ -249,6 +252,7 @@ describe('runner', () => {
     });
     runner.addJourney(j2);
     const result = await runner.run({
+      wsEndpoint,
       outfd: fs.openSync(dest, 'w'),
     });
     expect(result).toEqual({
@@ -263,6 +267,7 @@ describe('runner', () => {
     let count = 0;
     runner.on('journey:register', () => count++);
     const result = await runner.run({
+      wsEndpoint,
       outfd: fs.openSync(dest, 'w'),
       dryRun: true,
     });
@@ -288,6 +293,7 @@ describe('runner', () => {
     runner.addJourney(j2);
 
     await runner.run({
+      wsEndpoint,
       outfd: fs.openSync(dest, 'w'),
     });
     expect(result).toEqual([
diff --git a/__tests__/plugins/browser-console.test.ts b/__tests__/plugins/browser-console.test.ts
index bd26be0..0abb670 100644
--- a/__tests__/plugins/browser-console.test.ts
+++ b/__tests__/plugins/browser-console.test.ts
@@ -25,7 +25,8 @@
 
 import { Gatherer } from '../../src/core/gatherer';
 import { BrowserConsole } from '../../src/plugins';
-import { Server } from '../../utils/server';
+import { Server } from '../utils/server';
+import { wsEndpoint } from '../utils/test-config';
 
 describe('BrowserConsole', () => {
   let server: Server;
@@ -37,7 +38,7 @@ describe('BrowserConsole', () => {
   });
 
   it('should capture browser console logs', async () => {
-    const driver = await Gatherer.setupDriver();
+    const driver = await Gatherer.setupDriver(true, wsEndpoint);
     const browserConsole = new BrowserConsole(driver.page);
     browserConsole.start();
     await driver.page.goto(server.TEST_PAGE);
diff --git a/__tests__/plugins/network.test.ts b/__tests__/plugins/network.test.ts
index 9f8a71e..4991817 100644
--- a/__tests__/plugins/network.test.ts
+++ b/__tests__/plugins/network.test.ts
@@ -25,7 +25,8 @@
 
 import { Gatherer } from '../../src/core/gatherer';
 import { NetworkManager } from '../../src/plugins/network';
-import { Server } from '../../utils/server';
+import { Server } from '../utils/server';
+import { wsEndpoint } from '../utils/test-config';
 
 describe('network', () => {
   let server: Server;
@@ -36,8 +37,8 @@ describe('network', () => {
     await server.close();
   });
 
-  it('capture network info', async () => {
-    const driver = await Gatherer.setupDriver();
+  it('should capture network info', async () => {
+    const driver = await Gatherer.setupDriver(true, wsEndpoint);
     const network = new NetworkManager();
     await network.start(driver.client);
     await driver.page.goto(server.TEST_PAGE);
@@ -53,7 +54,7 @@ describe('network', () => {
   });
 
   it('produce distinct events for redirects', async () => {
-    const driver = await Gatherer.setupDriver();
+    const driver = await Gatherer.setupDriver(true, wsEndpoint);
     const network = new NetworkManager();
     await network.start(driver.client);
     /**
diff --git a/__tests__/plugins/performance.test.ts b/__tests__/plugins/performance.test.ts
index c5d34ee..179854b 100644
--- a/__tests__/plugins/performance.test.ts
+++ b/__tests__/plugins/performance.test.ts
@@ -25,7 +25,8 @@
 
 import { Gatherer } from '../../src/core/gatherer';
 import { PerformanceManager } from '../../src/plugins/performance';
-import { Server } from '../../utils/server';
+import { Server } from '../utils/server';
+import { wsEndpoint } from '../utils/test-config';
 
 describe('performance', () => {
   let server: Server;
@@ -37,7 +38,7 @@ describe('performance', () => {
   });
 
   it('should capture page metrics', async () => {
-    const driver = await Gatherer.setupDriver();
+    const driver = await Gatherer.setupDriver(true, wsEndpoint);
     const performance = new PerformanceManager(driver.client);
     await performance.start();
     await driver.page.goto(server.TEST_PAGE);
diff --git a/__tests__/plugins/tracing.test.ts b/__tests__/plugins/tracing.test.ts
index dc70691..de50e7a 100644
--- a/__tests__/plugins/tracing.test.ts
+++ b/__tests__/plugins/tracing.test.ts
@@ -25,7 +25,8 @@
 
 import { Tracing, filterFilmstrips } from '../../src/plugins/tracing';
 import { Gatherer } from '../../src/core/gatherer';
-import { Server } from '../../utils/server';
+import { Server } from '../utils/server';
+import { wsEndpoint } from '../utils/test-config';
 
 describe('tracing', () => {
   let server: Server;
@@ -37,7 +38,7 @@ describe('tracing', () => {
   });
 
   it('should capture filmstrips', async () => {
-    const driver = await Gatherer.setupDriver();
+    const driver = await Gatherer.setupDriver(true, wsEndpoint);
     const tracer = new Tracing();
     await tracer.start(driver.client);
     await driver.page.goto(server.TEST_PAGE);
diff --git a/__tests__/utils/jest-global-setup.ts b/__tests__/utils/jest-global-setup.ts
new file mode 100644
index 0000000..6cae183
--- /dev/null
+++ b/__tests__/utils/jest-global-setup.ts
@@ -0,0 +1,51 @@
+/**
+ * MIT License
+ *
+ * Copyright (c) 2020-present, Elastic NV
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+import { spawn } from 'child_process';
+import { wsEndpoint } from './test-config';
+
+module.exports = async () => {
+  if (wsEndpoint) {
+    return new Promise((resolve, reject) => {
+      console.log(`\nRunning BrowserService ${wsEndpoint}`);
+      const process = spawn('node', ['./dist/browser-service.js']);
+      (global as any).__browserServiceProcess = process;
+      process.stdout.on('data', data => {
+        if (data.indexOf('Listening on port: 9322') >= 0) {
+          console.log(`${data}`);
+          resolve(process);
+        }
+      });
+
+      process.stderr.on('data', data => {
+        const message = `BrowserService: ${data}`;
+        process.kill();
+        reject(message);
+      });
+    });
+  } else {
+    console.log(`\nRunning without BrowserService`);
+  }
+};
diff --git a/__tests__/utils/jest-global-teardown.ts b/__tests__/utils/jest-global-teardown.ts
new file mode 100644
index 0000000..0e8ece4
--- /dev/null
+++ b/__tests__/utils/jest-global-teardown.ts
@@ -0,0 +1,31 @@
+/**
+ * MIT License
+ *
+ * Copyright (c) 2020-present, Elastic NV
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+module.exports = async () => {
+  const browserService = (global as any).__browserServiceProcess;
+  if (browserService) {
+    browserService.kill();
+  }
+};
diff --git a/utils/server.ts b/__tests__/utils/server.ts
similarity index 100%
rename from utils/server.ts
rename to __tests__/utils/server.ts
diff --git a/__tests__/utils/test-config.ts b/__tests__/utils/test-config.ts
new file mode 100644
index 0000000..ee2397d
--- /dev/null
+++ b/__tests__/utils/test-config.ts
@@ -0,0 +1,26 @@
+/**
+ * MIT License
+ *
+ * Copyright (c) 2020-present, Elastic NV
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+export const wsEndpoint = process.env.WSENDPOINT;