def test_ping(self):
        application = Application()
        with ManagedServerLoop(application,
                               keep_alive_milliseconds=0) as server:
            session = ClientSession(session_id='test_ping',
                                    websocket_url=ws_url(server),
                                    io_loop=server.io_loop)
            session.connect()
            assert session.connected
            assert session.document is None

            connection = next(iter(server._tornado._clients))
            expected_pong = connection._ping_count
            server._tornado._keep_alive()  # send ping
            session.force_roundtrip()

            assert expected_pong == connection._socket.latest_pong

            # check that each ping increments by 1
            server._tornado._keep_alive()
            session.force_roundtrip()

            assert (expected_pong + 1) == connection._socket.latest_pong

            session.close()
            session.loop_until_closed(suppress_warning=True)
            assert not session.connected
Ejemplo n.º 2
0
    def test_ping(self):
        application = Application()
        with ManagedServerLoop(application, keep_alive_milliseconds=0) as server:
            session = ClientSession(session_id='test_ping',
                                    websocket_url=ws_url(server),
                                    io_loop=server.io_loop)
            session.connect()
            assert session.connected
            assert session.document is None

            connection = next(iter(server._tornado._clients))
            expected_pong = connection._ping_count
            server._tornado.keep_alive() # send ping
            session.force_roundtrip()

            self.assertEqual(expected_pong, connection._socket.latest_pong)

            # check that each ping increments by 1
            server._tornado.keep_alive()
            session.force_roundtrip()

            self.assertEqual(expected_pong + 1, connection._socket.latest_pong)

            session.close()
            session.loop_until_closed()
            assert not session.connected
Ejemplo n.º 3
0
 def test_minimal_connect_and_disconnect(self):
     application = Application()
     with ManagedServerLoop(application) as server:
         # we don't have to start the server because it
         # uses the same main loop as the client, so
         # if we start either one it starts both
         session = ClientSession(io_loop=server.io_loop, url=server.ws_url)
         session.connect()
         assert session.connected
         session.close()
         session.loop_until_closed()
         assert not session.connected
Ejemplo n.º 4
0
 def test_minimal_connect_and_disconnect(self):
     application = Application()
     with ManagedServerLoop(application) as server:
         # we don't have to start the server because it
         # uses the same main loop as the client, so
         # if we start either one it starts both
         session = ClientSession(io_loop = server.io_loop,
                                 url = server.ws_url)
         session.connect()
         assert session.connected
         session.close()
         session.loop_until_closed()
         assert not session.connected
Ejemplo n.º 5
0
    def test_host_whitelist_success(self):
        application = Application()

        # succeed no host value with defaults
        with ManagedServerLoop(application, host=None) as server:
            session = ClientSession(url=ws_url(server), io_loop = server.io_loop)
            session.connect()
            assert session.connected
            session.close()
            session.loop_until_closed()

        # succeed no host value with port
        with ManagedServerLoop(application, port=8080, host=None) as server:
            session = ClientSession(url=ws_url(server), io_loop = server.io_loop)
            session.connect()
            assert session.connected
            session.close()
            session.loop_until_closed()

        # succeed matching host value
        with ManagedServerLoop(application, port=8080, host=["localhost:8080"]) as server:
            session = ClientSession(url=ws_url(server), io_loop = server.io_loop)
            session.connect()
            assert session.connected
            session.close()
            session.loop_until_closed()

        # succeed matching host value one of multiple
        with ManagedServerLoop(application, port=8080, host=["bad_host", "localhost:8080"]) as server:
            session = ClientSession(url=ws_url(server), io_loop = server.io_loop)
            session.connect()
            assert session.connected
            session.close()
            session.loop_until_closed()
Ejemplo n.º 6
0
    def test_host_whitelist_success(self):
        application = Application()

        # succeed no host value with defaults
        with ManagedServerLoop(application, host=None) as server:
            session = ClientSession(websocket_url=ws_url(server), io_loop = server.io_loop)
            session.connect()
            assert session.connected
            session.close()
            session.loop_until_closed()

        # succeed no host value with port
        with ManagedServerLoop(application, port=8080, host=None) as server:
            session = ClientSession(websocket_url=ws_url(server), io_loop = server.io_loop)
            session.connect()
            assert session.connected
            session.close()
            session.loop_until_closed()

        # succeed matching host value
        with ManagedServerLoop(application, port=8080, host=["localhost:8080"]) as server:
            session = ClientSession(websocket_url=ws_url(server), io_loop = server.io_loop)
            session.connect()
            assert session.connected
            session.close()
            session.loop_until_closed()

        # succeed matching host value one of multiple
        with ManagedServerLoop(application, port=8080, host=["bad_host", "localhost:8080"]) as server:
            session = ClientSession(websocket_url=ws_url(server), io_loop = server.io_loop)
            session.connect()
            assert session.connected
            session.close()
            session.loop_until_closed()
