def test_close_packet_after_started_delay(self):
        self.url.path = b'/client_close/engine.io/'
        engineio, handler = self._mkengineio()

        def _sendclose(event): # pylint: disable=unused-argument
            reactor.callLater(1, engineio.sendeiopacket, EIO_TYPE_CLOSE)

        engineio.register('open', _sendclose)

        yield engineio.start()
        self.assertTrue(engineio.running)

        yield txrc.deferredtimeout(reactor, 10, self.close_d)
        self.assertTrue(self.close_d.called)
        self.assertFalse(engineio.running)
        self.assertIsNone(engineio._transport_context.session_id) # pylint: disable=protected-access

        call_args_list = [ i for i in handler.call_args_list if i[0][0] not in ( 'noop', 'pong' ) ]

        expected = [
            mock.call('open'),
            mock.call('message', b'Hello!'),
            mock.call('close'),
        ]

        self.assertEqual(call_args_list, expected)
    def test_no_deadlock(self):
        self.url.path = b'/client_close/engine.io/'
        engineio, handler = self._mkengineio()

        yield engineio.start()

        ping_ds = []

        for _ in range(10):
            ping_ds.append(engineio.sendeiopacket(EIO_TYPE_PING, 'probe'))

        for ping_d in ping_ds:
            yield txrc.deferredtimeout(reactor, 10, ping_d)

        yield engineio.stop()

        yield txrc.deferredtimeout(reactor, 10, self.close_d)
        self.assertTrue(self.close_d.called)
        self.assertFalse(engineio.running)
        self.assertIsNone(engineio._transport_context.session_id) # pylint: disable=protected-access

        call_args_list = [ i for i in handler.call_args_list if i[0][0] not in ( 'noop', 'pong' ) ]

        expected = [
            mock.call('open'),
        ]

        self.assertEqual(call_args_list[0], expected[0])
    def test_echo_ack(self):
        self.url.path = b'/echo_ack/socket.io/'
        socketio, handler = self._mksocketio()

        yield socketio.start()
        self.assertTrue(socketio.running)

        callback_d = t_defer.Deferred()

        def _callback(path, *args, **kw):
            callback_d.callback(( path, args, kw ))

        yield socketio.emit('msg', 'Hey')
        yield socketio.emit('msg', 'you')
        yield socketio.emit('msg', 'guys!', callback=_callback)
        yield txrc.deferredtimeout(reactor, 5, callback_d)
        self.assertTrue(callback_d.called)
        self.assertEqual(callback_d.result, ( '/', ( [], ), {} ))

        yield socketio.stop()
        yield txrc.deferredtimeout(reactor, 10, self.close_d)
        self.assertTrue(self.close_d.called)
        self.assertFalse(socketio.running)

        self.assertGreaterEqual(len(handler.call_args_list), 4)
        self.assertIn(mock.call('open'), handler.call_args_list)
        self.assertIn(mock.call('connect', '/', ''), handler.call_args_list)
        self.assertIn(mock.call('event', '/', [ 'msg', 'Hey' ]), handler.call_args_list)
        self.assertIn(mock.call('event', '/', [ 'msg', 'you' ]), handler.call_args_list)
        self.assertIn(mock.call('event', '/', [ 'msg', 'guys!' ]), handler.call_args_list)
        self.assertIn(mock.call('ack', '/', []), handler.call_args_list)
        self.assertIn(mock.call('close'), handler.call_args_list)
    def test_hello_delay(self):
        self.url.path = b'/hello_delay/engine.io/'
        engineio, handler = self._mkengineio()

        yield engineio.start()
        self.assertTrue(engineio.running)

        yield txrc.deferredtimeout(reactor, 10, self.close_d)
        self.assertTrue(self.close_d.called)
        self.assertFalse(engineio.running)
        self.assertIsNone(engineio._transport_context.session_id) # pylint: disable=protected-access

        call_args_list = [ i for i in handler.call_args_list if i[0][0] not in ( 'noop', 'pong' ) ]

        expected = [
            mock.call('open'),
            mock.call('message', b'Hello!'),
            mock.call('close'),
        ]

        self.assertEqual(call_args_list, expected)
    def test_close_packet_after_started(self):
        self.url.path = b'/client_close/engine.io/'
        engineio, handler = self._mkengineio()

        yield engineio.start()
        self.assertTrue(engineio.running)

        send_d = engineio.sendeiopacket(EIO_TYPE_CLOSE)
        yield send_d

        yield txrc.deferredtimeout(reactor, 10, self.close_d)
        self.assertTrue(self.close_d.called)
        self.assertFalse(engineio.running)
        self.assertIsNone(engineio._transport_context.session_id) # pylint: disable=protected-access

        call_args_list = [ i for i in handler.call_args_list if i[0][0] not in ( 'noop', 'pong' ) ]

        expected = [
            mock.call('open'),
            mock.call('close'),
        ]

        self.assertEqual(call_args_list[0], expected[0])
        self.assertEqual(call_args_list[-1], expected[-1])
    def test_stop_after_started(self):
        self.url.path = b'/client_close/engine.io/'
        engineio, handler = self._mkengineio()

        yield engineio.start()
        self.assertTrue(engineio.running)

        engineio.stop()
        self.assertRaises(TransportStateError, engineio.stop)

        yield txrc.deferredtimeout(reactor, 10, self.close_d)
        self.assertTrue(self.close_d.called)
        self.assertFalse(engineio.running)
        self.assertIsNone(engineio._transport_context.session_id) # pylint: disable=protected-access

        call_args_list = [ i for i in handler.call_args_list if i[0][0] not in ( 'noop', 'pong' ) ]

        expected = [
            mock.call('open'),
        ]

        self.assertEqual(call_args_list[0], expected[0])