def test_websocketrails_run(self):
        global foo

        def fail(ev):
            global foo
            foo = ev

        ev = websocket_rails.Event(
            ['testname', {
                'id': 1,
                'data': 1,
                'success': False
            }],
            failure_cb=fail)
        self.ws.queue.update({ev.id: ev})
        self.ws._run(ev)
        self.assertEqual(foo, ev.data)

        ev = websocket_rails.Event(['testname', {'id': 16, 'data': 2}])
        self.ws.queue.update({ev.id: ev})
        self.ws._run(ev)
        self.assertNotEqual(foo, ev.data)

        ev = websocket_rails.Event(
            ['testname', {
                'id': 1,
                'data': 3,
                'success': True
            }],
            success_cb=None)
        self.ws.queue.update({ev.id: ev})
        self.ws._run(ev)
        self.assertNotEqual(foo, ev.data)
    def test005_channel(self):
        ws = websocket_rails.WebsocketRails(self.URL)
        self.conn._mock_message(
            websocket_rails.Event.simple('client_connected',
                                         {'connection_id': 15}))
        ws.run()

        name = 'test'
        ws.channel(name).subscribe()
        ev = websocket_rails.Event(self.conn._get_message())
        self.assertTrue(ev.name == 'websocket_rails.subscribe')
        self.assertTrue(ev.data.get('channel') == name)

        self.temp = None

        def _action(data):
            self.temp = data

        # Send message to channel
        chan = ws.channel(name)
        chan.actions.update({'action': _action})
        ev2 = websocket_rails.Event.simple('action', {'foobar': 'test'})
        chan.send(ev2)

        msg = self.conn._get_message()

        ev3 = websocket_rails.Event(msg)
        self.assertTrue(ev3.channel == name)
        self.assertTrue(ev2.name == ev3.name)
        self.assertTrue(ev2.data == ev3.data)
        self.assertTrue(ev2.id == ev3.id)

        # Received a message from channel (pretend not from us)
        del ws.queue[ev3.id]
        self.conn._mock_message(ev3)
        ws.run()
        self.assertTrue(self.temp)
        self.assertTrue(self.temp.get('foobar') == 'test')

        self.assertTrue(len(self.conn.messages_out) == 0)
        self.conn.fail = True
        ws.run()
        self.assertTrue(len(self.conn.messages_out) == 0)
        self.conn.fail = False
        ws.run()
        self.assertTrue(len(self.conn.messages_out) == 1)

        ev = websocket_rails.Event(self.conn._get_message())
        self.assertTrue(ev.name == 'websocket_rails.subscribe')
        self.assertTrue(ev.data.get('channel') == name)

        chan.unsubscribe()
        ev = websocket_rails.Event(self.conn._get_message())
        self.assertTrue(ev.name == 'websocket_rails.unsubscribe')
        self.assertTrue(ev.data.get('channel') == name)
    def test006_thread(self):
        ws = websocket_rails.WebsocketRails(self.URL)
        ws.start()
        thread = ws.thread
        self.assertTrue(ws.running)
        ws.start()
        self.assertTrue(ws.thread == thread)
        self.assertTrue(ws.running)
        ev = websocket_rails.Event.simple('client_connected',
                                          {'connection_id': 15})
        self.conn._mock_message(ev)
        time.sleep(0.1)
        self.assertTrue(ws.conn_id == 15)

        ev = websocket_rails.Event.simple('websocket_rails.ping', {})
        self.conn._mock_message(ev)
        time.sleep(0.1)
        msg = self.conn._get_message()
        ev2 = websocket_rails.Event(msg)
        self.assertTrue(ev2.name == 'websocket_rails.pong')
        self.assertTrue(ev2.data['connection_id'] == 15)
        ws.stop()
        self.assertFalse(ws.running)
        ws.stop()
        self.assertFalse(ws.running)
    def test004_run(self):
        ws = websocket_rails.WebsocketRails(self.URL)
        ev = websocket_rails.Event.simple('client_connected',
                                          {'connection_id': 15})
        self.conn._mock_message(ev)
        ws.run()
        self.assertTrue(ws.conn_id == 15)

        ev = websocket_rails.Event.simple('websocket_rails.ping', {})
        self.conn._mock_message(ev)
        ws.run()
        msg = self.conn._get_message()
        ev2 = websocket_rails.Event(msg)
        self.assertTrue(ev2.name == 'websocket_rails.pong')
        self.assertTrue(ev2.data['connection_id'] == 15)
 def test_event_with_no_data_dict(self):
     ev = websocket_rails.Event(['testname', ''])
     self.assertEqual(ev.name, 'testname')