forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_test.py
67 lines (48 loc) · 1.75 KB
/
context_test.py
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
from caffe2.python import context, test_util
from threading import Thread
class MyContext(context.Managed):
pass
class DefaultMyContext(context.DefaultManaged):
pass
class ChildMyContext(MyContext):
pass
class TestContext(test_util.TestCase):
def use_my_context(self):
try:
for _ in range(100):
with MyContext() as a:
for _ in range(100):
self.assertTrue(MyContext.current() == a)
except Exception as e:
self._exceptions.append(e)
def testMultiThreaded(self):
threads = []
self._exceptions = []
for _ in range(8):
thread = Thread(target=self.use_my_context)
thread.start()
threads.append(thread)
for t in threads:
t.join()
for e in self._exceptions:
raise e
@MyContext()
def testDecorator(self):
self.assertIsNotNone(MyContext.current())
def testNonDefaultCurrent(self):
with self.assertRaises(AssertionError):
MyContext.current()
ctx = MyContext()
self.assertEqual(MyContext.current(value=ctx), ctx)
self.assertIsNone(MyContext.current(required=False))
def testDefaultCurrent(self):
self.assertIsInstance(DefaultMyContext.current(), DefaultMyContext)
def testNestedContexts(self):
with MyContext() as ctx1:
with DefaultMyContext() as ctx2:
self.assertEqual(DefaultMyContext.current(), ctx2)
self.assertEqual(MyContext.current(), ctx1)
def testChildClasses(self):
with ChildMyContext() as ctx:
self.assertEqual(ChildMyContext.current(), ctx)
self.assertEqual(MyContext.current(), ctx)