Commit 45f127cc authored by Romain Courteaud's avatar Romain Courteaud

Fix bad usage of queue.

Ensure to cancel the ongoing promise if queue is cancelled.
parent b413244c
......@@ -56,6 +56,11 @@
return new RSVP.Queue()
.push(function returnPushableResult() {
return result;
}, function (e) {
if ((result !== undefined) && (result.cancel !== undefined)) {
result.cancel();
}
throw e;
});
}
......@@ -426,6 +431,12 @@
var queue = new RSVP.Queue()
.push(function waitForPromiseToMonitor() {
return promise_to_monitor;
}, function (e) {
if ((promise_to_monitor !== undefined) &&
(promise_to_monitor.cancel !== undefined)) {
promise_to_monitor.cancel();
}
throw e;
})
.push(function handlePromiseToMonitorSuccess(fulfillmentValue) {
// Promise to monitor is fullfilled, remove it from the list
......@@ -608,6 +619,7 @@
gadget.__job_dict[name].cancel();
}
gadget.__job_dict[name] = job_promise;
// gadget.__monitor.monitor(job_promise
gadget.__monitor.monitor(new RSVP.Queue()
.push(function waitForJobPromise() {
return job_promise;
......@@ -1203,6 +1215,11 @@
return new RSVP.Queue()
.push(function () {
return result;
}, function (e) {
if ((result !== undefined) && (result.cancel !== undefined)) {
result.cancel();
}
throw e;
})
.push(function setAsyncGadgetInstanceHTMLContext(gadget_instance) {
return setGadgetInstanceHTMLContext(gadget_instance, options,
......
......@@ -253,6 +253,39 @@
});
});
test('lockAndRun cancel stop first execution', function () {
var mutex = new Mutex(),
counter = 0;
stop();
expect(2);
function assertCounter(value) {
equal(counter, value);
counter += 1;
}
function callback1() {
assertCounter(0);
return new RSVP.Queue()
.push(function () {
return RSVP.delay(50);
})
.push(function () {
assertCounter(-999);
ok(false, 'Should not reach that code');
});
}
return new RSVP.Queue()
.push(function () {
var promise = mutex.lockAndRun(callback1);
promise.cancel('cancel callback1');
return RSVP.delay(200);
})
.push(function () {
assertCounter(1);
})
.always(function () {
start();
});
});
test('lockAndRun cancel does not cancel previous execution', function () {
var mutex = new Mutex(),
......
......@@ -2229,6 +2229,153 @@
});
});
test('mutex prevent concurrent execution', function () {
// Subclass RenderJSGadget to not pollute its namespace
var Klass = function () {
RenderJSGadget.call(this);
}, gadget,
counter = 0;
Klass.prototype = new RenderJSGadget();
Klass.prototype.constructor = Klass;
Klass.declareMethod = RenderJSGadget.declareMethod;
gadget = new Klass();
function assertCounter(value) {
equal(counter, value);
counter += 1;
}
Klass.declareMethod('testFoo', function (expected_counter) {
assertCounter(expected_counter);
return new RSVP.Queue()
.push(function () {
return RSVP.delay(50);
})
.push(function () {
assertCounter(expected_counter + 1);
return counter;
});
}, {mutex: 'foo'});
// method can be called
stop();
expect(10);
return new RSVP.Queue()
.push(function () {
return RSVP.all([
gadget.testFoo(0),
gadget.testFoo(2),
gadget.testFoo(4)
]);
})
.push(function (result_list) {
equal(result_list[0], 2);
equal(result_list[1], 4);
equal(result_list[2], 6);
assertCounter(6);
})
.always(function () {
start();
});
});
test('mutex first cancellation stop execution', function () {
// Subclass RenderJSGadget to not pollute its namespace
var Klass = function () {
RenderJSGadget.call(this);
}, gadget,
counter = 0;
Klass.prototype = new RenderJSGadget();
Klass.prototype.constructor = Klass;
Klass.declareMethod = RenderJSGadget.declareMethod;
gadget = new Klass();
function assertCounter(value) {
equal(counter, value);
counter += 1;
}
Klass.declareMethod('testFoo', function (expected_counter) {
assertCounter(expected_counter);
return new RSVP.Queue()
.push(function () {
return RSVP.delay(50);
})
.push(function () {
assertCounter(expected_counter + 1);
ok(false, 'Should not reach that code');
});
}, {mutex: 'foo'});
// method can be called
stop();
expect(2);
return new RSVP.Queue()
.push(function () {
// Immediately cancel the first call
gadget.testFoo(0).cancel();
return RSVP.delay(200);
})
.push(function () {
assertCounter(1);
})
.always(function () {
start();
});
});
test('not mutex first cancellation stop execution', function () {
// Subclass RenderJSGadget to not pollute its namespace
var Klass = function () {
RenderJSGadget.call(this);
}, gadget,
counter = 0;
Klass.prototype = new RenderJSGadget();
Klass.prototype.constructor = Klass;
Klass.declareMethod = RenderJSGadget.declareMethod;
gadget = new Klass();
function assertCounter(value) {
equal(counter, value);
counter += 1;
}
Klass.declareMethod('testFoo', function (expected_counter) {
// console.log('aaaaa', expected_counter);
assertCounter(expected_counter);
return new RSVP.Queue()
.push(function () {
return RSVP.delay(50);
})
.push(function () {
// console.log('bbbbbbb', expected_counter);
assertCounter(expected_counter + 1);
ok(false, 'Should not reach that code');
});
});
// method can be called
stop();
expect(2);
return new RSVP.Queue()
.push(function () {
// Immediately cancel the first call
gadget.testFoo(0).cancel();
return RSVP.delay(200);
})
.push(function () {
assertCounter(1);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// RenderJSGadgetKlass.ready
/////////////////////////////////////////////////////////////////
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment