tioselectors.nim 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. discard """
  2. output: "All tests passed!"
  3. """
  4. import selectors
  5. const hasThreadSupport = compileOption("threads")
  6. template processTest(t, x: untyped) =
  7. #stdout.write(t)
  8. #stdout.flushFile()
  9. if not x: echo(t & " FAILED\r\n")
  10. when not defined(windows):
  11. import os, posix, nativesockets
  12. when ioselSupportedPlatform:
  13. import osproc
  14. proc socket_notification_test(): bool =
  15. proc create_test_socket(): SocketHandle =
  16. var sock = posix.socket(posix.AF_INET, posix.SOCK_STREAM,
  17. posix.IPPROTO_TCP)
  18. var x: int = fcntl(sock, F_GETFL, 0)
  19. if x == -1: raiseOSError(osLastError())
  20. else:
  21. var mode = x or O_NONBLOCK
  22. if fcntl(sock, F_SETFL, mode) == -1:
  23. raiseOSError(osLastError())
  24. result = sock
  25. var client_message = "SERVER HELLO =>"
  26. var server_message = "CLIENT HELLO"
  27. var buffer : array[128, char]
  28. var selector = newSelector[int]()
  29. var client_socket = create_test_socket()
  30. var server_socket = create_test_socket()
  31. var option : int32 = 1
  32. if setsockopt(server_socket, cint(SOL_SOCKET), cint(SO_REUSEADDR),
  33. addr(option), sizeof(option).SockLen) < 0:
  34. raiseOSError(osLastError())
  35. var aiList = getAddrInfo("0.0.0.0", Port(13337))
  36. if bindAddr(server_socket, aiList.ai_addr,
  37. aiList.ai_addrlen.Socklen) < 0'i32:
  38. freeAddrInfo(aiList)
  39. raiseOSError(osLastError())
  40. if server_socket.listen() == -1:
  41. raiseOSError(osLastError())
  42. freeAddrInfo(aiList)
  43. aiList = getAddrInfo("127.0.0.1", Port(13337))
  44. discard posix.connect(client_socket, aiList.ai_addr,
  45. aiList.ai_addrlen.Socklen)
  46. registerHandle(selector, server_socket, {Event.Read}, 0)
  47. registerHandle(selector, client_socket, {Event.Write}, 0)
  48. freeAddrInfo(aiList)
  49. # make sure both sockets are selected
  50. var nevs = 0
  51. while nevs < 2:
  52. nevs += selector.select(100).len
  53. var sockAddress: SockAddr
  54. var addrLen = sizeof(sockAddress).Socklen
  55. var server2_socket = accept(server_socket,
  56. cast[ptr SockAddr](addr(sockAddress)),
  57. addr(addrLen))
  58. assert(server2_socket != osInvalidSocket)
  59. selector.registerHandle(server2_socket, {Event.Read}, 0)
  60. if posix.send(client_socket, addr(client_message[0]),
  61. len(client_message), 0) == -1:
  62. raiseOSError(osLastError())
  63. selector.updateHandle(client_socket, {Event.Read})
  64. var rc2 = selector.select(100)
  65. assert(len(rc2) == 1)
  66. var read_count = posix.recv(server2_socket, addr buffer[0], 128, 0)
  67. if read_count == -1:
  68. raiseOSError(osLastError())
  69. assert(read_count == len(client_message))
  70. var test1 = true
  71. for i in 0..<read_count:
  72. if client_message[i] != buffer[i]:
  73. test1 = false
  74. break
  75. assert(test1)
  76. selector.updateHandle(server2_socket, {Event.Write})
  77. var rc3 = selector.select(0)
  78. assert(len(rc3) == 1)
  79. if posix.send(server2_socket, addr(server_message[0]),
  80. len(server_message), 0) == -1:
  81. raiseOSError(osLastError())
  82. selector.updateHandle(server2_socket, {Event.Read})
  83. var rc4 = selector.select(100)
  84. assert(len(rc4) == 1)
  85. read_count = posix.recv(client_socket, addr(buffer[0]), 128, 0)
  86. if read_count == -1:
  87. raiseOSError(osLastError())
  88. assert(read_count == len(server_message))
  89. var test2 = true
  90. for i in 0..<read_count:
  91. if server_message[i] != buffer[i]:
  92. test2 = false
  93. break
  94. assert(test2)
  95. selector.unregister(server_socket)
  96. selector.unregister(server2_socket)
  97. selector.unregister(client_socket)
  98. discard posix.close(server_socket)
  99. discard posix.close(server2_socket)
  100. discard posix.close(client_socket)
  101. assert(selector.isEmpty())
  102. close(selector)
  103. result = true
  104. proc event_notification_test(): bool =
  105. var selector = newSelector[int]()
  106. var event = newSelectEvent()
  107. selector.registerEvent(event, 1)
  108. var rc0 = selector.select(0)
  109. event.trigger()
  110. var rc1 = selector.select(0)
  111. event.trigger()
  112. var rc2 = selector.select(0)
  113. var rc3 = selector.select(0)
  114. assert(len(rc0) == 0 and len(rc1) == 1 and len(rc2) == 1 and len(rc3) == 0)
  115. var ev1 = selector.getData(rc1[0].fd)
  116. var ev2 = selector.getData(rc2[0].fd)
  117. assert(ev1 == 1 and ev2 == 1)
  118. selector.unregister(event)
  119. event.close()
  120. assert(selector.isEmpty())
  121. selector.close()
  122. result = true
  123. when ioselSupportedPlatform:
  124. proc timer_notification_test(): bool =
  125. var selector = newSelector[int]()
  126. var timer = selector.registerTimer(100, false, 0)
  127. var rc1 = selector.select(10000)
  128. var rc2 = selector.select(10000)
  129. # if this flakes, see tests/m14634.nim
  130. assert len(rc1) == 1 and len(rc2) == 1, $(len(rc1), len(rc2))
  131. selector.unregister(timer)
  132. discard selector.select(0)
  133. selector.registerTimer(100, true, 0)
  134. var rc4 = selector.select(10000)
  135. var rc5 = selector.select(1000) # this will be an actual wait, keep it small
  136. assert len(rc4) == 1 and len(rc5) == 0, $(len(rc4), len(rc5))
  137. assert(selector.isEmpty())
  138. selector.close()
  139. result = true
  140. proc process_notification_test(): bool =
  141. var selector = newSelector[int]()
  142. var process2 = startProcess("sleep", "", ["2"], nil,
  143. {poStdErrToStdOut, poUsePath})
  144. discard startProcess("sleep", "", ["1"], nil,
  145. {poStdErrToStdOut, poUsePath})
  146. selector.registerProcess(process2.processID, 0)
  147. var rc1 = selector.select(1500)
  148. var rc2 = selector.select(1500)
  149. var r = len(rc1) + len(rc2)
  150. assert(r == 1)
  151. result = true
  152. proc signal_notification_test(): bool =
  153. var sigset1n, sigset1o, sigset2n, sigset2o: Sigset
  154. var pid = posix.getpid()
  155. discard sigemptyset(sigset1n)
  156. discard sigemptyset(sigset1o)
  157. discard sigemptyset(sigset2n)
  158. discard sigemptyset(sigset2o)
  159. when hasThreadSupport:
  160. if pthread_sigmask(SIG_BLOCK, sigset1n, sigset1o) == -1:
  161. raiseOSError(osLastError())
  162. else:
  163. if sigprocmask(SIG_BLOCK, sigset1n, sigset1o) == -1:
  164. raiseOSError(osLastError())
  165. var selector = newSelector[int]()
  166. var s1 = selector.registerSignal(SIGUSR1, 1)
  167. var s2 = selector.registerSignal(SIGUSR2, 2)
  168. var s3 = selector.registerSignal(SIGTERM, 3)
  169. discard selector.select(0)
  170. discard posix.kill(pid, SIGUSR1)
  171. discard posix.kill(pid, SIGUSR2)
  172. discard posix.kill(pid, SIGTERM)
  173. var rc = selector.select(0)
  174. var cd0 = selector.getData(rc[0].fd)
  175. var cd1 = selector.getData(rc[1].fd)
  176. var cd2 = selector.getData(rc[2].fd)
  177. selector.unregister(s1)
  178. selector.unregister(s2)
  179. selector.unregister(s3)
  180. when hasThreadSupport:
  181. if pthread_sigmask(SIG_BLOCK, sigset2n, sigset2o) == -1:
  182. raiseOSError(osLastError())
  183. else:
  184. if sigprocmask(SIG_BLOCK, sigset2n, sigset2o) == -1:
  185. raiseOSError(osLastError())
  186. assert(len(rc) == 3)
  187. assert(cd0 + cd1 + cd2 == 6, $(cd0 + cd1 + cd2)) # 1 + 2 + 3
  188. assert(equalMem(addr sigset1o, addr sigset2o, sizeof(Sigset)))
  189. assert(selector.isEmpty())
  190. result = true
  191. when defined(macosx) or defined(freebsd) or defined(openbsd) or
  192. defined(netbsd):
  193. proc rename(frompath: cstring, topath: cstring): cint
  194. {.importc: "rename", header: "<stdio.h>".}
  195. proc createFile(name: string): cint =
  196. result = posix.open(cstring(name), posix.O_CREAT or posix.O_RDWR)
  197. if result == -1:
  198. raiseOsError(osLastError())
  199. proc writeFile(name: string, data: string) =
  200. let fd = posix.open(cstring(name), posix.O_APPEND or posix.O_RDWR)
  201. if fd == -1:
  202. raiseOsError(osLastError())
  203. let length = len(data).cint
  204. if posix.write(fd, cast[pointer](addr data[0]),
  205. len(data).cint) != length:
  206. raiseOsError(osLastError())
  207. if posix.close(fd) == -1:
  208. raiseOsError(osLastError())
  209. proc closeFile(fd: cint) =
  210. if posix.close(fd) == -1:
  211. raiseOsError(osLastError())
  212. proc removeFile(name: string) =
  213. let err = posix.unlink(cstring(name))
  214. if err == -1:
  215. raiseOsError(osLastError())
  216. proc createDir(name: string) =
  217. let err = posix.mkdir(cstring(name), 0x1FF)
  218. if err == -1:
  219. raiseOsError(osLastError())
  220. proc removeDir(name: string) =
  221. let err = posix.rmdir(cstring(name))
  222. if err == -1:
  223. raiseOsError(osLastError())
  224. proc chmodPath(name: string, mode: cint) =
  225. let err = posix.chmod(cstring(name), Mode(mode))
  226. if err == -1:
  227. raiseOsError(osLastError())
  228. proc renameFile(names: string, named: string) =
  229. let err = rename(cstring(names), cstring(named))
  230. if err == -1:
  231. raiseOsError(osLastError())
  232. proc symlink(names: string, named: string) =
  233. let err = posix.symlink(cstring(names), cstring(named))
  234. if err == -1:
  235. raiseOsError(osLastError())
  236. proc openWatch(name: string): cint =
  237. result = posix.open(cstring(name), posix.O_RDONLY)
  238. if result == -1:
  239. raiseOsError(osLastError())
  240. const
  241. testDirectory = "/tmp/kqtest"
  242. type
  243. valType = object
  244. fd: cint
  245. events: set[Event]
  246. proc vnode_test(): bool =
  247. proc validate(test: openArray[ReadyKey],
  248. check: openArray[valType]): bool =
  249. result = false
  250. if len(test) == len(check):
  251. for checkItem in check:
  252. result = false
  253. for testItem in test:
  254. if testItem.fd == checkItem.fd and
  255. checkItem.events <= testItem.events:
  256. result = true
  257. break
  258. if not result:
  259. break
  260. var res: seq[ReadyKey]
  261. var selector = newSelector[int]()
  262. var events = {Event.VnodeWrite, Event.VnodeDelete, Event.VnodeExtend,
  263. Event.VnodeAttrib, Event.VnodeLink, Event.VnodeRename,
  264. Event.VnodeRevoke}
  265. result = true
  266. discard posix.unlink(testDirectory)
  267. createDir(testDirectory)
  268. var dirfd = posix.open(cstring(testDirectory), posix.O_RDONLY)
  269. if dirfd == -1:
  270. raiseOsError(osLastError())
  271. selector.registerVnode(dirfd, events, 1)
  272. discard selector.select(0)
  273. # chmod testDirectory to 0777
  274. chmodPath(testDirectory, 0x1FF)
  275. res = selector.select(0)
  276. doAssert(len(res) == 1)
  277. doAssert(len(selector.select(0)) == 0)
  278. doAssert(res[0].fd == dirfd and
  279. {Event.Vnode, Event.VnodeAttrib} <= res[0].events)
  280. # create subdirectory
  281. createDir(testDirectory & "/test")
  282. res = selector.select(0)
  283. doAssert(len(res) == 1)
  284. doAssert(len(selector.select(0)) == 0)
  285. doAssert(res[0].fd == dirfd and
  286. {Event.Vnode, Event.VnodeWrite,
  287. Event.VnodeLink} <= res[0].events)
  288. # open test directory for watching
  289. var testfd = openWatch(testDirectory & "/test")
  290. selector.registerVnode(testfd, events, 2)
  291. doAssert(len(selector.select(0)) == 0)
  292. # rename test directory
  293. renameFile(testDirectory & "/test", testDirectory & "/renamed")
  294. res = selector.select(0)
  295. doAssert(len(res) == 2)
  296. doAssert(len(selector.select(0)) == 0)
  297. doAssert(validate(res,
  298. [valType(fd: dirfd, events: {Event.Vnode, Event.VnodeWrite}),
  299. valType(fd: testfd,
  300. events: {Event.Vnode, Event.VnodeRename})])
  301. )
  302. # remove test directory
  303. removeDir(testDirectory & "/renamed")
  304. res = selector.select(0)
  305. doAssert(len(res) == 2)
  306. doAssert(len(selector.select(0)) == 0)
  307. doAssert(validate(res,
  308. [valType(fd: dirfd, events: {Event.Vnode, Event.VnodeWrite,
  309. Event.VnodeLink}),
  310. valType(fd: testfd,
  311. events: {Event.Vnode, Event.VnodeDelete})])
  312. )
  313. # create file new test file
  314. testfd = createFile(testDirectory & "/testfile")
  315. res = selector.select(0)
  316. doAssert(len(res) == 1)
  317. doAssert(len(selector.select(0)) == 0)
  318. doAssert(res[0].fd == dirfd and
  319. {Event.Vnode, Event.VnodeWrite} <= res[0].events)
  320. # close new test file
  321. closeFile(testfd)
  322. doAssert(len(selector.select(0)) == 0)
  323. doAssert(len(selector.select(0)) == 0)
  324. # chmod test file with 0666
  325. chmodPath(testDirectory & "/testfile", 0x1B6)
  326. doAssert(len(selector.select(0)) == 0)
  327. testfd = openWatch(testDirectory & "/testfile")
  328. selector.registerVnode(testfd, events, 1)
  329. discard selector.select(0)
  330. # write data to test file
  331. writeFile(testDirectory & "/testfile", "TESTDATA")
  332. res = selector.select(0)
  333. doAssert(len(res) == 1)
  334. doAssert(len(selector.select(0)) == 0)
  335. doAssert(res[0].fd == testfd and
  336. {Event.Vnode, Event.VnodeWrite,
  337. Event.VnodeExtend} <= res[0].events)
  338. # symlink test file
  339. symlink(testDirectory & "/testfile", testDirectory & "/testlink")
  340. res = selector.select(0)
  341. doAssert(len(res) == 1)
  342. doAssert(len(selector.select(0)) == 0)
  343. doAssert(res[0].fd == dirfd and
  344. {Event.Vnode, Event.VnodeWrite} <= res[0].events)
  345. # remove test file
  346. removeFile(testDirectory & "/testfile")
  347. res = selector.select(0)
  348. doAssert(len(res) == 2)
  349. doAssert(len(selector.select(0)) == 0)
  350. doAssert(validate(res,
  351. [valType(fd: testfd, events: {Event.Vnode, Event.VnodeDelete}),
  352. valType(fd: dirfd, events: {Event.Vnode, Event.VnodeWrite})])
  353. )
  354. # remove symlink
  355. removeFile(testDirectory & "/testlink")
  356. res = selector.select(0)
  357. doAssert(len(res) == 1)
  358. doAssert(len(selector.select(0)) == 0)
  359. doAssert(res[0].fd == dirfd and
  360. {Event.Vnode, Event.VnodeWrite} <= res[0].events)
  361. # remove testDirectory
  362. removeDir(testDirectory)
  363. res = selector.select(0)
  364. doAssert(len(res) == 1)
  365. doAssert(len(selector.select(0)) == 0)
  366. doAssert(res[0].fd == dirfd and
  367. {Event.Vnode, Event.VnodeDelete} <= res[0].events)
  368. proc pipe_test(): bool =
  369. # closing the read end of a pipe will result in it automatically
  370. # being removed from the kqueue; make sure no exception is raised
  371. var s = newSelector[int]()
  372. var fds: array[2, cint]
  373. discard pipe(fds)
  374. s.registerHandle(fds[1], {Write}, 0)
  375. discard close(fds[0])
  376. let res = s.select(-1)
  377. doAssert(res.len == 1)
  378. s.unregister(fds[1])
  379. discard close(fds[1])
  380. return true
  381. when hasThreadSupport:
  382. var counter = 0
  383. proc event_wait_thread(event: SelectEvent) {.thread.} =
  384. var selector = newSelector[int]()
  385. selector.registerEvent(event, 1)
  386. var rc = selector.select(1000)
  387. if len(rc) == 1:
  388. inc(counter)
  389. selector.unregister(event)
  390. assert(selector.isEmpty())
  391. proc mt_event_test(): bool =
  392. var
  393. thr: array[0..7, Thread[SelectEvent]]
  394. var selector = newSelector[int]()
  395. var sock = createNativeSocket()
  396. var event = newSelectEvent()
  397. for i in 0..high(thr):
  398. createThread(thr[i], event_wait_thread, event)
  399. selector.registerHandle(sock, {Event.Read}, 1)
  400. discard selector.select(500)
  401. selector.unregister(sock)
  402. event.trigger()
  403. joinThreads(thr)
  404. assert(counter == 1)
  405. result = true
  406. processTest("Socket notification test...", socket_notification_test())
  407. processTest("User event notification test...", event_notification_test())
  408. when hasThreadSupport:
  409. processTest("Multithreaded user event notification test...",
  410. mt_event_test())
  411. when ioselSupportedPlatform:
  412. processTest("Timer notification test...", timer_notification_test())
  413. processTest("Process notification test...", process_notification_test())
  414. processTest("Signal notification test...", signal_notification_test())
  415. when defined(macosx) or defined(freebsd) or defined(openbsd) or
  416. defined(netbsd):
  417. processTest("File notification test...", vnode_test())
  418. processTest("Pipe test...", pipe_test())
  419. echo("All tests passed!")
  420. else:
  421. import nativesockets, winlean, os, osproc
  422. proc socket_notification_test(): bool =
  423. proc create_test_socket(): SocketHandle =
  424. var sock = createNativeSocket()
  425. setBlocking(sock, false)
  426. result = sock
  427. var client_message = "SERVER HELLO =>"
  428. var server_message = "CLIENT HELLO"
  429. var buffer : array[128, char]
  430. var selector = newSelector[int]()
  431. var client_socket = create_test_socket()
  432. var server_socket = create_test_socket()
  433. selector.registerHandle(server_socket, {Event.Read}, 0)
  434. selector.registerHandle(client_socket, {Event.Write}, 0)
  435. var option : int32 = 1
  436. if setsockopt(server_socket, cint(SOL_SOCKET), cint(SO_REUSEADDR),
  437. addr(option), sizeof(option).SockLen) < 0:
  438. raiseOSError(osLastError())
  439. var aiList = getAddrInfo("0.0.0.0", Port(13337))
  440. if bindAddr(server_socket, aiList.ai_addr,
  441. aiList.ai_addrlen.Socklen) < 0'i32:
  442. freeAddrInfo(aiList)
  443. raiseOSError(osLastError())
  444. discard server_socket.listen()
  445. freeAddrInfo(aiList)
  446. aiList = getAddrInfo("127.0.0.1", Port(13337))
  447. discard connect(client_socket, aiList.ai_addr,
  448. aiList.ai_addrlen.Socklen)
  449. freeAddrInfo(aiList)
  450. # for some reason Windows select doesn't return both
  451. # descriptors from first call, so we need to make 2 calls
  452. var n = 0
  453. var rcm = selector.select(1000)
  454. while n < 10 and len(rcm) < 2:
  455. sleep(1000)
  456. rcm = selector.select(1000)
  457. inc(n)
  458. assert(len(rcm) == 2)
  459. var sockAddress = SockAddr()
  460. var addrLen = sizeof(sockAddress).Socklen
  461. var server2_socket = accept(server_socket,
  462. cast[ptr SockAddr](addr(sockAddress)),
  463. addr(addrLen))
  464. assert(server2_socket != osInvalidSocket)
  465. selector.registerHandle(server2_socket, {Event.Read}, 0)
  466. if send(client_socket, cast[pointer](addr(client_message[0])),
  467. cint(len(client_message)), 0) == -1:
  468. raiseOSError(osLastError())
  469. selector.updateHandle(client_socket, {Event.Read})
  470. var rc2 = selector.select(1000)
  471. assert(len(rc2) == 1)
  472. var read_count = recv(server2_socket, addr buffer[0], 128, 0)
  473. if read_count == -1:
  474. raiseOSError(osLastError())
  475. assert(read_count == len(client_message))
  476. var test1 = true
  477. for i in 0..<read_count:
  478. if client_message[i] != buffer[i]:
  479. test1 = false
  480. break
  481. assert(test1)
  482. if send(server2_socket, cast[pointer](addr(server_message[0])),
  483. cint(len(server_message)), 0) == -1:
  484. raiseOSError(osLastError())
  485. var rc3 = selector.select(0)
  486. assert(len(rc3) == 1)
  487. read_count = recv(client_socket, addr(buffer[0]), 128, 0)
  488. if read_count == -1:
  489. raiseOSError(osLastError())
  490. assert(read_count == len(server_message))
  491. var test2 = true
  492. for i in 0..<read_count:
  493. if server_message[i] != buffer[i]:
  494. test2 = false
  495. break
  496. assert(test2)
  497. selector.unregister(server_socket)
  498. selector.unregister(server2_socket)
  499. selector.unregister(client_socket)
  500. close(server_socket)
  501. close(server2_socket)
  502. close(client_socket)
  503. assert(selector.isEmpty())
  504. close(selector)
  505. result = true
  506. proc event_notification_test(): bool =
  507. var selector = newSelector[int]()
  508. var event = newSelectEvent()
  509. selector.registerEvent(event, 1)
  510. discard selector.select(0)
  511. event.trigger()
  512. var rc1 = selector.select(0)
  513. event.trigger()
  514. var rc2 = selector.select(0)
  515. var rc3 = selector.select(0)
  516. assert(len(rc1) == 1 and len(rc2) == 1 and len(rc3) == 0)
  517. var ev1 = selector.getData(rc1[0].fd)
  518. var ev2 = selector.getData(rc2[0].fd)
  519. assert(ev1 == 1 and ev2 == 1)
  520. selector.unregister(event)
  521. event.close()
  522. assert(selector.isEmpty())
  523. selector.close()
  524. result = true
  525. when hasThreadSupport:
  526. var counter = 0
  527. proc event_wait_thread(event: SelectEvent) {.thread.} =
  528. var selector = newSelector[int]()
  529. selector.registerEvent(event, 1)
  530. var rc = selector.select(1500)
  531. if len(rc) == 1:
  532. inc(counter)
  533. selector.unregister(event)
  534. assert(selector.isEmpty())
  535. proc mt_event_test(): bool =
  536. var thr: array[0..7, Thread[SelectEvent]]
  537. var event = newSelectEvent()
  538. for i in 0..high(thr):
  539. createThread(thr[i], event_wait_thread, event)
  540. event.trigger()
  541. joinThreads(thr)
  542. assert(counter == 1)
  543. result = true
  544. processTest("Socket notification test...", socket_notification_test())
  545. processTest("User event notification test...", event_notification_test())
  546. when hasThreadSupport:
  547. processTest("Multithreaded user event notification test...",
  548. mt_event_test())
  549. echo("All tests passed!")