async-queue-test.js revision b40f5113b5f28daabc29b6ad49c905f290cf6cb7
// FIXME: remove this and update the tests to handle the asynchronicity
function f() {}
}
name : "Queue isntantiation",
test_instantiation : function () {
var basic = new Y.AsyncQueue(),
withCallbacks = new Y.AsyncQueue(f,f,f,f);
}
}));
name : "queue-base",
test_next : function () {
var i = 0;
YUI({
useBrowserConsole : false,
logInclude : { TestRunner: true }
}).use('queue-base', function (Y) {
function inc() { i++; }
var callback,
callback();
}
}
});
}
}));
name : "Test API",
test_chaining : function () {
var q = new Y.AsyncQueue();
q.defaults = {
timeout : 10
};
},
test_add : function () {
var q = new Y.AsyncQueue(f);
q = new Y.AsyncQueue().add(f);
q.add("Only functions and objects are allowed",
null,
1,
true);
q.add({},{}); // empty objects are ok, since config can be defaulted
// Add from within a callback
var count = 0;
function x() {
count++;
}
function addToQueue() {
this.add(x);
}
// Three x calls scheduled. A fourth added during a callback
},
test_remove : function () {
var results = '',
self = this,
q = new Y.AsyncQueue(
function () {
results += 'R';
},
{
id: "not removed",
fn: function () {
results += 'E';
this.remove('me too');
},
timeout: 10
},
function () {
this.remove("fail");
if (q.size() !== 4) {
Y.Assert.fail("Expected 3, got " + q.size() + " - remove(n) should defer until callback completion");
});
}
results += 'M';
},
{ id: "fail",
fn: function () {
});
}
},
function () {
if (q.size() !== 2) {
});
}
results += 'OV';
},
function () {
results += 'E';
});
});
function X() {
q.run();
results += 'X';
}
// Removal when the Queue is inactive is immediate
q.remove("remove me");
q.run();
q.remove("not removed");
this.wait();
},
test_promote : function () {
function O() {
results += 'O';
}
var results = '',
self = this,
q = new Y.AsyncQueue(
function () {
results += "R";
},
{
id: "p",
},
O,
{
id: 'm',
fn: function () {
if (this.count++ > 3) {
results += 'M';
} else if (!this.count) {
q.promote('o');
}
},
iterations : 5
},
{
id : 'o',
fn: O,
timeout: 10
},
function () { results += 'E'; },
{
id : 't',
fn : function () {
results += 'T';
}
},
function () {
});
});
q.promote('p');
q.run();
q.promote('t');
this.wait();
},
test_pause : function () {
var results = '',
self = this,
q = new Y.AsyncQueue(
function () { results += 'P'; },
{
fn: function () {
results += 'A';
},
timeout : 10
},
function () {
results += 'U';
},
function () {
results += 'S';
this.pause();
setTimeout(function () {
q.run();
},10);
});
},
function () {
results += 'E';
});
});
q.run();
// Test during timeout
q.pause();
setTimeout(function () {
q.run();
});
}, 20);
this.wait();
},
test_stop : function () {
var results = "",
self = this,
q = new Y.AsyncQueue(
function () { results += 'S'; },
function () { results += 'T'; },
function () { results += 'O'; },
function () { results += 'P'; },
{
fn: function () {
});
},
timeout: 10
});
q.run();
q.stop();
setTimeout(function () {
});
},100);
q.run();
this.wait();
},
test_getCallback : function () {
var c,
q = new Y.AsyncQueue(
this.pause();
}
},
fn: function () {
}
},
c = q.getCallback('a');
q.run();
c = q.getCallback('a');
q.run();
},
test_isRunning : function () {
var self = this,
q = new Y.AsyncQueue(
function () {
},
{
fn: function () {
q.pause();
});
},
timeout: 10
});
q.run();
/*
setTimeout(function () {
self.resume(function () {
Y.Assert.areSame(false, q.isRunning());
q.run(); // run to completion
Y.Assert.areSame(false, q.isRunning());
});
},100);
*/
this.wait();
}
}));
name : "Test callback config",
test_fn : function () {
var results = '',
q = new Y.AsyncQueue(
function () { results += 'R'; },
{},
function () { results += 'N'; });
q.run();
},
test_context : function () {
var a = { id : 'a',
test : 'A',
fn : function () {
}
},
{
fn: function () {
this.test = 'Z';
}
},
a,
{
fn: function () {
},
});
q.test = 'X';
q.run();
},
test_args : function () {
(new Y.AsyncQueue(
function () {
},
{
fn: function () {
},
},
{
fn: function () {
},
args : 'X'
})).run();
},
test_iterations : function () {
var results = '',
self = this;
(new Y.AsyncQueue(
function () { results += 'A'; },
{ fn: function () {
});
}
})).run();
this.wait();
},
test_until : function () {
var results = '',
self = this;
(new Y.AsyncQueue(
function () { results += 'A'; },
{
fn: function () {
results += 'B';
},
until: function () {
return !this.data;
},
data : '1234'
},
{
fn: function () {
results += 'C';
},
until: function () {
},
timeout: 10
},
{ fn: function () {
});
}
})).run();
this.wait();
},
test_timeout : function () {
var results = 0,
self = this,
// default timeout -1 triggers synchronous mode
q = new Y.AsyncQueue(
inc, // -1 == sync
// garbage timeout doesn't throw error, but is async
until: function () {
return results >= 10;
}
},
function () {
});
}).run();
this.wait();
}
/*
test_waitForIOResponse : function () {
function good() {
var url = 'queue.html?cachebuster='+Y.guid();
Y.io(url, {
on : {
success : function () { results.success++; },
failure : function () { results.failure++; }
}
});
}
function bad() {
var url = Y.guid() + (Math.random() * 1000) + '.html'; // 404
Y.io(url, {
on : {
success : function () { results.success++; },
failure : function () { results.failure++; }
}
});
}
function late() {
var url = 'io_timeout.php?cachebuster=' + Y.guid();
Y.io(url, {
on : {
success : function () { results.success++; },
failure : function () { results.failure++; },
abort : function () { results.failure++; }
},
timeout : 10
});
}
function test(s,f,step) {
return function () {
var msg = "Incorrect number of ",
data;
if (results.success !== s) {
msg += 'successes';
data = [s,results.success];
} else if (results.failure !== f) {
msg += 'failures';
data = [f,results.failure];
} else {
msg = '';
}
if (msg) {
msg += ' at step ' + step +
'. Expected ' + data[0] + ', got ' + data[1];
q.stop();
self.resume(function () {
Y.Assert.fail(msg);
});
}
}
}
var results = { success: 0, failure: 0 },
self = this,
q = new Y.AsyncQueue(
{
fn : good,
waitForIOResponse: true
},
test(1,0,1),
{
fn : function () { good(); good(); good(); },
waitForIOResponse: true
},
test(4,0,2),
{
fn : function () { bad(); good(); late(); },
waitForIOResponse: true
},
test(5,2,3),
{
fn : function () { late(); good(); },
waitForIOResponse: true
},
test(6,3,4),
{
// wait not triggered
fn : function () {
bad(); bad();
}
},
test(6,3,5),
function () { self.resume(function () {}); }).run();
this.wait();
}
*/
}));
name : "Test Events",
test_events : function () {
var results = [],
self = this,
q = new Y.AsyncQueue(
{
timeout: 10
},
{
id: 'x',
},
{
id: 'v',
iterations: 3
},
{
fn: function () {
on : {
failure : function () {
}
}
});
},
waitForIOResponse : true
});
q.after("add", function (e) {
});
}
});
q.after("promote", function () {
setTimeout(function () {
q.run();
}, 0);
});
q.on("complete", function () {
"(onAdd)",
"(afterAdd)",
"(onRemove)",
"(afterRemove)",
"(onExec)",
"E",
"(afterExec)",
"(onShift)",
"(afterShift)",
"(onPromote)",
"(afterPromote)",
"(onExec)",
"V",
"(afterExec)",
"(onExec)",
"V",
"(afterExec)",
"(onExec)",
"V",
"(afterExec)",
"(onShift)",
"(afterShift)",
"(onExec)",
"E",
"(afterExec)",
"(onExec)",
"E",
"(afterExec)",
"(onShift)",
"(afterShift)",
"(onExec)",
"N",
"(afterExec)",
"(onShift)",
"(afterShift)",
/*
"(onExec)",
"T",
"(afterExec)",
"(onShift)",
"(afterShift)",
*/
"(onExec)",
"S",
"(afterExec)",
/* // no shift because stop() flushed _q
"(onShift)",
"(afterShift)",
*/
"(onComplete)"
], results);
});
});
q.remove('x');
q.run();
q.promote('v');
this.wait();
},
test_preventCallback : function () {
function inc () { i++; }
var i = 0,
{
foo: true,
iterations: 20
},
{
until : function () {
return i >= 10;
}
});
q.on('execute', function (e) {
e.preventDefault();
}
});
q.run();
q.on('shift', function (e) {
if (i % 2) {
e.preventDefault();
}
});
q.run();
}
}));
/*
// Avoiding a Y.Test bug where tests repeat infinitely
suite.add(new Y.Test.Case({
name : "From bugs",
// Bug 2528602
test_double_exec_when_pause_and_run_async : function () {
var q = new Y.AsyncQueue(),
register = 0,
self = this;
q.defaults.timeout = 10;
q.add({
id: 'one',
fn: function() {
q.pause();
register += 1;
q.run();
}
}, {
id: 'two',
fn: function() {
register += 10;
},
iterations: 1
});
q.on( 'complete', function () {
self.resume( function () {
Y.log( register );
Y.Assert.areSame( 11, register );
} );
} );
q.run();
this.wait();
}
}));
*/