• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from testing.embedding.test_basic import EmbeddingTests
2
3
4class TestThread(EmbeddingTests):
5    def test_first_calls_in_parallel(self):
6        add1_cffi = self.prepare_module('add1')
7        self.compile('thread1-test', [add1_cffi], threads=True)
8        for i in range(20):
9            output = self.execute('thread1-test')
10            assert output == ("starting\n"
11                              "preparing...\n" +
12                              "adding 40 and 2\n" * 10 +
13                              "done\n")
14
15    def _take_out(self, text, content):
16        assert content in text
17        i = text.index(content)
18        return text[:i] + text[i+len(content):]
19
20    def test_init_different_modules_in_different_threads(self):
21        add1_cffi = self.prepare_module('add1')
22        add2_cffi = self.prepare_module('add2')
23        self.compile('thread2-test', [add1_cffi, add2_cffi], threads=True)
24        for i in range(3):
25            output = self.execute('thread2-test')
26            print('='*79)
27            print(output)
28            print('='*79)
29            output = self._take_out(output, "preparing")
30            output = self._take_out(output, ".")
31            output = self._take_out(output, ".")
32            # at least the 3rd dot should be after everything from ADD2
33            assert output == ("starting\n"
34                              "prepADD2\n"
35                              "adding 1000 and 200 and 30\n"
36                              ".\n"
37                              "adding 40 and 2\n"
38                              "done\n")
39
40    def test_alt_issue(self):
41        add1_cffi = self.prepare_module('add1')
42        add2_cffi = self.prepare_module('add2')
43        self.compile('thread2-test', [add1_cffi, add2_cffi],
44                     threads=True, defines={'T2TEST_AGAIN_ADD1': '1'})
45        output = self.execute('thread2-test')
46        output = self._take_out(output, "adding 40 and 2\n")
47        assert output == ("starting\n"
48                          "preparing...\n"
49                          "adding -1 and -1\n"
50                          "prepADD2\n"
51                          "adding 1000 and 200 and 30\n"
52                          "done\n")
53
54    def test_load_in_parallel_more(self):
55        add2_cffi = self.prepare_module('add2')
56        add3_cffi = self.prepare_module('add3')
57        self.compile('thread3-test', [add2_cffi, add3_cffi], threads=True)
58        for i in range(150):
59            output = self.execute('thread3-test')
60            for j in range(10):
61                output = self._take_out(output, "adding 40 and 2 and 100\n")
62                output = self._take_out(output, "adding 1000, 200, 30, 4\n")
63            assert output == ("starting\n"
64                              "prepADD2\n"
65                              "done\n")
66