Created
March 18, 2019 06:47
-
-
Save Varun-garg/6103ed33c4a2bcd6cab66ed59f3ebf16 to your computer and use it in GitHub Desktop.
Got no race condition in ticks_race.js, but got in request_eace.js
This file contains 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
/* eslint-disable strict */ | |
const http = require('http') | |
const https = require('https') | |
const assert = require('assert') | |
const port = 9000 | |
let global_start = null; | |
// function sleep(ms) { | |
// return new Promise(resolve => { | |
// setTimeout(resolve, ms) | |
// }) | |
// } | |
function requestPromiseToAsync(options) { | |
return new Promise((resolve) => { | |
https.get(options, function (resp) { | |
resp.on('data', function (data) { | |
Response = data; | |
resolve(resp.statusCode); | |
}); | |
}); | |
}); | |
} | |
const server = http.createServer(async function (request, response) { | |
let start = Date.now(); | |
global_start = start; | |
console.log(`starting: ${start}`) | |
for (i = 0; i < 10000; i++) { | |
console.log(`starting request ${i + 1} for ${start}`) | |
let resultStatus = await requestPromiseToAsync('https://recaptcha.google.com/recaptcha/api/siteverify') | |
// await sleep(2500) | |
console.log(`completed request ${i + 1} for ${start}, global_start was ${global_start}`) | |
assert(resultStatus == 200); | |
if (global_start != start) { | |
console.log(`Detected race condition, start: ${start}, global_start: ${global_start}`) | |
} | |
} | |
end = Date.now(); | |
response.end(`${start}`); | |
}); | |
server.listen(port); | |
let count = 0; | |
let timer = setInterval(() => { | |
count += 1; | |
if (count == 50) | |
clearInterval(timer); | |
console.log('starting request'); | |
http.get(`http://localhost:${port}`, function (res) { | |
res.on('data', function (chunk) { | |
console.log('finished request: ' + chunk); | |
}); | |
}) | |
}, 50) |
This file contains 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
/* eslint-disable strict */ | |
const http = require('http') | |
const port = 9000 | |
let global_start = null; | |
function f1() { | |
let start = Date.now() | |
global_start = start | |
console.log(`starting: ${start}`) | |
for (i = 0; i < 100000; i++) { | |
for (j = 0; j < 10000; j++) { | |
if (global_start != start) { | |
console.log(`Detected race condition, start: ${start}, global_start: ${global_start}`) | |
} | |
} | |
} | |
end = Date.now(); | |
return `${start}`; | |
} | |
const server = http.createServer(function (request, response) { | |
response.end(f1()); | |
}); | |
server.listen(port); | |
setInterval(() => { | |
console.log('starting request'); | |
http.get(`http://localhost:${port}`, function (res) { | |
res.on('data', function (chunk) { | |
console.log('finished request: ' + chunk); | |
}); | |
}) | |
}, 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment