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) {
|
||||
if (!requests[type][userId]) {
|
||||
requests[type][userId] = [];
|
||||
// avoid special properties (e.g. 'constructor')
|
||||
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
|
||||
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.length > 1) {
|
||||
logging.debug(callbacks.length, "simultaneous requests for", userId);
|
||||
@ -142,7 +145,7 @@ function resume(userId, type, err, hash) {
|
||||
}
|
||||
|
||||
// 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
|
||||
function store_images(rid, userId, cache_details, type, callback) {
|
||||
var is_uuid = userId.length > 16;
|
||||
if (requests[type][userId]) {
|
||||
if (requests[type]["!" + userId]) {
|
||||
logging.debug(rid, "adding to request queue");
|
||||
push_request(userId, type, callback);
|
||||
} 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) {
|
||||
var url = "http://localhost:3000/avatars/696a82ce41f44b51aa31b8709b8686f0";
|
||||
// 10 requests at once
|
||||
var requests = 10;
|
||||
var finished = 0;
|
||||
function partDone() {
|
||||
finished++;
|
||||
if (requests === finished) {
|
||||
done();
|
||||
// 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
|
||||
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