Skip to content

Commit

Permalink
feat(start): start selenium without making web requests (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
cnishina authored and heathkit committed Apr 24, 2017
1 parent 2448b52 commit 242a72f
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 13 deletions.
3 changes: 3 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ dependencies:
- mozdownload --version latest --destination firefox.tar.bz2
- mozinstall firefox.tar.bz2

# NPM install node module dependencies
- npm install

test:
override:
- npm run check_format
Expand Down
29 changes: 17 additions & 12 deletions lib/binaries/config_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,31 @@ export abstract class XmlConfigSource extends ConfigSource {
protected getXml(): Promise<any> {
let fileName = this.getFileName();
let content = this.readResponse();
if (content != null) {
if (content) {
return Promise.resolve(content);
} else {
return this.requestXml().then(text => {
let xml = this.convertXml2js(text);
fs.writeFileSync(fileName, text);
return xml;
});
}
return this.requestXml().then(text => {
let xml = this.convertXml2js(text);
fs.writeFileSync(fileName, text);
return xml;
});
}

private readResponse(): any {
let fileName = this.getFileName();
try {
let contents = fs.readFileSync(fileName).toString();
let timestamp = new Date(fs.statSync(fileName).mtime).getTime();

let now = Date.now();

// Ignore validating the cache OR if we are validating the cache, make
// it is within the cache time.
// 60 minutes * 60 seconds / minute * 1000 ms / second
if (now - (60 * 60 * 1000) < timestamp) {
if (Config.runCommand === 'start' || (now - (60 * 60 * 1000) < timestamp)) {
return this.convertXml2js(contents);
} else {
return null;
}
} catch (err) {
return null;
Expand Down Expand Up @@ -121,7 +126,7 @@ export abstract class GithubApiConfigSource extends JsonConfigSource {
getJson(): Promise<any> {
let fileName = this.getFileName();
let content = this.readResponse();
if (content != null) {
if (content) {
return Promise.resolve(JSON.parse(content));
} else {
return this.requestJson().then(body => {
Expand Down Expand Up @@ -164,11 +169,11 @@ export abstract class GithubApiConfigSource extends JsonConfigSource {
try {
let contents = fs.readFileSync(fileName).toString();
let timestamp = new Date(fs.statSync(fileName).mtime).getTime();

let now = Date.now();
// 60 minutes * 60 seconds / minute * 1000 ms / second
if (now - (60 * 60 * 1000) < timestamp) {
if (Config.runCommand === 'start' || (now - (60 * 60 * 1000) < timestamp)) {
return contents;
} else {
return null;
}
} catch (err) {
return null;
Expand Down
1 change: 1 addition & 0 deletions lib/cmds/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import * as Opt from './';
import {Opts} from './opts';

const commandName = 'start';
Config.runCommand = commandName;

let logger = new Logger('start');
let prog = new Program()
Expand Down
2 changes: 2 additions & 0 deletions lib/cmds/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import * as Opt from './';
import {android as initializeAndroid, iOS as checkIOS} from './initialize';
import {Opts} from './opts';

Config.runCommand = 'update';

let logger = new Logger('update');
let prog = new Program()
.command('update', 'install or update selected binaries')
Expand Down
2 changes: 2 additions & 0 deletions lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export interface ConfigFile {
*
*/
export class Config {
static runCommand: string;

static configFile: string = 'config.json';
static packageFile: string = 'package.json';
static nodeModuleName = 'webdriver-manager';
Expand Down
137 changes: 137 additions & 0 deletions spec/binaries/config_source_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import * as fs from 'fs';
import {GithubApiConfigSource, XmlConfigSource} from '../../lib/binaries/config_source';
import {Config} from '../../lib/config';

export class XMLConfig extends XmlConfigSource {
constructor(public name: string, public xmlUrl: string) {
super(name, xmlUrl);
}
getUrl(version: string): Promise<{url: string, version: string}> {
return null;
}
getVersionList(): Promise<string[]> {
return null;
}
testGetXml(): Promise<any> {
return this.getXml();
}
}

export class JSONConfig extends GithubApiConfigSource {
constructor(name: string, url: string) {
super(name, url);
}
getUrl(version: string): Promise<{url: string, version: string}> {
return null;
}
getVersionList(): Promise<string[]> {
return null;
}
testGetJson(): Promise<any> {
return this.getJson();
}
}

describe('config', () => {
describe('xml config source', () => {
it('on start: should read the xml file and not check the timestamp', done => {
spyOn(fs, 'readFileSync').and.callFake(() => {
return `
<?xml version='1.0' encoding='UTF-8'?>
<ListBucketResult xmlns="http://doc.s3.amazonaws.com/2006-03-01">
<Name>foobar-release</Name>
<Contents>
<Key>0.01/foobar.zip</Key>
</Contents>
</ListBucketResult>
`;
});
spyOn(fs, 'statSync').and.callFake(() => {
return {
mtime: 0
}
});
Config.runCommand = 'start';
let xmlConfig = new XMLConfig('xml', 'url');
xmlConfig.testGetXml()
.then(xml => {
expect(xml.ListBucketResult.Name).toEqual(['foobar-release']);
done();
})
.catch(err => {
done.fail(err);
});
});

it('on udpate: should check the timestamp, invaidate cache, and try to make a web request',
done => {
spyOn(fs, 'readFileSync').and.callFake(() => {
return 'foobar';
});
spyOn(fs, 'statSync').and.callFake(() => {
return {
mtime: 0
}
});
Config.runCommand = 'update';
let xmlConfig = new XMLConfig('xml', 'url');
xmlConfig.testGetXml()
.then(xml => {
// should do nothing
done.fail('this should not work');
})
.catch(err => {
expect(err.toString()).toContain('Invalid URI "url"');
done();
});

});
});

describe('github json', () => {
it('on start: should read the json file and not check the timestamp', done => {
spyOn(fs, 'readFileSync').and.callFake(() => {
return '{ "foo": "bar" }';
});
spyOn(fs, 'statSync').and.callFake(() => {
return {
mtime: 0
}
});
Config.runCommand = 'start';
let jsonConfig = new JSONConfig('json', 'url');
jsonConfig.testGetJson()
.then(json => {
expect(json.foo).toEqual('bar');
done();
})
.catch(err => {
done.fail(err);
});
});

it('on udpate: should check the timestamp, invaidate cache, and try to make a web request',
done => {
spyOn(fs, 'readFileSync').and.callFake(() => {
return 'foobar';
});
spyOn(fs, 'statSync').and.callFake(() => {
return {
mtime: 0
}
});
Config.runCommand = 'update';
let jsonConfig = new JSONConfig('json', 'url');
jsonConfig.testGetJson()
.then(json => {
// should do nothing
done.fail('this should not work');
})
.catch(err => {
expect(err.toString()).toContain('Invalid URI "url"');
done();
});

});
});
});
2 changes: 1 addition & 1 deletion spec/binaries/iedriver_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('iedriver', () => {
it('should get version 2.53.1', (done) => {
let iedriver = new IEDriver();
iedriver.configSource.out_dir = out_dir;
iedriver.getUrl().then(binaryUrl => {
iedriver.getUrl('2.53.1').then(binaryUrl => {
expect(binaryUrl.url)
.toEqual(
'https://selenium-release.storage.googleapis.com/2.53/IEDriverServer_Win32_2.53.1.zip');
Expand Down

0 comments on commit 242a72f

Please sign in to comment.