You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have set up a feathers service that receives a post request containing user input for a long running computation. I would like the POST request to return to the user before running the asynchronous long running code.
From the feathers documentation on testing, I know how to test for the return result of this POST request. However, I would like to test for the side effect produced from the long running computation as well.
The side effect of this long running computation is that it would save the computation results into a feathers-mongoose database.
I think this question is not specific to feathers framework but I just happen to design the web service in feathers.
A simple example of what I am trying to do is attached below: I am using piscina to spin up a worker thread for the long running computation.
create(data: any, params?: Params): Promise<MessageData | MessageData[]> {
const piscina = new Piscina({
filename: path.resolve(__dirname, 'worker.js')
})
piscina.run()
.then((IUser) => {
const user = new User(IUser);
user.save().then(user => console.log(user))
})
return new Promise((resolve, reject) => resolve(new MessageData('helloWorld')));
}
Thank you all for your help.
--EDIT--
One possible solution I could think of would be to perhaps save the promise returned by piscina.run() into a global resultCache variable. The test could probably look something like this
const app = require('../../src/app');
const resultCache = require('../../src/resultCache')
describe('\'users\' service', () => {
it('registered the service', () => {
const jobId = await app.service('compute');
await resultCache.get(jobId)
// code to check mongoose database has the correct computed result
});
});
This would however mean that I am creating the resultCache module just for testing purposes and would not be used so much in production. Would there be a better solution to this problem ?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have set up a
feathers service
that receives a post request containing user input for a long running computation. I would like the POST request to return to the user before running the asynchronous long running code.From the feathers documentation on testing, I know how to test for the return result of this POST request. However, I would like to test for the side effect produced from the long running computation as well.
The side effect of this long running computation is that it would save the computation results into a feathers-mongoose database.
I think this question is not specific to feathers framework but I just happen to design the web service in feathers.
A simple example of what I am trying to do is attached below: I am using piscina to spin up a worker thread for the long running computation.
Thank you all for your help.
--EDIT--
One possible solution I could think of would be to perhaps save the promise returned by
piscina.run()
into a globalresultCache
variable. The test could probably look something like thisThis would however mean that I am creating the
resultCache
module just for testing purposes and would not be used so much in production. Would there be a better solution to this problem ?Beta Was this translation helpful? Give feedback.
All reactions