@@ -419,16 +419,13 @@ def test_getfqdn(self):
419419 pass
420420
421421 def test_cp5814 (self ):
422- global EXIT_CODE
423- global HAS_EXITED
424422 EXIT_CODE = - 1
425423 HAS_EXITED = False
426424
427425 portFile = os .path .join (self .temporary_dir , "cp5814port_%d" % os .getpid ())
428426
429427 #Server code
430428 server = """
431- from time import sleep
432429import _socket
433430import os
434431
@@ -454,21 +451,21 @@ def test_cp5814(self):
454451 #Verifications
455452 if not addr[0] in [HOST, '127.0.0.1']:
456453 raise Exception('The address, %s, was unexpected' % str(addr))
457- if data!=b'stuff':
458- raise Exception('%s!=stuff' % str(data))
459- sleep(10)
454+ if data != b'stuff':
455+ raise Exception('%s != stuff' % str(data))
460456
461457finally:
462458 conn.close()
459+ s.close()
463460 try:
464461 os.remove(r"{PORTFILE}")
465462 except:
466463 pass
467464""" .format (PORTFILE = portFile )
468465 #Spawn off a thread to startup the server
469466 def server_thread ():
470- global EXIT_CODE
471- global HAS_EXITED
467+ nonlocal EXIT_CODE
468+ nonlocal HAS_EXITED
472469 serverFile = os .path .join (self .temporary_dir , "cp5814server_%d.py" % os .getpid ())
473470 self .write_to_file (serverFile , server )
474471 EXIT_CODE = os .system ('"%s" %s' %
@@ -484,7 +481,7 @@ def server_thread():
484481 portex = None
485482 startTime = time .perf_counter ()
486483 for _ in range (20 ):
487- time .sleep (1 )
484+ time .sleep (0.5 )
488485 if EXIT_CODE > 0 :
489486 self .fail ("Server died with exit code %d" % EXIT_CODE )
490487 try :
@@ -506,13 +503,14 @@ def server_thread():
506503 s .close ()
507504
508505 #Ensure the server didn't die
509- for i in range (100 ):
510- if not HAS_EXITED :
511- print ("*" , end = "" )
512- time .sleep (1 )
513- else :
506+ for _ in range (100 ):
507+ if HAS_EXITED :
514508 self .assertEqual (EXIT_CODE , 0 )
515509 break
510+
511+ print ("*" , end = "" )
512+ time .sleep (0.5 )
513+
516514 self .assertTrue (HAS_EXITED )
517515
518516 #Verification
@@ -526,29 +524,38 @@ def server_thread():
526524
527525class SocketMakefileTest (IronPythonTestCase ):
528526 def test_misc (self ):
529- f = socket .socket ().makefile ()
527+ s = socket .socket ()
528+ f = s .makefile ()
530529 f .bufsize = 4096
531530 self .assertEqual (4096 , f .bufsize )
531+ f .close ()
532+ s .close ()
532533
533534 def test_makefile_refcount (self ):
534535 "Ensures that the _socket stays open while there's still a file associated"
535536
536- global PORT
537+ GPORT = None
537538 def echoer ():
538- global PORT
539+ nonlocal GPORT
539540 s = socket .socket ()
540541 s .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEADDR , 1 ) # prevents an "Address already in use" error when the socket is in a TIME_WAIT state
541542 s .settimeout (15 ) # prevents the server from staying open if the client never connects
542543 s .bind (('localhost' , 0 ))
543- PORT = s .getsockname ()[1 ]
544+ GPORT = s .getsockname ()[1 ]
544545 s .listen (5 )
545- (s2 , ignore ) = s .accept ()
546+ (s2 , _ ) = s .accept ()
546547 s2 .send (s2 .recv (10 ))
548+ s2 .close ()
549+ s .close ()
547550
548551 _thread .start_new_thread (echoer , ())
549- time .sleep (1 )
552+ for _ in range (20 ):
553+ time .sleep (0.5 )
554+ if GPORT is not None :
555+ break
556+
550557 s = socket .socket ()
551- s .connect (('localhost' , PORT ))
558+ s .connect (('localhost' , GPORT ))
552559 f1 = s .makefile ('r' )
553560 f2 = s .makefile ('w' )
554561 s .close ()
@@ -558,17 +565,17 @@ def echoer():
558565 str = f1 .readline ()
559566 self .assertEqual (str , test_msg )
560567
568+ f2 .close ()
569+ f1 .close ()
570+
561571 def test_cp7451 (self ):
562- global EXIT_CODE
563- global HAS_EXITED
564572 EXIT_CODE = - 1
565573 HAS_EXITED = False
566574
567575 portFile = os .path .join (self .temporary_dir , "cp7451port_%d" % os .getpid ())
568576
569577 #Server code
570578 server = """
571- from time import sleep
572579import socket as _socket
573580import os
574581
@@ -593,21 +600,21 @@ def test_cp7451(self):
593600 #Verifications
594601 if not addr[0] in [HOST, '127.0.0.1']:
595602 raise Exception('The address, %s, was unexpected' % str(addr))
596- if data!=b'stuff2':
597- raise Exception('%s!=stuff2' % str(data))
598- sleep(10)
603+ if data != b'stuff2':
604+ raise Exception('%s != stuff2' % str(data))
599605
600606finally:
601607 conn.close()
608+ s.close()
602609 try:
603610 os.remove(r"{PORTFILE}")
604611 except:
605612 pass
606613""" .format (PORTFILE = portFile )
607614 #Spawn off a thread to startup the server
608615 def server_thread ():
609- global EXIT_CODE
610- global HAS_EXITED
616+ nonlocal EXIT_CODE
617+ nonlocal HAS_EXITED
611618 serverFile = os .path .join (self .temporary_dir , "cp7451server_%d.py" % os .getpid ())
612619 self .write_to_file (serverFile , server )
613620 EXIT_CODE = os .system ('"%s" %s' %
@@ -623,7 +630,7 @@ def server_thread():
623630 portex = None
624631 startTime = time .perf_counter ()
625632 for _ in range (20 ):
626- time .sleep (1 )
633+ time .sleep (0.5 )
627634 if EXIT_CODE > 0 :
628635 self .fail ("Server died with exit code %d" % EXIT_CODE )
629636 try :
@@ -645,16 +652,19 @@ def server_thread():
645652 s .close ()
646653
647654 #Ensure the server didn't die
648- for i in range (100 ):
649- if not HAS_EXITED :
650- print ("*" , end = "" )
651- time .sleep (1 )
652- else :
655+ for _ in range (100 ):
656+ if HAS_EXITED :
653657 self .assertEqual (EXIT_CODE , 0 )
654658 break
659+
660+ print ("*" , end = "" )
661+ time .sleep (0.5 )
662+
655663 self .assertTrue (HAS_EXITED )
656664
657665 #Verification
658666 self .assertEqual (f .read (6 ), "stuff2" )
659667
668+ f .close ()
669+
660670run_test (__name__ )
0 commit comments