asyncengine 0.0.1.testing1 → 0.0.2.alpha1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (251) hide show
  1. data/README.markdown +3 -0
  2. data/Rakefile +38 -0
  3. data/asyncengine.gemspec +8 -4
  4. data/ext/asyncengine/ae_call_from_other_thread.c +106 -0
  5. data/ext/asyncengine/ae_call_from_other_thread.h +12 -0
  6. data/ext/asyncengine/ae_handle_common.c +193 -48
  7. data/ext/asyncengine/ae_handle_common.h +40 -13
  8. data/ext/asyncengine/ae_ip_utils.c +246 -0
  9. data/ext/asyncengine/ae_ip_utils.h +25 -0
  10. data/ext/asyncengine/ae_next_tick.c +81 -21
  11. data/ext/asyncengine/ae_next_tick.h +4 -2
  12. data/ext/asyncengine/ae_resolver.c +156 -0
  13. data/ext/asyncengine/ae_resolver.h +10 -0
  14. data/ext/asyncengine/ae_tcp.c +908 -0
  15. data/ext/asyncengine/ae_tcp.h +20 -0
  16. data/ext/asyncengine/ae_timer.c +355 -81
  17. data/ext/asyncengine/ae_timer.h +11 -4
  18. data/ext/asyncengine/ae_udp.c +579 -13
  19. data/ext/asyncengine/ae_udp.h +15 -2
  20. data/ext/asyncengine/ae_utils.c +192 -0
  21. data/ext/asyncengine/ae_utils.h +16 -0
  22. data/ext/asyncengine/asyncengine_ruby.c +469 -26
  23. data/ext/asyncengine/asyncengine_ruby.h +49 -11
  24. data/ext/asyncengine/debug.h +68 -0
  25. data/ext/asyncengine/extconf.rb +26 -2
  26. data/ext/asyncengine/ip_parser.c +5954 -0
  27. data/ext/asyncengine/ip_parser.h +16 -0
  28. data/ext/asyncengine/libuv/AUTHORS +16 -0
  29. data/ext/asyncengine/libuv/common.gypi +4 -4
  30. data/ext/asyncengine/libuv/config-mingw.mk +6 -6
  31. data/ext/asyncengine/libuv/config-unix.mk +13 -13
  32. data/ext/asyncengine/libuv/gyp_uv +5 -1
  33. data/ext/asyncengine/libuv/ibc_tests/exec_test.sh +8 -0
  34. data/ext/asyncengine/libuv/ibc_tests/uv_shutdown_write_issue.c +171 -0
  35. data/ext/asyncengine/libuv/ibc_tests/uv_tcp_close_while_connecting.c +102 -0
  36. data/ext/asyncengine/libuv/include/uv-private/ngx-queue.h +3 -1
  37. data/ext/asyncengine/libuv/include/uv-private/uv-unix.h +103 -50
  38. data/ext/asyncengine/libuv/include/uv-private/uv-win.h +76 -24
  39. data/ext/asyncengine/libuv/include/uv.h +353 -88
  40. data/ext/asyncengine/libuv/src/ares/ares__close_sockets.o +0 -0
  41. data/ext/asyncengine/libuv/src/ares/ares__get_hostent.o +0 -0
  42. data/ext/asyncengine/libuv/src/ares/ares__read_line.o +0 -0
  43. data/ext/asyncengine/libuv/src/ares/ares__timeval.o +0 -0
  44. data/ext/asyncengine/libuv/src/ares/ares_cancel.o +0 -0
  45. data/ext/asyncengine/libuv/src/ares/ares_data.o +0 -0
  46. data/ext/asyncengine/libuv/src/ares/ares_destroy.o +0 -0
  47. data/ext/asyncengine/libuv/src/ares/ares_expand_name.o +0 -0
  48. data/ext/asyncengine/libuv/src/ares/ares_expand_string.o +0 -0
  49. data/ext/asyncengine/libuv/src/ares/ares_fds.o +0 -0
  50. data/ext/asyncengine/libuv/src/ares/ares_free_hostent.o +0 -0
  51. data/ext/asyncengine/libuv/src/ares/ares_free_string.o +0 -0
  52. data/ext/asyncengine/libuv/src/ares/ares_gethostbyaddr.o +0 -0
  53. data/ext/asyncengine/libuv/src/ares/ares_gethostbyname.o +0 -0
  54. data/ext/asyncengine/libuv/src/ares/ares_getnameinfo.o +0 -0
  55. data/ext/asyncengine/libuv/src/ares/ares_getopt.o +0 -0
  56. data/ext/asyncengine/libuv/src/ares/ares_getsock.o +0 -0
  57. data/ext/asyncengine/libuv/src/ares/ares_init.o +0 -0
  58. data/ext/asyncengine/libuv/src/ares/ares_library_init.o +0 -0
  59. data/ext/asyncengine/libuv/src/ares/ares_llist.o +0 -0
  60. data/ext/asyncengine/libuv/src/ares/ares_mkquery.o +0 -0
  61. data/ext/asyncengine/libuv/src/ares/ares_nowarn.o +0 -0
  62. data/ext/asyncengine/libuv/src/ares/ares_options.o +0 -0
  63. data/ext/asyncengine/libuv/src/ares/ares_parse_a_reply.o +0 -0
  64. data/ext/asyncengine/libuv/src/ares/ares_parse_aaaa_reply.o +0 -0
  65. data/ext/asyncengine/libuv/src/ares/ares_parse_mx_reply.o +0 -0
  66. data/ext/asyncengine/libuv/src/ares/ares_parse_ns_reply.o +0 -0
  67. data/ext/asyncengine/libuv/src/ares/ares_parse_ptr_reply.o +0 -0
  68. data/ext/asyncengine/libuv/src/ares/ares_parse_srv_reply.o +0 -0
  69. data/ext/asyncengine/libuv/src/ares/ares_parse_txt_reply.o +0 -0
  70. data/ext/asyncengine/libuv/src/ares/ares_process.o +0 -0
  71. data/ext/asyncengine/libuv/src/ares/ares_query.o +0 -0
  72. data/ext/asyncengine/libuv/src/ares/ares_search.o +0 -0
  73. data/ext/asyncengine/libuv/src/ares/ares_send.o +0 -0
  74. data/ext/asyncengine/libuv/src/ares/ares_strcasecmp.o +0 -0
  75. data/ext/asyncengine/libuv/src/ares/ares_strdup.o +0 -0
  76. data/ext/asyncengine/libuv/src/ares/ares_strerror.o +0 -0
  77. data/ext/asyncengine/libuv/src/ares/ares_timeout.o +0 -0
  78. data/ext/asyncengine/libuv/src/ares/ares_version.o +0 -0
  79. data/ext/asyncengine/libuv/src/ares/ares_writev.o +0 -0
  80. data/ext/asyncengine/libuv/src/ares/bitncmp.o +0 -0
  81. data/ext/asyncengine/libuv/src/ares/inet_net_pton.o +0 -0
  82. data/ext/asyncengine/libuv/src/ares/inet_ntop.o +0 -0
  83. data/ext/asyncengine/libuv/src/cares.c +225 -0
  84. data/ext/asyncengine/libuv/src/cares.o +0 -0
  85. data/ext/asyncengine/libuv/src/fs-poll.c +237 -0
  86. data/ext/asyncengine/libuv/src/fs-poll.o +0 -0
  87. data/ext/asyncengine/libuv/src/unix/async.c +78 -17
  88. data/ext/asyncengine/libuv/src/unix/async.o +0 -0
  89. data/ext/asyncengine/libuv/src/unix/core.c +305 -213
  90. data/ext/asyncengine/libuv/src/unix/core.o +0 -0
  91. data/ext/asyncengine/libuv/src/unix/cygwin.c +1 -1
  92. data/ext/asyncengine/libuv/src/unix/darwin.c +2 -1
  93. data/ext/asyncengine/libuv/src/unix/dl.c +36 -44
  94. data/ext/asyncengine/libuv/src/unix/dl.o +0 -0
  95. data/ext/asyncengine/libuv/src/unix/eio/eio.o +0 -0
  96. data/ext/asyncengine/libuv/src/unix/error.c +6 -0
  97. data/ext/asyncengine/libuv/src/unix/error.o +0 -0
  98. data/ext/asyncengine/libuv/src/unix/ev/ev.c +8 -4
  99. data/ext/asyncengine/libuv/src/unix/ev/ev.o +0 -0
  100. data/ext/asyncengine/libuv/src/unix/freebsd.c +1 -1
  101. data/ext/asyncengine/libuv/src/unix/fs.c +25 -33
  102. data/ext/asyncengine/libuv/src/unix/fs.o +0 -0
  103. data/ext/asyncengine/libuv/src/unix/internal.h +50 -31
  104. data/ext/asyncengine/libuv/src/unix/kqueue.c +2 -7
  105. data/ext/asyncengine/libuv/src/unix/linux/core.o +0 -0
  106. data/ext/asyncengine/libuv/src/unix/linux/inotify.c +12 -14
  107. data/ext/asyncengine/libuv/src/unix/linux/inotify.o +0 -0
  108. data/ext/asyncengine/libuv/src/unix/linux/{core.c → linux-core.c} +1 -1
  109. data/ext/asyncengine/libuv/src/unix/linux/linux-core.o +0 -0
  110. data/ext/asyncengine/libuv/src/unix/linux/syscalls.c +147 -1
  111. data/ext/asyncengine/libuv/src/unix/linux/syscalls.h +39 -2
  112. data/ext/asyncengine/libuv/src/unix/linux/syscalls.o +0 -0
  113. data/ext/asyncengine/libuv/src/unix/loop-watcher.c +63 -0
  114. data/ext/asyncengine/libuv/src/unix/loop-watcher.o +0 -0
  115. data/ext/asyncengine/libuv/src/unix/loop.c +29 -6
  116. data/ext/asyncengine/libuv/src/unix/loop.o +0 -0
  117. data/ext/asyncengine/libuv/src/unix/netbsd.c +1 -1
  118. data/ext/asyncengine/libuv/src/unix/openbsd.c +1 -1
  119. data/ext/asyncengine/libuv/src/unix/pipe.c +31 -36
  120. data/ext/asyncengine/libuv/src/unix/pipe.o +0 -0
  121. data/ext/asyncengine/libuv/src/unix/poll.c +116 -0
  122. data/ext/asyncengine/libuv/src/unix/poll.o +0 -0
  123. data/ext/asyncengine/libuv/src/unix/process.c +193 -115
  124. data/ext/asyncengine/libuv/src/unix/process.o +0 -0
  125. data/ext/asyncengine/libuv/src/unix/stream.c +146 -153
  126. data/ext/asyncengine/libuv/src/unix/stream.o +0 -0
  127. data/ext/asyncengine/libuv/src/unix/sunos.c +45 -36
  128. data/ext/asyncengine/libuv/src/unix/tcp.c +6 -5
  129. data/ext/asyncengine/libuv/src/unix/tcp.o +0 -0
  130. data/ext/asyncengine/libuv/src/unix/thread.c +82 -25
  131. data/ext/asyncengine/libuv/src/unix/thread.o +0 -0
  132. data/ext/asyncengine/libuv/src/unix/timer.c +69 -58
  133. data/ext/asyncengine/libuv/src/unix/timer.o +0 -0
  134. data/ext/asyncengine/libuv/src/unix/tty.c +3 -3
  135. data/ext/asyncengine/libuv/src/unix/tty.o +0 -0
  136. data/ext/asyncengine/libuv/src/unix/udp.c +57 -66
  137. data/ext/asyncengine/libuv/src/unix/udp.o +0 -0
  138. data/ext/asyncengine/libuv/src/unix/uv-eio.c +33 -50
  139. data/ext/asyncengine/libuv/src/unix/uv-eio.o +0 -0
  140. data/ext/asyncengine/libuv/src/uv-common.c +68 -38
  141. data/ext/asyncengine/libuv/src/uv-common.h +104 -20
  142. data/ext/asyncengine/libuv/src/uv-common.o +0 -0
  143. data/ext/asyncengine/libuv/src/win/async.c +20 -17
  144. data/ext/asyncengine/libuv/src/win/core.c +44 -31
  145. data/ext/asyncengine/libuv/src/win/dl.c +40 -36
  146. data/ext/asyncengine/libuv/src/win/error.c +21 -1
  147. data/ext/asyncengine/libuv/src/win/fs-event.c +19 -21
  148. data/ext/asyncengine/libuv/src/win/fs.c +541 -189
  149. data/ext/asyncengine/libuv/src/win/getaddrinfo.c +56 -63
  150. data/ext/asyncengine/libuv/src/win/handle-inl.h +145 -0
  151. data/ext/asyncengine/libuv/src/win/handle.c +26 -101
  152. data/ext/asyncengine/libuv/src/win/internal.h +92 -107
  153. data/ext/asyncengine/libuv/src/win/loop-watcher.c +6 -14
  154. data/ext/asyncengine/libuv/src/win/pipe.c +78 -64
  155. data/ext/asyncengine/libuv/src/win/poll.c +618 -0
  156. data/ext/asyncengine/libuv/src/win/process-stdio.c +479 -0
  157. data/ext/asyncengine/libuv/src/win/process.c +147 -274
  158. data/ext/asyncengine/libuv/src/win/req-inl.h +225 -0
  159. data/ext/asyncengine/libuv/src/win/req.c +0 -149
  160. data/ext/asyncengine/libuv/src/{unix/check.c → win/stream-inl.h} +31 -42
  161. data/ext/asyncengine/libuv/src/win/stream.c +9 -43
  162. data/ext/asyncengine/libuv/src/win/tcp.c +200 -82
  163. data/ext/asyncengine/libuv/src/win/thread.c +42 -2
  164. data/ext/asyncengine/libuv/src/win/threadpool.c +3 -2
  165. data/ext/asyncengine/libuv/src/win/timer.c +13 -63
  166. data/ext/asyncengine/libuv/src/win/tty.c +26 -20
  167. data/ext/asyncengine/libuv/src/win/udp.c +26 -17
  168. data/ext/asyncengine/libuv/src/win/util.c +312 -167
  169. data/ext/asyncengine/libuv/src/win/winapi.c +16 -1
  170. data/ext/asyncengine/libuv/src/win/winapi.h +33 -9
  171. data/ext/asyncengine/libuv/src/win/winsock.c +88 -1
  172. data/ext/asyncengine/libuv/src/win/winsock.h +36 -3
  173. data/ext/asyncengine/libuv/test/benchmark-ares.c +16 -17
  174. data/ext/asyncengine/libuv/test/benchmark-fs-stat.c +164 -0
  175. data/ext/asyncengine/libuv/test/benchmark-list.h +9 -0
  176. data/ext/asyncengine/libuv/{src/unix/prepare.c → test/benchmark-loop-count.c} +42 -33
  177. data/ext/asyncengine/libuv/test/benchmark-million-timers.c +65 -0
  178. data/ext/asyncengine/libuv/test/benchmark-pound.c +1 -1
  179. data/ext/asyncengine/libuv/test/benchmark-sizes.c +2 -0
  180. data/ext/asyncengine/libuv/test/benchmark-spawn.c +7 -1
  181. data/ext/asyncengine/libuv/test/benchmark-udp-packet-storm.c +1 -1
  182. data/ext/asyncengine/libuv/test/echo-server.c +8 -0
  183. data/ext/asyncengine/libuv/test/run-tests.c +30 -0
  184. data/ext/asyncengine/libuv/test/runner-unix.c +6 -26
  185. data/ext/asyncengine/libuv/test/runner-win.c +5 -63
  186. data/ext/asyncengine/libuv/test/runner.c +10 -1
  187. data/ext/asyncengine/libuv/test/task.h +0 -8
  188. data/ext/asyncengine/libuv/test/test-async.c +43 -141
  189. data/ext/asyncengine/libuv/test/test-callback-order.c +76 -0
  190. data/ext/asyncengine/libuv/test/test-counters-init.c +2 -3
  191. data/ext/asyncengine/libuv/test/test-dlerror.c +17 -8
  192. data/ext/asyncengine/libuv/test/test-fs-event.c +31 -39
  193. data/ext/asyncengine/libuv/test/test-fs-poll.c +146 -0
  194. data/ext/asyncengine/libuv/test/test-fs.c +114 -2
  195. data/ext/asyncengine/libuv/test/test-gethostbyname.c +8 -8
  196. data/ext/asyncengine/libuv/test/test-hrtime.c +18 -15
  197. data/ext/asyncengine/libuv/test/test-ipc.c +8 -2
  198. data/ext/asyncengine/libuv/test/test-list.h +59 -9
  199. data/ext/asyncengine/libuv/test/test-loop-handles.c +2 -25
  200. data/ext/asyncengine/libuv/{src/unix/idle.c → test/test-poll-close.c} +37 -39
  201. data/ext/asyncengine/libuv/test/test-poll.c +573 -0
  202. data/ext/asyncengine/libuv/test/test-ref.c +79 -63
  203. data/ext/asyncengine/libuv/test/test-run-once.c +15 -11
  204. data/ext/asyncengine/libuv/test/test-semaphore.c +111 -0
  205. data/ext/asyncengine/libuv/test/test-spawn.c +368 -20
  206. data/ext/asyncengine/libuv/test/test-stdio-over-pipes.c +25 -35
  207. data/ext/asyncengine/libuv/test/test-tcp-close-while-connecting.c +80 -0
  208. data/ext/asyncengine/libuv/test/test-tcp-close.c +1 -1
  209. data/ext/asyncengine/libuv/test/test-tcp-connect-error-after-write.c +95 -0
  210. data/ext/asyncengine/libuv/test/test-tcp-connect-timeout.c +85 -0
  211. data/ext/asyncengine/libuv/test/test-tcp-shutdown-after-write.c +131 -0
  212. data/ext/asyncengine/libuv/test/test-tcp-write-error.c +2 -2
  213. data/ext/asyncengine/libuv/test/test-tcp-writealot.c +29 -54
  214. data/ext/asyncengine/libuv/test/test-timer-again.c +1 -1
  215. data/ext/asyncengine/libuv/test/test-timer.c +23 -1
  216. data/ext/asyncengine/libuv/test/test-udp-options.c +1 -1
  217. data/ext/asyncengine/libuv/test/{test-eio-overflow.c → test-walk-handles.c} +31 -44
  218. data/ext/asyncengine/libuv/uv.gyp +26 -9
  219. data/ext/asyncengine/rb_utilities.c +54 -0
  220. data/ext/asyncengine/rb_utilities.h +63 -0
  221. data/lib/asyncengine.rb +45 -38
  222. data/lib/asyncengine/asyncengine_ext.so +0 -0
  223. data/lib/asyncengine/debug.rb +37 -0
  224. data/lib/asyncengine/handle.rb +9 -0
  225. data/lib/asyncengine/tcp.rb +28 -0
  226. data/lib/asyncengine/timer.rb +18 -28
  227. data/lib/asyncengine/udp.rb +29 -0
  228. data/lib/asyncengine/utils.rb +32 -0
  229. data/lib/asyncengine/uv_error.rb +17 -0
  230. data/lib/asyncengine/version.rb +9 -1
  231. data/test/ae_test_helper.rb +62 -0
  232. data/test/test_basic.rb +169 -0
  233. data/test/test_call_from_other_thread.rb +55 -0
  234. data/test/test_error.rb +92 -0
  235. data/test/test_ip_utils.rb +44 -0
  236. data/test/test_next_tick.rb +37 -0
  237. data/test/test_resolver.rb +51 -0
  238. data/test/test_threads.rb +69 -0
  239. data/test/test_timer.rb +95 -0
  240. data/test/test_udp.rb +216 -0
  241. data/test/test_utils.rb +49 -0
  242. metadata +84 -57
  243. data/ext/asyncengine/libuv/mkmf.log +0 -24
  244. data/ext/asyncengine/libuv/src/unix/cares.c +0 -194
  245. data/ext/asyncengine/libuv/src/unix/cares.o +0 -0
  246. data/ext/asyncengine/libuv/src/unix/check.o +0 -0
  247. data/ext/asyncengine/libuv/src/unix/idle.o +0 -0
  248. data/ext/asyncengine/libuv/src/unix/prepare.o +0 -0
  249. data/ext/asyncengine/libuv/src/win/cares.c +0 -290
  250. data/lib/asyncengine/errors.rb +0 -5
  251. data/lib/asyncengine/next_tick.rb +0 -24
