1 A timer that expired without being rearmed is released by the event loop, 2 so that it is freed together with its callback as soon as the script drops 3 its last reference, without an explicit cancel(). 4 5 -- Testcase -- 6 {% 7 import * as uloop from 'uloop'; 8 9 uloop.init(); 10 11 let fired = 0; 12 13 function on_fire() { 14 if (++fired == 20) 15 uloop.end(); 16 } 17 18 let base = gc('count'); 19 20 for (let i = 0; i < 20; i++) 21 uloop.timer(1, on_fire); 22 23 printf("armed: %d\n", gc('count') - base); 24 uloop.run(); 25 printf("fired: %d, alive: %d\n", fired, gc('count') - base); 26 27 base = gc('count'); 28 29 for (let i = 0; i < 10; i++) 30 uloop.timer(1, () => fired++); 31 32 uloop.timer(5, () => uloop.end()); 33 uloop.run(); 34 printf("fired: %d, alive with closures: %d\n", fired, gc('count') - base); 35 36 uloop.done(); 37 %} 38 -- End -- 39 40 -- Expect stdout -- 41 armed: 20 42 fired: 20, alive: 0 43 fired: 30, alive with closures: 0 44 -- End -- 45 46 47 A retained timer stays usable after it expired: set() rearms it, and 48 cancel() reports that nothing was pending. 49 50 -- Testcase -- 51 {% 52 import * as uloop from 'uloop'; 53 54 uloop.init(); 55 56 let count = 0; 57 let timer = uloop.timer(1, () => { 58 count++; 59 uloop.end(); 60 }); 61 62 uloop.run(); 63 printf("count: %d, remaining: %d\n", count, timer.remaining()); 64 65 printf("set: %s\n", timer.set(1)); 66 uloop.run(); 67 printf("count: %d, remaining: %d\n", count, timer.remaining()); 68 69 printf("cancel: %s\n", timer.cancel()); 70 printf("cancel again: %s\n", timer.cancel()); 71 72 uloop.done(); 73 %} 74 -- End -- 75 76 -- Expect stdout -- 77 count: 1, remaining: -1 78 set: true 79 count: 2, remaining: -1 80 cancel: false 81 cancel again: false 82 -- End -- 83 84 85 A timer rearmed from its own callback stays alive without a script 86 reference, as does an unarmed timer that is armed later. The instance is 87 freed once it expires without being rearmed. 88 89 -- Testcase -- 90 {% 91 import * as uloop from 'uloop'; 92 93 uloop.init(); 94 95 let rearmed = 0, deferred = 0; 96 let base = gc('count'); 97 98 uloop.timer(1, function() { 99 if (++rearmed < 3) 100 this.set(1); 101 }); 102 103 uloop.timer(null, function() { 104 deferred++; 105 }).set(2); 106 107 uloop.timer(20, () => uloop.end()); 108 uloop.run(); 109 110 printf("rearmed: %d, deferred: %d, alive: %d\n", rearmed, deferred, 111 gc('count') - base); 112 113 uloop.done(); 114 %} 115 -- End -- 116 117 -- Expect stdout -- 118 rearmed: 3, deferred: 1, alive: 0 119 -- End -- 120 121 122 A timer cancelled from its own callback, or by a script that still calls 123 cancel() after the timer fired, behaves as before. 124 125 -- Testcase -- 126 {% 127 import * as uloop from 'uloop'; 128 129 uloop.init(); 130 131 let results = []; 132 let base = gc('count'); 133 let timer; 134 135 timer = uloop.timer(1, () => { 136 push(results, timer.cancel()); 137 }); 138 139 uloop.timer(5, () => uloop.end()); 140 uloop.run(); 141 142 push(results, timer.cancel()); 143 timer = null; 144 145 printf("cancel: %J, alive: %d\n", results, gc('count') - base); 146 147 uloop.done(); 148 %} 149 -- End -- 150 151 -- Expect stdout -- 152 cancel: [ false, false ], alive: 0 153 -- End -- 154 155 156 Calling set() without a timeout or with null disarms the timer and keeps 157 its callback, so that it can be rearmed. A disarmed timer is freed as soon 158 as the script drops it. A negative timeout expires the timer on the next 159 event loop iteration, as in libubox. 160 161 -- Testcase -- 162 {% 163 import * as uloop from 'uloop'; 164 165 uloop.init(); 166 167 let fired = 0; 168 let base = gc('count'); 169 let timer = uloop.timer(1, () => fired++); 170 171 printf("set(null): %s, remaining: %d\n", timer.set(null), timer.remaining()); 172 uloop.timer(10, () => uloop.end()); 173 uloop.run(); 174 printf("fired: %d\n", fired); 175 176 timer.set(1); 177 printf("set(): %s, remaining: %d\n", timer.set(), timer.remaining()); 178 uloop.timer(10, () => uloop.end()); 179 uloop.run(); 180 printf("fired: %d\n", fired); 181 182 printf("set(-1): %s\n", timer.set(-1)); 183 uloop.timer(10, () => uloop.end()); 184 uloop.run(); 185 printf("fired: %d\n", fired); 186 187 timer.set(1000); 188 timer.set(); 189 timer = null; 190 printf("alive: %d\n", gc('count') - base); 191 192 uloop.done(); 193 %} 194 -- End -- 195 196 -- Expect stdout -- 197 set(null): true, remaining: -1 198 fired: 0 199 set(): true, remaining: -1 200 fired: 0 201 set(-1): true 202 fired: 1 203 alive: 0 204 -- End -- 205 206 207 Calling set() on an interval without a value or with null disarms it and 208 keeps its callback, so that it can be rearmed. A zero or negative interval 209 is rejected by set() and leaves a new interval unarmed. 210 211 -- Testcase -- 212 {% 213 import * as uloop from 'uloop'; 214 215 if (!uloop.interval) 216 exit(77); 217 218 uloop.init(); 219 220 function run(msecs) { 221 uloop.timer(msecs, () => uloop.end()); 222 uloop.run(); 223 } 224 225 let ticks = 0; 226 let interval = uloop.interval(5, () => ticks++); 227 228 run(30); 229 printf("armed: %s\n", ticks > 0); 230 231 printf("set(): %s, remaining: %d\n", interval.set(), interval.remaining()); 232 ticks = 0; 233 run(30); 234 printf("ticks: %d\n", ticks); 235 236 printf("set(-1): %s, %s\n", interval.set(-1), uloop.error()); 237 printf("set(0): %s, %s\n", interval.set(0), uloop.error()); 238 239 interval.set(5); 240 run(30); 241 printf("rearmed: %s\n", ticks > 0); 242 243 printf("set(null): %s, remaining: %d\n", interval.set(null), 244 interval.remaining()); 245 ticks = 0; 246 run(30); 247 printf("ticks: %d\n", ticks); 248 249 interval.cancel(); 250 251 interval = uloop.interval(0, () => ticks++); 252 printf("interval(0): remaining: %d\n", interval.remaining()); 253 run(30); 254 printf("ticks: %d\n", ticks); 255 256 interval.cancel(); 257 uloop.done(); 258 %} 259 -- End -- 260 261 -- Expect stdout -- 262 armed: true 263 set(): true, remaining: -1 264 ticks: 0 265 set(-1): (null), Invalid argument 266 set(0): (null), Invalid argument 267 rearmed: true 268 set(null): true, remaining: -1 269 ticks: 0 270 interval(0): remaining: -1 271 ticks: 0 272 -- End --
This page was automatically generated by LXR 0.3.1. • OpenWrt