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 | */
|
17 | import * as tf from './index';
|
18 | import { describeWithFlags, HAS_WORKER } from './jasmine_util';
|
19 | import { expectArraysClose } from './test_util';
|
20 | const fn2workerURL = (fn) => {
|
21 | const blob = new Blob(['(' + fn.toString() + ')()'], { type: 'application/javascript' });
|
22 | return URL.createObjectURL(blob);
|
23 | };
|
24 | // The source code of a web worker.
|
25 | const workerTest = () => {
|
26 | //@ts-ignore
|
27 | importScripts('http://bs-local.com:12345/base/dist/tf-core.min.js');
|
28 | //@ts-ignore
|
29 | importScripts('http://bs-local.com:12345/base/dist/tf-backend-cpu.min.js');
|
30 | let a = tf.tensor1d([1, 2, 3]);
|
31 | const b = tf.tensor1d([3, 2, 1]);
|
32 | a = tf.add(a, b);
|
33 | //@ts-ignore
|
34 | self.postMessage({ data: a.dataSync() });
|
35 | };
|
36 | describeWithFlags('computation in worker', HAS_WORKER, () => {
|
37 | it('tensor in worker', (done) => {
|
38 | const worker = new Worker(fn2workerURL(workerTest));
|
39 | worker.onmessage = (msg) => {
|
40 | const data = msg.data.data;
|
41 | expectArraysClose(data, [4, 4, 4]);
|
42 | done();
|
43 | };
|
44 | });
|
45 | });
|
46 | //# sourceMappingURL=worker_test.js.map |
\ | No newline at end of file |