Esempio n. 1
0
def thread(settings, results):
    global TEO

    duration = 5

    semaphore = Semaphore(
        resource_name='testing',
        rabbitmq_username=settings['username'],
        rabbitmq_password=settings['password'],
        rabbitmq_ip=settings['ip'],
        rabbitmq_port=settings['port'],
        rabbitmq_api_port=settings['api_port'],
        rabbitmq_virtual_host=settings['virtual_host'])

    try:
        TEO += 1
        order = TEO

        start = time.time()
        semaphore.acquire()
        delta = time.time() - start

        print 'Thread {} waited {} seconds for lock. OK!'.format(order, delta)

        """First thread should acquire lock very quickly. Threads 2 and 3 are
        waiting at least 5 and 10 seconds respectively.
        """
        switch = {1: (delta, 1),
                  2: (duration, delta),
                  3: (duration*2, delta)}
        results.append(switch[order][0] < switch[order][1])

        time.sleep(duration) # accesing hypothetical resource here
    finally:
        semaphore.release()
Esempio n. 2
0
def doit(endpoint_url, lock_path):
    while True:
        s = Semaphore(service_name=lock_path, concurrency_limit=1, host_url=endpoint_url)
        print('Attempting to get the lock')
        s.acquire(True)
        print('I has the lockz!!!')
        time.sleep(3)
        print('Releasing lock')
        time.sleep(1)
        s.release()
Esempio n. 3
0
class TestSemaphore(unittest.TestCase):
    def setUp(self):
        self.client = Redis()
        self.client.flushdb()

        self.semaphore = Semaphore(self.client, "test-semaphore")

        self.max_size = 3

    def test_raise_type_error_when_max_size_not_set(self):
        with self.assertRaises(TypeError) as T:
            self.semaphore.acquire("huangz")

    def test_set_max_size_and_get_max_size(self):
        self.semaphore.set_max_size(self.max_size)

        self.assertEqual(self.semaphore.get_max_size(), self.max_size)

        self.semaphore.set_max_size(10)
        self.assertEqual(self.semaphore.get_max_size(), 10)

    def test_get_current_size(self):
        self.semaphore.set_max_size(self.max_size)

        self.assertEqual(self.semaphore.get_current_size(), 0)

        self.semaphore.acquire("huangz")

        self.assertEqual(self.semaphore.get_current_size(), 1)

        self.semaphore.release("huangz")

        self.assertEqual(self.semaphore.get_current_size(), 0)

    def test_acquire_and_release_works(self):
        self.semaphore.set_max_size(self.max_size)

        self.assertTrue(self.semaphore.acquire("huangz"))
        self.assertTrue(self.semaphore.release("huangz"))

    def test_acquire_return_false_when_reach_max_size(self):
        self.semaphore.set_max_size(self.max_size)

        for i in range(self.max_size):
            self.assertTrue(self.semaphore.acquire(i))

        self.assertFalse(
            self.semaphore.acquire("you acquire too much semaphore!"))

    def test_release_return_false_when_identity_not_match(self):
        self.semaphore.set_max_size(self.max_size)

        self.semaphore.set_max_size(self.max_size)

        self.semaphore.acquire("huangz")

        self.assertFalse(self.semaphore.release("wrong identity"))