简体   繁体   English

如何在测试AJAX应用程序时测试断开连接?

[英]How to test a disconnection when testing an AJAX application?

I'm testing an AJAX application where we need to handle a temporary loss of connection to the server or other errors (ie like in Gmail or Google Calendar.) What's a good way to simulate this in testing? 我正在测试一个AJAX应用程序,我们需要处理暂时失去与服务器的连接或其他错误(例如在Gmail或Google日历中)。在测试中模拟这个的好方法是什么?

For example, is there a browser plugin that will allow one to temporarily "turn off" the internet connection for a tab? 例如,是否有一个浏览器插件可以暂时“关闭”标签的互联网连接? I do all my debugging in Chrome so it would be particularly helpful if someone could offer a suggestion for that browser. 我在Chrome中进行所有调试,因此如果有人可以为该浏览器提供建议,那将会特别有用。

Other options I've tried are toggling the network connection to a VM to the server, or shutting down the server and restarting it, neither of which is very lightweight or easy for testing (the latter also doesn't preserve any state.) 我尝试过的其他选项是将VM的网络连接切换到服务器,或者关闭服务器并重新启动它,这些都不是非常轻量级或易于测试(后者也不保留任何状态。)

Clarification: I'm not interested in how to test disconnection handling in test code. 澄清:我对如何在测试代码中测试断开处理感兴趣。 I want to create a disconnection to test when running my app. 我想在运行我的应用程序时创建断开连接以进行测试。 Ideally, it could be something easily toggled in one tab of my browser that doesn't involve borking my entire internet connection. 理想情况下,它可能是我的浏览器的一个选项卡中容易切换的东西,不涉及borking我的整个互联网连接。

On this question, knowing which framework you are using to test would be a very good thing. 在这个问题上,了解您使用哪个框架进行测试将是一件非常好的事情。 For the rest, if the AJAX stuff is handled by an XMLHttpRequest object, there is very little you can do, as once the request has fired, you cannot control it anymore. 对于其余部分,如果AJAX内容由XMLHttpRequest对象处理,那么您可以做的很少,因为一旦请求被触发,您就无法再控制它了。 If you are using a framework such as jQuery, consider juking the events. 如果您使用的是jQuery等框架,请考虑对事件进行juking。 This involves firing an event in a synthetic fashion. 这涉及以合成方式发射事件。 In order to do, if your event handlers are public in scope (which they should be if you are writing maintainable code), you should be able to fire them. 为了做到这一点,如果您的事件处理程序在范围内是公共的(如果您编写可维护的代码,它们应该是公共的),您应该能够触发它们。 If not, make them public. 如果没有,请公开。

Failing that, you can act on the server side by: 如果不这样做,您可以通过以下方式在服务器端执行操作:

  1. Setting up a "test mode" where your server drops connection at a specific time 设置服务器在特定时间断开连接的“测试模式”
  2. Consider routing through a configurable proxy (nginx?) to effectively implement step 1 without modifying your server 考虑通过可配置代理(nginx?)进行路由,以有效地实现步骤1,而无需修改服务器
  3. Fire your disconnection handler by itself synthetically rather than the event. 通过合成而不是事件来自动解除断开连接处理程序。 You know that your event handler for XMLHttpRequest failing is working - that's assured by your browser not being buggy. 您知道XMLHttpRequest失败的事件处理程序正在运行 - 您的浏览器确保没有错误。 What you need to test is if your function works. 您需要测试的是您的功能是否有效。

When we've had to do this in the past we usually have the nic settings open and disable the adapter. 当我们过去必须这样做时,我们通常会打开nic设置并禁用适配器。 Some of the guys w/laptops would run on a wired connection and just unplug it. 一些有笔记本电脑的家伙会在有线连接上运行,只需拔下插头即可。

You could also use netsh: 你也可以使用netsh:

netsh interface set interface <interface name> DISABLED/ENABLED 

I do not know any ready solution, but I have an idea how to achieve that. 我不知道任何现成的解决方案,但我知道如何实现这一目标。

If your server is running on linux you could write a simple script which would add a rule to iptables blocking the traffic on ip or tcp level. 如果您的服务器在Linux上运行,您可以编写一个简单的脚本,该脚本会向iptables添加规则,阻止ip或tcp级别的流量。 Then just run the script to block on unblock the traffic. 然后运行脚本以阻止解锁流量。

I would set up a virtual host on a different port, put there a few scripts to block allt raffic, unblock traffic, throttle etc, and then add those scripts to bookmarks to have a fast way to run them. 我会在不同的端口上设置一个虚拟主机,放置一些脚本来阻止allt raffic,解锁流量,限制等,然后将这些脚本添加到书签以便快速运行它们。 Those scripts would just call iptables with different parameters. 这些脚本只会使用不同的参数调用iptables。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何对 AJAX 应用程序进行黑盒测试? - How to conduct blackbox testing on an AJAX application? 单元测试时如何测试功能是否相等 - How to test functions for equality when unit testing 如何测试从实时网站提取数据的 AJAX 应用程序? - How to test an AJAX application that is pulling data from a live website? 单元测试时如何测试绑定函数的相等性? - How can I test for equality to a bound function when unit testing? 因互联网断开而失败后重试AJAX - Retry an AJAX after failure due to internet disconnection 使用 Jest 测试 React 应用程序时如何查询原生 html 元素? - How to query native html elements when testing a React application with Jest? 如何在前端应用程序的端到端测试流程中测试API? - How to test APIs in an end to end testing flow in front-end application? 使用jasmine测试backbone.js应用程序 - 如何在视图上测试模型绑定? - Testing backbone.js application with jasmine - how to test model bindings on a view? 如何用jsunit测试ajax? - How to test ajax with jsunit? 在用户离开选项卡或关闭屏幕后,如何检测浏览器何时限制计时器和 websockets 断开连接? (javascript) - How to detect when browser throttles timers and websockets disconnection after a user leaves a tab or turns off the screen? (javascript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM