Exemple #1
0
    def test_acquire_release_basic_case(self):
        sem = SlidingWindowSemaphore(1)
        # Count is 1

        num = sem.acquire('a', blocking=False)
        self.assertEqual(num, 0)
        sem.release('a', 0)
Exemple #2
0
    def test_acquire_release_basic_case(self):
        sem = SlidingWindowSemaphore(1)
        # Count is 1

        num = sem.acquire('a', blocking=False)
        self.assertEqual(num, 0)
        sem.release('a', 0)
Exemple #3
0
    def test_can_acquire_release_multiple_times(self):
        sem = SlidingWindowSemaphore(1)
        num = sem.acquire('a', blocking=False)
        self.assertEqual(num, 0)
        sem.release('a', num)

        num = sem.acquire('a', blocking=False)
        self.assertEqual(num, 1)
        sem.release('a', num)
Exemple #4
0
    def test_can_acquire_release_multiple_times(self):
        sem = SlidingWindowSemaphore(1)
        num = sem.acquire('a', blocking=False)
        self.assertEqual(num, 0)
        sem.release('a', num)

        num = sem.acquire('a', blocking=False)
        self.assertEqual(num, 1)
        sem.release('a', num)
Exemple #5
0
    def test_can_check_in_partial_range(self):
        sem = SlidingWindowSemaphore(4)
        sem.acquire('a', blocking=False)
        sem.acquire('a', blocking=False)
        sem.acquire('a', blocking=False)
        sem.acquire('a', blocking=False)

        sem.release('a', 1)
        sem.release('a', 3)
        sem.release('a', 0)
        self.assertEqual(sem.current_count(), 2)
Exemple #6
0
    def test_can_check_in_partial_range(self):
        sem = SlidingWindowSemaphore(4)
        sem.acquire('a', blocking=False)
        sem.acquire('a', blocking=False)
        sem.acquire('a', blocking=False)
        sem.acquire('a', blocking=False)

        sem.release('a', 1)
        sem.release('a', 3)
        sem.release('a', 0)
        self.assertEqual(sem.current_count(), 2)
Exemple #7
0
 def test_is_error_to_double_release(self):
     # This is different than other error tests because
     # we're verifying we can reset the state after an
     # acquire/release cycle.
     sem = SlidingWindowSemaphore(2)
     sem.acquire('a', blocking=False)
     sem.acquire('a', blocking=False)
     sem.release('a', 0)
     sem.release('a', 1)
     self.assertEqual(sem.current_count(), 2)
     with self.assertRaises(ValueError):
         sem.release('a', 0)
Exemple #8
0
 def test_is_error_to_double_release(self):
     # This is different than other error tests because
     # we're verifying we can reset the state after an
     # acquire/release cycle.
     sem = SlidingWindowSemaphore(2)
     sem.acquire('a', blocking=False)
     sem.acquire('a', blocking=False)
     sem.release('a', 0)
     sem.release('a', 1)
     self.assertEqual(sem.current_count(), 2)
     with self.assertRaises(ValueError):
         sem.release('a', 0)
Exemple #9
0
 def test_can_acquire_a_range(self):
     sem = SlidingWindowSemaphore(3)
     self.assertEqual(sem.acquire('a', blocking=False), 0)
     self.assertEqual(sem.acquire('a', blocking=False), 1)
     self.assertEqual(sem.acquire('a', blocking=False), 2)
     sem.release('a', 0)
     sem.release('a', 1)
     sem.release('a', 2)
     # Now we're reset so we should be able to acquire the same
     # sequence again.
     self.assertEqual(sem.acquire('a', blocking=False), 3)
     self.assertEqual(sem.acquire('a', blocking=False), 4)
     self.assertEqual(sem.acquire('a', blocking=False), 5)
     self.assertEqual(sem.current_count(), 0)
