Skip to content

Commit

Permalink
perf(core): reuse context by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Igmat committed Jun 2, 2018
1 parent 82aa097 commit a09b925
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 21 deletions.
55 changes: 36 additions & 19 deletions packages/baset-core/src/testGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface ITestGroupOptions {
readers: string[];
resolvers: string[];
imports: string[];
isolateContext: boolean;
}

export class TestGroup {
Expand All @@ -26,6 +27,8 @@ export class TestGroup {
private readerChain: AbstractReader[];
private resolvers: AbstractResolver[];
private indexOfResolver: (obj: any, context: NodeVM, sandbox: IDictionary<any>) => Promise<number>;
private context?: NodeVM;
private sandbox?: IDictionary<any>;
constructor(
pattern: string | RegExp,
private options: ITestGroupOptions,
Expand Down Expand Up @@ -68,25 +71,7 @@ export class TestGroup {

test = async (filePath: string) => {
const resolvedPath = path.resolve(filePath);
const compiler = this.getCompiler();
const sandbox: IDictionary<any> = {};
const envImport = this.environment && this.environment.getContextImport(sandbox);
const imports = [
envImport,
...this.options.imports,
].filter((importName): importName is string => !!importName);
const context = new NodeVM({
require: {
builtin: ['*'],
context: 'sandbox',
external: true,
import: imports,
},
sandbox: { basetSandbox: sandbox },
compiler: compiler.compile,
sourceExtensions: compiler.extensions,
resolveFilename: compiler.resolveFilename,
});
const { context, sandbox } = this.prepareContext();
const specSrc = readFile(resolvedPath, { encoding: 'utf8' });
const compiledSrc = await this.readerChain
.reduce<Promise<string | string[]>>((result, reader) => reader.read(filePath, result), specSrc);
Expand Down Expand Up @@ -189,6 +174,38 @@ export class TestGroup {
resolvers.reduce((result, resolver) => resolver(result), original)(request),
};
}

private prepareContext = () => {
if (!this.options.isolateContext && this.context && this.sandbox) {
return {
context: this.context,
sandbox: this.sandbox,
};
}
const compiler = this.getCompiler();
const sandbox: IDictionary<any> = {};
const envImport = this.environment && this.environment.getContextImport(sandbox);
const imports = [
envImport,
...this.options.imports,
].filter((importName): importName is string => !!importName);
const context = new NodeVM({
require: {
builtin: ['*'],
context: 'sandbox',
external: true,
import: imports,
},
sandbox: { basetSandbox: sandbox },
compiler: compiler.compile,
sourceExtensions: compiler.extensions,
resolveFilename: compiler.resolveFilename,
});
this.context = context;
this.sandbox = sandbox;

return { context, sandbox };
}
}

function defaultCompile(code: string, filename: string) {
Expand Down
12 changes: 10 additions & 2 deletions packages/baset-core/src/tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ export class Tester {
private testGroups: TestGroup[];

// tslint:disable-next-line:no-any
constructor(plugins: IDictionary<ITestGroupOptions>, pluginsOptions: IDictionary<any>) {
constructor(plugins: IDictionary<ITestGroupOptions>, pluginsOptions: IDictionary<any>, private isolateContext = false) {
this.testGroups = Object.keys(plugins)
.map(key => new TestGroup(key, plugins[key], pluginsOptions));
.map(key => new TestGroup(
key,
isolateContext
? {
...plugins[key],
isolateContext,
}
: plugins[key],
pluginsOptions));
}

test(specs: string[], baselines: string[]) {
Expand Down

0 comments on commit a09b925

Please sign in to comment.