|
14 | 14 | <script src="../../webcomponentsjs/webcomponents.js"></script>
|
15 | 15 | <script src="../../web-component-tester/browser.js"></script>
|
16 | 16 |
|
17 |
| - <!-- |
| 17 | + |
| 18 | + |
| 19 | + <link rel="import" href="../core-ajax.html"> |
| 20 | + |
| 21 | +</head> |
| 22 | +<body> |
| 23 | + |
| 24 | + <core-ajax |
| 25 | + id="ajax" |
| 26 | + handleAs="json" |
| 27 | + auto></core-ajax> |
| 28 | + |
| 29 | +<!-- |
18 | 30 | Test that when core-ajax fires multiple times as requests are updated,
|
19 | 31 | only the response from the most recent request is used to update the response
|
20 | 32 | object.
|
|
23 | 35 | test('race-condition', function(done) {
|
24 | 36 | var ajax = document.querySelector("core-ajax");
|
25 | 37 | var xhr = sinon.useFakeXMLHttpRequest();
|
| 38 | + var headers = { |
| 39 | + "Content-Type": "text/json" |
| 40 | + }; |
| 41 | + var body = function(url) { |
| 42 | + return '{"url": "' + url + '"}'; |
| 43 | + }; |
| 44 | + var requests = this.requests = []; |
26 | 45 | xhr.onCreate = function (xhr) {
|
27 |
| - var headers = { |
28 |
| - "Content-Type": "text/html" |
29 |
| - } |
30 |
| - var body = '{"url": "' + xhr.url + '"}'; |
31 |
| - if (xhr.url.match('request1')) { |
32 |
| - window.setTimeout(function(){ |
33 |
| - xhr.respond(200, headers, body); |
34 |
| - console.log('response 1 received'); |
35 |
| - }, 200); |
36 |
| - } else if (xhr.url.match('request2')) { |
37 |
| - window.setTimeout(function(){ |
38 |
| - xhr.respond(200, headers, body); |
39 |
| - console.log('response 2 received'); |
40 |
| - }, 100); |
41 |
| - } |
| 46 | + requests.push(xhr); |
| 47 | + }; |
| 48 | + var animationFrameFlush = function(callback) { |
| 49 | + requestAnimationFrame(function(){ |
| 50 | + flush(callback); |
| 51 | + }) |
42 | 52 | };
|
43 |
| - // Make a request that will return in .2 seconds. |
44 |
| - ajax.url="http://example.org/request1"; |
45 |
| - // Make a request that will return in .1 seconds. |
46 |
| - ajax.url="http://example.org/request2"; |
47 |
| - // Wait for the requests to resolve, then check that the later return |
48 |
| - // of request1 doesn't override. |
49 |
| - setTimeout(function(){ |
50 |
| - assert(ajax.response.match('request1'), |
51 |
| - "Race condition detected. An earlier request's delayed response " + |
| 53 | + |
| 54 | + // Make request1, then request2. request2 returns first, followed by request1. |
| 55 | + async.series([ |
| 56 | + function(cb) { |
| 57 | + ajax.url="http://example.org/request1" |
| 58 | + cb(); |
| 59 | + }, |
| 60 | + animationFrameFlush, |
| 61 | + function(cb) { |
| 62 | + ajax.url="http://example.org/request2" |
| 63 | + cb(); |
| 64 | + }, |
| 65 | + animationFrameFlush, |
| 66 | + function(cb) { |
| 67 | + debugger; |
| 68 | + requests[0].respond(200, headers, body("http://example.org/request2")); |
| 69 | + cb(); |
| 70 | + }, |
| 71 | + flush, |
| 72 | + function(cb) { |
| 73 | + debugger; |
| 74 | + requests[1].respond(200, headers, body("http://example.org/request1")); |
| 75 | + cb(); |
| 76 | + }, |
| 77 | + flush, |
| 78 | + function(cb) { |
| 79 | + debugger |
| 80 | + assert(ajax.response.url.match('request1'), |
| 81 | + "Race condition detected. An earlier request's delayed response " + |
52 | 82 | "caused the more recent request's response to be overwritten.");
|
53 |
| - }, 300); |
54 |
| - console.log("test done"); |
55 |
| - done(); |
| 83 | + done(); |
| 84 | + cb(); |
| 85 | + } |
| 86 | + ], function(){}); |
56 | 87 | });
|
57 |
| - </script> |
58 |
| - |
59 |
| - <link rel="import" href="../core-ajax.html"> |
60 |
| - |
61 |
| -</head> |
62 |
| -<body> |
63 |
| - |
64 |
| - <core-ajax |
65 |
| - handleAs="json" |
66 |
| - auto></core-ajax> |
67 |
| - |
| 88 | +</script> |
68 | 89 | </body>
|
69 | 90 | </html>
|
0 commit comments