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 testHB(gravityNode): hbListener = TestHBListener() gravityNode.registerHeartbeatListener("PythonTest", 100000, hbListener) gravityNode.startHeartbeat(100000) # .1 seconds count = 0 while count < 10 and hbListener.receivedCount < 5: count += 1 time.sleep(.1) if hbListener.receivedCount < 5: Log.critical("didn't receive enough heartbeats. Expected {}, but received {}".format(5, hbListener.receivedCount)) return 1 else: Log.message("Received {} heartbeats (needed {}, but more is OK)".format(hbListener.receivedCount, 5)) gravityNode.stopHeartbeat() count = 0 while count < 10 and hbListener.missedCount < 5: count += 1 time.sleep(.1) if hbListener.missedCount < 5: Log.critical("didn't miss enough heartbeats. Expected {}, but received {}".format(5, hbListener.missedCount)) return 1 else: Log.message("Missed {} heartbeats (needed {}, but more is OK)".format(hbListener.missedCount, 5)) gravityNode.unregisterHeartbeatListener("PythonTest") 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) pubPB = PythonTestPB() pubPB.count = 0 pubPB.message = "" gdp = GravityDataProduct("PubTest") while mySub.subCount < 5 and pubPB.count < 10: pubPB.count += 1 gdp.setData(pubPB) gravityNode.publish(gdp) time.sleep(.1) if mySub.subCount < 5: Log.critical("Pub/Sub failed") return 1 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
def testService(gravityNode): testProv = TestProvider() gravityNode.registerService("ServiceTest", gravity.TCP, testProv) myReq = MyRequestHandler(gravityNode) testPB = PythonTestPB() testPB.count = 0 gdp = GravityDataProduct("ServiceRequest") gdp.setData(testPB) gravityNode.request("ServiceTest", gdp, myReq) # test async loopCount = 0 while myReq.reqCount < 5 and loopCount < 5: loopCount += 1 time.sleep(1) if myReq.reqCount < 5: Log.critical("Asynchronous Service Request failed, expected {} on counter, but was {}".format(5, myReq.reqCount)) return 1 # test sync testPB.count = 0 gdp.setData(testPB) responsePB = PythonTestPB() for i in range(0, 5): responseGDP = gravityNode.request("ServiceTest", gdp) responseGDP.populateMessage(responsePB) if responsePB.count != testPB.count+1: Log.critical("Incorrect return value, got {} but expected {}".format(responsePB.count, testPB.count+1)) return 1 else: Log.message("Received return value {} on synchronous request".format(responsePB.count)) testPB.count += 1 gdp.setData(testPB) return 0