在mocha中同步执行事件(executing event synchronously in mocha)
我在mocha测试中运行异步函数时遇到问题。 我在beforeEach调用中启动节点服务器,并在执行任何其他it()语句之前链接客户端套接字以连接到它。
问题是 - 每次摩卡调用都会产生不同的输出
这是我的摩卡测试
//测试httpServer的事件
import chai,{expect} from 'chai'; import sinon from 'sinon' import SocketCluster from 'socketcluster-client'; import testServer from '../../server/server.js'; import net from 'net'; import chaiAsPromised from 'chai-as-promised'; function startServer(port){ return new Promise(function(resolve,reject){ resolve(testServer(port)) }) } chai.use(chaiAsPromised) describe('httpServer',() => { var client; var options = { port: 4000 } beforeEach(() => { startServer(4000).then(() => { console.log('Server started') client = SocketCluster.connect(options) }) }) it('should return Anonymous user if client doesnt send a valid JWT token on user_connected event',() => { return client.emit('user_connected',{id_token:false},(err,data) => { expect(data).to.eventually.be.a('string'); }) }) })
这是第一次测试调用的输出
httpServer Test server started on 4000 Server started user connected 1) "before each" hook for "should return Anonymous user if client doesnt send a valid JWT token on user_connected event" Main page ✓ should show a sign-in page if isAuthenticated is false (60ms) ✓ should show a welcome text if isAuthenticated is true SignUp login ✓ should return isAuthenticated=false on SIGNUP_REQUEST ✓ should return isAuthenticated=true on SIGNUP_SUCCESS ✓ should return isAuthenticated=false and errorMessage on SIGNUP_FAILURE 5 passing (2s) 1 failing 1) httpServer "before each" hook for "should return Anonymous user if client doesnt send a valid JWT token on user_connected event": Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
这是第二个调用输出
httpServer 1) should return Anonymous user if client doesnt send a valid JWT token on user_connected event Test server started on 4000 Server started Main page ✓ should show a sign-in page if isAuthenticated is false (82ms) ✓ should show a welcome text if isAuthenticated is true user connected SignUp login ✓ should return isAuthenticated=false on SIGNUP_REQUEST ✓ should return isAuthenticated=true on SIGNUP_SUCCESS ✓ should return isAuthenticated=false and errorMessage on SIGNUP_FAILURE 5 passing (347ms) 1 failing 1) httpServer should return Anonymous user if client doesnt send a valid JWT token on user_connected event: TypeError: Cannot read property 'emit' of undefined at Context.<anonymous> (server.test.js:34:14)
正如您所看到的,“用户连接”日志非常随机地发生。 我如何控制它同步发生?
I'm having trouble running async functions in mocha tests. I'm starting the node server in the beforeEach call and chaining a client socket to connect to it before executing any other it() statements.
the problem is - i'm getting varied outputs with each mocha call
this is my mocha test
//testing events of httpServer
import chai,{expect} from 'chai'; import sinon from 'sinon' import SocketCluster from 'socketcluster-client'; import testServer from '../../server/server.js'; import net from 'net'; import chaiAsPromised from 'chai-as-promised'; function startServer(port){ return new Promise(function(resolve,reject){ resolve(testServer(port)) }) } chai.use(chaiAsPromised) describe('httpServer',() => { var client; var options = { port: 4000 } beforeEach(() => { startServer(4000).then(() => { console.log('Server started') client = SocketCluster.connect(options) }) }) it('should return Anonymous user if client doesnt send a valid JWT token on user_connected event',() => { return client.emit('user_connected',{id_token:false},(err,data) => { expect(data).to.eventually.be.a('string'); }) }) })
this is the output from the first test call
httpServer Test server started on 4000 Server started user connected 1) "before each" hook for "should return Anonymous user if client doesnt send a valid JWT token on user_connected event" Main page ✓ should show a sign-in page if isAuthenticated is false (60ms) ✓ should show a welcome text if isAuthenticated is true SignUp login ✓ should return isAuthenticated=false on SIGNUP_REQUEST ✓ should return isAuthenticated=true on SIGNUP_SUCCESS ✓ should return isAuthenticated=false and errorMessage on SIGNUP_FAILURE 5 passing (2s) 1 failing 1) httpServer "before each" hook for "should return Anonymous user if client doesnt send a valid JWT token on user_connected event": Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
this is the second call output
httpServer 1) should return Anonymous user if client doesnt send a valid JWT token on user_connected event Test server started on 4000 Server started Main page ✓ should show a sign-in page if isAuthenticated is false (82ms) ✓ should show a welcome text if isAuthenticated is true user connected SignUp login ✓ should return isAuthenticated=false on SIGNUP_REQUEST ✓ should return isAuthenticated=true on SIGNUP_SUCCESS ✓ should return isAuthenticated=false and errorMessage on SIGNUP_FAILURE 5 passing (347ms) 1 failing 1) httpServer should return Anonymous user if client doesnt send a valid JWT token on user_connected event: TypeError: Cannot read property 'emit' of undefined at Context.<anonymous> (server.test.js:34:14)
as you can see , the 'user connected' log happens very randomly. How do i control it to happen synchronously?
原文:https://stackoverflow.com/questions/37069252
最满意答案
我找到了一种方法,但我不确定这是否是最好的方法:
public int GetMaximumFirstLevelRetries(IBus bus) { var unicastBus = (UnicastBus)bus; var transportConfig = unicastBus.Settings.GetConfigSection<TransportConfig>(); return transportConfig.MaxRetries; }
I found one way but I'm not sure if this is the best way to do it:
public int GetMaximumFirstLevelRetries(IBus bus) { var unicastBus = (UnicastBus)bus; var transportConfig = unicastBus.Settings.GetConfigSection<TransportConfig>(); return transportConfig.MaxRetries; }
相关问答
更多-
NServiceBus:如何从IBus获得最大的第一级重试次数(NServiceBus: How to get max first level retries from IBus)[2023-09-10]
我找到了一种方法,但我不确定这是否是最好的方法: public int GetMaximumFirstLevelRetries(IBus bus) { var unicastBus = (UnicastBus)bus; var transportConfig = unicastBus.Settings.GetConfigSection(); return transportConfig.MaxRetries; } I found one way b ... -
它是执行重试的底层urllib3库。 要设置不同的最大重试次数,请使用备用传输适配器 : from requests.adapters import HTTPAdapter s = requests.Session() s.mount('http://stackoverflow.com', HTTPAdapter(max_retries=5)) 老答案,早于释放请求1.2.1 : requests库没有真正使这个可配置,也没有打算(看到这个拉请求 )。 目前(请求1.1),重试次数设置为0.如果您真的要 ...
-
解决IBus问题 - 在1.5.11之前的IBus可能会导致输入问题(Resolving IBus Issue - IBus prior to 1.5.11 may cause input problems)[2022-03-01]
如果您只使用一个键盘布局,您可以去Ubuntu : 系统设置 - >语言支持 或者如果没有找到(fe mint desktop)在主菜单中搜索语言支持 并将页面底部的键盘输入法从ibus更改为none 。 重新启动Ubuntu 。 这是解决这个问题的最简单方法。 if you use only one keyboard layout you can go to Ubuntu: System Settings -> Language Support or if not found (f.e. mint des ... -
请求最大重试次数TOR(Request Max Retries TOR)[2023-01-25]
我搞定了。 我必须安装“expert”安装程序并将exe添加到我的PATH中。 谢谢 I got it working. I had to install the "expert" installer and add the exe to my PATH. Thank you -
你想要set -x XMODIFIERS @im=ibus 在bash export XMODIFIERS=@im=ibus ,只解释第一个=符号。 因此,这将设置一个名为XMODIFIERS的环境变量,其值为@im=ibus 。 You want set -x XMODIFIERS @im=ibus In bash export XMODIFIERS=@im=ibus, only the first = sign is interpreted. So this sets an environment va ...
-
消息传递系统中即时多次重试的重点是什么?(What is the point of the immediate multiple retries in messaging systems?)[2021-12-16]
我的背景是NServiceBus,所以我的答案可以用这些术语表达。 第一级重试对于非常短暂的错误非常有用。 僵局就是一个很好的例子。 您尝试更改数据库,并选择事务作为死锁受害者。 在这些情况下,第一级重试是完美的。 大多数情况下 ,只需要一次第一级重试。 如果数据库中存在很多争用,可能有2或3次重试就足够了。 第二级重试是为了避免短暂的错误。 考虑一下诸如Web服务停机10秒之类的事情,或者故障切换群集中的SQL Server数据库切换,这可能需要30-60秒。 如果你在几毫秒后重试,这对你没有任何好处,但 ... -
在mule 3.4.2中成功范围的最大重试次数是多少(what is the max retries for until successful scope in mule 3.4.2)[2022-08-05]
默认值为5次重试 。 您可以使用until-successfull范围上的maxRetries属性更改此值,例如: maxRetries="10" 该值为整数,因此其最大值为2147483647 The default value is 5 retries. You can change this value with the maxRetries attribute on the until-successfull scope, for ex: maxRetries="10" This value i ... -
MaxRetries的语义是“至少X倍”。 其原因是性能,因为在同步我们的线程时我们不能放松一些。 你也可以使你的传输非事务性,这将有效地给你每个消息一次尝试,但你也会失去错误队列,所以失败的消息将永远消失。 The semantics of the MaxRetries is "At least X times". The reason for this is performance since we can't be a little more relaxed when it comes to sync ...
-
似乎问题只发生在Gentoo上。 因为系统更新清除了一些字体。 解决方案是安装丢失的字体: emerge media-fonts/font-adobe-75dpi x11-apps/bdftopcf media-fonts/font-alias media-fonts/font-util 注销并重新登录后,我可以再次使用输入法。 It seems the problem only happen on Gentoo. Because system update clear up some fonts. The ...
-
您可以从任务的request.retries属性访问当前重试次数。 @app.task(retries=4, default_retry_delay=1) def fail(): try: assert False except Exception as e: print 'Try {0}/{1}'.format(fail.request.retries, fail.max_retries) # Print log message with curre ...