mirror of
https://github.com/azures04/crafatar.git
synced 2026-03-21 23:41:18 +01:00
avoid reserved property names (+ test), fixes #145
This commit is contained in:
parent
5e1e7f3701
commit
c8d74d47be
@ -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 {
|
||||||
|
|||||||
53
test/test.js
53
test/test.js
@ -301,30 +301,37 @@ 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" !
|
||||||
// 10 requests at once
|
// it's a reserved property name, we're testing for that
|
||||||
var requests = 10;
|
var sids = ["696a82ce41f44b51aa31b8709b8686f0", "constructor"];
|
||||||
var finished = 0;
|
|
||||||
function partDone() {
|
for (var j in sids) {
|
||||||
finished++;
|
var id = sids[j];
|
||||||
if (requests === finished) {
|
var url = "http://localhost:3000/avatars/" + id;
|
||||||
done();
|
// 10 requests at once
|
||||||
|
var requests = 10;
|
||||||
|
var finished = 0;
|
||||||
|
function partDone() {
|
||||||
|
finished++;
|
||||||
|
if (requests === finished) {
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function req() {
|
||||||
|
request.get(url, function(error, res, body) {
|
||||||
|
assert.ifError(error);
|
||||||
|
assert.strictEqual(res.statusCode, 200);
|
||||||
|
assert_headers(res);
|
||||||
|
assert(res.headers.etag);
|
||||||
|
assert.strictEqual(res.headers["content-type"], "image/png");
|
||||||
|
assert(body);
|
||||||
|
partDone();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// make simultanous requests
|
||||||
|
for (var k = 0; k < requests; k++) {
|
||||||
|
req(k);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
function req() {
|
|
||||||
request.get(url, function(error, res, body) {
|
|
||||||
assert.ifError(error);
|
|
||||||
assert.strictEqual(res.statusCode, 200);
|
|
||||||
assert_headers(res);
|
|
||||||
assert(res.headers.etag);
|
|
||||||
assert.strictEqual(res.headers["content-type"], "image/png");
|
|
||||||
assert(body);
|
|
||||||
partDone();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// make simultanous requests
|
|
||||||
for (var j = 0; j < requests; j++) {
|
|
||||||
req(j);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user