avoid reserved property names (+ test), fixes #145

This commit is contained in:
jomo 2015-09-25 19:14:53 +02:00
parent 5e1e7f3701
commit c8d74d47be
2 changed files with 39 additions and 29 deletions

View File

@ -119,15 +119,18 @@ var requests = {
}; };
function push_request(userId, type, fun) { function push_request(userId, type, fun) {
if (!requests[type][userId]) { // avoid special properties (e.g. 'constructor')
requests[type][userId] = []; var userId_safe = "!" + userId;
if (!requests[type][userId_safe]) {
requests[type][userId_safe] = [];
} }
requests[type][userId].push(fun); requests[type][userId_safe].push(fun);
} }
// calls back all queued requests that match userId and type // calls back all queued requests that match userId and type
function resume(userId, type, err, hash) { function resume(userId, type, err, hash) {
var callbacks = requests[type][userId]; var userId_safe = "!" + userId;
var callbacks = requests[type][userId_safe];
if (callbacks) { if (callbacks) {
if (callbacks.length > 1) { if (callbacks.length > 1) {
logging.debug(callbacks.length, "simultaneous requests for", userId); logging.debug(callbacks.length, "simultaneous requests for", userId);
@ -142,7 +145,7 @@ function resume(userId, type, err, hash) {
} }
// it's still an empty array // it's still an empty array
delete requests[type][userId]; delete requests[type][userId_safe];
} }
} }
@ -152,7 +155,7 @@ function resume(userId, type, err, hash) {
// callback: error, image hash // callback: error, image hash
function store_images(rid, userId, cache_details, type, callback) { function store_images(rid, userId, cache_details, type, callback) {
var is_uuid = userId.length > 16; var is_uuid = userId.length > 16;
if (requests[type][userId]) { if (requests[type]["!" + userId]) {
logging.debug(rid, "adding to request queue"); logging.debug(rid, "adding to request queue");
push_request(userId, type, callback); push_request(userId, type, callback);
} else { } else {

View File

@ -301,7 +301,13 @@ describe("Crafatar", function() {
}); });
it("should not fail on simultaneous requests", function(done) { it("should not fail on simultaneous requests", function(done) {
var url = "http://localhost:3000/avatars/696a82ce41f44b51aa31b8709b8686f0"; // do not change "constructor" !
// it's a reserved property name, we're testing for that
var sids = ["696a82ce41f44b51aa31b8709b8686f0", "constructor"];
for (var j in sids) {
var id = sids[j];
var url = "http://localhost:3000/avatars/" + id;
// 10 requests at once // 10 requests at once
var requests = 10; var requests = 10;
var finished = 0; var finished = 0;
@ -323,8 +329,9 @@ describe("Crafatar", function() {
}); });
} }
// make simultanous requests // make simultanous requests
for (var j = 0; j < requests; j++) { for (var k = 0; k < requests; k++) {
req(j); req(k);
}
} }
}); });