How to use Jest useFakeTimers with RXJS? - jestjs

My application is primarily using React Redux. We have a few places that use RXJS to simplify things. I just started using jest.useFakeTimers.
I realized that given code like this from(anAsyncMethod).pipe(timeout(FETCH_TOKEN_TIMEOUT)) will trigger a timeout error the second jest.runAllTimers(); fires in my afterEach.
The relevant stack is here:
stack: 'Error: \n' +
' at _super (webpack-internal:///../../node_modules/rxjs/dist/esm5/internal/util/createErrorClass.js:6:26)\n' +
' at new TimeoutErrorImpl (webpack-internal:///../../node_modules/rxjs/dist/esm5/internal/operators/timeout.js:21:9)\n' +
' at timeoutErrorFactory (webpack-internal:///../../node_modules/rxjs/dist/esm5/internal/operators/timeout.js:66:11)\n' +
' at AsyncAction.eval (webpack-internal:///../../node_modules/rxjs/dist/esm5/internal/operators/timeout.js:44:84)\n' +
' at AsyncAction.eval (webpack-internal:///../../node_modules/rxjs/dist/esm5/internal/util/caughtSchedule.js:7:21)\n' +
' at AsyncAction._execute (webpack-internal:///../../node_modules/rxjs/dist/esm5/internal/scheduler/AsyncAction.js:65:18)\n' +
' at AsyncAction.execute (webpack-internal:///../../node_modules/rxjs/dist/esm5/internal/scheduler/AsyncAction.js:53:26)\n' +
' at AsyncScheduler.flush (webpack-internal:///../../node_modules/rxjs/dist/esm5/internal/scheduler/AsyncScheduler.js:26:33)\n' +
' at callTimer (/Users/dstein/Repositories/uxp-plugin/node_modules/#sinonjs/fake-timers/src/fake-timers-src.js:729:24)\n' +
' at Object.next (/Users/dstein/Repositories/uxp-plugin/node_modules/#sinonjs/fake-timers/src/fake-timers-src.js:1409:17)\n' +
' at Object.runAll (/Users/dstein/Repositories/uxp-plugin/node_modules/#sinonjs/fake-timers/src/fake-timers-src.js:1468:23)\n' +
' at FakeTimers.runAllTimers (/Users/dstein/Repositories/uxp-plugin/node_modules/#jest/fake-timers/build/modernFakeTimers.js:75:19)\
Is there any way to work around this issue?

well looks like anAsyncMethod takes longer than FETCH_TOKEN_TIMEOUT therefore the error is thrown, this is expected.
when you subscribe to your observable you should check for errors:
from(anAsyncMethod).pipe(timeout(FETCH_TOKEN_TIMEOUT)).subscribe(
res => console.log('ok'),
err => console.error('timeout')
);
or you can catch the error
from(anAsyncMethod).pipe(
timeout(FETCH_TOKEN_TIMEOUT),
catchError(error => of(`Request timed out after: ${FETCH_TOKEN_TIMEOUT}`))
);

Related

Is there a way to get 'Ultimate Thread Group' parameter?

I'm new with jmeter.
I ran a test that at the end it generates a dashboard report.
Is there way to see the load details (no. of threads, initial Delay, Hold Load...)?
If not, is there a way to get 'Ultimate Thread Group' parameters so I can print it to DB?
Thanks
Dotan
You can do it using any suitable JSR223 Test Element and the code like:
if (ctx.getThreadNum() == 1 && vars.getIteration() == 1) {
ctx.getThreadGroup().getProperty("ultimatethreadgroupdata").getObjectValue().eachWithIndex { entry, index ->
log.info('----------------------------------------------')
log.info('Line number ' + (index + 1) + ' parameters:')
log.info('Start threads count: ' + entry.get(0).value)
log.info('Initial delay: ' + entry.get(1).value)
log.info('Startup time: ' + entry.get(2).value)
log.info('Hold load for: ' + entry.get(3).value)
log.info('Shutdown time: ' + entry.get(4).value)
}
}
Demo:
See Top 8 JMeter Java Classes You Should Be Using with Groovy to learn what do these ctx and vars objects mean.

How to overcome the error of 'exception of type IndexError'?

I have to get an specific output for a specific input as shown below:
code: soMe SuPPLies liKE Ice-cREAm aRe iMPORtant oNly tO THeir cReaTORS. tO DestroY thEm iS pOInTLess.
says: destroy their ice-cream supplies
I wrote a program below:
line = input("code: ")
words = line.split()
human = words[11] + ' ' + words[8] + ' ' + words[3] + ' ' + words[1]
print('says:',human.lower())
While I am getting the desired output, the compiler is giving me an error - 'Your submission raised an exception of type IndexError. This occurred on line 3 of your submission.'
Please help me.

