def test_adjusted_minimum_poll_interval(self): SUBSCRIPTION_MANAGER._set_tunable_parameters({"minimum_poll_interval": 0.2}) sid = SUBSCRIPTION_MANAGER.create_polled(self.nrt1to4) nrt = SUBSCRIPTION_MANAGER.node_reference_table(sid) if nrt != self.nrt1to4: raise "Initial node reference table mismatch." # Check that each invokation gets all values. for i in range(0, 10): all_values = SUBSCRIPTION_MANAGER.poll_all(sid) if len(all_values) != len(self.nrt1to4): # We did not get all 4 values! raise ( "poll_all(self.nrt1to4) did not return all values." " (%d out of %d)" % (len(all_values), len(self.nrt1to4)) ) # Check that (eventually) all the values are result dictionaries. t1 = time.time() while (time.time() - t1) < 1.0: all_values = SUBSCRIPTION_MANAGER.poll_all(sid) if None not in all_values.values(): # ID3 is /services/time/UTC/milliseconds which should # change "really fast." c1 = all_values[self.ID3]["changes"] time.sleep(1.0) all_values = SUBSCRIPTION_MANAGER.poll_all(sid) c2 = all_values[self.ID3]["changes"] if (c2 - c1) > 6: # 0.2 == Max 5/second. raise ("1/5th second throttle failed," " %r changed %d times in one second.") % ( self.ID3, (c2 - c1), ) return time.sleep(0.1) raise ("Never got changes for all four result dictionaries, %d." % len(all_values)) return
def test_polled_event_handling(self): event_maker = EventProducerTestClass() event_maker.configure({"name": "EventProducerTester", "parent": "/"}) event_maker.start() sid = SUBSCRIPTION_MANAGER.create_polled({1: event_maker}) # Wait for polling to start and verify value made it without any events t1 = time.time() all_values = {1: None} while all_values[1] is None: if (time.time() - t1) > 1.0: raise "Got tired of waiting..." all_values = SUBSCRIPTION_MANAGER.poll_all(sid) time.sleep(0.1) # Check that subscription value is the initial value of 100 if all_values[1]["value"] != 100: raise ("polled_event_handling did not return inital value: " + str(all_values[1]["value"])) # make a rapid series of changes to the node value for i in range(10): event_maker._cov_check(i) time.sleep(0.1) # check change count, should be approx 10 all_values = SUBSCRIPTION_MANAGER.poll_all(sid) change_count = all_values[1]["changes"] if change_count < 8 or change_count > 12: raise ("polled_event_handling change count wrong." " Should be approx 10, not %d" % (change_count,)) # Check that the last value is corrent. final_value = all_values[1]["value"] if final_value != 9: raise ("polled_event_handling final value incorrect." " Should be 9, not %d" % (final_value,)) return
def test_fast_minimum_poll_interval(self): SUBSCRIPTION_MANAGER._set_tunable_parameters({"minimum_poll_interval": 0.0}) sid = SUBSCRIPTION_MANAGER.create_polled(self.nrt1to4) nrt = SUBSCRIPTION_MANAGER.node_reference_table(sid) if nrt != self.nrt1to4: raise "Initial node reference table mismatch." # Check that each invokation gets all values. for i in range(0, 10): all_values = SUBSCRIPTION_MANAGER.poll_all(sid) if len(all_values) != len(self.nrt1to4): # We did not get all 4 values! raise ( "poll_all(self.nrt1to4) did not return all values." " (%d out of %d)" % (len(all_values), len(self.nrt1to4)) ) # Check that (eventually) all the values are result dictionaries. all_values = SUBSCRIPTION_MANAGER.poll_all(sid) t1 = time.time() while (time.time() - t1) < 1.0: time.sleep(0.1) all_values = SUBSCRIPTION_MANAGER.poll_all(sid) if None in all_values.values(): raise ( ("Never got changes for all four result dictionaries, %d.\n" "Values: %r") % (len(all_values), all_values) ) # ID3 is /services/time/UTC/milliseconds which should # change "really fast." c1 = all_values[self.ID3]["changes"] time.sleep(1.0) all_values = SUBSCRIPTION_MANAGER.poll_all(sid) c2 = all_values[self.ID3]["changes"] if (c2 - c1) < 25: # It's usually 500 on fearfactory... raise "%r only changed %d times in one second." % (self.ID3, (c2 - c1)) return
def test_timeout_batch(self): # nrtB10 changes "really fast." sid = SUBSCRIPTION_MANAGER.create_polled(self.nrtB10, 1.0) # Make sure it comes up. t1 = time.time() self.__values_changing(sid) # Double check the values are changing and that the subscriptions # stay valid while we poll for values. t1 = time.time() while (time.time() - t1) < 2.0: self.__values_changing(sid) time.sleep(0.1) if len(SUBSCRIPTION_MANAGER.diag_get_mnrs()) != 10: raise ("Bogus test, there should be 10 mnr at this point, not %r." % len(SUBSCRIPTION_MANAGER.diag_get_mnrs())) if len(SUBSCRIPTION_MANAGER.diag_get_mnbs()) != 1: raise ("Bogus test, there should be 1 mnb at this point, not %r." % len(SUBSCRIPTION_MANAGER.diag_get_mnbs())) t1 = time.time() while sid in SUBSCRIPTION_MANAGER.diag_get_sids(): if (time.time() - t1) > 2.0: raise "%r did not timeout." % sid time.sleep(0.1) # Make sure that the mnr is removed when the last snr is deleted. if len(SUBSCRIPTION_MANAGER.diag_get_mnrs()) != 0: raise ("There should not be any mnrs at this point," " but there are %r." % len( SUBSCRIPTION_MANAGER.diag_get_mnrs())) # Finally, make sure that the mnb is removed when the last mnr is # deleted. if len(SUBSCRIPTION_MANAGER.diag_get_mnbs()) != 0: raise ("There should not be any mnbs at this point," " but there are %r." % len( SUBSCRIPTION_MANAGER.diag_get_mnbs())) return
def test_timeout(self): sids = [] for i in range(2): if not i: timeout = 1.0 else: timeout = None # ID3 is /services/time/UTC/milliseconds which should # change "really fast." sid = SUBSCRIPTION_MANAGER.create_polled({self.ID3:self.ID3}, timeout) # Make sure it comes up. t1 = time.time() self.__values_changing(sid) sids.append(sid) # Double check the values are changing and that the subscriptions # stay valid while we poll for values. t1 = time.time() while (time.time() - t1) < 2.0: for sid in sids: self.__values_changing(sid) time.sleep(0.1) # Now ensure that sid[0] times out... sid = sids.pop(0) t1 = time.time() while sid in SUBSCRIPTION_MANAGER.diag_get_sids(): if (time.time()-t1) > 2.0: raise "%r did not timeout." % sid time.sleep(0.1) # Finally, make sure that the other subscription is valid. sid = sids.pop(0) self.__values_changing(sid) return
def test_poll_all(self): sid = SUBSCRIPTION_MANAGER.create_polled(self.nrt1to4) nrt = SUBSCRIPTION_MANAGER.node_reference_table(sid) if nrt != self.nrt1to4: raise "Initial node reference table mismatch." # Check that each invokation gets all values. for i in range(0,10): all_values = SUBSCRIPTION_MANAGER.poll_all(sid) if len(all_values) != len(self.nrt1to4): # We did not get all 4 values! raise ( "poll_all(self.nrt1to4) did not return all values." " (%d out of %d)" % (len(all_values),len(self.nrt1to4)) ) # Check that (eventually) all the values are result dictionaries. all_values = SUBSCRIPTION_MANAGER.poll_all(sid) t1 = time.time() while (time.time() - t1) < 1.0: if None not in all_values.values(): return time.sleep(0.1) all_values = SUBSCRIPTION_MANAGER.poll_all(sid) if None in all_values.values(): raise ( "Never got changes for all four result dictionaries, %d." % len(all_values) ) return
def test_polled_event_handling(self): event_maker = EventProducerTestClass() event_maker.configure({'name': 'EventProducerTester', 'parent': '/'}) event_maker.start() sid = SUBSCRIPTION_MANAGER.create_polled({1: event_maker}) # Wait for polling to start and verify value made it without any events t1 = time.time() all_values = {1: None} while all_values[1] is None: if (time.time() - t1) > 1.0: raise "Got tired of waiting..." all_values = SUBSCRIPTION_MANAGER.poll_all(sid) time.sleep(0.1) # Check that subscription value is the initial value of 100 if all_values[1]['value'] != 100: raise ("polled_event_handling did not return inital value: " + str(all_values[1]['value'])) # make a rapid series of changes to the node value for i in range(10): event_maker._cov_check(i) time.sleep(0.1) # check change count, should be approx 10 all_values = SUBSCRIPTION_MANAGER.poll_all(sid) change_count = all_values[1]['changes'] if change_count < 8 or change_count > 12: raise ("polled_event_handling change count wrong." " Should be approx 10, not %d" % (change_count, )) # Check that the last value is corrent. final_value = all_values[1]['value'] if final_value != 9: raise ("polled_event_handling final value incorrect." " Should be 9, not %d" % (final_value, )) return
def test_poll_all(self): sid = SUBSCRIPTION_MANAGER.create_polled(self.nrt1to4) nrt = SUBSCRIPTION_MANAGER.node_reference_table(sid) if nrt != self.nrt1to4: raise "Initial node reference table mismatch." # Check that each invokation gets all values. for i in range(0, 10): all_values = SUBSCRIPTION_MANAGER.poll_all(sid) if len(all_values) != len(self.nrt1to4): # We did not get all 4 values! raise ("poll_all(self.nrt1to4) did not return all values." " (%d out of %d)" % (len(all_values), len(self.nrt1to4))) # Check that (eventually) all the values are result dictionaries. all_values = SUBSCRIPTION_MANAGER.poll_all(sid) t1 = time.time() while (time.time() - t1) < 1.0: if None not in all_values.values(): return time.sleep(0.1) all_values = SUBSCRIPTION_MANAGER.poll_all(sid) if None in all_values.values(): raise ("Never got changes for all four result dictionaries, %d." % len(all_values)) return
def test_timeout(self): sids = [] for i in range(2): if not i: timeout = 1.0 else: timeout = None # ID3 is /services/time/UTC/milliseconds which should # change "really fast." sid = SUBSCRIPTION_MANAGER.create_polled({self.ID3: self.ID3}, timeout) # Make sure it comes up. t1 = time.time() self.__values_changing(sid) sids.append(sid) # Double check the values are changing and that the subscriptions # stay valid while we poll for values. t1 = time.time() while (time.time() - t1) < 2.0: for sid in sids: self.__values_changing(sid) time.sleep(0.1) # Now ensure that sid[0] times out... sid = sids.pop(0) t1 = time.time() while sid in SUBSCRIPTION_MANAGER.diag_get_sids(): if (time.time() - t1) > 2.0: raise "%r did not timeout." % sid time.sleep(0.1) # Finally, make sure that the other subscription is valid. sid = sids.pop(0) self.__values_changing(sid) return
def test_poll_all_plus_exceptions(self): SUBSCRIPTION_MANAGER._set_tunable_parameters({ 'minimum_poll_interval':0.0, }) nrt1to4bad5to6 = {} nrt1to4bad5to6.update(self.nrt1to4) nrt1to4bad5to6['/services/time/is/an/illusion'] = ( '/services/time/is/an/illusion' ) nrt1to4bad5to6['/services/exception'] = '/services/exception' sid = SUBSCRIPTION_MANAGER.create_polled(nrt1to4bad5to6) nrt = SUBSCRIPTION_MANAGER.node_reference_table(sid) if nrt != nrt1to4bad5to6: raise "Initial node reference table mismatch." # Check that each invokation gets all values. for i in range(0,10): all_values = SUBSCRIPTION_MANAGER.poll_all(sid) if len(all_values) != len(nrt1to4bad5to6): # We did not get all 4 values! raise ( "poll_all(self.nrt1to4) did not return all values." " (%d out of %d)" % (len(all_values),len(nrt1to4bad5to6)) ) # Check that (eventually) all the values are result dictionaries. all_values = SUBSCRIPTION_MANAGER.poll_all(sid) t1 = time.time() while (time.time() - t1) < 1.0: if None not in all_values.values(): self.__all_plus_exceptions_check(all_values) # Finally, test that a new subscription gets the correct # results. time.sleep(0.1) sid = SUBSCRIPTION_MANAGER.create_polled(nrt1to4bad5to6) time.sleep(0.1) all_values = SUBSCRIPTION_MANAGER.poll_all(sid) self.__all_plus_exceptions_check(all_values) time.sleep(0.1) all_values = SUBSCRIPTION_MANAGER.poll_all(sid) self.__all_plus_exceptions_check(all_values) return time.sleep(0.1) all_values = SUBSCRIPTION_MANAGER.poll_all(sid) if None in all_values.values(): raise ("Never got values for all nodes: %r." % all_values) return
def subscription_create_polled(self, sessionID, node_reference_table=None, timeout=60.0): # Raise exception if this is not a valid session if not self.manager().validate(sessionID, touch=1): raise EInvalidSession('Invalid session') return SUBSCRIPTION_MANAGER.create_polled(node_reference_table, timeout)
def test_poll_all_plus_exceptions(self): SUBSCRIPTION_MANAGER._set_tunable_parameters({ 'minimum_poll_interval': 0.0, }) nrt1to4bad5to6 = {} nrt1to4bad5to6.update(self.nrt1to4) nrt1to4bad5to6['/services/time/is/an/illusion'] = ( '/services/time/is/an/illusion') nrt1to4bad5to6['/services/exception'] = '/services/exception' sid = SUBSCRIPTION_MANAGER.create_polled(nrt1to4bad5to6) nrt = SUBSCRIPTION_MANAGER.node_reference_table(sid) if nrt != nrt1to4bad5to6: raise "Initial node reference table mismatch." # Check that each invokation gets all values. for i in range(0, 10): all_values = SUBSCRIPTION_MANAGER.poll_all(sid) if len(all_values) != len(nrt1to4bad5to6): # We did not get all 4 values! raise ("poll_all(self.nrt1to4) did not return all values." " (%d out of %d)" % (len(all_values), len(nrt1to4bad5to6))) # Check that (eventually) all the values are result dictionaries. all_values = SUBSCRIPTION_MANAGER.poll_all(sid) t1 = time.time() while (time.time() - t1) < 1.0: if None not in all_values.values(): self.__all_plus_exceptions_check(all_values) # Finally, test that a new subscription gets the correct # results. time.sleep(0.1) sid = SUBSCRIPTION_MANAGER.create_polled(nrt1to4bad5to6) time.sleep(0.1) all_values = SUBSCRIPTION_MANAGER.poll_all(sid) self.__all_plus_exceptions_check(all_values) time.sleep(0.1) all_values = SUBSCRIPTION_MANAGER.poll_all(sid) self.__all_plus_exceptions_check(all_values) return time.sleep(0.1) all_values = SUBSCRIPTION_MANAGER.poll_all(sid) if None in all_values.values(): raise ("Never got values for all nodes: %r." % all_values) return
def test_replace(self): sid = SUBSCRIPTION_MANAGER.create_polled(self.nrt1to2) nrt = SUBSCRIPTION_MANAGER.node_reference_table(sid) if nrt != self.nrt1to2: raise "Initial node reference table mismatch." SUBSCRIPTION_MANAGER.replace(sid, self.nrt3to4) nrt = SUBSCRIPTION_MANAGER.node_reference_table(sid) if nrt != self.nrt3to4: raise "Replaced node reference table mismatch." return
def test_empty(self): sid = SUBSCRIPTION_MANAGER.create_polled(self.nrt1to4) nrt = SUBSCRIPTION_MANAGER.node_reference_table(sid) if nrt != self.nrt1to4: raise "Initial node reference table mismatch." SUBSCRIPTION_MANAGER.empty(sid) nrt = SUBSCRIPTION_MANAGER.node_reference_table(sid) if nrt != {}: raise "Node reference table not empty." return
def test_destroy_batch(self): sids = [] for i in range(2): # BatchNodes change "really fast." sid = SUBSCRIPTION_MANAGER.create_polled(self.nrtB10) # Make sure it comes up. t1 = time.time() self.__values_changing(sid) sids.append(sid) # Double check the values are changing. for sid in sids: self.__values_changing(sid) # Now nuke one of the suscriptions and see that the other stays valid. sid = sids.pop(0) SUBSCRIPTION_MANAGER.destroy(sid) try: SUBSCRIPTION_MANAGER.destroy(sid) except ENoSuchSubscription: pass else: raise "No such subscription not detected." # Make sure that the other subscription is valid. sid = sids.pop(0) self.__values_changing(sid) if len(SUBSCRIPTION_MANAGER.diag_get_mnrs()) != 10: raise ( "Bogus test, there should be 10 mnr at this point, not %r." % len(SUBSCRIPTION_MANAGER.diag_get_mnrs())) if len(SUBSCRIPTION_MANAGER.diag_get_mnbs()) != 1: raise ( "Bogus test, there should be 1 mnb at this point, not %r." % len(SUBSCRIPTION_MANAGER.diag_get_mnbs())) SUBSCRIPTION_MANAGER.destroy(sid) # Make sure that the mnr is removed when the last snr is deleted. if len(SUBSCRIPTION_MANAGER.diag_get_mnrs()) != 0: raise ( "There should not be any mnrs at this point," " but there are %r." % len(SUBSCRIPTION_MANAGER.diag_get_mnrs())) # Finally, make sure that the mnb is removed when the last mnr is # deleted. if len(SUBSCRIPTION_MANAGER.diag_get_mnbs()) != 0: raise ( "There should not be any mnbs at this point," " but there are %r." % len(SUBSCRIPTION_MANAGER.diag_get_mnbs())) return
def test_poll_changed(self): sid = SUBSCRIPTION_MANAGER.create_polled(self.nrt1to4) nrt = SUBSCRIPTION_MANAGER.node_reference_table(sid) if nrt != self.nrt1to4: raise "Initial node reference table mismatch." all_values = {} time.sleep(0.1) t1 = time.time() while (time.time() - t1) < 1.0: time.sleep(0.1) changed_values = SUBSCRIPTION_MANAGER.poll_changed(sid) all_values.update(changed_values) if len(all_values) == 4: # We got all 4 values! return raise "Never got changes for all four values, %d." % len(all_values) return
def test_modify(self): sid = SUBSCRIPTION_MANAGER.create_polled(self.nrt1to2) nrt = SUBSCRIPTION_MANAGER.node_reference_table(sid) if nrt != self.nrt1to2: raise "Initial node reference table mismatch." SUBSCRIPTION_MANAGER.modify(sid, self.ID2, self.ID3) nrt = SUBSCRIPTION_MANAGER.node_reference_table(sid) if nrt == self.nrt1to2: raise "Modified node reference table not modified." if nrt != {self.ID1:self.ID1,self.ID2:self.ID3}: raise "Modified node reference table mismatch." try: SUBSCRIPTION_MANAGER.modify(sid, self.ID3, self.ID2) except ENoSuchNodeID: pass else: raise "No such NodeID not detected." return
def test_modify(self): sid = SUBSCRIPTION_MANAGER.create_polled(self.nrt1to2) nrt = SUBSCRIPTION_MANAGER.node_reference_table(sid) if nrt != self.nrt1to2: raise "Initial node reference table mismatch." SUBSCRIPTION_MANAGER.modify(sid, self.ID2, self.ID3) nrt = SUBSCRIPTION_MANAGER.node_reference_table(sid) if nrt == self.nrt1to2: raise "Modified node reference table not modified." if nrt != {self.ID1: self.ID1, self.ID2: self.ID3}: raise "Modified node reference table mismatch." try: SUBSCRIPTION_MANAGER.modify(sid, self.ID3, self.ID2) except ENoSuchNodeID: pass else: raise "No such NodeID not detected." return
def test_destroy_batch(self): sids = [] for i in range(2): # BatchNodes change "really fast." sid = SUBSCRIPTION_MANAGER.create_polled(self.nrtB10) # Make sure it comes up. t1 = time.time() self.__values_changing(sid) sids.append(sid) # Double check the values are changing. for sid in sids: self.__values_changing(sid) # Now nuke one of the suscriptions and see that the other stays valid. sid = sids.pop(0) SUBSCRIPTION_MANAGER.destroy(sid) try: SUBSCRIPTION_MANAGER.destroy(sid) except ENoSuchSubscription: pass else: raise "No such subscription not detected." # Make sure that the other subscription is valid. sid = sids.pop(0) self.__values_changing(sid) if len(SUBSCRIPTION_MANAGER.diag_get_mnrs()) != 10: raise ( "Bogus test, there should be 10 mnr at this point, not %r." % len(SUBSCRIPTION_MANAGER.diag_get_mnrs())) if len(SUBSCRIPTION_MANAGER.diag_get_mnbs()) != 1: raise ("Bogus test, there should be 1 mnb at this point, not %r." % len(SUBSCRIPTION_MANAGER.diag_get_mnbs())) SUBSCRIPTION_MANAGER.destroy(sid) # Make sure that the mnr is removed when the last snr is deleted. if len(SUBSCRIPTION_MANAGER.diag_get_mnrs()) != 0: raise ("There should not be any mnrs at this point," " but there are %r." % len(SUBSCRIPTION_MANAGER.diag_get_mnrs())) # Finally, make sure that the mnb is removed when the last mnr is # deleted. if len(SUBSCRIPTION_MANAGER.diag_get_mnbs()) != 0: raise ("There should not be any mnbs at this point," " but there are %r." % len(SUBSCRIPTION_MANAGER.diag_get_mnbs())) return
def test_add(self): sid = SUBSCRIPTION_MANAGER.create_polled(self.nrt1to2) nrt = SUBSCRIPTION_MANAGER.node_reference_table(sid) if nrt != self.nrt1to2: raise "Initial node reference table mismatch." SUBSCRIPTION_MANAGER.add(sid, self.ID5, self.ID5) nrt = SUBSCRIPTION_MANAGER.node_reference_table(sid) nrt125 = {} nrt125.update(self.nrt1to2) nrt125[self.ID5] = self.ID5 if nrt != nrt125: raise "Node reference table mismatch." try: SUBSCRIPTION_MANAGER.add(sid, self.ID5, self.ID5) except ENodeIDExists: pass else: raise "Node ID in use not detected." return
def nodebrowser_handler(self, nb, path, node, node_url): # the purpose of this special handler is to get all the points in # one subscription instead of individually. This greatly speeds up # the rz net peer driver sid = None html = '' if len(self.children_names()): node_table = {} for c in self.children_nodes(): #pre AML if isinstance(c, PointNode): node_table[id(c)] = c if len(node_table): sid = SM.create_polled(node_table) try: html = nb.get_default_view(node, node_url) finally: if sid: SM.destroy(sid) return html
def test_remove(self): sid = SUBSCRIPTION_MANAGER.create_polled(self.nrt1to4) nrt = SUBSCRIPTION_MANAGER.node_reference_table(sid) if nrt != self.nrt1to4: raise "Initial node reference table mismatch." SUBSCRIPTION_MANAGER.remove(sid, self.ID2) nrt = SUBSCRIPTION_MANAGER.node_reference_table(sid) nrt134 = {} nrt134.update(self.nrt1to4) del nrt134[self.ID2] if nrt != nrt134: raise "Node reference table mismatch." try: SUBSCRIPTION_MANAGER.remove(sid, self.ID2) except ENoSuchNodeID: pass else: raise "No such NodeID not detected." return
def nodebrowser_handler(self, nb, path, node, node_url): # the purpose of this special handler is to get all the points in # one subscription instead of individually. This greatly speeds up # the rz net peer driver sid = None html = '' if len(self.children_names()): node_table = {} for c in self.children_nodes(): #pre AML if isinstance(c, PointNode): node_table[id(c)]=c if len(node_table): sid = SM.create_polled(node_table) try: html = nb.get_default_view(node, node_url) finally: if sid: SM.destroy(sid) return html
def test_adjusted_minimum_poll_interval(self): SUBSCRIPTION_MANAGER._set_tunable_parameters({ 'minimum_poll_interval': 0.2, }) sid = SUBSCRIPTION_MANAGER.create_polled(self.nrt1to4) nrt = SUBSCRIPTION_MANAGER.node_reference_table(sid) if nrt != self.nrt1to4: raise "Initial node reference table mismatch." # Check that each invokation gets all values. for i in range(0, 10): all_values = SUBSCRIPTION_MANAGER.poll_all(sid) if len(all_values) != len(self.nrt1to4): # We did not get all 4 values! raise ("poll_all(self.nrt1to4) did not return all values." " (%d out of %d)" % (len(all_values), len(self.nrt1to4))) # Check that (eventually) all the values are result dictionaries. t1 = time.time() while (time.time() - t1) < 1.0: all_values = SUBSCRIPTION_MANAGER.poll_all(sid) if None not in all_values.values(): # ID3 is /services/time/UTC/milliseconds which should # change "really fast." c1 = all_values[self.ID3]['changes'] time.sleep(1.0) all_values = SUBSCRIPTION_MANAGER.poll_all(sid) c2 = all_values[self.ID3]['changes'] if (c2 - c1) > 6: # 0.2 == Max 5/second. raise ("1/5th second throttle failed," " %r changed %d times in one second.") % ( self.ID3, (c2 - c1), ) return time.sleep(0.1) raise ("Never got changes for all four result dictionaries, %d." % len(all_values)) return
def test_fast_minimum_poll_interval(self): SUBSCRIPTION_MANAGER._set_tunable_parameters({ 'minimum_poll_interval': 0.0, }) sid = SUBSCRIPTION_MANAGER.create_polled(self.nrt1to4) nrt = SUBSCRIPTION_MANAGER.node_reference_table(sid) if nrt != self.nrt1to4: raise "Initial node reference table mismatch." # Check that each invokation gets all values. for i in range(0, 10): all_values = SUBSCRIPTION_MANAGER.poll_all(sid) if len(all_values) != len(self.nrt1to4): # We did not get all 4 values! raise ("poll_all(self.nrt1to4) did not return all values." " (%d out of %d)" % (len(all_values), len(self.nrt1to4))) # Check that (eventually) all the values are result dictionaries. all_values = SUBSCRIPTION_MANAGER.poll_all(sid) t1 = time.time() while (time.time() - t1) < 1.0: time.sleep(0.1) all_values = SUBSCRIPTION_MANAGER.poll_all(sid) if None in all_values.values(): raise (("Never got changes for all four result dictionaries, %d.\n" "Values: %r") % (len(all_values), all_values)) # ID3 is /services/time/UTC/milliseconds which should # change "really fast." c1 = all_values[self.ID3]['changes'] time.sleep(1.0) all_values = SUBSCRIPTION_MANAGER.poll_all(sid) c2 = all_values[self.ID3]['changes'] if (c2 - c1) < 25: # It's usually 500 on fearfactory... raise "%r only changed %d times in one second." % ( self.ID3, (c2 - c1), ) return
def resync(self): try: print 'resync' new_sheet = self.get_sheet() #subscribe to nodes that need to be read sub_dict = {} #dictionary to create subscriptions for row_index in new_sheet.keys(): url, mode, value = new_sheet[row_index] if mode < 3: #need to subscribe to non-write-only sub_dict[row_index] = url #for nodes that need to be set from values in the spreadsheet if mode == 2 or mode == 3: #need to set try: as_node(url).set(value) #value is a string or None except: msglog.exception() self.sheet = new_sheet if sub_dict: #create a new subscription then delete the old one sid = SM.create_polled(sub_dict) if self.sid: SM.destroy(self.sid) self.sid = sid sub_dict = SM.poll_all(sid) self.node_values = sub_dict #update the spread sheet for row_index in sub_dict.keys(): url, mode, value = new_sheet[row_index] if mode == 0 or mode == 1: #need to write try: self.set_cell(row_index, 3, sub_dict[row_index]['value']) except: msglog.exception() #the spreadsheet and mediator should now be in sync self.scan() except: msglog.exception()
def test_timeout_batch(self): # nrtB10 changes "really fast." sid = SUBSCRIPTION_MANAGER.create_polled(self.nrtB10, 1.0) # Make sure it comes up. t1 = time.time() self.__values_changing(sid) # Double check the values are changing and that the subscriptions # stay valid while we poll for values. t1 = time.time() while (time.time() - t1) < 2.0: self.__values_changing(sid) time.sleep(0.1) if len(SUBSCRIPTION_MANAGER.diag_get_mnrs()) != 10: raise ( "Bogus test, there should be 10 mnr at this point, not %r." % len(SUBSCRIPTION_MANAGER.diag_get_mnrs())) if len(SUBSCRIPTION_MANAGER.diag_get_mnbs()) != 1: raise ("Bogus test, there should be 1 mnb at this point, not %r." % len(SUBSCRIPTION_MANAGER.diag_get_mnbs())) t1 = time.time() while sid in SUBSCRIPTION_MANAGER.diag_get_sids(): if (time.time() - t1) > 2.0: raise "%r did not timeout." % sid time.sleep(0.1) # Make sure that the mnr is removed when the last snr is deleted. if len(SUBSCRIPTION_MANAGER.diag_get_mnrs()) != 0: raise ("There should not be any mnrs at this point," " but there are %r." % len(SUBSCRIPTION_MANAGER.diag_get_mnrs())) # Finally, make sure that the mnb is removed when the last mnr is # deleted. if len(SUBSCRIPTION_MANAGER.diag_get_mnbs()) != 0: raise ("There should not be any mnbs at this point," " but there are %r." % len(SUBSCRIPTION_MANAGER.diag_get_mnbs())) return
def invoke_batch_async(self, sessionID, *qualified_method_list): subscription = None if not self.manager().validate(sessionID, touch=1): if self._subscriptions.has_key(sessionID): try: subscription = self._subscriptions[sessionID] del self._subscriptions[sessionID] del self._qualified_method_list[sessionID] del self._services[sessionID] del self._methods[sessionID] SUBSCRIPTION_MANAGER.destroy(subscription) except: msglog.log( 'RNA_XMLRPC_Handler', msglog.types.WARN, "Error destroying subscription %r on stale session %r." % (subscription, sessionID)) msglog.exception() raise EInvalidSession('Invalid session') if self._subscriptions.has_key(sessionID): subscription = self._subscriptions[sessionID] if (self._qualified_method_list[sessionID] != qualified_method_list): # New batch, destroy the out of date subscription. try: del self._subscriptions[sessionID] del self._qualified_method_list[sessionID] del self._services[sessionID] del self._methods[sessionID] SUBSCRIPTION_MANAGER.destroy(subscription) except: msglog.log( 'RNA_XMLRPC_Handler', msglog.types.WARN, "Error destroying previous subscription %r." % (subscription, )) msglog.exception() subscription = None if subscription is None: # # No subscription matching the batch, create one! # Build a node_reference_table: subscription_map = {} services = [] methods = [] for i in range(0, len(qualified_method_list)): rna = qualified_method_list[i] i_method = rna.rfind(':') if i_method == -1 or (i_method == 3 and rna[:3] == "mpx"): # There is no method specified. rna_node = rna rna_method = '' else: rna_node = rna[:i_method] rna_method = rna[i_method + 1:] services.append(rna_node) methods.append(rna_method) if methods[i] == 'get': subscription_map[services[i]] = services[i] # Create the subscription using the genereated node_reference_table: subscription = SUBSCRIPTION_MANAGER.create_polled( subscription_map, 5 * 60.0) self._subscriptions[sessionID] = subscription self._qualified_method_list[sessionID] = qualified_method_list self._services[sessionID] = services self._methods[sessionID] = methods # # Now that we've added our node's, validate the other sessions. # validate_list = [] validate_list.extend(self._subscriptions.keys()) for test_session in validate_list: if not self.manager().validate(test_session, touch=0): expired = None try: expired = self._subscriptions[test_session] del self._subscriptions[test_session] del self._qualified_method_list[test_session] del self._services[test_session] del self._methods[test_session] SUBSCRIPTION_MANAGER.destroy(expired) except: msglog.log( 'RNA_XMLRPC_Handler', msglog.types.WARN, "Error destroying subscription %r on stale session %r." % (expired, test_session)) msglog.exception() # Get all 'ready' values. services = self._services[sessionID] methods = self._methods[sessionID] polled_values = SUBSCRIPTION_MANAGER.poll_all(subscription) results = [] for i in range(0, len(services)): service = services[i] try: if methods[i] == 'get': result = polled_values[service] if result is None: result = 'error: Waiting for update...' else: result = result['value'] if isinstance(result, Exception): result = self._exception_string(result) else: result = 'error: %r method not supported, only get().' % ( methods[i]) results.append(result) except Exception, e: results.append(self._exception_string(e))
def invoke_batch_async(self, sessionID, *qualified_method_list): subscription = None if not self.manager().validate(sessionID, touch=1): if self._subscriptions.has_key(sessionID): try: subscription = self._subscriptions[sessionID] del self._subscriptions[sessionID] del self._qualified_method_list[sessionID] del self._services[sessionID] del self._methods[sessionID] SUBSCRIPTION_MANAGER.destroy(subscription) except: msglog.log('RNA_XMLRPC_Handler',msglog.types.WARN, "Error destroying subscription %r on stale session %r." % (subscription, sessionID)) msglog.exception() raise EInvalidSession('Invalid session') if self._subscriptions.has_key(sessionID): subscription = self._subscriptions[sessionID] if (self._qualified_method_list[sessionID] != qualified_method_list): # New batch, destroy the out of date subscription. try: del self._subscriptions[sessionID] del self._qualified_method_list[sessionID] del self._services[sessionID] del self._methods[sessionID] SUBSCRIPTION_MANAGER.destroy(subscription) except: msglog.log('RNA_XMLRPC_Handler',msglog.types.WARN, "Error destroying previous subscription %r." % (subscription,)) msglog.exception() subscription = None if subscription is None: # # No subscription matching the batch, create one! # Build a node_reference_table: subscription_map = {} services = [] methods = [] for i in range(0,len(qualified_method_list)): rna = qualified_method_list[i] i_method = rna.rfind(':') if i_method == -1 or (i_method == 3 and rna[:3] == "mpx"): # There is no method specified. rna_node = rna rna_method = '' else: rna_node = rna[:i_method] rna_method = rna[i_method+1:] services.append(rna_node) methods.append(rna_method) if methods[i] == 'get': subscription_map[services[i]] = services[i] # Create the subscription using the genereated node_reference_table: subscription = SUBSCRIPTION_MANAGER.create_polled(subscription_map, 5*60.0) self._subscriptions[sessionID] = subscription self._qualified_method_list[sessionID] = qualified_method_list self._services[sessionID] = services self._methods[sessionID] = methods # # Now that we've added our node's, validate the other sessions. # validate_list = [] validate_list.extend(self._subscriptions.keys()) for test_session in validate_list: if not self.manager().validate(test_session, touch=0): expired = None try: expired = self._subscriptions[test_session] del self._subscriptions[test_session] del self._qualified_method_list[test_session] del self._services[test_session] del self._methods[test_session] SUBSCRIPTION_MANAGER.destroy(expired) except: msglog.log('RNA_XMLRPC_Handler',msglog.types.WARN, "Error destroying subscription %r on stale session %r." % (expired, test_session)) msglog.exception() # Get all 'ready' values. services = self._services[sessionID] methods = self._methods[sessionID] polled_values = SUBSCRIPTION_MANAGER.poll_all(subscription) results = [] for i in range(0,len(services)): service = services[i] try: if methods[i] == 'get': result = polled_values[service] if result is None: result = 'error: Waiting for update...' else: result = result['value'] if isinstance(result,Exception): result = self._exception_string(result) else: result = 'error: %r method not supported, only get().' % ( methods[i] ) results.append(result) except Exception, e: results.append(self._exception_string(e))
def test_create_polled(self): sid = SUBSCRIPTION_MANAGER.create_polled() return
def test_node_reference_table(self): sid = SUBSCRIPTION_MANAGER.create_polled(self.nrt1to2) nrt = SUBSCRIPTION_MANAGER.node_reference_table(sid) if nrt != self.nrt1to2: raise "Node reference table mismatch." return