OLD | NEW |
| (Empty) |
1 // Copyright 2012 the V8 project authors. All rights reserved. | |
2 // Redistribution and use in source and binary forms, with or without | |
3 // modification, are permitted provided that the following conditions are | |
4 // met: | |
5 // | |
6 // * Redistributions of source code must retain the above copyright | |
7 // notice, this list of conditions and the following disclaimer. | |
8 // * Redistributions in binary form must reproduce the above | |
9 // copyright notice, this list of conditions and the following | |
10 // disclaimer in the documentation and/or other materials provided | |
11 // with the distribution. | |
12 // * Neither the name of Google Inc. nor the names of its | |
13 // contributors may be used to endorse or promote products derived | |
14 // from this software without specific prior written permission. | |
15 // | |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 | |
28 // Flags: --allow-natives-syntax | |
29 | |
30 var ordering = []; | |
31 function reset() { | |
32 ordering = []; | |
33 } | |
34 | |
35 function assertArrayValues(expected, actual) { | |
36 assertEquals(expected.length, actual.length); | |
37 for (var i = 0; i < expected.length; i++) { | |
38 assertEquals(expected[i], actual[i]); | |
39 } | |
40 } | |
41 | |
42 function assertOrdering(expected) { | |
43 %RunMicrotasks(); | |
44 assertArrayValues(expected, ordering); | |
45 } | |
46 | |
47 function newPromise(id, fn) { | |
48 var r; | |
49 var t = 1; | |
50 var promise = new Promise(function(resolve) { | |
51 r = resolve; | |
52 if (fn) fn(); | |
53 }); | |
54 | |
55 var next = promise.then(function(value) { | |
56 ordering.push('p' + id); | |
57 return value; | |
58 }); | |
59 | |
60 return { | |
61 resolve: r, | |
62 then: function(fn) { | |
63 next = next.then(function(value) { | |
64 ordering.push('p' + id + ':' + t++); | |
65 return fn ? fn(value) : value; | |
66 }); | |
67 | |
68 return this; | |
69 } | |
70 }; | |
71 } | |
72 | |
73 function newObserver(id, fn, obj) { | |
74 var observer = { | |
75 value: 1, | |
76 recordCounts: [] | |
77 }; | |
78 | |
79 Object.observe(observer, function(records) { | |
80 ordering.push('o' + id); | |
81 observer.recordCounts.push(records.length); | |
82 if (fn) fn(); | |
83 }); | |
84 | |
85 return observer; | |
86 } | |
87 | |
88 | |
89 (function PromiseThens() { | |
90 reset(); | |
91 | |
92 var p1 = newPromise(1).then(); | |
93 var p2 = newPromise(2).then(); | |
94 | |
95 p1.resolve(); | |
96 p2.resolve(); | |
97 | |
98 assertOrdering(['p1', 'p2', 'p1:1', 'p2:1']); | |
99 })(); | |
100 | |
101 | |
102 (function ObserversBatch() { | |
103 reset(); | |
104 | |
105 var p1 = newPromise(1); | |
106 var p2 = newPromise(2); | |
107 var p3 = newPromise(3); | |
108 | |
109 var ob1 = newObserver(1); | |
110 var ob2 = newObserver(2, function() { | |
111 ob3.value++; | |
112 p3.resolve(); | |
113 ob1.value++; | |
114 }); | |
115 var ob3 = newObserver(3); | |
116 | |
117 p1.resolve(); | |
118 ob1.value++; | |
119 p2.resolve(); | |
120 ob2.value++; | |
121 | |
122 assertOrdering(['p1', 'o1', 'o2', 'p2', 'o1', 'o3', 'p3']); | |
123 assertArrayValues([1, 1], ob1.recordCounts); | |
124 assertArrayValues([1], ob2.recordCounts); | |
125 assertArrayValues([1], ob3.recordCounts); | |
126 })(); | |
127 | |
128 | |
129 (function ObserversGetAllRecords() { | |
130 reset(); | |
131 | |
132 var p1 = newPromise(1); | |
133 var p2 = newPromise(2); | |
134 var ob1 = newObserver(1, function() { | |
135 ob2.value++; | |
136 }); | |
137 var ob2 = newObserver(2); | |
138 | |
139 p1.resolve(); | |
140 ob1.value++; | |
141 p2.resolve(); | |
142 ob2.value++; | |
143 | |
144 assertOrdering(['p1', 'o1', 'o2', 'p2']); | |
145 assertArrayValues([1], ob1.recordCounts); | |
146 assertArrayValues([2], ob2.recordCounts); | |
147 })(); | |
148 | |
149 | |
150 (function NewObserverDeliveryGetsNewMicrotask() { | |
151 reset(); | |
152 | |
153 var p1 = newPromise(1); | |
154 var p2 = newPromise(2); | |
155 var ob1 = newObserver(1); | |
156 var ob2 = newObserver(2, function() { | |
157 ob1.value++; | |
158 }); | |
159 | |
160 p1.resolve(); | |
161 ob1.value++; | |
162 p2.resolve(); | |
163 ob2.value++; | |
164 | |
165 assertOrdering(['p1', 'o1', 'o2', 'p2', 'o1']); | |
166 assertArrayValues([1, 1], ob1.recordCounts); | |
167 assertArrayValues([1], ob2.recordCounts); | |
168 })(); | |
OLD | NEW |