4
<title>Test Page</title>
5
<style type="text/css">
7
font: normal 125%/1.4 Arial, sans-serif;
9
.yui3-skin-sam .yui3-console .yui3-console-content {
13
.yui3-skin-sam .yui3-console .yui3-console-bd {
16
.yui3-skin-sam .yui3-console-entry-time {
21
<body class="yui3-skin-sam">
23
<div id="testbed"></div>
25
<script type="text/javascript" src="../../../build/yui/yui.js"></script>
26
<!--script type="text/javascript" src="../../../build/async-queue/async-queue.js"></script-->
27
<script type="text/javascript">
30
}).use('test','console','async-queue','io-base',function (Y) {
32
var suite = new Y.Test.Suite("Tests");
34
// FIXME: remove this and update the tests to handle the asynchronicity
35
Y.AsyncQueue.defaults.timeout = -1;
39
if (!window.console) {
43
suite.add(new Y.Test.Case({
44
name : "Queue isntantiation",
46
test_instantiation : function () {
47
var basic = new Y.AsyncQueue(),
48
withCallbacks = new Y.AsyncQueue(f,f,f,f);
50
Y.Assert.areSame(true, basic instanceof Y.AsyncQueue);
52
Y.Assert.areSame(0, basic.size());
53
Y.Assert.areSame(0, basic._q.length);
55
Y.Assert.areSame(4, withCallbacks.size());
56
Y.Assert.isFunction(withCallbacks._q[0]);
57
Y.Assert.isFunction(withCallbacks.next());
58
Y.Assert.areSame(f, withCallbacks.next().fn);
62
suite.add(new Y.Test.Case({
65
test_next : function () {
69
useBrowserConsole : false,
70
logInclude : { TestRunner: true }
71
}).use('queue-base', function (Y) {
72
function inc() { i++; }
76
q = new Y.Queue(inc, inc, "string", inc);
78
while ((callback = q.next())) {
79
if (Y.Lang.isFunction(callback)) {
85
Y.Assert.areSame(3, i);
89
suite.add(new Y.Test.Case({
92
test_chaining : function () {
93
var q = new Y.AsyncQueue();
98
Y.Assert.areSame(q, q.add());
99
Y.Assert.areSame(q, q.add(f));
100
Y.Assert.areSame(q, q.add(f,f,{fn:f,id:'a'},"garbage"));
102
Y.Assert.areSame(q, q.pause());
103
Y.Assert.areSame(q, q.promote('a'));
104
Y.Assert.areSame(q, q.remove('a'));
105
Y.Assert.areSame(q, q.run());
106
Y.Assert.areSame(q, q.stop());
109
test_add : function () {
110
var q = new Y.AsyncQueue(f);
112
Y.Assert.areSame(1, q.size());
114
q = new Y.AsyncQueue().add(f);
115
Y.Assert.areSame(1, q.size());
117
q.add(f,f).add(f,f,f);
118
Y.Assert.areSame(6, q.size());
120
q.add("Only functions and objects are allowed",
126
Y.Assert.areSame(6, q.size());
128
q.add({},{}); // empty objects are ok, since config can be defaulted
129
Y.Assert.areSame(8, q.size());
131
// Add from within a callback
136
function addToQueue() {
140
// Three x calls scheduled. A fourth added during a callback
141
q = new Y.AsyncQueue(x,f,x,addToQueue,f,x).run();
143
Y.Assert.areSame(4,count);
146
test_remove : function () {
154
q = new Y.AsyncQueue(
156
Y.Assert.areSame(7, this.size());
159
{ id: "remove me", fn: X },
164
this.remove('me too');
168
{ id: "me too", fn: X },
171
if (q.size() !== 4) {
172
self.resume(function () {
173
Y.Assert.fail("Expected 3, got " + q.size() + " - remove(n) should defer until callback completion");
180
self.resume(function () {
181
Y.Assert.fail("This callback should have been removed");
186
if (q.size() !== 2) {
187
self.resume(function () {
188
Y.Assert.fail("Size should be 1");
194
self.resume(function () {
196
Y.Assert.areSame('REMOVE', results);
200
Y.Assert.areSame(8, q.size());
202
// Removal when the Queue is inactive is immediate
203
q.remove("remove me");
204
Y.Assert.areSame(7, q.size());
207
Y.Assert.areSame('R',results);
208
Y.Assert.areSame(6, q.size());
210
q.remove("not removed");
211
Y.Assert.areSame(6, q.size());
216
test_promote : function () {
223
q = new Y.AsyncQueue(
229
fn: function () { results += 'P'; }
235
if (this.count++ > 3) {
237
} else if (!this.count) {
241
context : { count : 0 },
249
function () { results += 'E'; },
257
self.resume(function () {
258
Y.Assert.areSame('PROMOTE', results);
262
Y.Assert.isUndefined(q._q[0].id);
265
Y.Assert.areSame('p', q._q[0].id);
268
Y.Assert.areSame('PROM', results);
275
test_pause : function () {
278
q = new Y.AsyncQueue(
279
function () { results += 'P'; },
293
self.resume(function () {
294
Y.Assert.areSame('PAUS',results);
296
setTimeout(function () {
306
self.resume(function () {
307
Y.Assert.areSame('PAUSE',results);
311
Y.Assert.areSame(5,q.size());
314
// Test during timeout
315
Y.Assert.areSame('P', results);
318
setTimeout(function () {
319
self.resume(function () {
328
test_stop : function () {
331
q = new Y.AsyncQueue(
332
function () { results += 'S'; },
333
function () { results += 'T'; },
334
function () { results += 'O'; },
335
function () { results += 'P'; },
338
self.resume(function () {
339
Y.Assert.fail("Synchronous q.stop() should have cleared this async callback");
347
Y.Assert.areSame('STOP',results);
348
Y.Assert.areSame(0,q.size());
350
setTimeout(function () {
351
self.resume(function () {
352
Y.Assert.areSame('STOP',results);
361
test_getCallback : function () {
363
q = new Y.AsyncQueue(
364
{ id : 'a', test: 1 },
365
{ id : 'b', test: 2, fn: function () {
369
{ id : 'c', test: 3 },
372
Y.Assert.areSame(this._q[0], this.getCallback('d'));
375
{ id : 'a', test: 5 });
377
q.defaults = { fn: function () {} };
379
c = q.getCallback('a');
380
Y.Assert.isObject(c);
381
Y.Assert.areSame(1, c.test);
384
c = q.getCallback('a');
385
Y.Assert.isObject(c);
386
Y.Assert.areSame(5, c.test);
391
test_isRunning : function () {
393
q = new Y.AsyncQueue(
395
Y.Assert.areSame(true, this.isRunning());
400
self.resume(function () {
401
Y.Assert.areSame(false, q.isRunning());
407
Y.Assert.areSame(false, q.isRunning());
410
Y.Assert.areSame(true, q.isRunning());
413
setTimeout(function () {
414
self.resume(function () {
415
Y.Assert.areSame(false, q.isRunning());
416
q.run(); // run to completion
417
Y.Assert.areSame(false, q.isRunning());
426
suite.add(new Y.Test.Case({
427
name : "Test callback config",
429
test_fn : function () {
431
q = new Y.AsyncQueue(
432
function () { results += 'R'; },
434
function () { results += 'N'; });
436
q.defaults = { fn: function () { results += 'U' } };
439
Y.Assert.areSame("RUN", results);
441
q.add({ fn : "results += 'X'" },
442
{ fn : /results += 'X'/ },
443
{ fn : function () { Y.Assert.areSame("RUN", results); } }).run();
446
test_context : function () {
450
Y.Assert.areSame('A', this.test);
454
q = new Y.AsyncQueue({ test : 'callbacks exec from Queue ctx by default' },
455
function () { Y.Assert.areSame('X', this.test); },
458
Y.Assert.areSame('X', this.test);
462
function () { Y.Assert.areSame('Z', this.test); },
466
Y.Assert.areSame('B', this.test);
468
context : { test : 'B' }
471
q.getCallback('a').context = a;
477
test_args : function () {
480
Y.Assert.areSame(0,arguments.length);
484
Y.ArrayAssert.itemsAreSame([1,2,3],arguments);
490
Y.ArrayAssert.itemsAreSame(['X'],arguments);
496
test_iterations : function () {
501
function () { results += 'A'; },
502
{ fn: function () { results += 'B'; } },
503
{ fn: function () { results += 'C'; }, iterations: 3 },
504
{ fn: function () { results += 'D'; }, iterations: 3, timeout: 10 },
506
self.resume(function () {
507
Y.Assert.areSame('ABCCCDDD', results);
515
test_until : function () {
520
function () { results += 'A'; },
526
this.data = this.data.slice(1);
536
return results.length >= 7;
541
self.resume(function () {
542
Y.Assert.areSame('ABBBCCC', results);
547
Y.Assert.areSame('ABBB', results);
552
test_timeout : function () {
553
function inc() { ++results; }
557
// default timeout -1 triggers synchronous mode
558
q = new Y.AsyncQueue(
560
{ fn: inc }, // -1 == sync
561
{ fn: inc, timeout: 10, iterations: 4 },
562
{ fn: inc, timeout: -300, iterations: 4 }, // neg == sync
563
// garbage timeout doesn't throw error, but is async
564
{ fn: inc, timeout: 'a',
566
return results >= 10;
570
self.resume(function () {
571
Y.Assert.areSame(10,results);
575
Y.Assert.areSame(2, results);
581
test_waitForIOResponse : function () {
583
var url = 'queue.html?cachebuster='+Y.guid();
586
success : function () { results.success++; },
587
failure : function () { results.failure++; }
593
var url = Y.guid() + (Math.random() * 1000) + '.html'; // 404
596
success : function () { results.success++; },
597
failure : function () { results.failure++; }
603
var url = 'io_timeout.php?cachebuster=' + Y.guid();
606
success : function () { results.success++; },
607
failure : function () { results.failure++; },
608
abort : function () { results.failure++; }
614
function test(s,f,step) {
616
var msg = "Incorrect number of ",
619
if (results.success !== s) {
621
data = [s,results.success];
622
} else if (results.failure !== f) {
624
data = [f,results.failure];
630
msg += ' at step ' + step +
631
'. Expected ' + data[0] + ', got ' + data[1];
633
self.resume(function () {
640
var results = { success: 0, failure: 0 },
642
q = new Y.AsyncQueue(
645
waitForIOResponse: true
649
fn : function () { good(); good(); good(); },
650
waitForIOResponse: true
654
fn : function () { bad(); good(); late(); },
655
waitForIOResponse: true
659
fn : function () { late(); good(); },
660
waitForIOResponse: true
664
// wait not triggered
670
function () { self.resume(function () {}); }).run();
677
suite.add(new Y.Test.Case({
678
name : "Test Events",
680
test_events : function () {
683
q = new Y.AsyncQueue(
684
function () { results.push("E"); this.pause(); },
686
fn: function () { results.push("E"); },
687
until: function () { return results.length > 25; },
692
fn: function () { results.push("X"); }
696
fn: function () { results.push("V"); },
702
Y.io(Y.guid() + '.html', { // 404
704
failure : function () {
710
waitForIOResponse : true
713
q.on('execute',function () { results.push("(onExec)"); });
714
q.after('execute', function () { results.push("(afterExec)"); });
716
q.on("shift", function () { results.push("(onShift)"); });
717
q.after("shift", function () { results.push("(afterShift)"); });
719
q.on("remove", function () { results.push("(onRemove)"); });
720
q.after("remove", function () { results.push("(afterRemove)"); });
722
q.on("add", function (e) { results.push("(onAdd)"); });
723
q.after("add", function (e) {
725
results.push("(afterAdd)");
726
if (!data || data.length !== 4) {
727
self.resume(function () {
728
Y.Assert.fail("add args not right");
733
q.on("promote", function () { results.push("(onPromote)"); });
735
q.after("promote", function () {
736
results.push("(afterPromote)");
737
setTimeout(function () {
742
q.on("complete", function () {
743
results.push("(onComplete)");
744
self.resume(function () {
745
Y.ArrayAssert.itemsAreEqual([
804
/* // no shift because stop() flushed _q
814
q.add(function () { results.push("S"); this.stop(); },f,f,f);
823
test_preventCallback : function () {
824
function inc () { i++; }
827
q = new Y.AsyncQueue(inc,inc,
835
until : function () {
840
q.on('execute', function (e) {
841
if (e.callback.foo) {
848
Y.Assert.areSame(10,i);
850
q = new Y.AsyncQueue(inc, inc, inc, inc, inc, inc, inc, inc, inc, inc);
851
q.on('shift', function (e) {
854
q._q[0].iterations++;
860
Y.Assert.areSame(30, i);
865
// Avoiding a Y.Test bug where tests repeat infinitely
866
suite.add(new Y.Test.Case({
870
test_double_exec_when_pause_and_run_async : function () {
871
var q = new Y.AsyncQueue(),
875
q.defaults.timeout = 10;
891
q.on( 'complete', function () {
892
self.resume( function () {
894
Y.Assert.areSame( 11, register );
906
var yconsole = new Y.Console({
912
//yconsole.hideCategory('info');
914
Y.Test.Runner.add(suite);