-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_globals.py
56 lines (44 loc) · 1.38 KB
/
test_globals.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
from time import sleep
from unittest2 import TestCase
from threading import Thread
from cosmic.globals import *
from cosmic.globals import storage
class TestSwappableDict(TestCase):
def test_dict_methods(self):
s = SwappableDict()
s['a'] = 1
self.assertEqual(s['a'], 1)
self.assertEqual(len(s), 1)
del s['a']
self.assertEqual(len(s), 0)
def test_swap(self):
s = SwappableDict()
s.update({'a': 1})
self.assertEqual(dict(s), {'a': 1})
with s.swap({'b': 2}):
self.assertEqual(dict(s), {'b': 2})
self.assertEqual(dict(s), {'a': 1})
class TestThreadLocalDict(TestCase):
def test_unbound(self):
s = ThreadLocalDict()
with self.assertRaises(Exception):
s['a'] = 1
def test_threads(self):
s = ThreadLocalDict()
def run():
sleep(0.2)
with thread_local():
self.assertEqual(dict(s), {})
s['a'] = 1
self.assertEqual(dict(s), {'a': 1})
del s['a']
self.assertEqual(dict(s), {})
sleep(0.2)
threads = []
for i in range(10):
thread = Thread(target=run)
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
self.assertEqual(storage, {})