Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes
test.test_threading.BarrierTests Class Reference
Inheritance diagram for test.test_threading.BarrierTests:
Inheritance graph
[legend]
Collaboration diagram for test.test_threading.BarrierTests:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def setUp
def tearDown
def run_threads
def multipass
def test_barrier
def test_barrier_10
def test_wait_return
def test_action
def test_abort
def test_reset
def test_abort_and_reset
def test_timeout
def test_default_timeout
def test_single_thread
def assertTimeout

Public Attributes

 barrier

Static Public Attributes

tuple barriertype = staticmethod(threading.Barrier)
int N = 5
float defaultTimeout = 2.0

Detailed Description

Definition at line 748 of file test_threading.py.


Member Function Documentation

def test.lock_tests.BaseTestCase.assertTimeout (   self,
  actual,
  expected 
) [inherited]

Definition at line 65 of file lock_tests.py.

00065 
00066     def assertTimeout(self, actual, expected):
00067         # The waiting and/or time.time() can be imprecise, which
00068         # is why comparing to the expected value would sometimes fail
00069         # (especially under Windows).
00070         self.assertGreaterEqual(actual, expected * 0.6)
00071         # Test nothing insane happened
00072         self.assertLess(actual, expected * 10.0)
00073 

Here is the call graph for this function:

Here is the caller graph for this function:

def test.lock_tests.BarrierTests.multipass (   self,
  results,
  n 
) [inherited]

Definition at line 675 of file lock_tests.py.

00675 
00676     def multipass(self, results, n):
00677         m = self.barrier.parties
00678         self.assertEqual(m, self.N)
00679         for i in range(n):
00680             results[0].append(True)
00681             self.assertEqual(len(results[1]), i * m)
00682             self.barrier.wait()
00683             results[1].append(True)
00684             self.assertEqual(len(results[0]), (i + 1) * m)
00685             self.barrier.wait()
00686         self.assertEqual(self.barrier.n_waiting, 0)
00687         self.assertFalse(self.barrier.broken)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.lock_tests.BarrierTests.run_threads (   self,
  f 
) [inherited]

Definition at line 670 of file lock_tests.py.

00670 
00671     def run_threads(self, f):
00672         b = Bunch(f, self.N-1)
00673         f()
00674         b.wait_for_finished()

Here is the caller graph for this function:

def test.lock_tests.BarrierTests.setUp (   self) [inherited]

Reimplemented from test.lock_tests.BaseTestCase.

Definition at line 665 of file lock_tests.py.

00665 
00666     def setUp(self):
        self.barrier = self.barriertype(self.N, timeout=self.defaultTimeout)

Here is the caller graph for this function:

def test.lock_tests.BarrierTests.tearDown (   self) [inherited]

Reimplemented from test.lock_tests.BaseTestCase.

Definition at line 667 of file lock_tests.py.

00667 
00668     def tearDown(self):
00669         self.barrier.abort()

Here is the caller graph for this function:

def test.lock_tests.BarrierTests.test_abort (   self) [inherited]
Test that an abort will put the barrier in a broken state

Definition at line 729 of file lock_tests.py.

00729 
00730     def test_abort(self):
00731         """
00732         Test that an abort will put the barrier in a broken state
00733         """
00734         results1 = []
00735         results2 = []
00736         def f():
00737             try:
00738                 i = self.barrier.wait()
00739                 if i == self.N//2:
00740                     raise RuntimeError
00741                 self.barrier.wait()
00742                 results1.append(True)
00743             except threading.BrokenBarrierError:
00744                 results2.append(True)
00745             except RuntimeError:
00746                 self.barrier.abort()
00747                 pass
00748 
00749         self.run_threads(f)
00750         self.assertEqual(len(results1), 0)
00751         self.assertEqual(len(results2), self.N-1)
00752         self.assertTrue(self.barrier.broken)

Here is the call graph for this function:

Test that a barrier can be reset after being broken.

Definition at line 783 of file lock_tests.py.

00783 
00784     def test_abort_and_reset(self):
00785         """
00786         Test that a barrier can be reset after being broken.
00787         """
00788         results1 = []
00789         results2 = []
00790         results3 = []
00791         barrier2 = self.barriertype(self.N)
00792         def f():
00793             try:
00794                 i = self.barrier.wait()
00795                 if i == self.N//2:
00796                     raise RuntimeError
00797                 self.barrier.wait()
00798                 results1.append(True)
00799             except threading.BrokenBarrierError:
00800                 results2.append(True)
00801             except RuntimeError:
00802                 self.barrier.abort()
00803                 pass
00804             # Synchronize and reset the barrier.  Must synchronize first so
00805             # that everyone has left it when we reset, and after so that no
00806             # one enters it before the reset.
00807             if barrier2.wait() == self.N//2:
00808                 self.barrier.reset()
00809             barrier2.wait()
00810             self.barrier.wait()
00811             results3.append(True)
00812 
00813         self.run_threads(f)
00814         self.assertEqual(len(results1), 0)
00815         self.assertEqual(len(results2), self.N-1)
00816         self.assertEqual(len(results3), self.N)