FreeSwitch ESL: NodeJS/JS/Freeswitch Syntax Confliction

I'm building a Twillio-like Dialer API using Modesl in Node.JS to send commands and parameters to Freeswitch Console.
Edit: I've narrowed down the issue to a syntax issue, where the javascript I'm using to input my variables are conflicting with FreeSwitchs syntax.
uuid_send_dtmf needs to have a ' in front of it, whereas uuid is a NodeJS parameter that needs to be passed after one space, as is dmtf, and the api_on_answer requires a ' for closing after my parameters are passed.
Syntax has always been my weak point, any help would be greatly appreciated.
,api_on_answer='uuid_send_dtmf ' + uuid + ' ' + dmtf +' ' }
conn.api('originate {
origination_uuid=' + uuid
+ ',origination_caller_id_number=' + cid
+ ',api_on_answer=uuid_send_dtmf ' + uuid
+ ' ' + dmtf +' }
sofia/external/' + pnumber + '#provider', function(res) {
Currently the command is giving a very vague error of little help:
2019-03-17 08:53:22.755065 [DEBUG] switch_ivr_originate.c:2204 Parsing global variables
2019-03-17 08:53:22.755065 [ERR] switch_ivr_originate.c:2209 Parse Error!
2019-03-17 08:53:22.755065 [DEBUG] switch_ivr_originate.c:3941 Originate Resulted in Error Cause: 27 [DESTINATION_OUT_OF_ORDER]
What is the correct way to do what I need?
Fixed using '\' to input ' inline.
var onanswer = '\'' + uuid + ' ' + dmtf;
try this,
conn.api(`originate {origination_uuid=${uuid},origination_caller_id_number=${cid},api_on_answer='${uuid_send_dtmf} ${uuid} ${dtmf}'}sofia/external/${pnumber}#${provider}`, function(res) {
template literals or strings, enclosed by back-ticks, this would provide you the required format, cheers :)

Want to run several db.query() method in parallel without getting any error

I am using Node JS . I am a beginner. I use OrientJS to connect orientdb from Node JS. I want to run several db.query() method in parallel. This queries are formed by reading a large text file using line-by-line module.
For example,
var queryForGeoUpdate = 'update (' +
'\nselect from (' +
"\n select expand(outE('GeoAgentSummary')) " +
'\n from Agent ' +
'\n where name = "' + name + '" and number = \'' + number + "' and type = '" + type + "'" +
"\n) where in.name = '" + Geo + "'" +
'\n) increment _' + FiscalYear + ' = ' + TMSSalesAllocatedBookingsNet + 'f, _' +
FiscalPeriodID + ' = ' + TMSSalesAllocatedBookingsNet +
'f, _' + FiscalQuarterID + ' = ' + TMSSalesAllocatedBookingsNet + 'f'
// console.log(queryForGeoUpdate)
db.query(queryForGeoUpdate) // query and db call for Country ends here
like db.query(queryForGeoUpdate) there are seven queries like db.query(queryForRegionUpdate) and so on...
if I run it asynchronously "process out of memory occurrs". If I run it synchronously it takes too much time. How can I solve it within very less time..
Any help is appreciated..
I am not familiar with the DB you are using. If you are sure the DB works "correctly" and the calls you make are correct (e.g. if there isn't a way to make you queries much smaller) you could try running with:
node --max_old_space_size="memmory in MB, try something large like 8192" app.js
It seems rather strange that a DB query would run out of memory thought... I always assumed queries are, generally speaking,a lot more CPU intensive and require relatively little memory.
For these sort of large queries you might also try spawning separate processes:
https://nodejs.org/api/child_process.html

Node.js - "RangeError: Maximum call stack size exceeded" error on new Date()

I'm creating a Node.js module to enhance the basic console output (I know there are 100+ node modules that do that, my only purpose is to practice node).
I would like to display the time with the console message. Here is the module code:
getTime = function(){
var date = new Date()
return date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds()
}
module.exports.log = function(text){
console.log(getTime() + ' - ' + text)
}
So I can be able to use my module:
myConsole = require('./myconsole.js')
myConsole.log('Hello from my own console module')
It works well for few tests, but multiple calls (in short time) of myConsole result the following error message from node:
var date = new Date()
^
RangeError: Maximum call stack size exceeded
Any idea how to avoid that? Do I have to destroy the date instance? (I tried to use delete date but it doesn't work)
Thanks.
The RangeError doesn't occur when I test with your code. I don't think it can cause call stack to overflow. Maximum call stack size exceeded error is often caused by a call loop in a program. I guess you need to check the call flow of your program to find out what caused the error.

Resources