def inner(self): if self.roothandle: return self.roothandle.inner.Sub(self.basename) elif self.basename: return handle.Handle(self.obj).Sub(self.basename) else: return handle.Handle(self.obj)
def testNonexistent(self): root = TestObject() cpe = api.CPE(handle.Handle(root)) cpe.parameter_attrs.set_notification_parameters_cb = SetNotification cpe.parameter_attrs.new_value_change_session_cb = NewSession f = FakeAttrs() f.Name = 'AutoThingy.3' f.Notification = 2 f.NotificationChange = 'true' cpe.SetParameterAttributes(f) self.assertEqual(len(cpe.parameter_attrs.params), 1) root.NumAutoThingies = 1 # Check that this doesn't raise an exception cpe.parameter_attrs.CheckForTriggers() self.assertEqual(0, len(set_notification_arg[0])) root.NumAutoThingies = 5 self.assertEqual(0, len(set_notification_arg[0])) root.AutoThingyList['3'].word = 'word1' cpe.parameter_attrs.CheckForTriggers() self.assertEqual(1, len(set_notification_arg[0]))
def testSetAttr(self): root = TestSimpleRoot() cpe = api.CPE(handle.Handle(root)) f = FakeAttrs() f.Name = 'SomeParam' f.Notification = 2 cpe.SetParameterAttributes(f) self.assertEqual(len(cpe.parameter_attrs.params), 1) self.assertEqual(cpe.parameter_attrs.params['SomeParam'].notification, 0) f.Name = 'SomeParam' f.Notification = 2 f.NotificationChange = 'true' cpe.SetParameterAttributes(f) self.assertEqual(len(cpe.parameter_attrs.params), 1) self.assertEqual(2, cpe.parameter_attrs.params['SomeParam'].notification) cpe.parameter_attrs.set_notification_parameters_cb = SetNotification cpe.parameter_attrs.new_value_change_session_cb = NewSession # The value hasn't changed, so this shouldn't do anything. cpe.parameter_attrs.CheckForTriggers() self.assertEqual(0, len(set_notification_arg[0])) self.assertEqual(0, new_session_called[0]) # Change the value and make sure a new session is triggered. root.SomeParam = 'NewValue' cpe.parameter_attrs.CheckForTriggers() self.assertEqual(1, len(set_notification_arg[0])) self.assertEqual('SomeParam', set_notification_arg[0][0][0]) self.assertEqual(root.SomeParam, set_notification_arg[0][0][1]) self.assertEqual(1, new_session_called[0])
def handle(self, thread_id): most_wait = 10 while self.threadNums['download'] or not self.__dataQueue.empty(): data = self.__getData() if not data: if self.threadNums['download'] <= 1 and self.__dataQueue.empty( ): if most_wait <= 0: break most_wait -= 1 time.sleep(1) continue o_handle = handle.Handle(self.__config, data, self.__com, self.__comLock) o_handle.setThreadId(thread_id) o_handle.run() self.__handleTime += o_handle.getUsedTime() self.__handleCount += 1 funcUtil.recordStatus( self.__id, '%s uri: %s use time: %.2f size: %d' % (thread_id, str(o_handle.getUri()), o_handle.getUsedTime(), self.__dataQueue.qsize())) # print thread_id + ' uri: ' + str(o_handle.getUri()) + ' use time: ' + str(o_handle.getUsedTime()) + ' size: ' + str(self.__dataQueue.qsize()) self.status[thread_id] = self.STATUS_END self.threadNums['handle'] -= 1
def testDeleteParam(self): root = TestObject() cpe = api.CPE(handle.Handle(root)) (unused_idx, unused_obj) = cpe.AddObject('Thingy.', '1') f = FakeAttrs() f.Name = 'Thingy.1' f.Notification = 2 cpe.SetParameterAttributes(f) self.assertEqual(len(cpe.parameter_attrs.params), 1) cpe.DeleteObject('Thingy.1.', 'fake-key') self.assertEqual(len(cpe.parameter_attrs.params), 0)
def testCanonicalName(self): o = TestObject() assert hasattr(o, '_lastindex') assert hasattr(o.SubObj, '_lastindex') print o.export_params, o.export_objects, o.export_object_lists h = handle.Handle(o) self.assertTrue(o) h.ValidateExports() name = handle.Handle.GetCanonicalName(o, o.SubObj) self.assertEqual('SubObj', name) (unused_idx1, unused_obj1) = h.AddExportObject('Counter') (unused_idx2, unused_obj2) = h.AddExportObject('Counter') (unused_idx3, obj3) = h.AddExportObject('Counter') name = handle.Handle.GetCanonicalName(o, obj3) self.assertEqual('Counter.3', name)
def testSetAttrErrors(self): root = TestSimpleRoot() cpe = api.CPE(handle.Handle(root)) error_list = [] cpe._Apply(error_list, 'fullname', api.ParameterNotWritableError, RaiseAttributeError, []) cpe._Apply(error_list, 'fullname', None, RaiseTypeError, []) cpe._Apply(error_list, 'fullname', None, RaiseValueError, []) cpe._Apply(error_list, 'fullname', None, RaiseKeyError, []) cpe._Apply(error_list, 'fullname', None, RaiseIOError, []) self.assertEqual(5, len(error_list)) self.assertEqual(api.ParameterNotWritableError, type(error_list[0])) self.assertEqual(api.ParameterTypeError, type(error_list[1])) self.assertEqual(api.ParameterValueError, type(error_list[2])) self.assertEqual(api.ParameterNameError, type(error_list[3])) self.assertEqual(api.ParameterInternalError, type(error_list[4]))
def getCpe(self): dm_root.PLATFORMDIR = '../platform' root = dm_root.DeviceModelRoot(self.io_loop, 'fakecpe', ext_dir=None) cpe = api.CPE(handle.Handle(root)) dldir = tempfile.mkdtemp() self.removedirs.append(dldir) cfdir = tempfile.mkdtemp() self.removedirs.append(cfdir) cpe.download_manager.SetDirectories(config_dir=cfdir, download_dir=dldir) cpe_machine = http.Listen(ip=None, port=0, ping_path='/ping/http_test', acs=None, cpe=cpe, cpe_listener=False, acs_config=FakeAcsConfig( self.get_http_port()), ioloop=self.io_loop) return cpe_machine
def testCore(self): o = TestObject() assert hasattr(o, '_lastindex') assert hasattr(o.SubObj, '_lastindex') h = handle.Handle(o) self.assertTrue(o) h.ValidateExports() h.AddExportObject('Counter') h.AddExportObject('Counter') h.AddExportObject('Counter') print h.ListExports(recursive=False) print h.ListExports(recursive=True) self.assertEqual(list(h.ListExports()), ['Counter.', 'ReadParam', 'SubObj.', 'TestParam']) self.assertEqual(list(h.ListExports(recursive=True)), [ 'Counter.', 'Counter.1.', 'Counter.1.Count', 'Counter.2.', 'Counter.2.Count', 'Counter.3.', 'Counter.3.Count', 'ReadParam', 'SubObj.', 'SubObj.Count', 'TestParam' ]) ds1 = handle.DumpSchema(TestObject) ds2 = handle.DumpSchema(o) self.assertEqual(ds1, ds2) h.DeleteExportObject('Counter', 2) self.assertEqual(list(h.ListExports(recursive=True)), [ 'Counter.', 'Counter.1.', 'Counter.1.Count', 'Counter.3.', 'Counter.3.Count', 'ReadParam', 'SubObj.', 'SubObj.Count', 'TestParam' ]) self.assertEqual([(idx, i.Count) for idx, i in o.CounterList.items()], [(1, 2), (3, 4)]) # NOTE(jnewlin): Note that is actually outside the spec, the spec says that # the index is an integer, but I guess it's neat that we can do this. idx, eo = h.AddExportObject('Counter', 'fred') eo.Count = 99 print h.ListExports(recursive=True) self.assertEqual([(idx, i.Count) for idx, i in o.CounterList.items()], [(1, 2), (3, 4), ('fred', 99)]) print handle.Dump(o) h.ValidateExports()
def testIndexError(self): """Test for b/33414470.""" root = IndexErrorAutoObject() h = handle.Handle(root) self.assertEqual(h.GetExport('Sub.1'), 1) self.gccheck.Check()
def testException(self): root = TestObject() h = handle.Handle(root) with self.assertRaises(AttributeError): h.SetExportParam('ReadParam', 6) self.gccheck.Check()
def testLifecycle3(self): # AutoObject() regenerates its children, with a new count, every time # you look for them. (This simulates a "virtual" hierarchy, such as # a Unix process list, that is different every time you look at it.) # However, we should expect that if we retrieve multiple values at once, # they all refer to the same instance of an object. # # The Count parameter is generated sequentially each time AutoObject # creates a child, and we can use that to confirm that the code doesn't # do unnecessary tree traversals without caching intermediate objects. root = AutoObject() h = handle.Handle(root) s0 = root.SubList[0] self.assertEqual(s0.Count, 1) self.assertEqual(s0.Count, 1) self.assertEqual(root.SubList[0].Count, 2) self.assertEqual(s0.Count, 1) # old object still exists w = weakref.ref(s0) self.assertEqual(s0, w()) del s0 self.assertEqual(w(), None) # all remaining refs are definitely gone self.assertEqual(handle.Handle(root.SubList[0]).GetExport('Count'), 3) self.gccheck.Check() # FindExport of Sub.0 shouldn't actually instantiate the .0 hp = h.FindExport('Sub.0') self.assertEqual((hp[0].obj, hp[1]), (root.SubList, '0')) self.assertEqual(root.SubList[0].Count, 4) self.gccheck.Check() # FindExport of Sub.0.Count should instantiate Sub.0 exactly once s0, name = h.FindExport('Sub.0.Count') self.assertEqual(name, 'Count') self.assertEqual(s0.obj.Count, 5) self.assertEqual(s0.obj.Count, 5) self.assertEqual(handle.Handle(root.SubList[0]).GetExport('Count'), 6) self.assertEqual(h.GetExport('Sub.0.Count'), 7) self.assertEqual(h.GetExport('Sub.576.Count'), 8) self.assertEqual(list(h.ListExports(recursive=False)), ['Sub.']) self.assertEqual(root.SubList[0].Count, 9) self.assertEqual(list(h.ListExports(recursive=True)), [ 'Sub.', 'Sub.0.', 'Sub.0.Count', 'Sub.1.', 'Sub.1.Count', 'Sub.2.', 'Sub.2.Count' ]) self.assertEqual(root.SubList[0].Count, 13) self.gccheck.Check() # LookupExports gives us a list of useful object pointers that # should only generate each requested object once. print 'lookup test' out = list( h.LookupExports([ 'Sub.0.Count', 'Sub.1.Count', 'Sub.0.Count', 'Sub.1.', 'Sub.', '.' ])) s0 = out[0][0].obj s1 = out[1][0].obj self.assertEqual([(io.obj, iname) for (io, iname) in out], [(s0, 'Count'), (s1, 'Count'), (s0, 'Count'), (s1, ''), (root.SubList, ''), (root, '')]) vals = [getattr(o.obj, param) for o, param in out[0:3]] self.assertEqual(vals, [14, 15, 14]) vals = [getattr(o.obj, param) for o, param in out[0:3]] self.assertEqual(vals, [14, 15, 14]) self.gccheck.Check() out = list( h.LookupExports(['Sub.1.Count', 'Sub.0.Count', 'Sub.1.Count'])) self.assertNotEqual(out[1][0], s0) self.assertNotEqual(out[0][0], s1) s0 = out[1][0].obj s1 = out[0][0].obj self.assertEqual([s0.Count, s1.Count], [17, 16]) for i, (o, param) in enumerate(out): setattr(o, param, i * 1000) vals = [getattr(o, param) for o, param in out] self.assertEqual(vals, [2000, 1000, 2000]) self.gccheck.Check()
def testLifecycle2(self): root = AutoObject() _ = handle.Handle(root)
def testGetParameterValuesEmpty(self): cpe = api.CPE(handle.Handle(TestSimpleRoot())) result = cpe.GetParameterValues(['']) self.assertTrue(result) self.assertEqual(result[0], ('SomeParam', 'SomeParamValue'))
def testObject(self): root = core.Exporter() root.Export(objects=['Test']) root.Test = TestObject() h = handle.Handle(root) h.ValidateExports() cpe = api.CPE(h) (idx, unused_status) = cpe.AddObject('Test.Thingy.', 0) name = 'Test.Thingy.%d' % int(idx) cpe.SetParameterValues([('%s.word' % name, 'word1')], 0) self.assertEqual(h.GetExport(name).word, 'word1') self.assertRaises(KeyError, cpe._SetParameterValue, '%s.not_exist' % name, 'word1') result = cpe.GetParameterValues(['%s.word' % name]) self.assertEqual(result, [('%s.word' % name, 'word1')]) self.assertEqual(changes, 1) self.assertRaises(api.SetParameterErrors, cpe.SetParameterValues, [('%s.word' % name, 'snorkleberry'), ('nonexist', 'broken')], 0) # word was not changed because nonexist didn't exist and we check for # existence first. self.assertEqual(result, [('%s.word' % name, 'word1')]) self.assertEqual(changes, 1) # word changed, but then changed back, because readonlyword has no # validator and it failed in the set phase. self.assertRaises(api.SetParameterErrors, cpe.SetParameterValues, [('%s.word' % name, 'snorkleberry'), ('%s.readonlyword' % name, 'broken')], 0) self.assertEqual(result, [('%s.word' % name, 'word1')]) self.assertEqual(changes, 3) # word changed, but then changed back. Strictly speaking we could have # aborted the set as soon as reasonlyword failed, but then the set of # error messages wouldn't be as thorough as possible, so we deliberately # choose to try all the sets if we get to the setting phase. self.assertRaises(api.SetParameterErrors, cpe.SetParameterValues, [('%s.readonlyword' % name, 'broken'), ('%s.word' % name, 'snorkleberry')], 0) self.assertEqual(result, [('%s.word' % name, 'word1')]) self.assertEqual(changes, 5) self.assertRaises(api.SetParameterErrors, cpe.SetParameterValues, [('%s.word' % name, 'snorkleberry'), ('%s.validatedword' % name, 'broken')], 0) self.assertEqual(result, [('%s.word' % name, 'word1')]) self.assertEqual(changes, 5) (objidx_list, status) = cpe.X_CATAWAMPUS_ORG_AddObjects( [('Test.Thingy.', 5), ('Test.Thingy.', 2)], 0) self.assertEqual(status, 0) self.assertEqual(len(objidx_list), 2) idxlist = objidx_list[0][1] + objidx_list[1][1] self.assertEqual(len(set(idxlist)), 7) result = cpe.GetParameterValues([('Test.Thingy.%d' % int(idx)) for idx in idxlist]) self.assertEqual([i.word for idx, i in result], [None] * 7)
def testStd(self): o = MyModel() handle.Handle(o).ValidateExports() print handle.Dump(o)