예제 #1
0
    def test_custom_json_dumps_callable(self):
        class CustomJSONEncoder(json.JSONEncoder):
            def default(self, o):
                if isinstance(o, datetime.date):
                    return o.isoformat()
                return super(CustomJSONEncoder, self).default(o)

        def custom_json_dumps(data):
            return json.dumps(data, cls=CustomJSONEncoder)

        self.announce = Announce(_test_mode=True,
                                 namespace='namespace',
                                 json_dumps=custom_json_dumps)

        expected = {
            'nodeId':
            149241983,
            'args': [
                '/namespace/room',
                '5::/namespace:{"name":"message","args":[{"datetime":"2014-02-24T18:01:53.395214"}]}',
                None, []
            ]
        }

        data = {'datetime': datetime.datetime(2014, 2, 24, 18, 1, 53, 395214)}

        result_text = self.announce.emit('message', data, room='room')

        self.compare_payloads(expected, result_text)
        self.check_json_payload(expected, result_text)
예제 #2
0
    def announce(self, method):
        """
        Announces to SocketIO Redis store that a model has changed.
        Includes the model name as a data_type, method, and a serialized
        representation of the model instance.
        """
        try:
            room = self.announce_room
        except AttributeError as e:
            logger.exception(e)
            return None

        data = {"data_type": self.__class__.__name__.lower(), "method": method, "data": self.to_dict()}

        redis_configuration = {}

        if not settings.ANNOUNCE_TEST_MODE:
            redis_url = urlparse.urlparse(settings.BOARDS_SOCKETS_REDIS_URL)

            redis_configuration.update(
                {
                    "host": redis_url.hostname,
                    "password": redis_url.password,
                    "port": int(redis_url.port) if redis_url.port else 6379,
                    "db": int(redis_url.path[1:]) if redis_url.path[1:] else 0,
                }
            )

        announce = Announce(json_dumps=json_renderer, _test_mode=settings.ANNOUNCE_TEST_MODE, **redis_configuration)

        try:
            announce.emit("message", data, room=room)
        except Exception as e:
            logger.exception(e)
예제 #3
0
    def test_send_in_namespace(self):
        self.announce = Announce(_test_mode=True, namespace='namespace')

        expected = {
            'nodeId': 149241983,
            'args': ['/namespace', '3::/namespace:Hello world!', None, []]
        }

        result_text = self.announce.send('Hello world!')

        self.compare_payloads(expected, result_text)
예제 #4
0
    def test_custom_json_dumps_callable(self):

        class CustomJSONEncoder(json.JSONEncoder):
            def default(self, o):
                if isinstance(o, datetime.date):
                    return o.isoformat()
                return super(CustomJSONEncoder, self).default(o)

        def custom_json_dumps(data):
            return json.dumps(data, cls=CustomJSONEncoder)

        self.announce = Announce(_test_mode=True, namespace='namespace',
                                 json_dumps=custom_json_dumps)

        expected = {
            'nodeId': 149241983,
            'args': [
                '/namespace/room',
                '5::/namespace:{"name":"message","args":[{"datetime":"2014-02-24T18:01:53.395214"}]}',
                None,
                []
            ]
        }

        data = {
            'datetime': datetime.datetime(2014, 2, 24, 18, 1, 53, 395214)
        }

        result_text = self.announce.emit('message', data, room='room')

        self.compare_payloads(expected, result_text)
        self.check_json_payload(expected, result_text)
예제 #5
0
    def test_emit_in_namespace(self):
        self.announce = Announce(_test_mode=True, namespace='namespace')

        expected = {
            'nodeId':
            149241983,
            'args': [
                '/namespace',
                '5::/namespace:{"name":"message","args":[{"user":"******"}]}',
                None, []
            ]
        }

        result_text = self.announce.emit('message', {'user': '******'})

        self.compare_payloads(expected, result_text)
        self.check_json_payload(expected, result_text)
