node tls,模拟套接字ETIMEDOUT

时间:2017-01-23 15:06:07

标签: node.js sockets tcp

我尝试从我的测试中做的是模拟[2017-01-23 09:56:11] request.INFO: Matched route "app_default_starremotecontrol". {"route_parameters":{"_controller":"AppBundle\\Controller\\DefaultController::starRemoteControlAction","_route":"app_default_starremotecontrol"},"request_uri":"http://127.0.0.1:8000/star-remote-control"} [] [2017-01-23 09:56:11] security.DEBUG: Read existing security token from the session. {"key":"_security_main"} [] [2017-01-23 09:56:11] doctrine.DEBUG: SELECT t0.username AS username_1, t0.username_canonical AS username_canonical_2, t0.email AS email_3, t0.email_canonical AS email_canonical_4, t0.enabled AS enabled_5, t0.salt AS salt_6, t0.password AS password_7, t0.last_login AS last_login_8, t0.locked AS locked_9, t0.expired AS expired_10, t0.expires_at AS expires_at_11, t0.confirmation_token AS confirmation_token_12, t0.password_requested_at AS password_requested_at_13, t0.roles AS roles_14, t0.credentials_expired AS credentials_expired_15, t0.credentials_expire_at AS credentials_expire_at_16, t0.id AS id_17 FROM fos_user t0 WHERE t0.id = ? LIMIT 1 [1] [] [2017-01-23 09:56:11] security.DEBUG: User was reloaded from a user provider. {"username":"scottgutman","provider":"FOS\\UserBundle\\Security\\UserProvider"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure". {"event":"kernel.request","listener":"Symfony\\Component\\HttpKernel\\EventListener\\DebugHandlersListener::configure"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest". {"event":"kernel.request","listener":"Symfony\\Component\\HttpKernel\\EventListener\\ProfilerListener::onKernelRequest"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ValidateRequestListener::onKernelRequest". {"event":"kernel.request","listener":"Symfony\\Component\\HttpKernel\\EventListener\\ValidateRequestListener::onKernelRequest"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.request" to listener "Uecode\Bundle\QPushBundle\EventListener\RequestListener::onKernelRequest". {"event":"kernel.request","listener":"Uecode\\Bundle\\QPushBundle\\EventListener\\RequestListener::onKernelRequest"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest". {"event":"kernel.request","listener":"Symfony\\Bundle\\FrameworkBundle\\EventListener\\SessionListener::onKernelRequest"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\FragmentListener::onKernelRequest". {"event":"kernel.request","listener":"Symfony\\Component\\HttpKernel\\EventListener\\FragmentListener::onKernelRequest"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest". {"event":"kernel.request","listener":"Symfony\\Component\\HttpKernel\\EventListener\\RouterListener::onKernelRequest"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest". {"event":"kernel.request","listener":"Symfony\\Component\\HttpKernel\\EventListener\\LocaleListener::onKernelRequest"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\TranslatorListener::onKernelRequest". {"event":"kernel.request","listener":"Symfony\\Component\\HttpKernel\\EventListener\\TranslatorListener::onKernelRequest"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest". {"event":"kernel.request","listener":"Symfony\\Component\\Security\\Http\\Firewall::onKernelRequest"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest". {"event":"kernel.request","listener":"Symfony\\Bundle\\AsseticBundle\\EventListener\\RequestListener::onKernelRequest"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RouterDataCollector::onKernelController". {"event":"kernel.controller","listener":"Symfony\\Bundle\\FrameworkBundle\\DataCollector\\RouterDataCollector::onKernelController"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.controller" to listener "Symfony\Component\HttpKernel\DataCollector\RequestDataCollector::onKernelController". {"event":"kernel.controller","listener":"Symfony\\Component\\HttpKernel\\DataCollector\\RequestDataCollector::onKernelController"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController". {"event":"kernel.controller","listener":"Sensio\\Bundle\\FrameworkExtraBundle\\EventListener\\ControllerListener::onKernelController"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController". {"event":"kernel.controller","listener":"Sensio\\Bundle\\FrameworkExtraBundle\\EventListener\\ParamConverterListener::onKernelController"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\HttpCacheListener::onKernelController". {"event":"kernel.controller","listener":"Sensio\\Bundle\\FrameworkExtraBundle\\EventListener\\HttpCacheListener::onKernelController"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\SecurityListener::onKernelController". {"event":"kernel.controller","listener":"Sensio\\Bundle\\FrameworkExtraBundle\\EventListener\\SecurityListener::onKernelController"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController". {"event":"kernel.controller","listener":"Sensio\\Bundle\\FrameworkExtraBundle\\EventListener\\TemplateListener::onKernelController"} [] [2017-01-23 09:56:11] always.INFO: ClientIP:127.0.0.1 [] [] [2017-01-23 09:56:11] always.INFO: ClientIP:127.0.0.1 [] [] [2017-01-23 09:56:11] security.DEBUG: Stored the security token in the session. {"key":"_security_main"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\ResponseListener::onKernelResponse". {"event":"kernel.response","listener":"Symfony\\Component\\HttpKernel\\EventListener\\ResponseListener::onKernelResponse"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\Security\Http\RememberMe\ResponseListener::onKernelResponse". {"event":"kernel.response","listener":"Symfony\\Component\\Security\\Http\\RememberMe\\ResponseListener::onKernelResponse"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.response" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\HttpCacheListener::onKernelResponse". {"event":"kernel.response","listener":"Sensio\\Bundle\\FrameworkExtraBundle\\EventListener\\HttpCacheListener::onKernelResponse"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelResponse". {"event":"kernel.response","listener":"Symfony\\Component\\HttpKernel\\EventListener\\ProfilerListener::onKernelResponse"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener::onKernelResponse". {"event":"kernel.response","listener":"Symfony\\Bundle\\WebProfilerBundle\\EventListener\\WebDebugToolbarListener::onKernelResponse"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\SaveSessionListener::onKernelResponse". {"event":"kernel.response","listener":"Symfony\\Component\\HttpKernel\\EventListener\\SaveSessionListener::onKernelResponse"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\StreamedResponseListener::onKernelResponse". {"event":"kernel.response","listener":"Symfony\\Component\\HttpKernel\\EventListener\\StreamedResponseListener::onKernelResponse"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.finish_request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelFinishRequest". {"event":"kernel.finish_request","listener":"Symfony\\Component\\HttpKernel\\EventListener\\LocaleListener::onKernelFinishRequest"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.finish_request" to listener "Symfony\Component\HttpKernel\EventListener\TranslatorListener::onKernelFinishRequest". {"event":"kernel.finish_request","listener":"Symfony\\Component\\HttpKernel\\EventListener\\TranslatorListener::onKernelFinishRequest"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.finish_request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelFinishRequest". {"event":"kernel.finish_request","listener":"Symfony\\Component\\HttpKernel\\EventListener\\RouterListener::onKernelFinishRequest"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.finish_request" to listener "Symfony\Component\Security\Http\Firewall::onKernelFinishRequest". {"event":"kernel.finish_request","listener":"Symfony\\Component\\Security\\Http\\Firewall::onKernelFinishRequest"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.terminate" to listener "Symfony\Bundle\SwiftmailerBundle\EventListener\EmailSenderListener::onTerminate". {"event":"kernel.terminate","listener":"Symfony\\Bundle\\SwiftmailerBundle\\EventListener\\EmailSenderListener::onTerminate"} [] [2017-01-23 09:56:11] event.DEBUG: Notified event "kernel.terminate" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelTerminate". {"event":"kernel.terminate","listener":"Symfony\\Component\\HttpKernel\\EventListener\\ProfilerListener::onKernelTerminate"} [] 应该捕获的ETIMEDOUT。实际上,我使用的是第三方TCP服务器,ETIMEDOUT总是被socket.on('error', () => {...})事件捕获。想在我的测试中模仿这种情况。通过error文档,只有可用于此目的的候选者是tls,但它无法实现我的期望:

socket.setTimeout

从控制台我可以看到我正在等待describe('TCP timeout', () => { const TIMEOUT_AFTER_IN_MILLISECONDS = 1 const socket = getActiveSocketFromSomewhere() it('should simulate timeout', () => { socket.setTimeout(TIMEOUT_AFTER_IN_MILLISECONDS, () => { console.log('are we here') }) /** * This will trigger socket communication * with dummy TCP server where socket from * above will be used */ return something() ... }) }) 的答案,并且提供给14ms的回调已被执行,但在我看到我收到来自TCP服务器的响应后:

setTimeout

1 个答案:

答案 0 :(得分:0)

是的,这就是行为。
达到超时后,套接字不会显式结束。文档中明确提到了它,无论何时它都会收到响应
https://nodejs.org/dist/latest-v6.x/docs/api/net.html#net_socket_settimeout_timeout_callback

如果要结束套接字,则需要在触发socket.end()事件后手动呼叫socket.destroy()timeout

<强>代码:

socket.on('timeout',function(){
    socket.end();
})