File: test_trace_threaded.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 (58 lines) | stat: -rw-r--r-- 1,403 bytes parent folder | download | duplicates (8)
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
import sys
import threading
import time
import unittest

from test import test_support

class UntracedThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        sys.settrace(None)
        for i in range(10):
            self.untracedcall()

    def untracedcall(self):
        pass

class TracedThread(threading.Thread):
    def __init__(self, on_trace):
        threading.Thread.__init__(self)
        self.on_trace = on_trace

    def trace(self, frame, event, arg):
        self.on_trace(frame.f_code.co_name)

    def tracedcall(self):
        pass

    def run(self):
        sys.settrace(self.trace)
        for i in range(10):
            self.tracedcall()

class TracePerThreadTest(unittest.TestCase):
    def testTracePerThread(self):
        called = []
        def ontrace(co_name):
            called.append(str(co_name))

        untraced = UntracedThread()
        traced = TracedThread(ontrace)
        untraced.start()
        traced.start()
        untraced.join()
        traced.join()

        self.assertEquals(10, called.count('tracedcall'),
                "10 tracedcall should be in %s" % called)
        self.assert_('untracedcall' not in called,
                "untracedcall shouldn't be in %s" % called)

def test_main():
    test_support.run_unittest(TracePerThreadTest)

if __name__ == "__main__":
    test_main()