예제 #6
0
    def announce(self, method):
        """
        Announces to SocketIO Redis store that a model has changed.
        Includes the model name as a data_type, method, and a serialized
        representation of the model instance.
        """
        try:
            room = self.announce_room
        except AttributeError as e:
            logger.exception(e)
            return None

        data = {
            'data_type': self.__class__.__name__.lower(),
            'method': method,
            'data': self.to_dict()
        }

        redis_configuration = {}

        if not settings.ANNOUNCE_TEST_MODE:
            redis_url = urlparse.urlparse(settings.BOARDS_SOCKETS_REDIS_URL)

            redis_configuration.update({
                'host':
                redis_url.hostname,
                'password':
                redis_url.password,
                'port':
                int(redis_url.port) if redis_url.port else 6379,
                'db':
                int(redis_url.path[1:]) if redis_url.path[1:] else 0,
            })

        announce = Announce(json_dumps=json_renderer,
                            _test_mode=settings.ANNOUNCE_TEST_MODE,
                            **redis_configuration)

        try:
            announce.emit('message', data, room=room)
        except Exception as e:
            logger.exception(e)
예제 #7
0
 def handle_message(self):
     while not self._stop_flag_.is_set():
         if self._buffer_:
             local_buffer = self._buffer_
             self._buffer_ = []
             for message in local_buffer:
                 if message['msg_type'] == 'request':
                     Response(self, message)
                 elif message['msg_type'] == 'response':
                     Request(self, message)
                 elif message['msg_type'] == 'announce':
                     Announce(self, message)
                 else:
                     logging.error('{0}Peer #{1}: received a message not valid!'.format(self.type,self.index))
         sleep(1)
예제 #8
0
    def test_send_in_namespace_with_room(self):
        self.announce = Announce(_test_mode=True, namespace='namespace')

        expected = {
            'nodeId': 149241983,
            'args': [
                '/namespace/room',
                '3::/namespace:Hello world!',
                None,
                []
            ]
        }

        result_text = self.announce.send('Hello world!', room='room')

        self.compare_payloads(expected, result_text)
예제 #9
0
    def test_emit_in_namespace_with_room(self):
        self.announce = Announce(_test_mode=True, namespace='namespace')

        expected = {
            'nodeId': 149241983,
            'args': [
                '/namespace/room',
                '5::/namespace:{"name":"message","args":[{"user":"******"}]}',
                None,
                []
            ]
        }

        result_text = self.announce.emit('message', {'user': '******'}, room='room')

        self.compare_payloads(expected, result_text)
        self.check_json_payload(expected, result_text)
