File tree Expand file tree Collapse file tree 4 files changed +38
-1
lines changed Expand file tree Collapse file tree 4 files changed +38
-1
lines changed Original file line number Diff line number Diff line change 279
279
fail ("""nodejs_binary/nodejs_test only support chdir inside the current package
280
280
but %s is not a subfolder of %s""" % (ctx .attr .chdir , ctx .label .package ))
281
281
chdir_script = ctx .actions .declare_file (_join (relative_dir , "__chdir.js__" ))
282
- ctx .actions .write (chdir_script , "process.chdir(__dirname)" )
282
+ ctx .actions .write (chdir_script , """
283
+ /* This script is preloaded with --require, meaning it will run for the main node process
284
+ as well as each worker thread that gets spawned. Calling process.chdir() in a worker
285
+ is an error in node, so ensure it's only called once for the main process.
286
+ */
287
+ if (process.cwd() !== __dirname) {
288
+ process.chdir(__dirname);
289
+ }
290
+ """ )
283
291
runfiles .append (chdir_script )
284
292
285
293
# this join is effectively a $(rootdir) expansion
Original file line number Diff line number Diff line change @@ -52,3 +52,10 @@ nodejs_test(
52
52
data = ["build/app.js" ],
53
53
entry_point = "test.js" ,
54
54
)
55
+
56
+ nodejs_test (
57
+ name = "test_multithread" ,
58
+ chdir = package_name (),
59
+ data = ["worker.js" ],
60
+ entry_point = "multithread.js" ,
61
+ )
Original file line number Diff line number Diff line change
1
+ const { Worker } = require ( "worker_threads" )
2
+
3
+ function runWorker ( message ) {
4
+ return new Promise ( ( resolve , reject ) => {
5
+ const worker = new Worker ( "./worker.js" , { workerData : { message} } ) ;
6
+ worker . on ( "message" , resolve ) ;
7
+ worker . on ( "error" , reject ) ;
8
+ worker . on ( "exit" , code => {
9
+ if ( 0 !== code ) {
10
+ reject ( new Error ( `Worker exited with code ${ code } ` ) ) ;
11
+ }
12
+ } ) ;
13
+ } )
14
+ }
15
+
16
+ ( async ( ) => {
17
+ await runWorker ( "foobar" ) ;
18
+ } ) ( ) ;
Original file line number Diff line number Diff line change
1
+ const { parentPort, workerData} = require ( "worker_threads" ) ;
2
+
3
+ console . log ( workerData . message ) ;
4
+ parentPort . postMessage ( true ) ;
You can’t perform that action at this time.
0 commit comments