Skip to content

Commit

Permalink
wip: revive worker context
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed May 27, 2022
1 parent 0c0f356 commit 2806d31
Show file tree
Hide file tree
Showing 45 changed files with 548 additions and 633 deletions.
2 changes: 1 addition & 1 deletion detox/local-cli/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-nocheck
const realm = require('../realms/root');
const realm = require('../realms/primary');

const TestRunnerCommand = require('./testCommand/TestRunnerCommand');

Expand Down
6 changes: 3 additions & 3 deletions detox/local-cli/test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ describe('CLI', () => {
});

test.each([['-R'], ['--retries']])('%s <value> should execute unsuccessful run N extra times', async (__retries) => {
const context = require('../realms/root');
const context = require('../realms/primary');
jest.spyOn(context, 'lastFailedTests', 'get')
.mockReturnValueOnce(['e2e/failing1.test.js', 'e2e/failing2.test.js'])
.mockReturnValueOnce(['e2e/failing2.test.js']);
Expand All @@ -188,7 +188,7 @@ describe('CLI', () => {
});

test.each([['-R'], ['--retries']])('%s <value> should not restart test runner if there are no failing tests paths', async (__retries) => {
const context = require('../realms/root');
const context = require('../realms/primary');
jest.spyOn(context, 'lastFailedTests', 'get')
.mockReturnValueOnce([]);
cp.execSync.mockImplementation(() => { throw new Error; });
Expand All @@ -199,7 +199,7 @@ describe('CLI', () => {
});

test.each([['-R'], ['--retries']])('%s <value> should retain -- <...explicitPassthroughArgs>', async (__retries) => {
const context = require('../realms/root');
const context = require('../realms/primary');
jest.spyOn(context, 'lastFailedTests', 'get')
.mockReturnValueOnce(['tests/failing.test.js']);
cp.execSync.mockImplementation(() => { throw new Error; });
Expand Down
20 changes: 11 additions & 9 deletions detox/local-cli/testCommand/TestRunnerCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const os = require('os');
const _ = require('lodash');
const unparse = require('yargs-unparser');

const context = require('../../realms/root');
const context = require('../../realms/primary');
const { printEnvironmentVariables, prependNodeModulesBinToPATH } = require('../../src/utils/envUtils');
const { quote } = require('../../src/utils/shellQuote');

Expand Down Expand Up @@ -126,14 +126,16 @@ class TestRunnerCommand {
printEnvironmentVariables(this._envHint) + fullCommand
);

cp.execSync(fullCommand, {
stdio: 'inherit',
env: _({})
.assign(process.env)
.assign(this._env)
.omitBy(_.isUndefined)
.tap(prependNodeModulesBinToPATH)
.value()
return new Promise((resolve, reject) => {
cp.spawn(command, fullCommand.split(' ').slice(1), {
stdio: 'inherit',
env: _({})
.assign(process.env)
.assign(this._env)
.omitBy(_.isUndefined)
.tap(prependNodeModulesBinToPATH)
.value()
}).on('exit', (code) => code === 0 ? resolve() : reject(code));
});
}
}
Expand Down
3 changes: 2 additions & 1 deletion detox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@types/child-process-promise": "^2.2.1",
"@types/jest": "^27.0.3",
"@types/node": "^12.20.37",
"@types/node-ipc": "^9.2.0",
"@types/ws": "^7.4.0",
"@typescript-eslint/eslint-plugin": "^5.4.0",
"@typescript-eslint/parser": "^5.4.0",
Expand Down Expand Up @@ -63,7 +64,7 @@
"ini": "^1.3.4",
"lodash": "^4.17.5",
"minimist": "^1.2.0",
"node-ipc": "^10.1.0",
"node-ipc": "^9.2.1",
"proper-lockfile": "^3.0.2",
"resolve-from": "^5.0.0",
"sanitize-filename": "^1.6.1",
Expand Down
10 changes: 1 addition & 9 deletions detox/realms/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
function current() {
if (process.env.JEST_WORKER_ID) {
return require('./worker');
}

if (process.env.DETOX_IPC_SERVER_ID) {
return require('./runner');
}

return require('./root');
return process.env.DETOX_IPC_SERVER_ID ? require('./secondary') : require('./primary');
}

module.exports = current();
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const _ = require('lodash');
const configuration = require('../../src/configuration');
const DeviceRegistry = require('../../src/devices/DeviceRegistry');
const GenyDeviceRegistryFactory = require('../../src/devices/allocation/drivers/android/genycloud/GenyDeviceRegistryFactory');
const environmentFactory = require('../../src/environmentFactory');
const NullLogger = require('../../src/logger/NullLogger');
const DetoxServer = require('../../src/server/DetoxServer');

Expand All @@ -19,6 +20,7 @@ class DetoxRootContext {
this._wss = null;
this._ipc = null;
this._logger = new NullLogger();
this._globalLifecycleHandler = null;

this.setup = this.setup.bind(this);
this.teardown = this.teardown.bind(this);
Expand Down Expand Up @@ -48,6 +50,10 @@ class DetoxRootContext {
this._wss = null;
}

if (this._globalLifecycleHandler) {
await this._globalLifecycleHandler.globalCleanup();
}

// TODO: move the artifacts
}

Expand All @@ -66,6 +72,7 @@ class DetoxRootContext {

async _doSetup() {
const config = this._config;

this._logger = new BunyanLogger({
loglevel: config.cliConfig.loglevel || 'info',
});
Expand All @@ -84,13 +91,21 @@ class DetoxRootContext {
);

this._ipc = new IPCServer({
sessionId: `detox-${process.pid}`,
id: 'detox-' + process.pid,
detoxConfig: this._config,
logger: this._logger,
});

process.env.DETOX_IPC_SERVER_ID = this._ipc.id;
await this._ipc.start();
const { cliConfig, sessionConfig } = config;

const { cliConfig, deviceConfig, sessionConfig } = config;

this._globalLifecycleHandler = await environmentFactory.createGlobalLifecycleHandler(deviceConfig);

if (this._globalLifecycleHandler) {
await this._globalLifecycleHandler.globalInit();
}

if (!cliConfig.keepLockFile) {
await this._resetLockFile();
Expand Down
53 changes: 53 additions & 0 deletions detox/realms/primary/IPCServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const { upperFirst, camelCase } = require('lodash');

const { DetoxInternalError } = require('../../src/errors');

class IPCServer {
constructor({ id, logger, detoxConfig }) {
this._id = id;
this._logger = logger;
this._state = {
workers: 0,
detoxConfig,
};
this._ipc = null;
}

get id() {
return this._id;
}

async start() {
this._ipc = require('node-ipc');
this._ipc.config.id = this._id;
this._ipc.config.retry = 1500;
// this._ipc.config.silent = true;
await new Promise((resolve) => {
// TODO: handle reject
this._ipc.serve(() => resolve());
this._ipc.server.on('registerWorker', this.onRegisterWorker.bind(this));
this._ipc.server.on('log', this.onLog.bind(this));
this._ipc.server.start();
});
}

async stop() {
return new Promise((resolve, reject) =>{
// @ts-ignore
this._ipc.server.server.close(e => e ? reject(e) : resolve());
});
}

onRegisterWorker({ workerId }, socket) {
const workersCount = this._state.workers = Math.max(this._state.workers, +workerId);
const detoxConfig = this._state.detoxConfig;
socket.emit('detoxConfig', detoxConfig);
this._ipc.server.broadcast('workersCount', { value: workersCount });
}

onLog({ level, args }) {
this._logger[level](args);
}
}

module.exports = IPCServer;
File renamed without changes.
96 changes: 0 additions & 96 deletions detox/realms/root/IPCServer.js

This file was deleted.

7 changes: 0 additions & 7 deletions detox/realms/runner/index.js

This file was deleted.

42 changes: 42 additions & 0 deletions detox/realms/secondary/DetoxRunnerContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const ipcClient = require('../../src/ipc/client');
const IPCLogger = require('../../src/logger/IPCLogger');
const NullLogger = require('../../src/logger/NullLogger');

class DetoxRunnerContext {
constructor() {
this.setup = this.setup.bind(this);
this.teardown = this.teardown.bind(this);
/** @type {NullLogger | IPCLogger} */
this._logger = new NullLogger();
this._fingerprint = Math.random();
this._ready = false;
this._config = null;
}

async setup() {
if (this._ready) {
return;
}

await ipcClient.init();
this._config = await ipcClient.getDetoxConfig();
this._logger = new IPCLogger({
level: this._config.cliConfig.loglevel,
});
this._ready = true;
}

async teardown() {
this._ready = false;
}

get config() {
return this._config;
}

get log() {
return this._logger;
}
}

module.exports = DetoxRunnerContext;
2 changes: 2 additions & 0 deletions detox/realms/secondary/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const DetoxRunnerContext = require('./DetoxRunnerContext');
module.exports = new DetoxRunnerContext();

This file was deleted.

9 changes: 0 additions & 9 deletions detox/realms/worker/index.js

This file was deleted.

1 change: 0 additions & 1 deletion detox/runners/jest-circus/environment.js

This file was deleted.

1 change: 1 addition & 0 deletions detox/runners/jest-circus/environment/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../migration')(__filename);
1 change: 0 additions & 1 deletion detox/runners/jest/environment.js

This file was deleted.

Loading

0 comments on commit 2806d31

Please sign in to comment.