1 Handles, processes and tasks are released by the event loop once they are 2 finished, so that they are freed as soon as the script drops its last 3 reference. A handle is finished when the event loop removes its descriptor 4 after an error or hangup. 5 6 -- Testcase -- 7 {% 8 import * as uloop from 'uloop'; 9 import * as fs from 'fs'; 10 11 uloop.init(); 12 13 let events = []; 14 let base = gc('count'); 15 16 let fds = fs.pipe(); 17 18 uloop.handle(fds[0], (flags, eof, error) => { 19 push(events, sprintf("handle error=%s", error)); 20 }, uloop.ULOOP_READ); 21 22 fds[1].close(); 23 fds = null; 24 25 uloop.process('/bin/sh', ['-c', 'exit 3'], null, (status) => { 26 push(events, sprintf("process status=%d", status)); 27 }); 28 29 uloop.task( 30 (pipe) => 'done', 31 (msg) => push(events, sprintf("task output=%s", msg)) 32 ); 33 34 uloop.timer(500, () => uloop.end()); 35 uloop.run(); 36 37 printf("%s\nalive: %d\n", join('\n', sort(events)), gc('count') - base); 38 39 uloop.done(); 40 %} 41 -- End -- 42 43 -- Expect stdout -- 44 handle error=true 45 process status=3 46 task output=done 47 alive: 0 48 -- End -- 49 50 51 Handles registered with ULOOP_ERROR_CB stay registered until they are 52 deleted, even without a script reference. 53 54 -- Testcase -- 55 {% 56 import * as uloop from 'uloop'; 57 import * as fs from 'fs'; 58 59 uloop.init(); 60 61 let base = gc('count'); 62 let fds = fs.pipe(); 63 let hangups = 0; 64 65 uloop.handle(fds[0], function(flags, eof, error) { 66 if (error && ++hangups == 2) 67 this.delete(); 68 }, uloop.ULOOP_READ | uloop.ULOOP_ERROR_CB); 69 70 fds[1].close(); 71 fds = null; 72 73 uloop.timer(50, () => uloop.end()); 74 75 printf("registered: %s\n", gc('count') > base); 76 uloop.run(); 77 printf("hangups: %d, alive: %d\n", hangups, gc('count') - base); 78 79 uloop.done(); 80 %} 81 -- End -- 82 83 -- Expect stdout -- 84 registered: true 85 hangups: 2, alive: 0 86 -- End -- 87 88 89 Intervals and signal handlers stay registered until they are cancelled or 90 deleted, even without a script reference. 91 92 -- Testcase -- 93 {% 94 import * as uloop from 'uloop'; 95 96 if (!uloop.interval || !uloop.signal) 97 exit(77); 98 99 uloop.init(); 100 101 let base = gc('count'); 102 let ticks = 0; 103 104 uloop.interval(1, function() { 105 if (++ticks == 3) 106 this.cancel(); 107 }); 108 109 uloop.signal('SIGUSR1', function() { 110 printf("signal received\n"); 111 this.delete(); 112 uloop.end(); 113 }); 114 115 uloop.timer(50, () => system('kill -USR1 $PPID')); 116 117 printf("registered: %s\n", gc('count') > base); 118 uloop.run(); 119 printf("ticks: %d, alive: %d\n", ticks, gc('count') - base); 120 121 uloop.done(); 122 %} 123 -- End -- 124 125 -- Expect stdout -- 126 registered: true 127 signal received 128 ticks: 3, alive: 0 129 -- End --
This page was automatically generated by LXR 0.3.1. • OpenWrt