Ejemplo n.º 1
0
    def test_2_exclusive_access(self):
        """
        Verify that the server does not grant simultaneous access
        to two clients if it is configured to allow only one at a time.
        """
        resource = 'my-resource'
        DELAY = 0.5

        client_1 = ResourceManagerClient('127.0.0.1', SERVER_PORT, _debug=True)
        client_2 = ResourceManagerClient('127.0.0.1', SERVER_PORT, _debug=True)

        task_started = threading.Event()

        def long_task():
            with client_1.access_context(resource, False, 1, 1000):
                task_started.set()
                time.sleep(DELAY)

        start = time.time()
        th = threading.Thread(target=long_task)
        th.start()

        task_started.wait()
        with client_2.access_context(resource, False, 1, 1000):
            assert time.time() - start >= DELAY, \
                "We shouldn't have been granted access to the resource so quickly!"

        th.join()
Ejemplo n.º 2
0
 def test_1_basic(self):
     """
     Request access to a resource.
     """
     resource = 'my-resource'
     client = ResourceManagerClient('127.0.0.1', SERVER_PORT, _debug=True)
     assert client.read_config()["write_reqs"] == 2
     with client.access_context(resource, False, 1, 1000):
         pass
Ejemplo n.º 3
0
    def test_5_pickle(self):
        """
        Copy the client via pickling and then use the copy after unpickling.
        """
        resource = 'my-resource'
        client = ResourceManagerClient('127.0.0.1', SERVER_PORT, _debug=True)
        with client.access_context(resource, False, 1, 1000):
            pass

        pickled_client = pickle.dumps(client)
        unpickled_client = pickle.loads(pickled_client)

        with unpickled_client.access_context(resource, False, 1, 1000):
            pass
Ejemplo n.º 4
0
    def test_3_parallel_read_write_access(self):
        """
        Verify that a single client can be used from multiple threads
        (since it creates low-level _ResourceManagerClient objects per-thread as needed).
        
        Also, verify that the server DOES grant simultaneous access
        to two threads if one is reading and the other is writing,
        as long as neither is over capacity already.
        """
        resource = 'my-resource'
        DELAY = 0.5

        # Without this sleep this test fails intermittently with a strange error.
        # See https://github.com/janelia-flyem/DVIDResourceManager/issues/4
        # Does the server need time to initialize?
        time.sleep(0.5)

        client = ResourceManagerClient('127.0.0.1', SERVER_PORT, _debug=True)

        task_started = threading.Event()

        def long_task():
            with client.access_context(resource, True, 1, 1000):
                task_started.set()
                time.sleep(DELAY)

        start = time.time()
        th = threading.Thread(target=long_task)
        th.start()

        task_started.wait()
        with client.access_context(resource, False, 1, 1000):
            assert (time.time() - start) < DELAY, \
                "The server seems to have incorrectly forbidden parallel access for reading and writing."

        th.join()