Coverage for tests/test_sharedData.py: 29%
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#
2# LSST Data Management System
3# Copyright 2008, 2009, 2010 LSST Corporation.
4#
5# This product includes software developed by the
6# LSST Project (http://www.lsst.org/).
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the LSST License Statement and
19# the GNU General Public License along with this program. If not,
20# see <http://www.lsstcorp.org/LegalNotices/>.
21#
23"""
24Tests of the SharedData class
25"""
27import unittest
28import threading
29import lsst.utils.tests
30from lsst.ctrl.orca.multithreading import SharedData
33def setup_module(module):
34 lsst.utils.tests.init()
37class ShareDataTestCase(lsst.utils.tests.TestCase):
39 def setUp(self):
40 self.sd = SharedData.SharedData()
42 def tearDown(self):
43 pass
45 def testCtor(self):
46 pass
48 def testAcquire(self):
49 self.assertFalse(self.sd._is_owned(), "lock without acquire")
50 self.sd.acquire()
51 self.assertTrue(self.sd._is_owned(), "lock not acquired")
52 self.sd.acquire()
53 self.assertTrue(self.sd._is_owned(), "lock not kept")
54 self.sd.release()
55 self.assertTrue(self.sd._is_owned(), "lock not kept after partial release")
56 self.sd.release()
57 self.assertFalse(self.sd._is_owned(), "lock not released")
59 def testNoData(self):
60 attrs = self.sd.dir()
61 self.assertEqual(len(attrs), 0, "Non-empty protected dir: " + str(attrs))
62 self.sd.acquire()
63 self.assertTrue(self.sd.__, "__ not set")
64 self.sd.release()
66 def testWith(self):
67 with self.sd:
68 self.assertTrue(self.sd._is_owned(), "lock not acquired")
69 self.assertFalse(self.sd._is_owned(), "lock not released")
71 def _initData(self):
72 self.sd.initData({"name": "Ray", "test": True, "config": {}})
74 def testNoLockRead(self):
75 self._initData()
76 with self.assertRaises(AttributeError):
77 self.sd.name
78 self.sd.dir()
80 def testInit(self):
81 self._initData()
82 attrs = self.sd.dir()
83 self.assertEqual(len(attrs), 3, "Wrong number of items: "+str(attrs))
84 for key in "name test config".split():
85 self.assertIn(key, attrs, "Missing attr: " + key)
87 def testAccess(self):
88 self._initData()
89 protected = None
90 with self.sd:
91 self.assertEqual(self.sd.name, "Ray")
92 self.assertIsInstance(self.sd.test, bool)
93 self.assertTrue(self.sd.test)
94 protected = self.sd.config
95 self.assertIsInstance(protected, dict)
96 self.assertEqual(len(protected), 0)
98 # test unsafe access
99 protected["goob"] = "gurn"
100 with self.sd:
101 self.assertEqual(self.sd.config["goob"], "gurn")
103 def testUpdate(self):
104 self._initData()
105 with self.sd:
106 self.sd.name = "Plante"
107 self.assertEqual(self.sd.name, "Plante")
108 attrs = self.sd.dir()
109 self.assertEqual(len(attrs), 3, "Wrong number of items: "+str(attrs))
111 def testAdd(self):
112 self._initData()
113 with self.sd:
114 self.sd.lname = "Plante"
115 attrs = self.sd.dir()
116 self.assertEqual(len(attrs), 4, "Wrong number of items: "+str(attrs))
117 self.assertEqual(self.sd.lname, "Plante")
120class ReadableShareDataTestCase(lsst.utils.tests.TestCase):
122 def setUp(self):
123 self.sd = SharedData.SharedData(False)
125 def tearDown(self):
126 pass
128 def testAcquire(self):
129 self.assertFalse(self.sd._is_owned(), "lock without acquire")
130 self.sd.acquire()
131 self.assertTrue(self.sd._is_owned(), "lock not acquired")
132 self.sd.acquire()
133 self.assertTrue(self.sd._is_owned(), "lock not kept")
134 self.sd.release()
135 self.assertTrue(self.sd._is_owned(), "lock not kept after partial release")
136 self.sd.release()
137 self.assertFalse(self.sd._is_owned(), "lock not released")
139 def testNoData(self):
140 attrs = self.sd.dir()
141 self.assertEqual(len(attrs), 0, "Non-empty protected dir: " + str(attrs))
142 self.assertTrue(self.sd.__, "__ not set")
144 def _initData(self):
145 self.sd.initData({"name": "Ray", "test": True, "config": {}})
147 def testNoLockRead(self):
148 self._initData()
149 self.sd.name
150 self.sd.dir()
151 with self.assertRaises(AttributeError):
152 self.sd.goob
154 def testInit(self):
155 self._initData()
156 attrs = self.sd.dir()
157 self.assertEqual(len(attrs), 3, "Wrong number of items: "+str(attrs))
158 for key in "name test config".split():
159 self.assertIn(key, attrs, "Missing attr: " + key)
161 def testAccess(self):
162 self._initData()
163 protected = None
165 self.assertEqual(self.sd.name, "Ray")
166 self.assertIsInstance(self.sd.test, bool)
167 self.assertTrue(self.sd.test)
168 protected = self.sd.config
169 self.assertIsInstance(protected, dict)
170 self.assertEqual(len(protected), 0)
172 # test unsafe access
173 protected["goob"] = "gurn"
174 self.assertEqual(self.sd.config["goob"], "gurn")
176 def testUpdate(self):
177 self._initData()
178 with self.sd:
179 self.sd.name = "Plante"
180 self.assertEqual(self.sd.name, "Plante")
181 attrs = self.sd.dir()
182 self.assertEqual(len(attrs), 3, "Wrong number of items: "+str(attrs))
184 def testAdd(self):
185 self._initData()
186 with self.sd:
187 self.sd.lname = "Plante"
188 attrs = self.sd.dir()
189 self.assertEqual(len(attrs), 4, "Wrong number of items: "+str(attrs))
190 self.assertEqual(self.sd.lname, "Plante")
193class MultiThreadTestCase(lsst.utils.tests.TestCase):
195 def setUp(self):
196 self.sd = SharedData.SharedData(False, {"c": 0})
198 def tearDown(self):
199 pass
201 def testThreads(self):
202 t = TstThread(self.sd)
203 # pdb.set_trace()
204 self.assertEqual(self.sd.c, 0)
205 # print "c = ", self.sd.c
207 with self.sd:
208 t.start()
210 # now take a turn
211 self.sd.wait()
212 # print "c = ", self.sd.c
213 self.assertEqual(self.sd.c, 1)
214 # print "WILL NOTIFY"
215 self.sd.notifyAll()
217 # time.sleep(1.01)
218 with self.sd:
219 # print "WILL WAIT"
220 self.sd.wait(2.0)
221 self.assertEqual(self.sd.c, 0)
224class TstThread(threading.Thread):
226 def __init__(self, data):
227 threading.Thread.__init__(self)
228 self.data = data
230 def run(self):
231 with self.data:
232 # pdb.set_trace()
233 self.data.c += 1
234 # print "++c = ", self.data.c
236 self.data.notifyAll()
237 # print "waiting", self.data.c
238 self.data.wait()
240 self.data.c -= 1
241 # print "--c = ", self.data.c
242 self.data.notifyAll()
245__all__ = "SharedDataTestCase ReadableShareDataTestCase MultiThreadTestCase".split()
248class SharedDataMemoryTester(lsst.utils.tests.MemoryTestCase):
249 pass
252if __name__ == "__main__": 252 ↛ 253line 252 didn't jump to line 253, because the condition on line 252 was never true
253 lsst.utils.tests.init()
254 unittest.main()