Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Migrate first race condition test to wct.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajo committed Nov 4, 2014
1 parent 7e6fa6a commit f7b4b6e
Showing 1 changed file with 45 additions and 86 deletions.
131 changes: 45 additions & 86 deletions test/core-ajax-response-and-error.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,102 +9,61 @@
-->
<html>
<head>
<title>core-ajax-response-and-error</title>
<title>core-ajax</title>

<script src="../../webcomponentsjs/webcomponents.js"></script>
<script src="../../web-component-tester/browser.js"></script>

<link rel="import" href="../core-ajax.html">
<!--
Test that when core-ajax fires multiple times as requests are updated,
only the response from the most recent request is used to update the response
object.
-->
<script>
test('race condition', function(done){console.log("running test")});
test('race-condition', function(done) {
var ajax = document.querySelector("core-ajax");
var xhr = sinon.useFakeXMLHttpRequest();
xhr.onCreate = function (xhr) {
var headers = {
"Content-Type": "text/html"
}
var body = '{"url": "' + xhr.url + '"}';
if (xhr.url.match('request1')) {
window.setTimeout(function(){
xhr.respond(200, headers, body);
console.log('response 1 received');
}, 200);
} else if (xhr.url.match('request2')) {
window.setTimeout(function(){
xhr.respond(200, headers, body);
console.log('response 2 received');
}, 100);
}
};
// Make a request that will return in .2 seconds.
ajax.url="http://example.org/request1";
// Make a request that will return in .1 seconds.
ajax.url="http://example.org/request2";
// Wait for the requests to resolve, then check that the later return
// of request1 doesn't override.
setTimeout(function(){
assert(ajax.response.match('request1'),
"Race condition detected. An earlier request's delayed response " +
"caused the more recent request's response to be overwritten.");
}, 300);
console.log("test done");
done();
});
</script>

<link rel="import" href="../core-ajax.html">

</head>
<body>

<polymer-element name="race-condition">
<template>
<style>section {margin-top: 20px;}</style>
<core-ajax
id='ajax'
url="http://httpbin.org/delay/{{delay}}"
handleas="json" auto
response="{{response}}"
on-core-response="{{handleResponse}}"></core-ajax>
<div>Response url: {{response.url}}</div>
<div>Result: {{testResult}}</div>
<core-ajax
handleAs="json"
auto></core-ajax>

<section>
<div>Requests</div>
<ul>
<template repeat='{{request in requests}}'>
<li>
{{request.delay}} second delay, Status: {{request.statusText}}
</li>
</template>
</ul>
</section>
</template>
<script>
Polymer('race-condition',{
delay: 1,
response: null,
testResult: 'pending...',
passed: false,
requests: [],
observe: {
'$.ajax.activeRequest': 'requestChanged'
},
domReady: function() {
setTimeout(function() {
if (this.response != null) {
console.error('HTTP request returned too quick!')
chai.assert.fail(
'', '', 'Indeterminate, initial request returned too quick');
this.testResult = 'indeterminate';
return;
}
this.delay = 2;
}.bind(this), 100);
// This will fail the test if it neither passes nor fails in time.
this.finalTimeout = setTimeout(function() {
chai.assert.fail('', '', 'Test timed out.');
}, 7000);
},
responseChanged: function() {
if (this.response.url != this.$.ajax.url) {
this.testResult = 'FAIL';
chai.assert.fail(this.$.ajax.url, this.response.url,
'Race condition in response attribute');
return;
}
this.passed = true;
},
passedChanged: function() {
if (this.passed && this.testResult == 'pending...') {
this.testResult = 'PASS';
clearTimeout(this.finalTimeout);
done();
}
},
requestChanged: function(o, n) {
this.requests.push({
'statusText': 'pending',
xhr: n,
delay: this.delay
});
},
handleResponse: function(resp) {
var xhr = resp.detail.xhr;
for (var i = 0; i < this.requests.length; i++) {
if (this.requests[i].xhr === xhr) {
this.requests[i].statusText = xhr.statusText;
}
}
}
});
</script>
</polymer-element>
<race-condition></race-condition>
</body>
</html>

0 comments on commit f7b4b6e

Please sign in to comment.