Exemple #10
0
 def test_can_acquire_a_range(self):
     sem = SlidingWindowSemaphore(3)
     self.assertEqual(sem.acquire('a', blocking=False), 0)
     self.assertEqual(sem.acquire('a', blocking=False), 1)
     self.assertEqual(sem.acquire('a', blocking=False), 2)
     sem.release('a', 0)
     sem.release('a', 1)
     sem.release('a', 2)
     # Now we're reset so we should be able to acquire the same
     # sequence again.
     self.assertEqual(sem.acquire('a', blocking=False), 3)
     self.assertEqual(sem.acquire('a', blocking=False), 4)
     self.assertEqual(sem.acquire('a', blocking=False), 5)
     self.assertEqual(sem.current_count(), 0)
Exemple #11
0
    def test_stress_invariants_random_order(self):
        sem = SlidingWindowSemaphore(100)
        for _ in range(10):
            recorded = []
            for _ in range(100):
                recorded.append(sem.acquire('a', blocking=False))
            # Release them in randomized order.  As long as we
            # eventually free all 100, we should have all the
            # resources released.
            random.shuffle(recorded)
            for i in recorded:
                sem.release('a', i)

        # Everything's freed so should be back at count == 100
        self.assertEqual(sem.current_count(), 100)
Exemple #12
0
    def test_stress_invariants_random_order(self):
        sem = SlidingWindowSemaphore(100)
        for _ in range(10):
            recorded = []
            for _ in range(100):
                recorded.append(sem.acquire('a', blocking=False))
            # Release them in randomized order.  As long as we
            # eventually free all 100, we should have all the
            # resources released.
            random.shuffle(recorded)
            for i in recorded:
                sem.release('a', i)

        # Everything's freed so should be back at count == 100
        self.assertEqual(sem.current_count(), 100)
Exemple #13
0
    def test_release_counters_can_increment_counter_repeatedly(self):
        sem = SlidingWindowSemaphore(3)
        sem.acquire('a', blocking=False)
        sem.acquire('a', blocking=False)
        sem.acquire('a', blocking=False)

        # These two releases don't increment the counter
        # because we're waiting on 0.
        sem.release('a', 1)
        sem.release('a', 2)
        self.assertEqual(sem.current_count(), 0)
        # But as soon as we release 0, we free up 0, 1, and 2.
        sem.release('a', 0)
        self.assertEqual(sem.current_count(), 3)
        sem.acquire('a', blocking=False)
        sem.acquire('a', blocking=False)
        sem.acquire('a', blocking=False)
Exemple #14
0
    def test_release_counters_can_increment_counter_repeatedly(self):
        sem = SlidingWindowSemaphore(3)
        sem.acquire('a', blocking=False)
        sem.acquire('a', blocking=False)
        sem.acquire('a', blocking=False)

        # These two releases don't increment the counter
        # because we're waiting on 0.
        sem.release('a', 1)
        sem.release('a', 2)
        self.assertEqual(sem.current_count(), 0)
        # But as soon as we release 0, we free up 0, 1, and 2.
        sem.release('a', 0)
        self.assertEqual(sem.current_count(), 3)
        sem.acquire('a', blocking=False)
        sem.acquire('a', blocking=False)
        sem.acquire('a', blocking=False)
Exemple #15
0
    def test_counter_release_only_on_min_element(self):
        sem = SlidingWindowSemaphore(3)
        sem.acquire('a', blocking=False)
        sem.acquire('a', blocking=False)
        sem.acquire('a', blocking=False)

        # The count only increases when we free the min
        # element.  This means if we're currently failing to
        # acquire now:
        with self.assertRaises(NoResourcesAvailable):
            sem.acquire('a', blocking=False)

        # Then freeing a non-min element:
        sem.release('a', 1)

        # doesn't change anything.  We still fail to acquire.
        with self.assertRaises(NoResourcesAvailable):
            sem.acquire('a', blocking=False)
        self.assertEqual(sem.current_count(), 0)
