-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathtest_Thread.py
More file actions
88 lines (64 loc) · 1.99 KB
/
test_Thread.py
File metadata and controls
88 lines (64 loc) · 1.99 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import threading
import unittest
import pytest
import STPyV8
class TestThread(unittest.TestCase):
@pytest.mark.order(1)
def testMultiPythonThread(self):
class Global:
count = 0
started = threading.Event()
finished = threading.Semaphore(0)
def sleep(self, ms):
time.sleep(ms / 1000.0)
self.count += 1
g = Global()
def run():
with STPyV8.JSIsolate():
with STPyV8.JSContext(g) as ctxt:
ctxt.eval(
"""
started.wait();
for (i=0; i<10; i++)
{
sleep(100);
}
finished.release();
"""
)
t = threading.Thread(target=run)
t.start()
now = time.time()
self.assertEqual(0, g.count)
g.started.set()
g.finished.acquire()
self.assertEqual(10, g.count)
self.assertTrue((time.time() - now) >= 1)
t.join()
@pytest.mark.order(2)
def testMultiJavascriptThread(self):
class Global(STPyV8.JSContext):
result = []
def add(self, value):
with STPyV8.JSUnlocker():
self.result.append(value)
g = Global()
def run():
with STPyV8.JSIsolate():
with STPyV8.JSContext(g) as ctxt:
ctxt.eval(
"""
for (i=0; i<10; i++)
add(i);
"""
)
threads = [threading.Thread(target=run), threading.Thread(target=run)]
with STPyV8.JSLocker():
for t in threads:
t.start()
for t in threads:
t.join()
self.assertEqual(20, len(g.result))