diff --git a/test/test-main.ts b/test/test-main.ts index 5f12e02c..bd1f6a59 100644 --- a/test/test-main.ts +++ b/test/test-main.ts @@ -36,7 +36,9 @@ for (const group of ONNX_JS_TEST_CONFIG.model) { }); after('release session', () => { - context.release(); + if (context) { + context.release(); + } }); for (const testCase of test.cases) { diff --git a/test/test-runner.ts b/test/test-runner.ts index 4c5556e7..9b0870f0 100644 --- a/test/test-runner.ts +++ b/test/test-runner.ts @@ -77,21 +77,33 @@ export class ModelTestContext { * create a ModelTestContext object that used in every test cases in the given ModelTest. */ static async create(modelTest: Test.ModelTest, profile: boolean): Promise { - const initStart = now(); - const session = await initializeSession(modelTest.modelUrl, modelTest.backend!, profile); - const initEnd = now(); - - for (const testCase of modelTest.cases) { - await loadTensors(testCase); + if (this.initializing) { + throw new Error(`cannot create a ModelTestContext object when the previous creation is not done`); } - return new ModelTestContext( - session, - modelTest.backend!, - {init: initEnd - initStart, firstRun: -1, runs: [], count: 0}, - profile, - ); + try { + this.initializing = true; + + const initStart = now(); + const session = await initializeSession(modelTest.modelUrl, modelTest.backend!, profile); + const initEnd = now(); + + for (const testCase of modelTest.cases) { + await loadTensors(testCase); + } + + return new ModelTestContext( + session, + modelTest.backend!, + {init: initEnd - initStart, firstRun: -1, runs: [], count: 0}, + profile, + ); + } finally { + this.initializing = false; + } } + + private static initializing = false; } export declare namespace ModelTestContext {