Exemple #16
0
    def test_counter_release_only_on_min_element(self):
        sem = SlidingWindowSemaphore(3)
        sem.acquire('a', blocking=False)
        sem.acquire('a', blocking=False)
        sem.acquire('a', blocking=False)

        # The count only increases when we free the min
        # element.  This means if we're currently failing to
        # acquire now:
        with self.assertRaises(NoResourcesAvailable):
            sem.acquire('a', blocking=False)

        # Then freeing a non-min element:
        sem.release('a', 1)

        # doesn't change anything.  We still fail to acquire.
        with self.assertRaises(NoResourcesAvailable):
            sem.acquire('a', blocking=False)
        self.assertEqual(sem.current_count(), 0)
Exemple #17
0
    def test_can_handle_multiple_tags_released(self):
        sem = SlidingWindowSemaphore(4)
        sem.acquire('a', blocking=False)
        sem.acquire('a', blocking=False)
        sem.acquire('b', blocking=False)
        sem.acquire('b', blocking=False)

        sem.release('b', 1)
        sem.release('a', 1)
        self.assertEqual(sem.current_count(), 0)

        sem.release('b', 0)
        self.assertEqual(sem.acquire('a', blocking=False), 2)

        sem.release('a', 0)
        self.assertEqual(sem.acquire('b', blocking=False), 2)
Exemple #18
0
    def test_can_handle_multiple_tags_released(self):
        sem = SlidingWindowSemaphore(4)
        sem.acquire('a', blocking=False)
        sem.acquire('a', blocking=False)
        sem.acquire('b', blocking=False)
        sem.acquire('b', blocking=False)

        sem.release('b', 1)
        sem.release('a', 1)
        self.assertEqual(sem.current_count(), 0)

        sem.release('b', 0)
        self.assertEqual(sem.acquire('a', blocking=False), 2)

        sem.release('a', 0)
        self.assertEqual(sem.acquire('b', blocking=False), 2)
Exemple #19
0
    def test_acquire_blocks_until_release_is_called(self):
        sem = SlidingWindowSemaphore(2)
        sem.acquire('a', blocking=False)
        sem.acquire('a', blocking=False)

        def acquire():
            # This next call to acquire will block.
            self.assertEqual(sem.acquire('a', blocking=True), 2)

        t = threading.Thread(target=acquire)
        self.threads.append(t)
        # Starting the thread will block the sem.acquire()
        # in the acquire function above.
        t.start()
        # This still will keep the thread blocked.
        sem.release('a', 1)
        # Releasing the min element will unblock the thread.
        sem.release('a', 0)
        t.join()
        sem.release('a', 2)
Exemple #20
0
    def test_acquire_blocks_until_release_is_called(self):
        sem = SlidingWindowSemaphore(2)
        sem.acquire('a', blocking=False)
        sem.acquire('a', blocking=False)

        def acquire():
            # This next call to acquire will block.
            self.assertEqual(sem.acquire('a', blocking=True), 2)

        t = threading.Thread(target=acquire)
        self.threads.append(t)
        # Starting the thread will block the sem.acquire()
        # in the acquire function above.
        t.start()
        # This still will keep the thread blocked.
        sem.release('a', 1)
        # Releasing the min element will unblock the thread.
        sem.release('a', 0)
        t.join()
        sem.release('a', 2)
Exemple #21
0
 def test_error_to_release_unknown_tag(self):
     sem = SlidingWindowSemaphore(3)
     with self.assertRaises(ValueError):
         sem.release('a', 0)
Exemple #22
0
 def test_is_error_to_release_unknown_sequence_number(self):
     sem = SlidingWindowSemaphore(3)
     sem.acquire('a', blocking=False)
     with self.assertRaises(ValueError):
         sem.release('a', 1)
Exemple #23
0
 def test_error_to_release_unknown_tag(self):
     sem = SlidingWindowSemaphore(3)
     with self.assertRaises(ValueError):
         sem.release('a', 0)
Exemple #24
0
 def test_is_error_to_release_unknown_sequence_number(self):
     sem = SlidingWindowSemaphore(3)
     sem.acquire('a', blocking=False)
     with self.assertRaises(ValueError):
         sem.release('a', 1)