Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolves #99 pass version to the worker #103

Merged
merged 3 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .taprc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
jobs: 1
check-coverage: false
timeout: 60000
# in seconds
timeout: 60
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { version } = require('./package.json')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cue the future issue where people transpiling everything will complain about needing the package.json....

const { EventEmitter } = require('events')
const { Worker } = require('worker_threads')
const { join } = require('path')
Expand Down Expand Up @@ -29,6 +30,7 @@ class FakeWeakRef {

const FinalizationRegistry = global.FinalizationRegistry || class FakeFinalizationRegistry {
register () {}

unregister () {}
}

Expand All @@ -55,7 +57,12 @@ function createWorker (stream, opts) {
: pathToFileURL(filename).href,
dataBuf: stream[kImpl].dataBuf,
stateBuf: stream[kImpl].stateBuf,
workerData
workerData: {
$context: {
threadStreamVersion: version
},
...workerData
}
}
})

Expand Down
21 changes: 21 additions & 0 deletions test/context.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

const { test } = require('tap')
const { join } = require('path')
const ThreadStream = require('..')
const { version } = require('../package.json')
require('why-is-node-running')

test('get context', (t) => {
const stream = new ThreadStream({
filename: join(__dirname, 'get-context.js'),
workerData: {},
sync: true
})
t.on('end', () => stream.end())
stream.on('context', (ctx) => {
t.same(ctx.threadStreamVersion, version)
t.end()
})
stream.write('hello')
})
5 changes: 3 additions & 2 deletions test/event.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
const { test } = require('tap')
const { join } = require('path')
const ThreadStream = require('..')
require('why-is-node-running')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a separate issue should be opened and worked on to investigate why this is "fixing" the test suite.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is some weird interaction between tap own use of async_hooks, worker_threads and code coverage.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's affecting the Pino tests as well. We get flakes over there all the time.


test('event propagate', function (t) {
test('event propagate', (t) => {
const stream = new ThreadStream({
filename: join(__dirname, 'emit-event.js'),
workerData: {},
sync: true
})
t.on('end', () => stream.end())
stream.on('socketError', function (a, b, c, n, error) {
t.same(a, 'list')
t.same(b, 'of')
Expand All @@ -19,5 +21,4 @@ test('event propagate', function (t) {
t.end()
})
stream.write('hello')
stream.end()
})
22 changes: 22 additions & 0 deletions test/get-context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

const { Writable } = require('stream')
const parentPort = require('worker_threads').parentPort

async function run (opts) {
return new Writable({
autoDestroy: true,
write (chunk, enc, cb) {
if (parentPort) {
parentPort.postMessage({
code: 'EVENT',
name: 'context',
args: opts.$context
})
}
cb()
}
})
}

module.exports = run