Last active
August 29, 2015 14:27
-
-
Save andrewwakeling/430db7720a4393c4324b to your computer and use it in GitHub Desktop.
Testing locking with slimjack/IWC
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="iwc-all.js"></script> | |
<script> | |
// The amount of time the async function is likely to take in (in milliseconds). | |
var ASYNC_DURATION_MIN = 10; | |
var ASYNC_DURATION_MAX = 5000; | |
// The amount of time before we attempt to send again (in milliseconds). | |
var MINIMUM_NEXT_TIME = 1000; | |
var MAXIMUM_NEXT_TIME = 43; | |
var LOCK_ID = 'unique.lock.id'; | |
var SHARED_VALUE_KEY = 'shared.value'; | |
/** | |
* Return an integer between the specified range. | |
*/ | |
function randomBetween(lower, higher) { | |
return Math.floor((Math.random() * (higher - lower)) + lower); | |
} | |
/** | |
* Use alert() so it is easy to pick up errors amongst multiple tabs. | |
*/ | |
function error(message) { | |
console.error(message); | |
alert(message); | |
} | |
// Capture any global errors | |
window.onerror = function () { | |
error(JSON.stringify(Array.prototype.splice.call(arguments, 0, 5))); | |
}; | |
function timestamp_log(message) { | |
var now = new Date(); | |
console.log(['[', now.valueOf(), '] ', now.toString(), ': ', message].join('')); | |
} | |
/** | |
* We're expecting the majority of this code to never be running more than once (across multiple tabs) | |
* concurrently. | |
* | |
* Call done() when everything is finished. | |
* If something goes wrong, it will console.error/alert and then never continue again. | |
*/ | |
function fakeAsync(duration, done) { | |
timestamp_log('fakeAsync: ' + duration); | |
var lock = SJ.lock(LOCK_ID, function () { | |
/** START CRITICAL SECTION **/ | |
// Set a shared localStorage value to a random value which will be checked later before releasing the lock. | |
var randomValue = Math.round(Math.random() * 9999999).toString(); | |
localStorage[SHARED_VALUE_KEY] = randomValue; | |
timestamp_log('Set: ' + randomValue); | |
// We need to yield immediately for this change to persist to LocalStorage. | |
setTimeout(function () { | |
var value = localStorage[SHARED_VALUE_KEY]; | |
if (value !== randomValue) { | |
error('The random value was not assigned. We expected "' + randomValue + '" but got "' + value + '" instead'); | |
} else { | |
// Mimic an asynchronous call that takes the specified time to complete. (e.g. This might be an AJAX request). | |
setTimeout(function () { | |
var value = localStorage[SHARED_VALUE_KEY]; | |
if (value !== randomValue) { | |
error('The random value appears to have changed in a ' + duration + ' ms duration. We expected "' + randomValue + '" but got "' + value + '" instead'); | |
} else { | |
lock.release(); | |
done(); | |
} | |
}, duration); | |
} | |
}, 1); | |
/** END CRITICAL SECTION **/ | |
}); | |
} | |
(function _test() { | |
fakeAsync(randomBetween(ASYNC_DURATION_MIN, ASYNC_DURATION_MAX), function () { | |
setTimeout(function () { | |
_test(); | |
}, randomBetween(MINIMUM_NEXT_TIME, MAXIMUM_NEXT_TIME)); | |
}); | |
}()); | |
</script> | |
</head> | |
<body> | |
<a href="./iwc.html">more</a> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment