Guest User

Untitled

a guest
Jul 16th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.90 KB | None | 0 0
  1. Index: rz_restrictions.net_testwillblock.py
  2. ===================================================================
  3. --- rz_restrictions.net_testwillblock.py (revision 3131)
  4. +++ rz_restrictions.net_testwillblock.py (working copy)
  5. @@ -1,8 +1,6 @@
  6. """
  7. We want to test that willblock's behavior is sane.
  8. - Namely:
  9. - 1) Prior to any writes, recv() should block and send() should not
  10. - 2) If we write, then recv() should not block
  11. + Namely prior to any writes, recv() should block and send() should not
  12. """
  13.  
  14. # What port should we use?
  15. @@ -37,51 +35,20 @@
  16. if write_will_block:
  17. print "Server write shouldn't block! We haven't filled the buffer!"
  18.  
  19. -
  20. - # Lets get some large amount of random data to exhaust the buffers
  21. - data = "Random data. This is junk."
  22. - data = data * 45000
  23. -
  24. -
  25. - # Lets try to send all this to the client
  26. - server_sent = server_sock.send(data)
  27. -
  28. - # Make sure we sent something...
  29. - if server_sent == 0:
  30. - print "Failed to send any data with an empty buffer!"
  31. - elif server_sent < 4096:
  32. - print "Only send 1 page of data! This seems too low for an empty buffer."
  33. -
  34. - # The client's read should NOT block now
  35. - read_will_block, write_will_block = client_sock.willblock()
  36. - if read_will_block:
  37. - print "Client read should not block! There should be available data!"
  38. -
  39. -
  40. - # Now, we will read 4096 bytes from the client, and this should unblock the server's send
  41. - client_read = client_sock.recv(4096)
  42. -
  43. - # The client's read should still not block, there should be more data
  44. - read_will_block, write_will_block = client_sock.willblock()
  45. - if read_will_block:
  46. - print "Client read (2) should not block! There should be more available data!"
  47. -
  48. -
  49. - # Try to send again from the server
  50. - server_sent_2 = server_sock.send(data)
  51. -
  52. - if server_sent_2 == 0:
  53. - print "Failed to send any data with a mostly full buffer! Should be room though."
  54. - elif server_sent_2 < 4096:
  55. - print "Was able to send less than 4096 bytes. Sent: " + str(server_sent_2)
  56. -
  57. server_sock.close()
  58.  
  59. # Done now
  60. DONE_LOCK.release()
  61.  
  62.  
  63. +def timeout():
  64. + print "Test timed out!"
  65. + exitall()
  66. +
  67. def main():
  68. + # Setup a timeout
  69. + timeh = settimer(10,timeout,())
  70. +
  71. # Get our ip
  72. ip = getmyip()
  73.  
  74. @@ -99,6 +66,7 @@
  75. DONE_LOCK.acquire()
  76.  
  77. # Cleanup
  78. + canceltimer(timeh)
  79. stopcomm(waith)
  80. client_sock.close()
  81.  
  82. Index: rz_restrictions.net_testwillblocksend.py
  83. ===================================================================
  84. --- rz_restrictions.net_testwillblocksend.py (revision 0)
  85. +++ rz_restrictions.net_testwillblocksend.py (revision 0)
  86. @@ -0,0 +1,93 @@
  87. +"""
  88. + We want to test that willblock's behavior is sane.
  89. + Namely if we write, then recv() should not block
  90. +"""
  91. +
  92. +# What port should we use?
  93. +PORT = <connport>
  94. +
  95. +# Get a lock and initialize it to being locked.
  96. +# Once we have connected to the waitforconn, we will release CONNECTED_LOCK
  97. +# Once we are done processing, we will release the DONE_LOCK
  98. +CONNECTED_LOCK = getlock()
  99. +DONE_LOCK = getlock()
  100. +if callfunc == "initialize":
  101. + CONNECTED_LOCK.acquire()
  102. + DONE_LOCK.acquire()
  103. +
  104. +# Incoming connection handler
  105. +def incoming(ip, port, server_sock, ch1,ch2):
  106. + # Get the client socket
  107. + CONNECTED_LOCK.acquire()
  108. + client_sock = mycontext["client"]
  109. +
  110. + # Lets get some large amount of random data to exhaust the buffers
  111. + data = "Random data. This is junk."
  112. + data = data * 45000
  113. +
  114. + # Lets try to send all this to the client
  115. + server_sent = server_sock.send(data)
  116. +
  117. + # Make sure we sent something...
  118. + if server_sent == 0:
  119. + print "Failed to send any data with an empty buffer!"
  120. + elif server_sent < 4096:
  121. + print "Only send 1 page of data! This seems too low for an empty buffer."
  122. + elif server_sent == len(data):
  123. + print "Sent all the data! This should have been more than the buffer!"
  124. +
  125. + # The client's read should NOT block now
  126. + read_will_block, write_will_block = client_sock.willblock()
  127. + if read_will_block:
  128. + print "Client read should not block! There should be available data!"
  129. +
  130. +
  131. + # Now, we will read 4096 bytes from the client, and this should unblock the server's send
  132. + client_read = client_sock.recv(4096)
  133. +
  134. + # The client's read should still not block, there should be more data
  135. + read_will_block, write_will_block = client_sock.willblock()
  136. + if read_will_block:
  137. + print "Client read (2) should not block! There should be more available data!"
  138. +
  139. + server_sock.close()
  140. +
  141. + # Done now
  142. + DONE_LOCK.release()
  143. +
  144. +
  145. +def timeout():
  146. + print "Test timed out!"
  147. + exitall()
  148. +
  149. +def main():
  150. + # Setup a timeout
  151. + timeh = settimer(10,timeout,())
  152. +
  153. + # Get our ip
  154. + ip = getmyip()
  155. +
  156. + # Setup a waitforconn
  157. + waith = waitforconn(ip, PORT, incoming)
  158. +
  159. + # Get the client socket
  160. + client_sock = openconn(ip, PORT)
  161. +
  162. + # Share the client socket and enable
  163. + mycontext["client"] = client_sock
  164. +
  165. + # Wait until we are done
  166. + CONNECTED_LOCK.release()
  167. + DONE_LOCK.acquire()
  168. +
  169. + # Cleanup
  170. + canceltimer(timeh)
  171. + stopcomm(waith)
  172. + client_sock.close()
  173. +
  174. +
  175. +# Call the main function
  176. +if callfunc == "initialize":
  177. + main()
  178. +
  179. +
  180. Index: rz_restrictions.net_testwillblockrecv.py
  181. ===================================================================
  182. --- rz_restrictions.net_testwillblockrecv.py (revision 0)
  183. +++ rz_restrictions.net_testwillblockrecv.py (revision 0)
  184. @@ -0,0 +1,95 @@
  185. +"""
  186. + We want to test that willblock's behavior is sane.
  187. + If we recv() from a full buffer, we should unblock send() for our partner.
  188. +"""
  189. +
  190. +# What port should we use?
  191. +PORT = <connport>
  192. +
  193. +# Get a lock and initialize it to being locked.
  194. +# Once we have connected to the waitforconn, we will release CONNECTED_LOCK
  195. +# Once we are done processing, we will release the DONE_LOCK
  196. +CONNECTED_LOCK = getlock()
  197. +DONE_LOCK = getlock()
  198. +if callfunc == "initialize":
  199. + CONNECTED_LOCK.acquire()
  200. + DONE_LOCK.acquire()
  201. +
  202. +# Incoming connection handler
  203. +def incoming(ip, port, server_sock, ch1,ch2):
  204. + # Get the client socket
  205. + CONNECTED_LOCK.acquire()
  206. + client_sock = mycontext["client"]
  207. +
  208. + # Lets get some large amount of random data to exhaust the buffers
  209. + data = "Random data. This is junk."
  210. + data = data * 45000
  211. +
  212. + # Lets try to send all this to the client, loop until we would block
  213. + wblock = False
  214. + sent = 1
  215. + while not wblock and sent > 0:
  216. + sent = server_sock.send(data)
  217. + sleep(0.04)
  218. + rblock,wblock = server_sock.willblock()
  219. +
  220. +
  221. + # Now, we will read 4096 bytes from the client, and this should unblock the server's send
  222. + client_read = client_sock.recv(4096)
  223. + client_sock.send("read") # Sending some data should force us to ACK the received data
  224. + sleep(0.1)
  225. +
  226. + # Check the server socket now
  227. + rblock,wblock = server_sock.willblock()
  228. + if wblock:
  229. + print "Write should not block since the client has read some data!"
  230. +
  231. + # Try to send again from the server
  232. + server_sent_2 = server_sock.send(data)
  233. +
  234. + if server_sent_2 == 0:
  235. + print "Failed to send any data with a mostly full buffer! Should be room though."
  236. + elif server_sent_2 < 4096:
  237. + print "Was able to send less than 4096 bytes. Sent: " + str(server_sent_2)
  238. +
  239. + server_sock.close()
  240. +
  241. + # Done now
  242. + DONE_LOCK.release()
  243. +
  244. +
  245. +def timeout():
  246. + print "Test timed out!"
  247. + exitall()
  248. +
  249. +def main():
  250. + # Setup a timeout
  251. + timeh = settimer(10,timeout,())
  252. +
  253. + # Get our ip
  254. + ip = getmyip()
  255. +
  256. + # Setup a waitforconn
  257. + waith = waitforconn(ip, PORT, incoming)
  258. +
  259. + # Get the client socket
  260. + client_sock = openconn(ip, PORT)
  261. +
  262. + # Share the client socket and enable
  263. + mycontext["client"] = client_sock
  264. +
  265. + # Wait until we are done
  266. + CONNECTED_LOCK.release()
  267. + DONE_LOCK.acquire()
  268. +
  269. + # Cleanup
  270. + canceltimer(timeh)
  271. + stopcomm(waith)
  272. + client_sock.close()
  273. +
  274. +
  275. +# Call the main function
  276. +if callfunc == "initialize":
  277. + main()
  278. +
  279. +
Add Comment
Please, Sign In to add comment