Skip to content

Commit

Permalink
Fixed review comments
Browse files Browse the repository at this point in the history
Co-authored-by: SrinivasanTarget <srinivasan.sekar1990@gmail.com>
  • Loading branch information
saikrishna321 and SrinivasanTarget committed Jan 20, 2025
1 parent 792d45a commit bd60874
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/commands/recordscreen.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from 'lodash';
import {fs, tempDir, logger, util} from 'appium/support';
import {SubProcess} from 'teen_process';
import {encodeBase64OrUpload, isLocalHost} from '../utils';
import {encodeBase64OrUpload} from '../utils';
import {WDA_BASE_URL} from 'appium-webdriveragent';
import {waitForCondition} from 'asyncbox';
import url from 'url';
Expand Down
22 changes: 16 additions & 6 deletions lib/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,10 @@ export class XCUITestDriver extends BaseDriver {
}
}

/**
* Handles MJPEG server-related capabilities
* @returns {Promise<void>}
*/
async handleMjpegOptions() {
await this.allocateMjpegServerPort();
// turn on mjpeg stream reading if requested
Expand All @@ -502,12 +506,18 @@ export class XCUITestDriver extends BaseDriver {
devicePort: mjpegServerPort,
usePortForwarding: this.isRealDevice(),
});
} catch (error) {
throw new Error(
`Cannot ensure MJPEG broadcast functionality by forwarding the local port ${mjpegServerPort} ` +
`requested by the 'mjpegServerPort' capability to the device port ${mjpegServerPort}. ` +
`Original error: ${error}`
);
} catch (error) {
if (_.isUndefined(this.opts.mjpegServerPort)) {
this.log.warn(`Cannot ensure MJPEG broadcast functionality by forwarding the ${DEFAULT_MJPEG_SERVER_PORT} ` +
`use mjpegServerPort capability to provide alternative port number`
);
} else {
throw new Error(
`Cannot ensure MJPEG broadcast functionality by forwarding the local port ${mjpegServerPort} ` +
`requested by the 'mjpegServerPort' capability to the device port ${mjpegServerPort}. ` +
`Original error: ${error}`
);
}
}
}

Expand Down
23 changes: 22 additions & 1 deletion test/unit/driver-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import * as utils from '../../lib/utils';
import {MOCHA_LONG_TIMEOUT} from './helpers';
import {RealDevice} from '../../lib/real-device';


const caps = {
fistMatch: [{}],
alwaysMatch: {
Expand Down Expand Up @@ -244,6 +243,28 @@ describe('XCUITestDriver', function () {
);
spy.notCalled.should.be.true;
});

it('should throw an error if mjpegServerPort is occupied', async function () {
this.timeout(MOCHA_LONG_TIMEOUT);
delete device.simctl;
device.devicectl = true;
const net = require('net');
const server = net.createServer();
await new Promise((resolve) => server.listen(9100, resolve));
try {
await driver.createSession(
null,
null,
_.merge({}, caps, {
alwaysMatch: {'appium:mjpegServerPort': 9100},
}),
).should.be.rejectedWith(
'Cannot ensure MJPEG broadcast functionality by forwarding the local port 9100 requested by the \'mjpegServerPort\' capability to the device port 9100. Original error: Error: The port #9100 is occupied by an other process'
);
} finally {
await new Promise((resolve) => server.close(resolve));
}
});
});

describe('execute', function () {
Expand Down

0 comments on commit bd60874

Please sign in to comment.