File: test_threading_local_jy.py

package info (click to toggle)
jython 2.5.3-16%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,772 kB
  • ctags: 106,434
  • sloc: python: 351,322; java: 216,349; xml: 1,584; sh: 330; perl: 114; ansic: 102; makefile: 45
file content (61 lines) | stat: -rw-r--r-- 1,801 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Test for thread locals"""
import random
import sys
import threading
import time
import unittest
from test import test_support
from threading import local

class LocalStuff(local):
    def __init__(self, stuff, foo=1):
        local.__init__(self)
        self.stuff = stuff
        self.foo = foo

class TestThread(threading.Thread):
    def __init__(self, stuff, name):
        threading.Thread.__init__(self)
        self.stuff = stuff
        self.name = name
        self.errors = []

    def run(self):
        for i in xrange(10):
            try:
                self.stuff.stuff = self.name
                myStuff = self.stuff.stuff
                time.sleep(random.random() * 2)
                if myStuff != self.stuff.stuff:
                    self.errors.append("myStuff should equal self.stuff.stuff")
                if self.stuff.foo != 1:
                    self.errors.append("foo should be 1")
            except TypeError, te:
                self.errors.append("TypeError: %s" % te)
            except:
                self.errors.append("unexpected error: %s" % sys.exc_info()[0] )

    def getErrors(self):
        return self.errors

class ThreadLocalConstructorTestCase(unittest.TestCase):

    def test_construct_locals(self):
        """Ensures that constructing a local can have arguments"""
        stuff = LocalStuff("main stuff")
        threads = []
        for i in xrange(20):
            threads.append(TestThread(stuff, name=("thread-%d" % i)))
            threads[i].start()
        for i in xrange(20):
            threads[i].join()
            errors = threads[i].getErrors()
            self.assertEquals(0, len(errors), errors)


def test_main():
    test_support.run_unittest(ThreadLocalConstructorTestCase)


if __name__ == "__main__":
    test_main()