JythonBook / src / chapter19 / test_lock.py

from __future__ import with_statement
from threading import Lock
from threadsafety import ThreadSafetyTestCase
import time
import unittest

class LockTestCase(ThreadSafetyTestCase):
    
    def test_with_lock(self):
        counter = [0]
        lock = Lock()
        def loop100(counter):
            for i in xrange(100):
                with lock:
                    counter[0] += 1
                # sleeping helps ensures that all threads run in our test
                time.sleep(0.0001)

        self.assertContended(loop100, args=(counter,), num_threads=20)
        self.assertEqual(counter[0], 2000) # 20 threads * 100 loops/thread

    def test_try_finally_lock(self):
        counter = [0]
        lock = Lock()
        def loop100(counter):
            for i in xrange(100):
                lock.acquire()
                try:
                    counter[0] += 1
                finally:
                    lock.release()
                time.sleep(0.0001)

        self.assertContended(loop100, args=(counter,), num_threads=20)
        self.assertEqual(counter[0], 2000)

    def test_unlocked_contention(self):
        # increase threads and loop iterations to make it very
        # unlikely the unlocked version will inadvertently pass this
        # test (but it still might)
        counter = [0]
        lock = Lock()
        def loop1000(counter):
            for i in xrange(1000):
                counter[0] += 1
                time.sleep(0.0001)

        self.assertContended(loop1000, args=(counter,), num_threads=200)
        self.assertNotEqual(counter[0], 200000)

if __name__ == '__main__':
    unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.