Ejemplo n.º 7
0
 def test_disconnect_on_error(self):
     application = Application()
     with ManagedServerLoop(application) as server:
         session = ClientSession(url=ws_url(server), io_loop = server.io_loop)
         session.connect()
         assert session.connected
         # send a bogus message using private fields
         session._connection._socket.write_message(b"xx", binary=True)
         # connection should now close on the server side
         # and the client loop should end
         session.loop_until_closed()
         assert not session.connected
         session.close()
         session.loop_until_closed()
         assert not session.connected
Ejemplo n.º 8
0
    def test_host_whitelist_failure(self):
        application = Application()

        # failure bad host
        with ManagedServerLoop(application, host=["bad_host"]) as server:
            session = ClientSession(websocket_url=ws_url(server),
                                    io_loop=server.io_loop)
            session.connect()
            assert not session.connected
            session.close()
            session.loop_until_closed()

        with ManagedServerLoop(application, host=["bad_host:5006"]) as server:
            session = ClientSession(websocket_url=ws_url(server),
                                    io_loop=server.io_loop)
            session.connect()
            assert not session.connected
            session.close()
            session.loop_until_closed()

        # failure good host, bad port
        with ManagedServerLoop(application, host=["localhost:80"]) as server:
            session = ClientSession(websocket_url=ws_url(server),
                                    io_loop=server.io_loop)
            session.connect()
            assert not session.connected
            session.close()
            session.loop_until_closed()

        # failure good host, bad default port
        with ManagedServerLoop(application, host=["localhost"]) as server:
            session = ClientSession(websocket_url=ws_url(server),
                                    io_loop=server.io_loop)
            session.connect()
            assert not session.connected
            session.close()
            session.loop_until_closed()

        # failure with custom port
        with ManagedServerLoop(application, port=8080,
                               host=["localhost:8081"]) as server:
            session = ClientSession(websocket_url=ws_url(server),
                                    io_loop=server.io_loop)
            session.connect()
            assert not session.connected
            session.close()
            session.loop_until_closed()
 def test_disconnect_on_error(self):
     application = Application()
     with ManagedServerLoop(application) as server:
         session = ClientSession(session_id='test_disconnect_on_error',
                                 websocket_url=ws_url(server),
                                 io_loop=server.io_loop)
         session.connect()
         assert session.connected
         # send a bogus message using private fields
         session._connection._socket.write_message(b"xx", binary=True)
         # connection should now close on the server side
         # and the client loop should end
         session.loop_until_closed(suppress_warning=True)
         assert not session.connected
         session.close()
         session.loop_until_closed(suppress_warning=True)
         assert not session.connected
Ejemplo n.º 10
0
    def test_request_server_info(self):
        application = Application()
        with ManagedServerLoop(application) as server:
            session = ClientSession(url=server.ws_url, io_loop=server.io_loop)
            session.connect()
            assert session.connected
            assert session.document is None

            info = session.request_server_info()

            from bokeh import __version__

            assert info['version_info']['bokeh'] == __version__
            assert info['version_info']['server'] == __version__

            session.close()
            session.loop_until_closed()
            assert not session.connected
Ejemplo n.º 11
0
 def test_disconnect_on_error(self, ManagedServerLoop: MSL) -> None:
     application = Application()
     with ManagedServerLoop(application) as server:
         session = ClientSession(session_id=ID("test_disconnect_on_error"),
                                 websocket_url=ws_url(server),
                                 io_loop=server.io_loop)
         session.connect()
         assert session.connected
         # send a bogus message using private fields
         server.io_loop.add_callback(
             session._connection._socket.write_message, b"xx", binary=True)
         # connection should now close on the server side
         # and the client loop should end
         session._loop_until_closed()
         assert not session.connected
         session.close()
         session._loop_until_closed()
         assert not session.connected
Ejemplo n.º 12
0
    def test_request_server_info(self):
        application = Application()
        with ManagedServerLoop(application) as server:
            session = ClientSession(url=server.ws_url, io_loop=server.io_loop)
            session.connect()
            assert session.connected
            assert session.document is None

            info = session.request_server_info()

            from bokeh import __version__

            assert info['version_info']['bokeh'] == __version__
            assert info['version_info']['server'] == __version__

            session.close()
            session.loop_until_closed()
            assert not session.connected
    def test_connect_with_prefix(self):
        application = Application()
        with ManagedServerLoop(application, prefix="foo") as server:
            # we don't have to start the server because it
            # uses the same main loop as the client, so
            # if we start either one it starts both
            session = ClientSession(io_loop=server.io_loop,
                                    websocket_url=ws_url(server, "/foo"))
            session.connect()
            assert session.connected
            session.close()
            session.loop_until_closed(suppress_warning=True)

            session = ClientSession(io_loop=server.io_loop,
                                    websocket_url=ws_url(server))
            session.connect()
            assert not session.connected
            session.close()
            session.loop_until_closed(suppress_warning=True)
