def test_throws_error_when_cannot_connect(self):
        def on_connect(future):
            self.assertRaises(ConnectError, future.get)
            self.stop()

        connector = Connector('localhost', 60000, io_loop=self.io_loop)
        deferred = connector.connect()
        deferred.add_callback(stack_context.wrap(on_connect))
        self.wait()
    def test_throws_timeout_error_when_connection_timeout(self):
        def on_connect(future):
            self.assertRaises(TimeoutError, future.get)
            self.stop()

        with serve(60000):
            connector = Connector('localhost', 60000, timeout=0.000001, io_loop=self.io_loop)
            deferred = connector.connect()
            deferred.add_callback(stack_context.wrap(on_connect))
            self.wait()
    def test_connect(self):
        def on_connect(future):
            stream = future.get()
            self.assertIsInstance(stream, IOStream)
            self.stop()

        with serve(60000):
            connector = Connector('localhost', 60000, io_loop=self.io_loop)
            deferred = connector.connect()
            deferred.add_callback(stack_context.wrap(on_connect))
            self.wait()
    def test_allow_multiple_invocation_of_connect_method(self):
        def on_connect(future):
            stream = future.get()
            self.assertIsInstance(stream, IOStream)
            self.stop()

        with serve(60000):
            connector = Connector('localhost', 60000, io_loop=self.io_loop)
            deferred = connector.connect()
            deferred_2 = connector.connect()
            deferred_3 = connector.connect()
            deferred.add_callback(stack_context.wrap(on_connect))
            self.assertEqual(deferred, deferred_2)
            self.assertEqual(deferred, deferred_3)
            self.wait()
    def test_consequentially_connect(self):
        def on_connect(future):
            stream = future.get()
            self.assertIsInstance(stream, IOStream)
            self.stop()

        with serve(60000):
            when(socket).getaddrinfo('localhost', 60000, 0, socket.SOCK_STREAM).thenReturn([
                (30, 1, 6, '', ('::1', 59999, 0, 0)),
                (30, 1, 6, '', ('::1', 60000, 0, 0))
            ])

            connector = Connector('localhost', 60000, io_loop=self.io_loop)
            deferred = connector.connect()
            deferred.add_callback(stack_context.wrap(on_connect))
            self.wait()