Example #1
0
def test_acquire_multiple_write():
    sync = multiprocessing.get_context(web.start_method).Manager()

    res_lock = web.ResLock(sync)

    print(res_lock.resources)
    assert res_lock.acquire('first', '/', True)
    print(res_lock.resources)

    process = multiprocessing.get_context(web.start_method).Process(target=acquire_wait_writer, args=(res_lock,))

    process.start()

    # wait a bit
    time.sleep(1)

    assert process.is_alive()
    assert res_lock.resources['/'][1] == 1

    res_lock.release('/', True)

    process.join(timeout=1)

    res_lock.release('/', True)

    assert not res_lock.resources
Example #2
0
def test_release_no_exists():
    sync = multiprocessing.get_context(web.start_method).Manager()

    res_lock = web.ResLock(sync)

    with pytest.raises(RuntimeError):
        res_lock.release('/', False)
Example #3
0
def test_acquire_write():
    sync = multiprocessing.get_context(web.start_method).Manager()

    res_lock = web.ResLock(sync)

    assert res_lock.acquire('first', '/', True)

    assert res_lock.resources

    res_lock.release('/', True)

    assert not res_lock.resources
Example #4
0
def test_acquire_request_multiple():
    sync = multiprocessing.get_context(web.start_method).Manager()

    res_lock = web.ResLock(sync)

    assert res_lock.acquire('first', '/first', True)
    assert res_lock.acquire('first', '/second', True)

    assert res_lock.resources['/first'][1] == 1
    assert res_lock.resources['/second'][1] == 1

    res_lock.release('/first', True)
    res_lock.release('/second', True)

    assert not res_lock.resources
Example #5
0
def test_acquire_reentrant():
    sync = multiprocessing.get_context(web.start_method).Manager()

    res_lock = web.ResLock(sync)

    request = 'token'

    assert res_lock.acquire(request, '/', True)
    assert res_lock.acquire(request, '/', True)

    assert not res_lock.acquire('first', '/', True)

    assert res_lock.resources['/'][1] == 2

    res_lock.release('/', True)
    res_lock.release('/', True)

    assert not res_lock.resources
Example #6
0
    def __init__(self,
                 address=None,
                 routes=None,
                 error_routes=None,
                 keyfile=None,
                 certfile=None,
                 *,
                 keepalive=5,
                 timeout=20,
                 backlog=5,
                 num_processes=2,
                 max_processes=6,
                 max_queue=4,
                 poll_interval=0.2,
                 log=None,
                 http_log=None,
                 control=None,
                 socket=None):
        if routes is None:
            routes = {}

        if error_routes is None:
            error_routes = {}

        # save server address
        self.address = address

        # make route dictionaries
        self.routes = collections.OrderedDict()
        self.error_routes = collections.OrderedDict()

        # compile the regex routes and add them
        for regex, handler in routes.items():
            self.routes[re.compile('^' + regex + '$')] = handler
        for regex, handler in error_routes.items():
            self.error_routes[re.compile('^' + regex + '$')] = handler

        # store constants
        self.keyfile = keyfile
        self.certfile = certfile

        self.using_tls = self.keyfile and self.certfile

        self.keepalive_timeout = keepalive
        self.request_timeout = timeout

        self.backlog = backlog

        self.num_processes = num_processes
        self.max_processes = max_processes
        self.max_queue = max_queue

        self.poll_interval = poll_interval

        # create the logs
        if log:
            self.log = log
        else:
            self.log = web.default_log

        if http_log:
            self.http_log = http_log
        else:
            self.http_log = web.default_http_log

        # create sync manager
        sigint = signal.signal(signal.SIGINT, signal.SIG_IGN)
        self.sync = multiprocessing.get_context(web.start_method).Manager()
        signal.signal(signal.SIGINT, sigint)

        # create process-ready server control object with manager
        if control:
            self.control = control
        else:
            self.control = web.HTTPServerControl(self.sync, self.backlog)

        # lock for automatic handling of resource safety/consistency
        self.res_lock = web.ResLock(self.sync)

        # prepare a socket
        if socket:
            self.socket = socket
        else:
            self.socket = MockSocket()

        # create process-ready server info object
        self.info = web.HTTPServerInfo(self)