def main(): gravityNode = GravityNode() count = 0 while gravityNode.init("PythonTest") != gravity.SUCCESS and count < 5: Log.warning("failed to init, retrying...") time.sleep(1) count += 1 if count == 5: Log.critical("Could not connect to ServiceDirectory") return 1 ret = testPubSub(gravityNode) if ret != 0: return ret ret = testService(gravityNode) if ret != 0: return ret ret = testHB(gravityNode) if ret != 0: return ret ret = testServiceTimeout(gravityNode) if ret != 0: return ret Log.message("Python tests successful!") return 0
def createTempService(): tempGravityNode = GravityNode() count = 0 while tempGravityNode.init("TempNode") != gravity.SUCCESS and count < 5: Log.warning("failed to init, retrying...") time.sleep(1) count += 1 if count == 5: Log.critical("Could not connect to ServiceDirectory") return 1 testProv = TestProvider() tempGravityNode.registerService("TempService", gravity.TCP, testProv) time.sleep(2) # give the registration time to complete return 0
def testPubSub(gravityNode): gravityNode.registerDataProduct("PubTest", gravity.TCP) mySub = MySubscriber() gravityNode.subscribe("PubTest", mySub) Log.warning("ref count = {}".format(sys.getrefcount(mySub))) if sys.getrefcount(mySub) != 2 + 1: # +1 for temp reference in call raise AssertionError( "Ref count didn't increment properly on subscribe") pubPB = PythonTestPB() pubPB.count = 0 pubPB.message = "" gdp = GravityDataProduct("PubTest") while mySub.subCount < 5 and pubPB.count < 10: pubPB.count += 1 gdp.data = pubPB gravityNode.publish(gdp) time.sleep(.1) if mySub.subCount < 5: Log.critical("Pub/Sub failed") return 1 # make sure ref count doesn't change when unsubscribe called with params we didn't use for subscribe gravityNode.unsubscribe("PubTest", mySub, "aFilterNotUsedAbove") if sys.getrefcount(mySub) != 2 + 1: # +1 for temp reference in call raise AssertionError( "Ref count didn't decrement properly on unsubscribe with different params" ) # make sure ref count decrements when unsubscribe with same params gravityNode.unsubscribe("PubTest", mySub) if sys.getrefcount(mySub) != 1 + 1: # +1 for temp reference in call raise AssertionError( "Ref count didn't decrement properly on unsubscribe with same different params" ) # make sure ref count doesn't dec a second time gravityNode.unsubscribe("PubTest", mySub) if sys.getrefcount(mySub) != 1 + 1: # +1 for temp reference in call raise AssertionError( "Ref count didn't decrement properly on second unsubscribe call") return 0
self.sample_data[pointPB.name].append( (pointPB.timestamp, pointPB.value)) # See if we have enough data from every stream to do a run of the autoencoder # A real example would probably need to time-align and possibly interpolate the data min_length = reduce( lambda a, b: min(a, len(self.sample_data[b])), self.sample_data, len(self.sample_data[list(self.sample_data)[0]])) if min_length >= model.n_steps: data.put(self.sample_data) self.sample_data = {} gn = GravityNode() while gn.init("AnomalyDetector") != gravity.SUCCESS: Log.warning("failed to init, retrying...") time.sleep(1) model_filename = gn.getStringParam("model_file", "model.json") model = GravityModel(model_filename) mySub = MySubscriber() channel = gn.getStringParam("subscription_name", "channel") gn.subscribe(channel, mySub) while True: d = data.get() mse = model.ComputeMSE(d) Log.message("MSE = %f" % (mse)) # notreached