简体   繁体   中英

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?

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.

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.)

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.

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. If you are using a framework such as jQuery, consider juking the events. 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
  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. 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. Some of the guys w/laptops would run on a wired connection and just unplug it.

You could also use 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. 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. Those scripts would just call iptables with different parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM