Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: rz_restrictions.net_testwillblock.py
- ===================================================================
- --- rz_restrictions.net_testwillblock.py (revision 3131)
- +++ rz_restrictions.net_testwillblock.py (working copy)
- @@ -1,8 +1,6 @@
- """
- We want to test that willblock's behavior is sane.
- - Namely:
- - 1) Prior to any writes, recv() should block and send() should not
- - 2) If we write, then recv() should not block
- + Namely prior to any writes, recv() should block and send() should not
- """
- # What port should we use?
- @@ -37,51 +35,20 @@
- if write_will_block:
- print "Server write shouldn't block! We haven't filled the buffer!"
- -
- - # Lets get some large amount of random data to exhaust the buffers
- - data = "Random data. This is junk."
- - data = data * 45000
- -
- -
- - # Lets try to send all this to the client
- - server_sent = server_sock.send(data)
- -
- - # Make sure we sent something...
- - if server_sent == 0:
- - print "Failed to send any data with an empty buffer!"
- - elif server_sent < 4096:
- - print "Only send 1 page of data! This seems too low for an empty buffer."
- -
- - # The client's read should NOT block now
- - read_will_block, write_will_block = client_sock.willblock()
- - if read_will_block:
- - print "Client read should not block! There should be available data!"
- -
- -
- - # Now, we will read 4096 bytes from the client, and this should unblock the server's send
- - client_read = client_sock.recv(4096)
- -
- - # The client's read should still not block, there should be more data
- - read_will_block, write_will_block = client_sock.willblock()
- - if read_will_block:
- - print "Client read (2) should not block! There should be more available data!"
- -
- -
- - # Try to send again from the server
- - server_sent_2 = server_sock.send(data)
- -
- - if server_sent_2 == 0:
- - print "Failed to send any data with a mostly full buffer! Should be room though."
- - elif server_sent_2 < 4096:
- - print "Was able to send less than 4096 bytes. Sent: " + str(server_sent_2)
- -
- server_sock.close()
- # Done now
- DONE_LOCK.release()
- +def timeout():
- + print "Test timed out!"
- + exitall()
- +
- def main():
- + # Setup a timeout
- + timeh = settimer(10,timeout,())
- +
- # Get our ip
- ip = getmyip()
- @@ -99,6 +66,7 @@
- DONE_LOCK.acquire()
- # Cleanup
- + canceltimer(timeh)
- stopcomm(waith)
- client_sock.close()
- Index: rz_restrictions.net_testwillblocksend.py
- ===================================================================
- --- rz_restrictions.net_testwillblocksend.py (revision 0)
- +++ rz_restrictions.net_testwillblocksend.py (revision 0)
- @@ -0,0 +1,93 @@
- +"""
- + We want to test that willblock's behavior is sane.
- + Namely if we write, then recv() should not block
- +"""
- +
- +# What port should we use?
- +PORT = <connport>
- +
- +# Get a lock and initialize it to being locked.
- +# Once we have connected to the waitforconn, we will release CONNECTED_LOCK
- +# Once we are done processing, we will release the DONE_LOCK
- +CONNECTED_LOCK = getlock()
- +DONE_LOCK = getlock()
- +if callfunc == "initialize":
- + CONNECTED_LOCK.acquire()
- + DONE_LOCK.acquire()
- +
- +# Incoming connection handler
- +def incoming(ip, port, server_sock, ch1,ch2):
- + # Get the client socket
- + CONNECTED_LOCK.acquire()
- + client_sock = mycontext["client"]
- +
- + # Lets get some large amount of random data to exhaust the buffers
- + data = "Random data. This is junk."
- + data = data * 45000
- +
- + # Lets try to send all this to the client
- + server_sent = server_sock.send(data)
- +
- + # Make sure we sent something...
- + if server_sent == 0:
- + print "Failed to send any data with an empty buffer!"
- + elif server_sent < 4096:
- + print "Only send 1 page of data! This seems too low for an empty buffer."
- + elif server_sent == len(data):
- + print "Sent all the data! This should have been more than the buffer!"
- +
- + # The client's read should NOT block now
- + read_will_block, write_will_block = client_sock.willblock()
- + if read_will_block:
- + print "Client read should not block! There should be available data!"
- +
- +
- + # Now, we will read 4096 bytes from the client, and this should unblock the server's send
- + client_read = client_sock.recv(4096)
- +
- + # The client's read should still not block, there should be more data
- + read_will_block, write_will_block = client_sock.willblock()
- + if read_will_block:
- + print "Client read (2) should not block! There should be more available data!"
- +
- + server_sock.close()
- +
- + # Done now
- + DONE_LOCK.release()
- +
- +
- +def timeout():
- + print "Test timed out!"
- + exitall()
- +
- +def main():
- + # Setup a timeout
- + timeh = settimer(10,timeout,())
- +
- + # Get our ip
- + ip = getmyip()
- +
- + # Setup a waitforconn
- + waith = waitforconn(ip, PORT, incoming)
- +
- + # Get the client socket
- + client_sock = openconn(ip, PORT)
- +
- + # Share the client socket and enable
- + mycontext["client"] = client_sock
- +
- + # Wait until we are done
- + CONNECTED_LOCK.release()
- + DONE_LOCK.acquire()
- +
- + # Cleanup
- + canceltimer(timeh)
- + stopcomm(waith)
- + client_sock.close()
- +
- +
- +# Call the main function
- +if callfunc == "initialize":
- + main()
- +
- +
- Index: rz_restrictions.net_testwillblockrecv.py
- ===================================================================
- --- rz_restrictions.net_testwillblockrecv.py (revision 0)
- +++ rz_restrictions.net_testwillblockrecv.py (revision 0)
- @@ -0,0 +1,95 @@
- +"""
- + We want to test that willblock's behavior is sane.
- + If we recv() from a full buffer, we should unblock send() for our partner.
- +"""
- +
- +# What port should we use?
- +PORT = <connport>
- +
- +# Get a lock and initialize it to being locked.
- +# Once we have connected to the waitforconn, we will release CONNECTED_LOCK
- +# Once we are done processing, we will release the DONE_LOCK
- +CONNECTED_LOCK = getlock()
- +DONE_LOCK = getlock()
- +if callfunc == "initialize":
- + CONNECTED_LOCK.acquire()
- + DONE_LOCK.acquire()
- +
- +# Incoming connection handler
- +def incoming(ip, port, server_sock, ch1,ch2):
- + # Get the client socket
- + CONNECTED_LOCK.acquire()
- + client_sock = mycontext["client"]
- +
- + # Lets get some large amount of random data to exhaust the buffers
- + data = "Random data. This is junk."
- + data = data * 45000
- +
- + # Lets try to send all this to the client, loop until we would block
- + wblock = False
- + sent = 1
- + while not wblock and sent > 0:
- + sent = server_sock.send(data)
- + sleep(0.04)
- + rblock,wblock = server_sock.willblock()
- +
- +
- + # Now, we will read 4096 bytes from the client, and this should unblock the server's send
- + client_read = client_sock.recv(4096)
- + client_sock.send("read") # Sending some data should force us to ACK the received data
- + sleep(0.1)
- +
- + # Check the server socket now
- + rblock,wblock = server_sock.willblock()
- + if wblock:
- + print "Write should not block since the client has read some data!"
- +
- + # Try to send again from the server
- + server_sent_2 = server_sock.send(data)
- +
- + if server_sent_2 == 0:
- + print "Failed to send any data with a mostly full buffer! Should be room though."
- + elif server_sent_2 < 4096:
- + print "Was able to send less than 4096 bytes. Sent: " + str(server_sent_2)
- +
- + server_sock.close()
- +
- + # Done now
- + DONE_LOCK.release()
- +
- +
- +def timeout():
- + print "Test timed out!"
- + exitall()
- +
- +def main():
- + # Setup a timeout
- + timeh = settimer(10,timeout,())
- +
- + # Get our ip
- + ip = getmyip()
- +
- + # Setup a waitforconn
- + waith = waitforconn(ip, PORT, incoming)
- +
- + # Get the client socket
- + client_sock = openconn(ip, PORT)
- +
- + # Share the client socket and enable
- + mycontext["client"] = client_sock
- +
- + # Wait until we are done
- + CONNECTED_LOCK.release()
- + DONE_LOCK.acquire()
- +
- + # Cleanup
- + canceltimer(timeh)
- + stopcomm(waith)
- + client_sock.close()
- +
- +
- +# Call the main function
- +if callfunc == "initialize":
- + main()
- +
- +
Add Comment
Please, Sign In to add comment