Here is the call graph for this function:

def test.lock_tests.BarrierTests.test_action (   self) [inherited]
Test the 'action' callback

Definition at line 715 of file lock_tests.py.

00715 
00716     def test_action(self):
00717         """
00718         Test the 'action' callback
00719         """
00720         results = []
00721         def action():
00722             results.append(True)
00723         barrier = self.barriertype(self.N, action)
00724         def f():
00725             barrier.wait()
00726             self.assertEqual(len(results), 1)
00727 
00728         self.run_threads(f)

Here is the call graph for this function:

def test.lock_tests.BarrierTests.test_barrier (   self,
  passes = 1 
) [inherited]
Test that a barrier is passed in lockstep

Definition at line 688 of file lock_tests.py.

00688 
00689     def test_barrier(self, passes=1):
00690         """
00691         Test that a barrier is passed in lockstep
00692         """
00693         results = [[],[]]
00694         def f():
00695             self.multipass(results, passes)
00696         self.run_threads(f)

Here is the call graph for this function:

Here is the caller graph for this function:

Test that a barrier works for 10 consecutive runs

Definition at line 697 of file lock_tests.py.

00697 
00698     def test_barrier_10(self):
00699         """
00700         Test that a barrier works for 10 consecutive runs
00701         """
00702         return self.test_barrier(10)

Here is the call graph for this function:

Test the barrier's default timeout

Definition at line 831 of file lock_tests.py.

00831 
00832     def test_default_timeout(self):
00833         """
00834         Test the barrier's default timeout
00835         """
00836         # create a barrier with a low default timeout
00837         barrier = self.barriertype(self.N, timeout=0.3)
00838         def f():
00839             i = barrier.wait()
00840             if i == self.N // 2:
00841                 # One thread is later than the default timeout of 0.3s.
00842                 time.sleep(1.0)
00843             self.assertRaises(threading.BrokenBarrierError, barrier.wait)
00844         self.run_threads(f)

Here is the call graph for this function:

def test.lock_tests.BarrierTests.test_reset (   self) [inherited]
Test that a 'reset' on a barrier frees the waiting threads

Definition at line 753 of file lock_tests.py.

00753 
00754     def test_reset(self):
00755         """
00756         Test that a 'reset' on a barrier frees the waiting threads
00757         """
00758         results1 = []
00759         results2 = []
00760         results3 = []
00761         def f():
00762             i = self.barrier.wait()
00763             if i == self.N//2:
00764                 # Wait until the other threads are all in the barrier.
00765                 while self.barrier.n_waiting < self.N-1:
00766                     time.sleep(0.001)
00767                 self.barrier.reset()
00768             else:
00769                 try:
00770                     self.barrier.wait()
00771                     results1.append(True)
00772                 except threading.BrokenBarrierError:
00773                     results2.append(True)
00774             # Now, pass the barrier again
00775             self.barrier.wait()
00776             results3.append(True)
00777 
00778         self.run_threads(f)
00779         self.assertEqual(len(results1), 0)
00780         self.assertEqual(len(results2), self.N-1)
00781         self.assertEqual(len(results3), self.N)
00782 

Here is the call graph for this function:

Definition at line 845 of file lock_tests.py.

00845 
00846     def test_single_thread(self):
00847         b = self.barriertype(1)
00848         b.wait()
00849         b.wait()
def test.lock_tests.BarrierTests.test_timeout (   self) [inherited]
Test wait(timeout)

Definition at line 817 of file lock_tests.py.

00817 
00818     def test_timeout(self):
00819         """
00820         Test wait(timeout)
00821         """
00822         def f():
00823             i = self.barrier.wait()
00824             if i == self.N // 2:
00825                 # One thread is late!
00826                 time.sleep(1.0)
00827             # Default timeout is 2.0, so this is shorter.
00828             self.assertRaises(threading.BrokenBarrierError,
00829                               self.barrier.wait, 0.5)
00830         self.run_threads(f)

Here is the call graph for this function:

test the return value from barrier.wait

Definition at line 703 of file lock_tests.py.

00703 
00704     def test_wait_return(self):
00705         """
00706         test the return value from barrier.wait
00707         """
00708         results = []
00709         def f():
00710             r = self.barrier.wait()
00711             results.append(r)
00712 
00713         self.run_threads(f)
00714         self.assertEqual(sum(results), sum(range(self.N)))

Here is the call graph for this function:


Member Data Documentation

Definition at line 666 of file lock_tests.py.

Definition at line 749 of file test_threading.py.

float test.lock_tests.BarrierTests.defaultTimeout = 2.0 [static, inherited]

Definition at line 663 of file lock_tests.py.

int test.lock_tests.BarrierTests.N = 5 [static, inherited]

Definition at line 662 of file lock_tests.py.


The documentation for this class was generated from the following file: