UNPKG

re

Version:
71 lines (64 loc) 2.21 kB
<!DOCTYPE html> <html> <head> <title>Re Test</title> <script src="../lib/re.js"></script> <script> function test(){ var RETRIES = 9, FAKE_FINISH_RETRIES = 7, strategy = { "type": Re.STRATEGIES.EXPONENTIAL, "initial":50, "base":2 }, options = {"retries": RETRIES, "strategy": strategy}, re = new Re(options), last = new Date().getTime(), first = last, results = document.getElementById("results"), fin = document.getElementById("final"), total = document.getElementById("total"), intro = document.getElementById("intro"), broken = document.getElementById("broken"), expectedDuration = calcDuration(re, Math.min(RETRIES, FAKE_FINISH_RETRIES)), introString = "Starting test (should take "+ Math.min(RETRIES, FAKE_FINISH_RETRIES)+" retries and about " + expectedDuration/1000 + " seconds)."; broken.style.display = "none"; intro.appendChild(document.createTextNode(introString)); re.try(function(retryCount, fail, callback){ var now = new Date().getTime(), text = "RETRY: " + retryCount + " of " + options.retries + ", ELAPSED: " + (now - last), result = document.createElement("li"); result.appendChild(document.createTextNode(text)); results.appendChild(result); last = now; if(retryCount < FAKE_FINISH_RETRIES) throw new Error("Not there yet."); else callback(null, retryCount); }, function(err, retries){ if(err) fin.appendChild(document.createTextNode(err)); else fin.appendChild(document.createTextNode("It took this many tries: " + retries)); total.appendChild(document.createTextNode("TOTAL ELAPSED TIME: " + (last - first))); }); } function calcDuration(re, retries){ var i = 0, total = 0; for(i = 0; i < retries; i++){ total += re.retryInterval(i); } return total; } </script> </head> <body> <h1 id="header">Re, Test It Again</h1> <div id="broken">If this is all you see, it's broken.</div> <div id="intro"></div> <ol id="results"> </ol> <div id="final"></div> <div id="total"></div> <script>test()</script> </body> </html>