Created
June 30, 2011 01:08
-
-
Save wwalser/1055426 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * This is the simplest form of the use case. This fails because the first start()'s async bit kicks in before | |
| * the second call to start() even though the semaphore is at 1 it will call process(). | |
| */ | |
| test("test synchronous calls to stop", 2, function() { | |
| stop(); | |
| setTimeout(function(){ | |
| ok(true, 'first'); | |
| start(); | |
| stop(); | |
| setTimeout(function(){ | |
| ok(true, 'second'); | |
| start(); | |
| }, 100); | |
| }, 100); | |
| }); | |
| /** | |
| * This is a more elaborate test case involving a utility function that can't be nested because of the | |
| * synchronous stop() problem. | |
| */ | |
| QUtil{ | |
| waitForCondition : function(options) { | |
| var intervalId, | |
| startTime = new Date(); | |
| options = jQuery.extend({ | |
| maxWait: 500, | |
| pollPeriod: 50, | |
| doAfterTimeout: function(){ | |
| ok(false, "waitForCondition timed out with no doAfterTimeout function to call"); | |
| }, | |
| doAfterReady: function(){}, | |
| condition: function(){ok(false, "no condition passed");} | |
| }, options); | |
| var intervalTrigger = function() { | |
| if (options.condition()) { | |
| start(); | |
| clearInterval(intervalId); | |
| options.doAfterReady(); | |
| } else if (((new Date()) - startTime) > options.maxWait) { | |
| start(); | |
| clearInterval(intervalId); | |
| options.doAfterTimeout(); | |
| } | |
| }; | |
| stop(); | |
| intervalId = setInterval(intervalTrigger, options.pollPeriod); | |
| } | |
| } | |
| test("launch button launches rocket", 2, function() { | |
| $("#myAwesomeButton").click(); //anything that triggers an async event | |
| QUtil.waitForCondition({ | |
| condition: function() { | |
| return true; //check something that async event changes | |
| }, | |
| doAfterReady: function() { | |
| ok(true, "async event success"); | |
| $("#myAwesomeButton").click(); //anything that triggers an async event | |
| QUtil.waitForCondition({ | |
| condition: function() { | |
| return true; //check something that second async event changes | |
| }, | |
| doAfterReady: function() { | |
| ok(true, "second async event success"); | |
| }, | |
| doAfterTimeout: function(){ | |
| ok(false, "second async event failed"); | |
| } | |
| }); | |
| }, | |
| doAfterTimeout: function(){ | |
| ok(false, "async event not successful"); | |
| } | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment