UNPKG

1.92 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2019 Google LLC. All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 * =============================================================================
16 */
17import { describeWithFlags, HAS_NODE_WORKER } from './jasmine_util';
18import { expectArraysClose } from './test_util';
19// tslint:disable:no-require-imports
20const fn2String = (fn) => {
21 const funcStr = '(' + fn.toString() + ')()';
22 return funcStr;
23};
24// The source code of a web worker.
25const workerTestNode = () => {
26 // Web worker scripts in node live relative to the CWD, not to the dir of the
27 // file that spawned them.
28 const tf = require('@tensorflow/tfjs');
29 const { parentPort } = require('worker_threads');
30 let a = tf.tensor1d([1, 2, 3]);
31 const b = tf.tensor1d([3, 2, 1]);
32 a = a.add(b);
33 parentPort.postMessage({ data: a.dataSync() });
34};
35describeWithFlags('computation in worker (node env)', HAS_NODE_WORKER, () => {
36 // tslint:disable-next-line: ban
37 it('tensor in worker', (done) => {
38 const { Worker } = require('worker_threads');
39 const worker = new Worker(fn2String(workerTestNode), { eval: true });
40 // tslint:disable-next-line:no-any
41 worker.on('message', (msg) => {
42 const data = msg.data;
43 expectArraysClose(data, [4, 4, 4]);
44 done();
45 });
46 });
47});
48//# sourceMappingURL=worker_node_test.js.map
\No newline at end of file