예제 #10
0
class TestAnnounce(BaseTestCase):
    def test_emit(self):
        expected = {
            'nodeId': 197520823,
            'args': [
                '',
                '5:::{"name": "message", "args": [{"countdown": 1000, "msg": "This is a test"}]}',
                None,
                []
            ]
        }

        result_text = self.announce.emit('message',
            {'msg': 'This is a test', 'countdown': 1000})

        self.compare_payloads(expected, result_text)
        self.check_json_payload(expected, result_text)


    def test_emit_with_unicode(self):
        expected = {
            'nodeId': 197520823,
            'args': [
                '',
                '5:::{"name": "message", "args": [{"msg": "Hello, 世界! áéíóúñüø"}]}',
                None,
                []
            ]
        }

        result_text = self.announce.emit('message', {'msg': 'Hello, 世界! áéíóúñüø'})

        self.compare_payloads(expected, result_text)
        self.check_json_payload(expected, result_text)


    def test_send(self):
        expected = {
            'nodeId': 286326561,
            'args': [
                '',
                '3:::Hello, world!',
                None,
                []
            ]
        }

        result_text = self.announce.send('Hello, world!')

        self.compare_payloads(expected, result_text)
        self.check_text_payload(expected, result_text)


    def test_send_with_unicode(self):
        expected = {
            'nodeId': 286326561,
            'args': [
                '',
                '3:::Hello, 世界! áéíóúñüø',
                None,
                []
            ]
        }

        result_text = self.announce.send('Hello, 世界! áéíóúñüø')

        self.compare_payloads(expected, result_text)
        self.check_text_payload(expected, result_text)


    def test_send_in_room(self):
        expected = {
            'nodeId': 286326561,
            'args': [
                '/room',
                '3:::Hello world!',
                None,
                []
            ]
        }

        result_text = self.announce.send('Hello world!', room='room')

        self.compare_payloads(expected, result_text)
        self.check_text_payload(expected, result_text)


    def test_emit_in_room(self):
        expected = {
            'nodeId': 286326561,
            'args': [
                '/room',
                '5:::{"name":"message","args":[{"user":"******"}]}',
                None,
                []
            ]
        }

        result_text = self.announce.emit('message', {'user': '******'}, room='room')

        self.compare_payloads(expected, result_text)
        self.check_json_payload(expected, result_text)


    def test_send_in_namespace(self):
        self.announce = Announce(_test_mode=True, namespace='namespace')

        expected = {
            'nodeId': 149241983,
            'args': [
                '/namespace',
                '3::/namespace:Hello world!',
                None,
                []
            ]
        }

        result_text = self.announce.send('Hello world!')

        self.compare_payloads(expected, result_text)
        self.check_text_payload(expected, result_text)


    def test_send_in_namespace_with_room(self):
        self.announce = Announce(_test_mode=True, namespace='namespace')

        expected = {
            'nodeId': 149241983,
            'args': [
                '/namespace/room',
                '3::/namespace:Hello world!',
                None,
                []
            ]
        }

        result_text = self.announce.send('Hello world!', room='room')

        self.compare_payloads(expected, result_text)
        self.check_text_payload(expected, result_text)


    def test_emit_in_namespace(self):
        self.announce = Announce(_test_mode=True, namespace='namespace')

        expected = {
            'nodeId': 149241983,
            'args': [
                '/namespace',
                '5::/namespace:{"name":"message","args":[{"user":"******"}]}',
                None,
                []
            ]
        }

        result_text = self.announce.emit('message', {'user': '******'})

        self.compare_payloads(expected, result_text)
        self.check_json_payload(expected, result_text)


    def test_emit_in_namespace_with_room(self):
        self.announce = Announce(_test_mode=True, namespace='namespace')

        expected = {
            'nodeId': 149241983,
            'args': [
                '/namespace/room',
                '5::/namespace:{"name":"message","args":[{"user":"******"}]}',
                None,
                []
            ]
        }

        result_text = self.announce.emit('message', {'user': '******'}, room='room')

        self.compare_payloads(expected, result_text)
        self.check_json_payload(expected, result_text)


    def test_custom_json_dumps_callable(self):

        class CustomJSONEncoder(json.JSONEncoder):
            def default(self, o):
                if isinstance(o, datetime.date):
                    return o.isoformat()
                return super(CustomJSONEncoder, self).default(o)

        def custom_json_dumps(data):
            return json.dumps(data, cls=CustomJSONEncoder)

        self.announce = Announce(_test_mode=True, namespace='namespace',
                                 json_dumps=custom_json_dumps)

        expected = {
            'nodeId': 149241983,
            'args': [
                '/namespace/room',
                '5::/namespace:{"name":"message","args":[{"datetime":"2014-02-24T18:01:53.395214"}]}',
                None,
                []
            ]
        }

        data = {
            'datetime': datetime.datetime(2014, 2, 24, 18, 1, 53, 395214)
        }

        result_text = self.announce.emit('message', data, room='room')

        self.compare_payloads(expected, result_text)
        self.check_json_payload(expected, result_text)
