source: Dev/trunk/src/client/dojox/lang/tests/async.js @ 527

Last change on this file since 527 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 4.4 KB
Line 
1dojo.provide("dojox.lang.tests.async");
2
3dojo.require("dojox.lang.async");
4dojo.require("dojox.lang.async.timeout");
5dojo.require("dojox.lang.async.topic");
6
7(function(){
8        var async = dojox.lang.async,
9                wait = async.timeout.from,
10                QUANT     = 50, // ms
11                MAX_TICKS = 5;
12
13        function randomTimes(n){
14                var a = [];
15                for(var i = 0; i < n; ++i){
16                        a.push((Math.floor(Math.random() * MAX_TICKS) + 1) * QUANT);
17                }
18                return a;
19        }
20
21        function getMin(a){
22                return Math.min.apply(Math, a);
23        }
24
25        function getMax(a){
26                return Math.max.apply(Math, a);
27        }
28
29        function waitAndExpect(expected, ms){
30                return function(value){
31                        console.log("waitAndExpect: ", value, ", expected: ", expected);
32                        if(expected !== value){
33                                console.log("ERROR: unexpected value");
34                                throw new Error("Unexpected value");
35                        }
36                        return wait(ms)();
37                }
38        }
39
40        function identity(x){
41                return x;
42        }
43
44        function is(a, b, visited){
45                if(a === b){
46                        return true;
47                }
48                if(typeof a != typeof b){
49                        return false;
50                }
51                if(Object.prototype.toString.call(a) != Object.prototype.toString.call(b)){
52                        return false;
53                }
54                if(Object.prototype.toString.call(a) == "[object Function]"){
55                        return false;
56                }
57                if(Object.prototype.toString.call(a) == "[object Array]"){
58                        if(a.length !== b.length){
59                                return false;
60                        }
61                        for(var i = 0; i < a.length; ++i){
62                                if(!is(a[i], b[i], visited)){
63                                        return false;
64                                }
65                                return true;
66                        }
67                }
68                if(typeof a == "object"){
69                        if(visited){
70                                for(var i = 0; i < visited.length; ++i){
71                                        if(visited[i] === a || visited[i] === b){
72                                                return true;
73                                        }
74                                }
75                                visited.push(a, b);
76                        }else{
77                                visited = [a, b];
78}
79                        var akeys = [];
80                        for(var i in a){
81                                akeys.push(i);
82                        }
83                        var bkeys = [];
84                        for(var i in b){
85                                bkeys.push(i);
86                        }
87                        akeys.sort();
88                        bkeys.sort();
89                        if(!is(akeys, bkeys)){
90                                return false;
91                        }
92                        for(var i = 0; i < akeys.length; ++i){
93                                if(!is(a[akeys[i]], b[bkeys[i]])){
94                                        return false;
95                                }
96                        }
97                        return true;
98                }
99                return false;
100        }
101
102        var waitFor0 = waitAndExpect(0, 20),
103                waitFor1 = waitAndExpect(1, 20),
104                waitFor2 = waitAndExpect(2, 20);
105
106        tests.register("dojox.lang.tests.async", [
107                function smokeTest(){
108                        var a = randomTimes(1),
109                                r = new dojo.Deferred();
110                        wait(a[0])().addCallback(function(x){
111                                if(r == a[0]){
112                                        console.log("ERROR: smokeTest: wrong result");
113                                        throw new Error("smokeTest: wrong result");
114                                }
115                                r.callback();
116                        });
117                        return r;
118                },
119                function testSeq(){
120                        var a = randomTimes(5),
121                                fs = dojo.map(a, function(ms, i){
122                                        return waitAndExpect(i && a[i - 1], ms);
123                                });
124                        return async.seq(fs)(0).addCallback(function(value){
125                                if(a[a.length - 1] !== value){
126                                        console.log("ERROR: testSeq: wrong time");
127                                        throw new Error("testSeq: wrong time");
128                                }
129                        });
130                },
131                function testPar(){
132                        var a = randomTimes(5),
133                                fs = dojo.map(a, function(ms){
134                                        return waitAndExpect(0, ms);
135                                });
136                        return async.par(fs)(0).addCallback(function(value){
137                                console.log(a, " - ", value);
138                                if(!is(a, value)){
139                                        console.log("ERROR: testPar: wrong time");
140                                        throw new Error("testPar: wrong time");
141                                }
142                        });
143                },
144                function testAny(){
145                        var a = randomTimes(5),
146                                min = getMin(a),
147                                fs = dojo.map(a, function(ms){
148                                        return waitAndExpect(0, ms);
149                                });
150                        return async.any(fs)(0).addCallback(function(value){
151                                console.log(min, " - ", value);
152                                if(min !== value){
153                                        console.log("ERROR: testAny: wrong time");
154                                        throw new Error("testAny: wrong time");
155                                }
156                        });
157                },
158                function testSelect0(){
159                        return async.select(
160                                identity,
161                                waitFor0,
162                                waitFor1,
163                                waitFor2
164                        )(0);
165                },
166                function testSelect1(){
167                        return async.select(
168                                identity,
169                                waitFor0,
170                                waitFor1,
171                                waitFor2
172                        )(1);
173                },
174                function testSelect2(){
175                        return async.select(
176                                identity,
177                                waitFor0,
178                                waitFor1,
179                                waitFor2
180                        )(2);
181                },
182                function testIfThenT(){
183                        return async.ifThen(
184                                identity,
185                                waitFor1,
186                                waitFor0
187                        )(1);
188                },
189                function testIfThenF(){
190                        return async.ifThen(
191                                identity,
192                                waitFor1,
193                                waitFor0
194                        )(0);
195                },
196                function testLoop(){
197                        var counter = 0;
198                        return async.seq(
199                                async.loop(
200                                        identity,
201                                        function(ms){
202                                                ++counter;
203                                                return wait(ms - 10)();
204                                        }
205                                ),
206                                function(){
207                                        console.log(counter, " - ", 3);
208                                        if(counter !== 3){
209                                                console.log("ERROR: testLoop: wrong number of iterations");
210                                                throw new Error("testLoop: wrong number of iterations");
211                                        }
212                                }
213                        )(30);
214                }
215        ]);
216})();
Note: See TracBrowser for help on using the repository browser.