-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
managed_server_loop.py
82 lines (65 loc) · 2.59 KB
/
managed_server_loop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#-----------------------------------------------------------------------------
# Copyright (c) 2012 - 2024, Anaconda, Inc., and Bokeh Contributors.
# All rights reserved.
#
# The full license is in the file LICENSE.txt, distributed with this software.
#-----------------------------------------------------------------------------
''' Define a Pytest plugin to provide a Bokeh server
'''
#-----------------------------------------------------------------------------
# Boilerplate
#-----------------------------------------------------------------------------
from __future__ import annotations
import logging # isort:skip
log = logging.getLogger(__name__)
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
# Standard library imports
from contextlib import contextmanager
from typing import (
TYPE_CHECKING,
Any,
ContextManager,
Iterator,
Protocol,
)
# External imports
import pytest
# Bokeh imports
from bokeh.server.server import Server
if TYPE_CHECKING:
from bokeh.application import Application
#-----------------------------------------------------------------------------
# Globals and constants
#-----------------------------------------------------------------------------
pytest_plugins = ()
__all__ = (
'ManagedServerLoop',
)
#-----------------------------------------------------------------------------
# General API
#-----------------------------------------------------------------------------
class MSL(Protocol):
def __call__(self, application: Application, port: int | None = None, **server_kwargs: Any) -> ContextManager[Server]: ...
@pytest.fixture
def ManagedServerLoop(unused_tcp_port: int) -> MSL:
@contextmanager
def msl(application: Application, port: int | None = None, **server_kwargs: Any) -> Iterator[Server]:
if port is None:
port = unused_tcp_port
server = Server(application, port=port, **server_kwargs)
server.start()
yield server
server.unlisten()
server.stop()
return msl
#-----------------------------------------------------------------------------
# Dev API
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Private API
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------