예제 #11
0
class TestAnnounce(BaseTestCase):
    def test_emit(self):
        expected = {
            'nodeId':
            197520823,
            'args': [
                '',
                '5:::{"name": "message", "args": [{"countdown": 1000, "msg": "This is a test"}]}',
                None, []
            ]
        }

        result_text = self.announce.emit('message', {
            'msg': 'This is a test',
            'countdown': 1000
        })

        self.compare_payloads(expected, result_text)
        self.check_json_payload(expected, result_text)

    def test_emit_with_unicode(self):
        expected = {
            'nodeId':
            197520823,
            'args': [
                '',
                '5:::{"name": "message", "args": [{"msg": "Hello, 世界! áéíóúñüø"}]}',
                None, []
            ]
        }

        result_text = self.announce.emit('message',
                                         {'msg': 'Hello, 世界! áéíóúñüø'})

        self.compare_payloads(expected, result_text)
        self.check_json_payload(expected, result_text)

    def test_send(self):
        expected = {
            'nodeId': 286326561,
            'args': ['', '3:::Hello, world!', None, []]
        }

        result_text = self.announce.send('Hello, world!')

        self.compare_payloads(expected, result_text)
        self.check_text_payload(expected, result_text)

    def test_send_with_unicode(self):
        expected = {
            'nodeId': 286326561,
            'args': ['', '3:::Hello, 世界! áéíóúñüø', None, []]
        }

        result_text = self.announce.send('Hello, 世界! áéíóúñüø')

        self.compare_payloads(expected, result_text)
        self.check_text_payload(expected, result_text)

    def test_send_in_room(self):
        expected = {
            'nodeId': 286326561,
            'args': ['/room', '3:::Hello world!', None, []]
        }

        result_text = self.announce.send('Hello world!', room='room')

        self.compare_payloads(expected, result_text)
        self.check_text_payload(expected, result_text)

    def test_emit_in_room(self):
        expected = {
            'nodeId':
            286326561,
            'args': [
                '/room', '5:::{"name":"message","args":[{"user":"******"}]}',
                None, []
            ]
        }

        result_text = self.announce.emit('message', {'user': '******'},
                                         room='room')

        self.compare_payloads(expected, result_text)
        self.check_json_payload(expected, result_text)

    def test_send_in_namespace(self):
        self.announce = Announce(_test_mode=True, namespace='namespace')

        expected = {
            'nodeId': 149241983,
            'args': ['/namespace', '3::/namespace:Hello world!', None, []]
        }

        result_text = self.announce.send('Hello world!')

        self.compare_payloads(expected, result_text)
        self.check_text_payload(expected, result_text)

    def test_send_in_namespace_with_room(self):
        self.announce = Announce(_test_mode=True, namespace='namespace')

        expected = {
            'nodeId': 149241983,
            'args':
            ['/namespace/room', '3::/namespace:Hello world!', None, []]
        }

        result_text = self.announce.send('Hello world!', room='room')

        self.compare_payloads(expected, result_text)
        self.check_text_payload(expected, result_text)

    def test_emit_in_namespace(self):
        self.announce = Announce(_test_mode=True, namespace='namespace')

        expected = {
            'nodeId':
            149241983,
            'args': [
                '/namespace',
                '5::/namespace:{"name":"message","args":[{"user":"******"}]}',
                None, []
            ]
        }

        result_text = self.announce.emit('message', {'user': '******'})

        self.compare_payloads(expected, result_text)
        self.check_json_payload(expected, result_text)

    def test_emit_in_namespace_with_room(self):
        self.announce = Announce(_test_mode=True, namespace='namespace')

        expected = {
            'nodeId':
            149241983,
            'args': [
                '/namespace/room',
                '5::/namespace:{"name":"message","args":[{"user":"******"}]}',
                None, []
            ]
        }

        result_text = self.announce.emit('message', {'user': '******'},
                                         room='room')

        self.compare_payloads(expected, result_text)
        self.check_json_payload(expected, result_text)

    def test_custom_json_dumps_callable(self):
        class CustomJSONEncoder(json.JSONEncoder):
            def default(self, o):
                if isinstance(o, datetime.date):
                    return o.isoformat()
                return super(CustomJSONEncoder, self).default(o)

        def custom_json_dumps(data):
            return json.dumps(data, cls=CustomJSONEncoder)

        self.announce = Announce(_test_mode=True,
                                 namespace='namespace',
                                 json_dumps=custom_json_dumps)

        expected = {
            'nodeId':
            149241983,
            'args': [
                '/namespace/room',
                '5::/namespace:{"name":"message","args":[{"datetime":"2014-02-24T18:01:53.395214"}]}',
                None, []
            ]
        }

        data = {'datetime': datetime.datetime(2014, 2, 24, 18, 1, 53, 395214)}

        result_text = self.announce.emit('message', data, room='room')

        self.compare_payloads(expected, result_text)
        self.check_json_payload(expected, result_text)
예제 #12
0
 def setUp(self):
     self.announce = Announce(_test_mode=True)
예제 #13
0
def announce_loop():
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    loop.run_until_complete(Announce().start())