1from PyObjCTools.TestSupport import *
2import objc
3import sys
4import time
5
6from PyObjCTest.testhelper import PyObjC_TestClass4
7
8from Foundation import *
9
10class ThreadingTest (TestCase):
11    def setUp(self):
12        # Set a very small check interval, this will make it more likely
13        # that the interpreter crashes when threading is done incorrectly.
14        self._int = sys.getcheckinterval()
15        sys.setcheckinterval(1)
16
17    def tearDown(self):
18        sys.setcheckinterval(self._int)
19
20    def testNSObjectString(self):
21
22        class PyObjCTestThreadRunnerString (NSObject):
23            def init(self):
24                self = super(PyObjCTestThreadRunnerString, self).init()
25                if self is None: return None
26
27                self.storage = []
28                return self
29
30            def run_(self, argument):
31                NSAutoreleasePool.alloc().init()
32                self.storage.append(argument)
33
34        myObj = PyObjCTestThreadRunnerString.alloc().init()
35
36        NSThread.detachNewThreadSelector_toTarget_withObject_(
37                'run:', myObj, u"hello world")
38
39        time.sleep(2)
40        self.assertEquals(myObj.storage[0], u"hello world")
41
42    def testNSObject(self):
43
44        class PyObjCTestThreadRunner (NSObject):
45            def run_(self, argument):
46                NSAutoreleasePool.alloc().init()
47                for i in range(100):
48                    argument.append(i)
49
50        myObj = PyObjCTestThreadRunner.alloc().init()
51        lst = []
52
53        NSThread.detachNewThreadSelector_toTarget_withObject_(
54                'run:', myObj, lst)
55
56        lst2 = []
57        for i in range(100):
58            lst2.append(i*2)
59
60        time.sleep(2)
61        self.assertEquals(lst, range(100))
62
63    def testPyObject(self):
64        import os
65
66        class TestThreadRunner :
67            def run_(self, argument):
68                for i in range(100):
69                    argument.append(i)
70
71        myObj = TestThreadRunner()
72        lst = []
73
74        # Redirect stderr to avoid spurious messages when running the
75        # tests.
76        dupped = os.dup(2)
77        fp = os.open('/dev/null', os.O_RDWR)
78        os.dup2(fp, 2)
79        os.close(fp)
80
81        try:
82            NSThread.detachNewThreadSelector_toTarget_withObject_(
83                'run:', myObj, lst)
84
85            lst2 = []
86            for i in range(100):
87                lst2.append(i*2)
88
89            time.sleep(2)
90            self.assertEquals(lst, range(100))
91
92        finally:
93            os.dup2(dupped, 2)
94
95    def testCalling(self):
96        class Dummy:
97            pass
98        class PyObjCTestCalling (NSObject) :
99            def call(self):
100                return Dummy()
101
102        my = PyObjC_TestClass4.alloc().init()
103        cb = PyObjCTestCalling.alloc().init()
104
105        NSThread.detachNewThreadSelector_toTarget_withObject_(
106                'runThread:', my,  cb)
107
108        time.sleep(2)
109
110        retval = my.returnObject()
111        self.assert_(isinstance(retval, Dummy))
112
113if __name__ == "__main__":
114    main()
115