testsuite.js revision b40f5113b5f28daabc29b6ad49c905f290cf6cb7
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek GathrightYUI.add('async-queue-test', function(Y) {
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathrightvar suite = new Y.Test.Suite("Y.AsyncQueue");
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright// FIXME: remove this and update the tests to handle the asynchronicity
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek GathrightY.AsyncQueue.defaults.timeout = -1;
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathrightfunction f() {}
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathrightif (!window.console) {
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright console = { log: f };
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright}
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathrightsuite.add(new Y.Test.Case({
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright name : "Queue isntantiation",
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright test_instantiation : function () {
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright var basic = new Y.AsyncQueue(),
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright withCallbacks = new Y.AsyncQueue(f,f,f,f);
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright Y.Assert.areSame(true, basic instanceof Y.AsyncQueue);
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright Y.Assert.areSame(0, basic.size());
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright Y.Assert.areSame(0, basic._q.length);
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright Y.Assert.areSame(4, withCallbacks.size());
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright Y.Assert.isFunction(withCallbacks._q[0]);
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright Y.Assert.isFunction(withCallbacks.next());
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright Y.Assert.areSame(f, withCallbacks.next().fn);
78ac1ef5c64e9e95a94bbfe859662da6d21b243aEric Ferraiuolo }
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright}));
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathrightsuite.add(new Y.Test.Case({
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright name : "queue-base",
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright test_next : function () {
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright var i = 0;
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright YUI({
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright useBrowserConsole : false,
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright logInclude : { TestRunner: true }
78ac1ef5c64e9e95a94bbfe859662da6d21b243aEric Ferraiuolo }).use('queue-base', function (Y) {
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright function inc() { i++; }
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright var callback,
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright q = new Y.Queue(inc, inc, "string", inc);
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright while ((callback = q.next())) {
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright if (Y.Lang.isFunction(callback)) {
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright callback();
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright }
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright }
78ac1ef5c64e9e95a94bbfe859662da6d21b243aEric Ferraiuolo });
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright Y.Assert.areSame(3, i);
acbf78fbb0e5ea3c5353314fcbaa6b7e3b82ad7cDerek Gathright }
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright}));
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathrightsuite.add(new Y.Test.Case({
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright name : "Test API",
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright test_chaining : function () {
78ac1ef5c64e9e95a94bbfe859662da6d21b243aEric Ferraiuolo var q = new Y.AsyncQueue();
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright q.defaults = {
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright timeout : 10
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright };
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright Y.Assert.areSame(q, q.add());
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright Y.Assert.areSame(q, q.add(f));
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright Y.Assert.areSame(q, q.add(f,f,{fn:f,id:'a'},"garbage"));
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright Y.Assert.areSame(q, q.pause());
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright Y.Assert.areSame(q, q.promote('a'));
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright Y.Assert.areSame(q, q.remove('a'));
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright Y.Assert.areSame(q, q.run());
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright Y.Assert.areSame(q, q.stop());
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright },
d35783d338067103badd5ebbb57676c129f5e563Eric Ferraiuolo
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright test_add : function () {
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright var q = new Y.AsyncQueue(f);
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright Y.Assert.areSame(1, q.size());
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright q = new Y.AsyncQueue().add(f);
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright Y.Assert.areSame(1, q.size());
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright q.add(f,f).add(f,f,f);
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright Y.Assert.areSame(6, q.size());
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright q.add("Only functions and objects are allowed",
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright undefined,
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright null,
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright 1,
78ac1ef5c64e9e95a94bbfe859662da6d21b243aEric Ferraiuolo true);
78ac1ef5c64e9e95a94bbfe859662da6d21b243aEric Ferraiuolo
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright Y.Assert.areSame(6, q.size());
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright q.add({},{}); // empty objects are ok, since config can be defaulted
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright Y.Assert.areSame(8, q.size());
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright // Add from within a callback
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright var count = 0;
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright function x() {
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright count++;
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright }
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright function addToQueue() {
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright this.add(x);
78ac1ef5c64e9e95a94bbfe859662da6d21b243aEric Ferraiuolo }
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright // Three x calls scheduled. A fourth added during a callback
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright q = new Y.AsyncQueue(x,f,x,addToQueue,f,x).run();
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright Y.Assert.areSame(4,count);
78ac1ef5c64e9e95a94bbfe859662da6d21b243aEric Ferraiuolo },
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright test_remove : function () {
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright var results = '',
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright self = this,
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright q = new Y.AsyncQueue(
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright function () {
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright Y.Assert.areSame(7, this.size());
d0f9ee99aee2d631a24062cd95d30c1d6955fc0fDerek Gathright results += 'R';
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright },
78ac1ef5c64e9e95a94bbfe859662da6d21b243aEric Ferraiuolo { id: "remove me", fn: X },
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright {
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright id: "not removed",
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright fn: function () {
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright results += 'E';
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright this.remove('me too');
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright },
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright timeout: 10
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright },
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright { id: "me too", fn: X },
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright function () {
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright this.remove("fail");
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright if (q.size() !== 4) {
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright self.resume(function () {
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright Y.Assert.fail("Expected 3, got " + q.size() + " - remove(n) should defer until callback completion");
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright });
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright }
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright results += 'M';
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright },
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright { id: "fail",
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright fn: function () {
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright self.resume(function () {
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright Y.Assert.fail("This callback should have been removed");
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright });
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright }
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright },
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright function () {
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright if (q.size() !== 2) {
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright self.resume(function () {
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright Y.Assert.fail("Size should be 1");
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright });
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright }
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright results += 'OV';
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright },
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright function () {
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright self.resume(function () {
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright results += 'E';
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright Y.Assert.areSame('REMOVE', results);
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright });
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright });
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright function X() {
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright q.run();
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright results += 'X';
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright }
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright Y.Assert.areSame(8, q.size());
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright // Removal when the Queue is inactive is immediate
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright q.remove("remove me");
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright Y.Assert.areSame(7, q.size());
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright q.run();
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright Y.Assert.areSame('R',results);
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright Y.Assert.areSame(6, q.size());
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright q.remove("not removed");
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright Y.Assert.areSame(6, q.size());
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright this.wait();
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright },
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright test_promote : function () {
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright function O() {
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright results += 'O';
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright }
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright var results = '',
78ac1ef5c64e9e95a94bbfe859662da6d21b243aEric Ferraiuolo self = this,
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright q = new Y.AsyncQueue(
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright function () {
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright results += "R";
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright },
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright {
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright id: "p",
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright fn: function () { results += 'P'; }
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright },
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright O,
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright {
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright id: 'm',
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright fn: function () {
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright if (this.count++ > 3) {
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright results += 'M';
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright } else if (!this.count) {
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright q.promote('o');
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright }
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright },
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright context : { count : 0 },
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright iterations : 5
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright },
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright {
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright id : 'o',
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright fn: O,
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright timeout: 10
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright },
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright function () { results += 'E'; },
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright {
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright id : 't',
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright fn : function () {
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright results += 'T';
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright }
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright },
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright function () {
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright self.resume(function () {
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright Y.Assert.areSame('PROMOTE', results);
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright });
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright });
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright Y.Assert.isUndefined(q._q[0].id);
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright
6ba8d3cf563f904d8a71f8408553c2a10431ea7dDerek Gathright q.promote('p');
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright Y.Assert.areSame('p', q._q[0].id);
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright q.run();
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright Y.Assert.areSame('PROM', results);
78ac1ef5c64e9e95a94bbfe859662da6d21b243aEric Ferraiuolo
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright q.promote('t');
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright this.wait();
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright },
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright test_pause : function () {
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright var results = '',
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright self = this,
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright q = new Y.AsyncQueue(
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright function () { results += 'P'; },
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright {
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright fn: function () {
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright results += 'A';
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright },
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright timeout : 10
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright },
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright function () {
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright results += 'U';
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright },
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright function () {
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright results += 'S';
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright this.pause();
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright self.resume(function () {
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright Y.Assert.areSame('PAUS',results);
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright setTimeout(function () {
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright q.run();
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright },10);
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright self.wait();
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright });
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright },
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright function () {
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright results += 'E';
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright self.resume(function () {
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright Y.Assert.areSame('PAUSE',results);
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright });
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright });
293b35d70157aeec052a53d4a3ecf875cb7c358bDerek Gathright
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright Y.Assert.areSame(5,q.size());
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright q.run();
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright // Test during timeout
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright Y.Assert.areSame('P', results);
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright q.pause();
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright setTimeout(function () {
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright self.resume(function () {
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright q.run();
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright self.wait();
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright });
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright }, 20);
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright this.wait();
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright },
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright test_stop : function () {
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright var results = "",
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright self = this,
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright q = new Y.AsyncQueue(
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright function () { results += 'S'; },
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright function () { results += 'T'; },
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright function () { results += 'O'; },
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright function () { results += 'P'; },
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright {
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright fn: function () {
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright self.resume(function () {
a22e94e50e241a5ae089c072604fec309a0db44eDerek Gathright Y.Assert.fail("Synchronous q.stop() should have cleared this async callback");
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright });
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright },
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright timeout: 10
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright });
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright q.run();
06c1e33a270f80929bf9b9dde81f4061d1949fffDerek Gathright q.stop();
d35783d338067103badd5ebbb57676c129f5e563Eric Ferraiuolo Y.Assert.areSame('STOP',results);
Y.Assert.areSame(0,q.size());
setTimeout(function () {
self.resume(function () {
Y.Assert.areSame('STOP',results);
});
},100);
q.run();
this.wait();
},
test_getCallback : function () {
var c,
q = new Y.AsyncQueue(
{ id : 'a', test: 1 },
{ id : 'b', test: 2, fn: function () {
this.pause();
}
},
{ id : 'c', test: 3 },
{ id : 'd', test: 4,
fn: function () {
Y.Assert.areSame(this._q[0], this.getCallback('d'));
}
},
{ id : 'a', test: 5 });
q.defaults = { fn: function () {} };
c = q.getCallback('a');
Y.Assert.isObject(c);
Y.Assert.areSame(1, c.test);
q.run();
c = q.getCallback('a');
Y.Assert.isObject(c);
Y.Assert.areSame(5, c.test);
q.run();
},
test_isRunning : function () {
var self = this,
q = new Y.AsyncQueue(
function () {
Y.Assert.areSame(true, this.isRunning());
},
{
fn: function () {
q.pause();
self.resume(function () {
Y.Assert.areSame(false, q.isRunning());
});
},
timeout: 10
});
Y.Assert.areSame(false, q.isRunning());
q.run();
Y.Assert.areSame(true, q.isRunning());
/*
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();
}
}));
suite.add(new Y.Test.Case({
name : "Test callback config",
test_fn : function () {
var results = '',
q = new Y.AsyncQueue(
function () { results += 'R'; },
{},
function () { results += 'N'; });
q.defaults = { fn: function () { results += 'U'; } };
q.run();
Y.Assert.areSame("RUN", results);
q.add({ fn : "results += 'X'" },
{ fn : /results += 'X'/ },
{ fn : function () { Y.Assert.areSame("RUN", results); } }).run();
},
test_context : function () {
var a = { id : 'a',
test : 'A',
fn : function () {
Y.Assert.areSame('A', this.test);
}
},
q = new Y.AsyncQueue({ test : 'callbacks exec from Queue ctx by default' },
function () { Y.Assert.areSame('X', this.test); },
{
fn: function () {
Y.Assert.areSame('X', this.test);
this.test = 'Z';
}
},
function () { Y.Assert.areSame('Z', this.test); },
a,
{
fn: function () {
Y.Assert.areSame('B', this.test);
},
context : { test : 'B' }
});
q.getCallback('a').context = a;
q.test = 'X';
q.run();
},
test_args : function () {
(new Y.AsyncQueue(
function () {
Y.Assert.areSame(0,arguments.length);
},
{
fn: function () {
Y.ArrayAssert.itemsAreSame([1,2,3],arguments);
},
args : [1,2,3]
},
{
fn: function () {
Y.ArrayAssert.itemsAreSame(['X'],arguments);
},
args : 'X'
})).run();
},
test_iterations : function () {
var results = '',
self = this;
(new Y.AsyncQueue(
function () { results += 'A'; },
{ fn: function () { results += 'B'; } },
{ fn: function () { results += 'C'; }, iterations: 3 },
{ fn: function () { results += 'D'; }, iterations: 3, timeout: 10 },
{ fn: function () {
self.resume(function () {
Y.Assert.areSame('ABCCCDDD', results);
});
}
})).run();
this.wait();
},
test_until : function () {
var results = '',
self = this;
(new Y.AsyncQueue(
function () { results += 'A'; },
{
fn: function () {
results += 'B';
},
until: function () {
this.data = this.data.slice(1);
return !this.data;
},
data : '1234'
},
{
fn: function () {
results += 'C';
},
until: function () {
return results.length >= 7;
},
timeout: 10
},
{ fn: function () {
self.resume(function () {
Y.Assert.areSame('ABBBCCC', results);
});
}
})).run();
Y.Assert.areSame('ABBB', results);
this.wait();
},
test_timeout : function () {
function inc() { ++results; }
var results = 0,
self = this,
// default timeout -1 triggers synchronous mode
q = new Y.AsyncQueue(
inc, // -1 == sync
{ fn: inc }, // -1 == sync
{ fn: inc, timeout: 10, iterations: 4 },
{ fn: inc, timeout: -300, iterations: 4 }, // neg == sync
// garbage timeout doesn't throw error, but is async
{ fn: inc, timeout: 'a',
until: function () {
return results >= 10;
}
},
function () {
self.resume(function () {
Y.Assert.areSame(10,results);
});
}).run();
Y.Assert.areSame(2, results);
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();
}
*/
}));
suite.add(new Y.Test.Case({
name : "Test Events",
test_events : function () {
var results = [],
self = this,
q = new Y.AsyncQueue(
function () { results.push("E"); this.pause(); },
{
fn: function () { results.push("E"); },
until: function () { return results.length > 25; },
timeout: 10
},
{
id: 'x',
fn: function () { results.push("X"); }
},
{
id: 'v',
fn: function () { results.push("V"); },
iterations: 3
},
{
fn: function () {
results.push("N");
Y.io(Y.guid() + '.html', { // 404
on : {
failure : function () {
results.push("T");
}
}
});
},
waitForIOResponse : true
});
q.on('execute',function () { results.push("(onExec)"); });
q.after('execute', function () { results.push("(afterExec)"); });
q.on("shift", function () { results.push("(onShift)"); });
q.after("shift", function () { results.push("(afterShift)"); });
q.on("remove", function () { results.push("(onRemove)"); });
q.after("remove", function () { results.push("(afterRemove)"); });
q.on("add", function (e) { results.push("(onAdd)"); });
q.after("add", function (e) {
var data = e.added;
results.push("(afterAdd)");
if (!data || data.length !== 4) {
self.resume(function () {
Y.Assert.fail("add args not right");
});
}
});
q.on("promote", function () { results.push("(onPromote)"); });
q.after("promote", function () {
results.push("(afterPromote)");
setTimeout(function () {
q.run();
}, 0);
});
q.on("complete", function () {
results.push("(onComplete)");
self.resume(function () {
Y.ArrayAssert.itemsAreEqual([
"(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.add(function () { results.push("S"); this.stop(); },f,f,f);
q.remove('x');
q.run();
q.promote('v');
this.wait();
},
test_preventCallback : function () {
function inc () { i++; }
var i = 0,
q = new Y.AsyncQueue(inc,inc,
{
foo: true,
fn: inc,
iterations: 20
},
{
fn: inc,
until : function () {
return i >= 10;
}
});
q.on('execute', function (e) {
if (e.callback.foo) {
e.preventDefault();
}
});
q.run();
Y.Assert.areSame(10,i);
q = new Y.AsyncQueue(inc, inc, inc, inc, inc, inc, inc, inc, inc, inc);
q.on('shift', function (e) {
if (i % 2) {
e.preventDefault();
q._q[0].iterations++;
}
});
q.run();
Y.Assert.areSame(30, i);
}
}));
/*
// 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.Assert.areSame( 11, register );
} );
} );
q.run();
this.wait();
}
}));
*/
Y.Test.Runner.add(suite);
}, '@VERSION@' ,{requires:['test', 'async-queue', 'io-base']});