@@ -0,0 +1,51 @@
1
+ require "ae_test_helper"
2
+
3
+
4
+ class TestResolver < AETest
5
+
6
+ def test_01_resolve_success
7
+ @num_ips = 0
8
+
9
+ AE.run do
10
+ AE::Resolver.resolve "google.com" do |error, ips|
11
+ assert_nil error
12
+ assert_equal Array, ips.class
13
+ @num_ips = ips.size
14
+ AE.stop
15
+ end
16
+ end
17
+
18
+ AE.run do
19
+ AE::Resolver.resolve "google.com", Socket::AF_UNSPEC do |error, ips|
20
+ assert_equal @num_ips, ips.size
21
+ AE.stop
22
+ end
23
+ end
24
+
25
+ AE.run do
26
+ AE::Resolver.resolve "google.com", Socket::AF_INET6 do |error, ips|
27
+ assert_true @num_ips > ips.size
28
+ AE.stop
29
+ end
30
+ end
31
+ end
32
+
33
+ def test_02_resolve_error
34
+ AE.run do
35
+ AE::Resolver.resolve "qweasdzxc.qwe" do |error, ips|
36
+ assert_true error.is_a? AE::UvError
37
+ assert_equal error.type, :ENOENT
38
+ AE.stop
39
+ end
40
+ end
41
+
42
+ AE.run do
43
+ AE::Resolver.resolve "ipv6.google.com", Socket::AF_INET do |error, ips|
44
+ assert_true error.is_a? AE::UvError
45
+ assert_equal error.type, :ENOENT
46
+ AE.stop
47
+ end
48
+ end
49
+ end
50
+
51
+ end
@@ -0,0 +1,69 @@
1
+ require "ae_test_helper"
2
+
3
+
4
+ class TestThreads < AETest
5
+
6
+ def test_01_thread_works_as_expected
7
+ str = ""
8
+
9
+ AE.run do
10
+ AE.add_periodic_timer(0.1) {}
11
+
12
+ Thread.new { str << "a" }
13
+ Thread.new { sleep 0.01 ; str << "b" }
14
+ Thread.new { sleep 0.02 ; str << "c" }
15
+ Thread.new { sleep 0.03 ; str << "d" }
16
+ Thread.new { sleep 0.04 ; AE.stop }
17
+ end
18
+
19
+ assert_false AE.running?
20
+ assert_equal "abcd", str
21
+ end
22
+
23
+ def test_02_can_run_anything_in_call_from_other_thread
24
+ str = ""
25
+
26
+ AE.run do
27
+ Thread.new do
28
+ AE.call_from_other_thread { AE.next_tick { str << "a" } }
29
+ end
30
+ Thread.new do
31
+ sleep 0.02
32
+ AE.call_from_other_thread { AE.add_timer(0) { str << "b" } }
33
+ end
34
+ Thread.new do
35
+ sleep 0.02
36
+ AE.call_from_other_thread { AE.add_timer(0.05) { str << "c" } }
37
+ end
38
+ str << "1"
39
+ AE.next_tick { str << "2" }
40
+ AE.add_timer(0.1) { str << "d" ; AE.stop }
41
+ end
42
+
43
+ assert_false AE.running?
44
+ assert_equal "12abcd", str
45
+ end
46
+
47
+ def test_03_run_and_stop_are_thread_safe
48
+ summ = 0
49
+
50
+ th1 = Thread.new do
51
+ AE.run { AE.add_timer(0.1) { AE.stop } }
52
+ end
53
+
54
+ th2 = Thread.new do
55
+ sleep 0.01
56
+ AE.run { summ+=1 }
57
+ end
58
+
59
+ th3 = Thread.new do
60
+ sleep 0.01
61
+ AE.run { AE.add_timer(0) { summ+=1 } }
62
+ end
63
+
64
+ th1.join ; th2.join ; th3.join
65
+
66
+ assert_equal 2, summ
67
+ end
68
+
69
+ end
@@ -0,0 +1,95 @@
1
+ require "ae_test_helper"
2
+
3
+
4
+ class TestTimer < AETest
5
+
6
+ def test_01_timer_fires
7
+ t1_executed = false
8
+
9
+ AE.run do
10
+ t1 = AE::Timer.new(0.01) { t1_executed = true ; assert_false t1.alive? }
11
+ assert_true t1.alive?
12
+ AE.add_timer(0.02) { assert_false t1.alive? }
13
+ AE.add_timer(0.03) { assert_false t1.close ; AE.stop }
14
+ end
15
+
16
+ assert_true t1_executed
17
+ end
18
+
19
+ def test_02_timer_closed
20
+ t1_executed = false
21
+ pt1_executed = false
22
+
23
+ AE.run do
24
+ t1 = AE::Timer.new(0.02) { t1_executed = true }
25
+ AE.add_timer(0.01) { assert_true t1.close ; assert_false t1.alive? }
26
+ AE.add_timer(0.03) { assert_false t1.close }
27
+
28
+ pt1 = AE::PeriodicTimer.new(0.02) { pt1_executed = true }
29
+ AE.add_timer(0.01) { assert_true pt1.close ; assert_false pt1.alive? }
30
+ AE.add_timer(0.03) { assert_false pt1.close ; AE.stop }
31
+ end
32
+
33
+ assert_false t1_executed
34
+ assert_false pt1_executed
35
+ end
36
+
37
+ def test_03_periodic_timer_closed_after_4_repeats
38
+ pt1_ticks = 0
39
+
40
+ AE.run do
41
+ pt1 = AE::PeriodicTimer.new(0.01) do
42
+ pt1_ticks += 1
43
+ AE.stop if pt1_ticks == 4
44
+ end
45
+ end
46
+
47
+ assert_equal 4, pt1_ticks
48
+ end
49
+
50
+ def test_04_periodic_timer_interval_increased
51
+ pt1_ticks = 0
52
+ pt1_interval = 0.001
53
+
54
+ AE.run do
55
+ pt1 = AE::PeriodicTimer.new(pt1_interval) do
56
+ # This should stop after 0.015 seconds.
57
+ AE.stop if (pt1_ticks += 1) == 4
58
+ pt1.restart (pt1_interval *= 2)
59
+ end
60
+
61
+ # So check that pt1 is terminated after 0.05 seconds (it should).
62
+ AE.add_timer(0.05) { assert_false pt1.alive? }
63
+ end
64
+
65
+ assert_equal 4, pt1_ticks
66
+ end
67
+
68
+ def test_05_timer_restarted
69
+ str1 = ""
70
+ str2 = ""
71
+
72
+ AE.run do
73
+ t1 = AE::Timer.new(0.01) { str1 << "0" }
74
+ t1.restart # So "0" will be written.
75
+ AE.next_tick { t1.restart }
76
+ AE.add_timer(0.02) { assert_false t1.restart } # Timer was terminated so nothing new occurs.
77
+
78
+ t2 = AE::Timer.new(0.01, Proc.new { str2 << "0" })
79
+ t2.pause
80
+ AE.add_timer(0.001) { t2.restart(0.002) }
81
+
82
+ AE.add_timer(0.05) { AE.stop }
83
+ end
84
+
85
+ assert_equal "0", str1
86
+ assert_equal "0", str2
87
+ end
88
+
89
+ def test_06_timer_closed_on_its_callback_does_not_crash
90
+ AE.run do
91
+ t1 = AE::Timer.new(0.001) { assert_false t1.close ; AE.stop }
92
+ end
93
+ end
94
+
95
+ end
@@ -0,0 +1,216 @@
1
+ require "ae_test_helper"
2
+
3
+
4
+ class TestUdp < AETest
5
+
6
+ RECEIVED_DATAGRAMS = []
7
+
8
+ class MyUDP < AE::UDPSocket
9
+ def initialize
10
+ @local_ip, @local_port = local_address()
11
+ end
12
+ end
13
+
14
+ def setup
15
+ super
16
+ RECEIVED_DATAGRAMS.clear
17
+ end
18
+
19
+ def test_01_udp_basic_ipv4
20
+ return false unless (ip = host_loopback_ipv4)
21
+ private_test_01_udp_basic ip
22
+ end
23
+
24
+ def test_01_udp_basic_ipv6
25
+ return false unless (ip = host_loopback_ipv6)
26
+ private_test_01_udp_basic ip
27
+ end
28
+
29
+ def test_02_set_encoding_ipv4
30
+ return false unless (ip = host_loopback_ipv4)
31
+ private_test_02_set_encoding ip
32
+ end
33
+
34
+ def test_02_set_encoding_ipv6
35
+ return false unless (ip = host_loopback_ipv6)
36
+ private_test_02_set_encoding ip
37
+ end
38
+
39
+ def test_03_send_callback_ipv4
40
+ return false unless (ip = host_loopback_ipv4)
41
+ private_test_03_send_callback ip
42
+ end
43
+
44
+ def test_03_send_callback_ipv6
45
+ return false unless (ip = host_loopback_ipv6)
46
+ private_test_03_send_callback ip
47
+ end
48
+
49
+ def test_04_datagram_source_address_ipv4
50
+ return false unless (ip = host_loopback_ipv4)
51
+ private_test_04_datagram_source_address ip
52
+ end
53
+
54
+ def test_04_datagram_source_address_ipv6
55
+ return false unless (ip = host_loopback_ipv6)
56
+ private_test_04_datagram_source_address ip
57
+ end
58
+
59
+
60
+ private
61
+
62
+ def private_test_01_udp_basic ip
63
+ AE.run do
64
+ sock = AE.open_udp_socket ip, 0
65
+ local_ip, local_port = sock.local_address()
66
+
67
+ assert_true [:ipv4, :ipv6].include?(sock.ip_type)
68
+
69
+ def sock.on_datagram_received datagram, ip, port
70
+ RECEIVED_DATAGRAMS << datagram
71
+ end
72
+
73
+ assert_true sock.alive?
74
+ assert_false sock.paused?
75
+
76
+ assert_equal ip, local_ip
77
+
78
+ # This will be received.
79
+ assert_true sock.send_datagram("1", local_ip, local_port)
80
+
81
+ AE.add_timer(0.1) do
82
+ sock.pause
83
+ assert_true sock.paused?
84
+
85
+ # This wont be received.
86
+ assert_true sock.send_datagram("2", local_ip, local_port)
87
+ end
88
+
89
+ AE.add_timer(0.15) do
90
+ sock.resume
91
+ assert_false sock.paused?
92
+
93
+ # This will be received.
94
+ assert_true sock.send_datagram("3", local_ip, local_port)
95
+ end
96
+
97
+ AE.add_timer(0.2) do
98
+ assert_true sock.close()
99
+ assert_false sock.alive?
100
+
101
+ # Socket closed, cannot be sent.
102
+ assert_false sock.send_datagram "hello", "1.2.3.4", 1234
103
+
104
+ AE.stop
105
+ end
106
+ end
107
+
108
+ assert_equal ["1", "3"], RECEIVED_DATAGRAMS
109
+ end
110
+
111
+ def private_test_02_set_encoding ip
112
+ AE.run do
113
+ sock = AE.open_udp_socket ip, 0
114
+ local_ip, local_port = sock.local_address()
115
+
116
+ def sock.on_datagram_received datagram, ip, port
117
+ RECEIVED_DATAGRAMS << datagram
118
+ AE.stop if RECEIVED_DATAGRAMS.size == 2
119
+ set_encoding_ascii()
120
+ end
121
+
122
+ # Default UDP receiving encoding is ASCII_8BIT.
123
+ assert_equal sock.encoding, :ascii
124
+
125
+ sock.set_encoding_utf8
126
+ assert_equal sock.encoding, :utf8
127
+ sock.send_datagram "\x80", local_ip, local_port
128
+
129
+ sock.send_datagram "\x80", local_ip, local_port
130
+ end
131
+
132
+ assert_equal RECEIVED_DATAGRAMS[0].encoding, Encoding::UTF_8
133
+ assert_false RECEIVED_DATAGRAMS[0].valid_encoding?
134
+ assert_equal RECEIVED_DATAGRAMS[1].encoding, Encoding::ASCII_8BIT
135
+ assert_true RECEIVED_DATAGRAMS[1].valid_encoding?
136
+ end
137
+
138
+ def private_test_03_send_callback ip
139
+ send_cb_ok = 0
140
+ send_cb_error = 0
141
+
142
+ AE.run do
143
+ sock = AE.open_udp_socket ip, 4444
144
+ local_ip, local_port = sock.local_address()
145
+
146
+ def sock.on_datagram_received datagram, ip, port
147
+ RECEIVED_DATAGRAMS << datagram
148
+ end
149
+
150
+ sock.send_datagram "1", local_ip, local_port do |error|
151
+ error ? (send_cb_error += 1) : (send_cb_ok += 1)
152
+ end
153
+
154
+ sock.send_datagram("2", local_ip, local_port, proc do |error|
155
+ error ? (send_cb_error += 1) : (send_cb_ok += 1)
156
+ end)
157
+
158
+ # Sending to destination port 0 causes a EINVAL error.
159
+ sock.send_datagram "3", local_ip, 0 do |error|
160
+ error ? (send_cb_error += 1) : (send_cb_ok += 1)
161
+ assert_equal error.class, AE::UvError
162
+ assert_equal error.type, :EINVAL
163
+ end
164
+
165
+ AE.add_timer(0.1) { assert_true sock.close() ; AE.stop }
166
+ end
167
+
168
+ assert_equal ["1", "2"], RECEIVED_DATAGRAMS
169
+ assert_equal 2, send_cb_ok
170
+ assert_equal 1, send_cb_error
171
+ end
172
+
173
+ def private_test_04_datagram_source_address ip
174
+ sock = nil
175
+ local_ip = nil
176
+ local_port = nil
177
+
178
+ AE.run do
179
+ sock = AE.open_udp_socket ip, 0
180
+ local_ip, local_port = sock.local_address()
181
+
182
+ def sock.on_datagram_received datagram, src_ip, src_port
183
+ RECEIVED_DATAGRAMS << datagram
184
+ @last_src_ip = src_ip
185
+ @last_src_port = src_port
186
+
187
+ case RECEIVED_DATAGRAMS.size
188
+ when 1
189
+ send_datagram "reply1", src_ip, src_port
190
+ when 2
191
+ send_datagram "reply2", src_ip, src_port do |error|
192
+ error ? (@reply_cb_error += 1) : (@reply_cb_ok += 1)
193
+ end
194
+ when 3
195
+ send_datagram("reply3", src_ip, src_port, proc do |error|
196
+ error ? (@reply_cb_error += 1) : (@reply_cb_ok += 1)
197
+ end)
198
+ when 4
199
+ AE.stop
200
+ end
201
+ end
202
+
203
+ sock.instance_variable_set :@reply_cb_ok, 0
204
+ sock.instance_variable_set :@reply_cb_error, 0
205
+
206
+ sock.send_datagram "hello", local_ip, local_port
207
+ end
208
+
209
+ assert_equal ["hello", "reply1", "reply2", "reply3"], RECEIVED_DATAGRAMS
210
+ assert_equal 2, sock.instance_variable_get(:@reply_cb_ok)
211
+ assert_equal 0, sock.instance_variable_get(:@reply_cb_error)
212
+ assert_equal sock.instance_variable_get(:@last_src_ip), local_ip
213
+ assert_equal sock.instance_variable_get(:@last_src_port), local_port
214
+ end
215
+
216
+ end
@@ -0,0 +1,49 @@
1
+ require "ae_test_helper"
2
+
3
+
4
+ class TestUtils < AETest
5
+
6
+ def test_01_hrtime
7
+ assert (AE::Utils.get_hrtime < AE::Utils.get_hrtime)
8
+ end
9
+
10
+ def test_02_memory
11
+ assert (AE::Utils.get_free_memory < AE::Utils.get_total_memory)
12
+ end
13
+
14
+ def test_03_loadavg
15
+ loadavg = AE::Utils.get_loadavg
16
+
17
+ assert_true loadavg[0].is_a? Float
18
+ assert_true loadavg[1].is_a? Float
19
+ assert_true loadavg[2].is_a? Float
20
+ end
21
+
22
+ def test_04_uptime
23
+ assert (AE::Utils.get_uptime < AE::Utils.get_uptime)
24
+ end
25
+
26
+ def test_05_network_interfaces
27
+ AE::Utils.get_network_interfaces.each do |iface|
28
+ assert_true (iface.is_a? AE::Utils::NetworkInterface)
29
+ assert_true (iface.name.is_a? String)
30
+ assert_true ([true, false].include? iface.internal?)
31
+ assert_true ([:ipv4, :ipv6].include? iface.ip_type)
32
+ assert_true (iface.ip.is_a? String)
33
+ end
34
+ end
35
+
36
+ def test_06_cpu_info
37
+ AE::Utils.get_cpu_info.each do |cpu|
38
+ assert_true (cpu.is_a? AE::Utils::CpuInfo)
39
+ assert_true (cpu.model.is_a? String)
40
+ assert_true (cpu.speed.is_a? Fixnum)
41
+ assert_true (cpu.time_sys.is_a? Float)
42
+ assert_true (cpu.time_user.is_a? Float)
43
+ assert_true (cpu.time_idle.is_a? Float)
44
+ assert_true (cpu.time_irq.is_a? Float)
45
+ assert_true (cpu.time_nice.is_a? Float)
46
+ end
47
+ end
48
+
49
+ end