'Sets up imap_handler for tests. imap_handler should inherit from either:
- SimpleIMAPHandler - for testing IMAP commands,
- socketserver.StreamRequestHandler - if raw access to stream is needed.
Returns (client, server).'

def _setup(self, imap_handler, connect=True):

    class TestTCPServer(self.server_class, ):
        def handle_error(self, request, client_address):
            '\n                                                                End    request    and    raise    the    error    if    one    occurs.\n                                                                '
            self.close_request(request)
            self.server_close()
            raise
    self.addCleanup(self._cleanup)
    self.server = self.server_class((support.HOST, 0), imap_handler)
    self.thread = threading.Thread(name=(self._testMethodName + '-server'), target=self.server.serve_forever, kwargs={'poll_interval': 0.01})
    self.thread.daemon = True
    self.thread.start()
    if connect:
        self.client = self.imap_class(*self.server.server_address)
    return (self.client, self.server)