Ejemplo n.º 14
0
    def test_connect_with_prefix(self):
        application = Application()
        with ManagedServerLoop(application, prefix="foo") as server:
            # we don't have to start the server because it
            # uses the same main loop as the client, so
            # if we start either one it starts both
            session = ClientSession(io_loop = server.io_loop,
                                    websocket_url = ws_url(server, "/foo"))
            session.connect()
            assert session.connected
            session.close()
            session.loop_until_closed()

            session = ClientSession(io_loop = server.io_loop,
                                    websocket_url = ws_url(server))
            session.connect()
            assert not session.connected
            session.close()
            session.loop_until_closed()
Ejemplo n.º 15
0
    def test_request_server_info(self):
        application = Application()
        with ManagedServerLoop(application) as server:
            session = ClientSession(session_id='test_request_server_info',
                                    websocket_url=ws_url(server),
                                    io_loop=server.io_loop)
            session.connect()
            assert session.connected
            assert session.document is None

            info = session.request_server_info()

            from bokeh import __version__

            assert info['version_info']['bokeh'] == __version__
            assert info['version_info']['server'] == __version__

            session.close()
            session.loop_until_closed(suppress_warning=True)
            assert not session.connected
    def test_request_server_info(self):
        application = Application()
        with ManagedServerLoop(application) as server:
            session = ClientSession(session_id='test_request_server_info',
                                    websocket_url=ws_url(server),
                                    io_loop=server.io_loop)
            session.connect()
            assert session.connected
            assert session.document is None

            info = session.request_server_info()

            from bokeh import __version__

            assert info['version_info']['bokeh'] == __version__
            assert info['version_info']['server'] == __version__

            session.close()
            session.loop_until_closed(suppress_warning=True)
            assert not session.connected
Ejemplo n.º 17
0
    def test_request_server_info(self, ManagedServerLoop: MSL) -> None:
        application = Application()
        with ManagedServerLoop(application) as server:
            session = ClientSession(session_id=ID("test_request_server_info"),
                                    websocket_url=ws_url(server),
                                    io_loop=server.io_loop)
            session.connect()
            assert session.connected
            assert session.document is None

            info = session.request_server_info()

            from bokeh import __version__

            assert info['version_info']['bokeh'] == __version__
            assert info['version_info']['server'] == __version__

            session.close()
            session._loop_until_closed()
            assert not session.connected
Ejemplo n.º 18
0
    def test_host_whitelist_failure(self):
        application = Application()

        # failure bad host
        with ManagedServerLoop(application, host=["bad_host"]) as server:
            session = ClientSession(url=ws_url(server), io_loop = server.io_loop)
            session.connect()
            assert not session.connected
            session.close()
            session.loop_until_closed()

        with ManagedServerLoop(application, host=["bad_host:5006"]) as server:
            session = ClientSession(url=ws_url(server), io_loop = server.io_loop)
            session.connect()
            assert not session.connected
            session.close()
            session.loop_until_closed()

        # failure good host, bad port
        with ManagedServerLoop(application, host=["localhost:80"]) as server:
            session = ClientSession(url=ws_url(server), io_loop = server.io_loop)
            session.connect()
            assert not session.connected
            session.close()
            session.loop_until_closed()

        # failure good host, bad default port
        with ManagedServerLoop(application, host=["localhost"]) as server:
            session = ClientSession(url=ws_url(server), io_loop = server.io_loop)
            session.connect()
            assert not session.connected
            session.close()
            session.loop_until_closed()

        # failure with custom port
        with ManagedServerLoop(application, port=8080, host=["localhost:8081"]) as server:
            session = ClientSession(url=ws_url(server), io_loop = server.io_loop)
            session.connect()
            assert not session.connected
            session.close()
            session.loop_until_closed()
