import socket
import time
from unittest import TestCase
from PluginManager import PluginManager

class TestPluginManager(TestCase):
    def test_stop(self):
        class Plugin:
            def __init__(self):
                sock = socket.socket()
                sock.bind(('', 0)) # bind to any available port
                self._port = sock.getsockname()[1]
                sock.close()
            def get_port(self):
                return self._port
        plugin_manager = PluginManager(Plugin(), lambda: None)
        plugin_manager.start()
        time.sleep(0.01)
        plugin_manager.stop()
        plugin_manager.join()
        self.assertFalse(plugin_manager.is_alive())


@@ -19,16 +19,44 @@
 rt time%0A
+from threading import Event%0A
 from uni
@@ -153,16 +153,17 @@
 tCase):%0A
+%0A
     def 
@@ -416,16 +416,58 @@
 close()%0A
+                self.run_called = Event()%0A
         
@@ -520,24 +520,139 @@
  self._port%0A
+            def run(self, sock, address, session):%0A                self.run_called.set()%0A        plugin = Plugin()%0A
         plug
@@ -678,24 +678,22 @@
 Manager(
-P
+p
 lugin
-()
 , lambda
@@ -756,16 +756,234 @@
 p(0.01)%0A
+        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)%0A        sock.connect(('localhost', plugin.get_port()))%0A        sock.close()%0A        time.sleep(0.01)%0A        self.assertTrue(plugin.run_called.is_set())%0A
         

Improve PluginManager test coverage