Ejemplo n.º 1
0
import tornadio2
from tornadio2.server import SocketServer
import tornado
from tornado.web import Application
from boards import BoardsController

boards_controller = BoardsController()


# MyConnection Class
class MyConnection(tornadio2.SocketConnection):
    def on_message(self, message):
        boards_controller.on_message(self, message)

    def on_close(self):
        boards_controller.unregister(self)


class WebSocketsServer:
    def __init__(self, uri, port):
        # Creating a Tornadio2 server for the connection
        MyRouter = tornadio2.TornadioRouter(MyConnection)

        application = Application(MyRouter.urls, socket_io_port=port)
        self.http_server = SocketServer(application,
                                        auto_start=False,
                                        xheaders=True)

    # Start Tornadio2 server
    def start(self):
        io_loop = tornado.ioloop.IOLoop.instance()
 def setUp(self):
     self.boards_controller = BoardsController()
Ejemplo n.º 3
0
 def setUp(self):
     self.boards_controller = BoardsController()
class BoardsControllerTest(unittest.TestCase):

    def setUp(self):
        self.boards_controller = BoardsController()

    def test_register(self):
        channel_id = "xxx"
        handle = MagicMock()

        self.boards_controller.register(channel_id, handle)

        assert handle in self.boards_controller.get_board_handles(channel_id)

    def test_unregister_handle_from_channel(self):
        channel_id = "xxx"
        handle = MagicMock()
        self.boards_controller.register(channel_id, handle)

        self.boards_controller.unregister(handle, channel_id)

        assert handle not in self.boards_controller.get_board_handles(channel_id)

    def test_unregister_handle(self):
        channel_id = "xxx"
        handle = MagicMock()
        self.boards_controller.register(channel_id, handle)

        self.boards_controller.unregister(handle)

        assert handle not in self.boards_controller.get_board_handles(channel_id)

    def test_routing_move_message_to_right_channel(self):
        sender_handle = MagicMock()
        handle = MagicMock()

        expected_channel_id = 1
        self.boards_controller.register(expected_channel_id, handle)

        self.boards_controller.on_message(sender_handle, '{"type":"move", "args": {"channel_id":1, "postit_id":1, "x":10, "y":20}}')

        assert handle.send.call_count == 1
Ejemplo n.º 5
0
class BoardsControllerTest(unittest.TestCase):
    def setUp(self):
        self.boards_controller = BoardsController()

    def test_register(self):
        channel_id = "xxx"
        handle = MagicMock()

        self.boards_controller.register(channel_id, handle)

        assert handle in self.boards_controller.get_board_handles(channel_id)

    def test_unregister_handle_from_channel(self):
        channel_id = "xxx"
        handle = MagicMock()
        self.boards_controller.register(channel_id, handle)

        self.boards_controller.unregister(handle, channel_id)

        assert handle not in self.boards_controller.get_board_handles(
            channel_id)

    def test_unregister_handle(self):
        channel_id = "xxx"
        handle = MagicMock()
        self.boards_controller.register(channel_id, handle)

        self.boards_controller.unregister(handle)

        assert handle not in self.boards_controller.get_board_handles(
            channel_id)

    def test_routing_move_message_to_right_channel(self):
        sender_handle = MagicMock()
        handle = MagicMock()

        expected_channel_id = 1
        self.boards_controller.register(expected_channel_id, handle)

        self.boards_controller.on_message(
            sender_handle,
            '{"type":"move", "args": {"channel_id":1, "postit_id":1, "x":10, "y":20}}'
        )

        assert handle.send.call_count == 1