Ejemplo n.º 19
0
class ScatterBokeh(Plot):
    def __init__(self,
                 plot_title: str,
                 number_of_objectives: int,
                 ws_url: str = 'localhost:5006'):
        super(ScatterBokeh, self).__init__(plot_title, number_of_objectives)

        if self.number_of_objectives == 2:
            self.source = ColumnDataSource(data=dict(x=[], y=[], str=[]))
        elif self.number_of_objectives == 3:
            self.source = ColumnDataSource(data=dict(x=[], y=[], z=[], str=[]))
        else:
            raise Exception(
                'Wrong number of objectives: {0}'.format(number_of_objectives))

        self.client = ClientSession(websocket_url='ws://{0}/ws'.format(ws_url))
        self.doc = curdoc()
        self.doc.title = plot_title
        self.figure_xy = None
        self.figure_xz = None
        self.figure_yz = None

        self.__initialize()

    def __initialize(self) -> None:
        """ Set-up tools for plot. """
        code = '''
            selected = source.selected['1d']['indices'][0]
            var str = source.front.str[selected]
            alert(str)
        '''

        callback = CustomJS(args=dict(source=self.source), code=code)
        self.plot_tools = [
            TapTool(callback=callback),
            WheelZoomTool(), 'save', 'pan',
            HoverTool(tooltips=[('index', '$index'), ('(x,y)', '($x, $y)')])
        ]

    def plot(self,
             front: List[S],
             reference: List[S] = None,
             output: str = '',
             show: bool = True) -> None:
        # This is important to purge front (if any) between calls
        reset_output()

        # Set up figure
        self.figure_xy = Figure(output_backend='webgl',
                                sizing_mode='scale_width',
                                title=self.plot_title,
                                tools=self.plot_tools)
        self.figure_xy.scatter(x='x',
                               y='y',
                               legend='solution',
                               fill_alpha=0.7,
                               source=self.source)
        self.figure_xy.xaxis.axis_label = self.xaxis_label
        self.figure_xy.yaxis.axis_label = self.yaxis_label

        x_values, y_values, z_values = self.get_objectives(front)

        if self.number_of_objectives == 2:
            # Plot reference solution list (if any)
            if reference:
                ref_x_values, ref_y_values, _ = self.get_objectives(reference)
                self.figure_xy.line(x=ref_x_values,
                                    y=ref_y_values,
                                    legend='reference',
                                    color='green')

            # Push front to server
            self.source.stream({
                'x': x_values,
                'y': y_values,
                'str': [s.__str__() for s in front]
            })
            self.doc.add_root(column(self.figure_xy))
        else:
            # Add new figures for each axis
            self.figure_xz = Figure(title='xz',
                                    output_backend='webgl',
                                    sizing_mode='scale_width',
                                    tools=self.plot_tools)
            self.figure_xz.scatter(x='x',
                                   y='z',
                                   legend='solution',
                                   fill_alpha=0.7,
                                   source=self.source)
            self.figure_xz.xaxis.axis_label = self.xaxis_label
            self.figure_xz.yaxis.axis_label = self.zaxis_label

            self.figure_yz = Figure(title='yz',
                                    output_backend='webgl',
                                    sizing_mode='scale_width',
                                    tools=self.plot_tools)
            self.figure_yz.scatter(x='y',
                                   y='z',
                                   legend='solution',
                                   fill_alpha=0.7,
                                   source=self.source)
            self.figure_yz.xaxis.axis_label = self.yaxis_label
            self.figure_yz.yaxis.axis_label = self.zaxis_label

            # Plot reference solution list (if any)
            if reference:
                ref_x_values, ref_y_values, ref_z_values = self.get_objectives(
                    reference)
                self.figure_xy.line(x=ref_x_values,
                                    y=ref_y_values,
                                    legend='reference',
                                    color='green')
                self.figure_xz.line(x=ref_x_values,
                                    y=ref_z_values,
                                    legend='reference',
                                    color='green')
                self.figure_yz.line(x=ref_y_values,
                                    y=ref_z_values,
                                    legend='reference',
                                    color='green')

            # Push front to server
            self.source.stream({
                'x': x_values,
                'y': y_values,
                'z': z_values,
                'str': [s.__str__() for s in front]
            })
            self.doc.add_root(
                row(self.figure_xy, self.figure_xz, self.figure_yz))

        self.client.push(self.doc)

        if output:
            self.__save(output)
        if show:
            self.client.show()

    def update(self,
               front: List[S],
               reference: List[S],
               new_title: str = '',
               persistence: bool = False) -> None:
        # Check if plot has not been initialized first
        if self.figure_xy is None:
            self.plot(front, reference)

        if not persistence:
            rollover = len(front)
        else:
            rollover = None

        self.figure_xy.title.text = new_title
        x_values, y_values, z_values = self.get_objectives(front)

        if self.number_of_objectives == 2:
            self.source.stream(
                {
                    'x': x_values,
                    'y': y_values,
                    'str': [s.__str__() for s in front]
                },
                rollover=rollover)
        else:
            self.source.stream(
                {
                    'x': x_values,
                    'y': y_values,
                    'z': z_values,
                    'str': [s.__str__() for s in front]
                },
                rollover=rollover)

    def __save(self, file_name: str):
        # env = Environment(loader=FileSystemLoader(BASE_PATH + '/util/'))
        # env.filters['json'] = lambda obj: Markup(json.dumps(obj))

        html = file_html(models=self.doc, resources=CDN)
        with open(file_name + '.html', 'w') as of:
            of.write(html)

    def disconnect(self):
        if self.is_connected():
            self.client.close()

    def is_connected(self) -> bool:
        return self.client.connected