@@ -864,22 +864,97 @@ def f():
864864
865865 self .assertEqual (received , obj )
866866
867+ def test_send_timeout (self ):
868+ obj = b'spam'
869+
870+ with self .subTest ('non-blocking with timeout' ):
871+ cid = channels .create ()
872+ with self .assertRaises (ValueError ):
873+ channels .send (cid , obj , blocking = False , timeout = 0.1 )
874+
875+ with self .subTest ('timeout hit' ):
876+ cid = channels .create ()
877+ with self .assertRaises (TimeoutError ):
878+ channels .send (cid , obj , blocking = True , timeout = 0.1 )
879+ with self .assertRaises (channels .ChannelEmptyError ):
880+ received = channels .recv (cid )
881+ print (repr (received ))
882+
883+ with self .subTest ('timeout not hit' ):
884+ cid = channels .create ()
885+ def f ():
886+ recv_wait (cid )
887+ t = threading .Thread (target = f )
888+ t .start ()
889+ channels .send (cid , obj , blocking = True , timeout = 10 )
890+ t .join ()
891+
892+ def test_send_buffer_timeout (self ):
893+ try :
894+ self ._has_run_once_timeout
895+ except AttributeError :
896+ # At the moment, this test leaks a few references.
897+ # It looks like the leak originates with the addition
898+ # of _channels.send_buffer() (gh-110246), whereas the
899+ # tests were added afterward. We want this test even
900+ # if the refleak isn't fixed yet, so we skip here.
901+ raise unittest .SkipTest ('temporarily skipped due to refleaks' )
902+ else :
903+ self ._has_run_once_timeout = True
904+
905+ obj = bytearray (b'spam' )
906+
907+ with self .subTest ('non-blocking with timeout' ):
908+ cid = channels .create ()
909+ with self .assertRaises (ValueError ):
910+ channels .send_buffer (cid , obj , blocking = False , timeout = 0.1 )
911+
912+ with self .subTest ('timeout hit' ):
913+ cid = channels .create ()
914+ with self .assertRaises (TimeoutError ):
915+ channels .send_buffer (cid , obj , blocking = True , timeout = 0.1 )
916+ with self .assertRaises (channels .ChannelEmptyError ):
917+ received = channels .recv (cid )
918+ print (repr (received ))
919+
920+ with self .subTest ('timeout not hit' ):
921+ cid = channels .create ()
922+ def f ():
923+ recv_wait (cid )
924+ t = threading .Thread (target = f )
925+ t .start ()
926+ channels .send_buffer (cid , obj , blocking = True , timeout = 10 )
927+ t .join ()
928+
867929 def test_send_closed_while_waiting (self ):
868930 obj = b'spam'
869931 wait = self .build_send_waiter (obj )
870- cid = channels .create ()
871- def f ():
872- wait ()
873- channels .close (cid , force = True )
874- t = threading .Thread (target = f )
875- t .start ()
876- with self .assertRaises (channels .ChannelClosedError ):
877- channels .send (cid , obj , blocking = True )
878- t .join ()
932+
933+ with self .subTest ('without timeout' ):
934+ cid = channels .create ()
935+ def f ():
936+ wait ()
937+ channels .close (cid , force = True )
938+ t = threading .Thread (target = f )
939+ t .start ()
940+ with self .assertRaises (channels .ChannelClosedError ):
941+ channels .send (cid , obj , blocking = True )
942+ t .join ()
943+
944+ with self .subTest ('with timeout' ):
945+ cid = channels .create ()
946+ def f ():
947+ wait ()
948+ channels .close (cid , force = True )
949+ t = threading .Thread (target = f )
950+ t .start ()
951+ with self .assertRaises (channels .ChannelClosedError ):
952+ channels .send (cid , obj , blocking = True , timeout = 30 )
953+ t .join ()
879954
880955 def test_send_buffer_closed_while_waiting (self ):
881956 try :
882- self ._has_run_once
957+ self ._has_run_once_closed
883958 except AttributeError :
884959 # At the moment, this test leaks a few references.
885960 # It looks like the leak originates with the addition
@@ -888,19 +963,32 @@ def test_send_buffer_closed_while_waiting(self):
888963 # if the refleak isn't fixed yet, so we skip here.
889964 raise unittest .SkipTest ('temporarily skipped due to refleaks' )
890965 else :
891- self ._has_run_once = True
966+ self ._has_run_once_closed = True
892967
893968 obj = bytearray (b'spam' )
894969 wait = self .build_send_waiter (obj , buffer = True )
895- cid = channels .create ()
896- def f ():
897- wait ()
898- channels .close (cid , force = True )
899- t = threading .Thread (target = f )
900- t .start ()
901- with self .assertRaises (channels .ChannelClosedError ):
902- channels .send_buffer (cid , obj , blocking = True )
903- t .join ()
970+
971+ with self .subTest ('without timeout' ):
972+ cid = channels .create ()
973+ def f ():
974+ wait ()
975+ channels .close (cid , force = True )
976+ t = threading .Thread (target = f )
977+ t .start ()
978+ with self .assertRaises (channels .ChannelClosedError ):
979+ channels .send_buffer (cid , obj , blocking = True )
980+ t .join ()
981+
982+ with self .subTest ('with timeout' ):
983+ cid = channels .create ()
984+ def f ():
985+ wait ()
986+ channels .close (cid , force = True )
987+ t = threading .Thread (target = f )
988+ t .start ()
989+ with self .assertRaises (channels .ChannelClosedError ):
990+ channels .send_buffer (cid , obj , blocking = True , timeout = 30 )
991+ t .join ()
904992
905993 #-------------------
906994 # close
0 commit comments