def testRestricted2ZStackCommunication(tdir, looper): """ Create 2 ZStack and make them send and receive messages. Both stacks allow communication only when keys are shared :return: """ names = ['Alpha', 'Beta'] genKeys(tdir, names) alphaP = Printer(names[0]) betaP = Printer(names[1]) alpha = ZStack(names[0], ha=genHa(), basedirpath=tdir, msgHandler=alphaP.print, restricted=True) beta = ZStack(names[1], ha=genHa(), basedirpath=tdir, msgHandler=betaP.print, restricted=True) prepStacks(looper, alpha, beta, connect=True, useKeys=True) alpha.send({'greetings': 'hi'}, beta.name) beta.send({'greetings': 'hello'}, alpha.name) looper.run(eventually(chkPrinted, alphaP, {'greetings': 'hello'})) looper.run(eventually(chkPrinted, betaP, {'greetings': 'hi'}))
def testMessageQuota(tdir, looper): names = ['Alpha', 'Beta'] genKeys(tdir, names) alphaP = Printer(names[0]) receivedMessages = [] betaMshHandler = makeHandler(receivedMessages) alpha = ZStack(names[0], ha=genHa(), basedirpath=tdir, msgHandler=alphaP.print, restricted=True) beta = ZStack(names[1], ha=genHa(), basedirpath=tdir, msgHandler=betaMshHandler, restricted=True, onlyListener=True) prepStacks(looper, alpha, beta, connect=True, useKeys=True) messages = [] numMessages = 100 * beta.listenerQuota for i in range(numMessages): msg = json.dumps({'random': randomSeed().decode()}).encode() messages.append(json.loads(msg.decode())) alpha.send(msg, beta.name) looper.runFor(2) assert messages == receivedMessages
def create_and_prep_stacks(names, tdir, looper): genKeys(tdir, names) printers = [Printer(n) for n in names] stacks = [ZStack(n, ha=genHa(), basedirpath=tdir, msgHandler=printers[i].print, restricted=True) for i, n in enumerate(names)] prepStacks(looper, *stacks, connect=True, useKeys=True) return stacks, printers
def testMessageQuota(tdir, looper): names = ['Alpha', 'Beta'] genKeys(tdir, names) alphaP = Printer(names[0]) betaMsgHandler = CollectingMsgsHandler() alpha = ZStack(names[0], ha=genHa(), basedirpath=tdir, msgHandler=alphaP.print, restricted=True) beta = ZStack(names[1], ha=genHa(), basedirpath=tdir, msgHandler=betaMsgHandler.handler, restricted=True, onlyListener=True) prepStacks(looper, alpha, beta, connect=True, useKeys=True) messages = [] numMessages = 150 * beta.listenerQuota for i in range(numMessages): msg = json.dumps({'random': randomSeed().decode()}).encode() if alpha.send(msg, beta.name): messages.append(json.loads(msg.decode())) def checkAllReceived(): assert len(messages) == len(betaMsgHandler.receivedMessages) assert messages == betaMsgHandler.receivedMessages looper.run(eventually(checkAllReceived, retryWait=0.5, timeout=5))
def testManyMessages(tdir, looper, set_info_log_level): names = ['Alpha', 'Beta'] genKeys(tdir, names) alphaP = Printer(names[0]) betaMsgHandler = CounterMsgsHandler() alpha = ZStack(names[0], ha=genHa(), basedirpath=tdir, msgHandler=alphaP.print, restricted=True) beta = ZStack(names[1], ha=genHa(), basedirpath=tdir, msgHandler=betaMsgHandler.handler, restricted=True) prepStacks(looper, alpha, beta, connect=True, useKeys=True) looper.runFor(1) msgNum = 100000 msgSender = MessageSender(msgNum, alpha, beta.name) looper.add(msgSender) def checkAllReceived(): assert msgSender.sentMsgCount == msgNum assert betaMsgHandler.receivedMsgCount == msgNum looper.run(eventually(checkAllReceived, retryWait=1, timeout=60))
def connected_nodestacks(registry, tdir, looper, connection_timeout, tconf): genKeys(tdir, registry.keys()) stacks = [] for name, ha in registry.items(): printer = Printer(name) stackParams = dict(name=name, ha=ha, basedirpath=tdir, auth_mode=AuthMode.RESTRICTED.value) reg = copy(registry) reg.pop(name) stack = KITZStack(stackParams, printer.print, reg) stack.listenerQuota = tconf.NODE_TO_NODE_STACK_QUOTA stack.listenerSize = tconf.NODE_TO_NODE_STACK_SIZE stacks.append(stack) motors = prepStacks(looper, *stacks, connect=False, useKeys=True) looper.run( eventually(checkStacksConnected, stacks, retryWait=1, timeout=connection_timeout)) return stacks, motors
def create_stack(name, looper, tdir, tconf): printer = Printer(name) stackParams = dict(name=name, ha=REGISTRY[name], basedirpath=tdir, auth_mode=AuthMode.RESTRICTED.value) stack = KITZStack(stackParams, msgHandler=printer.print, registry=REGISTRY, config=tconf) patch_ping_pong(stack) motor = prepStacks(looper, *[stack], connect=False, useKeys=True)[0] return stack, motor
def create_alpha(tdir, name): alphaP = Printer(name) alpha = ZStack(name, ha=genHa(), basedirpath=tdir, msgHandler=alphaP.print, restricted=True) return alpha
def dummyZStack(tdir, tconf): name = 'Alpha' alphaP = Printer(name) return ZStack(name, ha=genHa(), basedirpath=tdir, msgHandler=alphaP.print, seed=randomSeed(), config=tconf)
def test2RStackCommunication(tdir, looper): names = ['Alpha', 'Beta'] alphaP = Printer(names[0]) betaP = Printer(names[1]) stackParamsAlpha = { "name": names[0], "ha": genHa(), "auto": AutoMode.always, "main": True, "mutable": "mutable", "messageTimeout": 30, "basedirpath": tdir } stackParamsBeta = { "name": names[1], "ha": genHa(), "main": True, "auto": AutoMode.always, "mutable": "mutable", "messageTimeout": 30, "basedirpath": tdir } alpha = SimpleRStack(stackParamsAlpha, msgHandler=alphaP.print) beta = SimpleRStack(stackParamsBeta, msgHandler=betaP.print) alpha.connect(ha=beta.ha) beta.connect(ha=alpha.ha) prepStacks(looper, alpha, beta, connect=False, useKeys=False) looper.run( eventually(checkStacksConnected, [alpha, beta], retryWait=1, timeout=10)) alpha.send({'greetings': 'hi Beta'}, beta.name) beta.send({'greetings': 'hi Alpha'}, alpha.name) looper.run(eventually(chkPrinted, betaP, {'greetings': 'hi Beta'})) looper.run(eventually(chkPrinted, alphaP, {'greetings': 'hi Alpha'}))
def create_and_prep_stacks(names, basedir, looper, conf): genKeys(basedir, names) printers = [Printer(n) for n in names] # adict is used below to copy the config module since one stack might # have different config from others stacks = [ZStack(n, ha=genHa(), basedirpath=basedir, msgHandler=printers[i].print, restricted=True, config=adict(**conf.__dict__)) for i, n in enumerate(names)] prepStacks(looper, *stacks, connect=True, useKeys=True) return stacks, printers
def create_alpha(tdir, looper, name): alphaP = Printer(name) alpha = ZStack(name, ha=genHa(), basedirpath=tdir, msgHandler=alphaP.print, restricted=True) prepStacks(looper, alpha, connect=False) patch_ping_pong(alpha) return alpha
def testKitZStacksConnected(registry, tdir, looper): genKeys(tdir, registry.keys()) stacks = [] for name, ha in registry.items(): printer = Printer(name) stackParams = dict(name=name, ha=ha, basedirpath=tdir, auto=0) reg = copy(registry) reg.pop(name) stack = KITZStack(stackParams, printer.print, reg) stacks.append(stack) prepStacks(looper, *stacks, connect=False, useKeys=True) looper.run(eventually(checkStacksConnected, stacks, retryWait=1, timeout=10))
def testKitZStacksCommunication(registry, tdir, looper): # TODO: Use a packet capture tool genKeys(tdir, registry.keys()) stacks = [] names = [] for name, ha in registry.items(): printer = Printer(name) stackParams = dict(name=name, ha=ha, basedirpath=tdir, auth_mode=AuthMode.RESTRICTED.value) reg = copy(registry) reg.pop(name) stack = KITZStack(stackParams, printer.print, reg) stacks.append(stack) names.append((name, printer)) prepStacks(looper, *stacks, connect=False, useKeys=True) # TODO: the connection may not be established for the first try because # some of the stacks may not have had a remote yet (that is they haven't had yet called connect) timeout = 4 * KITZStack.RETRY_TIMEOUT_RESTRICTED + 1 looper.run(eventually( checkStacksConnected, stacks, retryWait=1, timeout=timeout)) def send_recv(): for i, j in combinations(range(len(stacks)), 2): alpha = stacks[i] beta = stacks[j] alpha.send({'greetings': 'hi'}, beta.name) beta.send({'greetings': 'hello'}, alpha.name) looper.run( eventually(chkPrinted, names[i][1], {'greetings': 'hello'}, timeout=15)) looper.run(eventually(chkPrinted, names[j][1], { 'greetings': 'hi'}, timeout=15)) names[i][1].reset() names[j][1].reset() def pr(): for stack in stacks: for r in stack.remotes.values(): print(r._lastSocketEvents()) for _ in range(100): # send_recv() looper.run(eventually( checkStacksConnected, stacks, retryWait=1, timeout=timeout)) # pr() looper.runFor(30)
def testKitZStacksConnected(registry, tdir, looper, tconf): genKeys(tdir, registry.keys()) stacks = [] for name, ha in registry.items(): printer = Printer(name) stackParams = dict(name=name, ha=ha, basedirpath=tdir, auth_mode=AuthMode.RESTRICTED.value) reg = copy(registry) reg.pop(name) stack = KITZStack(stackParams, printer.print, reg) stacks.append(stack) prepStacks(looper, *stacks, connect=False, useKeys=True) # TODO: the connection may not be established for the first try because # some of the stacks may not have had a remote yet (that is they haven't had yet called connect) timeout = 2*tconf.RETRY_TIMEOUT_RESTRICTED+1 looper.run(eventually( checkStacksConnected, stacks, retryWait=1, timeout=timeout))
def create_stack(count): genKeys(tdir, registry.keys()) stacks = [] for name, ha in registry.items(): printer = Printer(name) stackParams = dict(name=name, ha=ha, basedirpath=tdir, auth_mode=AuthMode.RESTRICTED.value) reg = copy(registry) reg.pop(name) stack = nodeStackClass(stackParams, printer.print, reg) stack.start() stacks.append(stack) if len(stacks) == count: break return stacks
def create_stacks(tdir, looper): names = ['Alpha', 'Beta'] genKeys(tdir, names) alphaP = Printer(names[0]) alpha = ZStack(names[0], ha=genHa(), basedirpath=tdir, msgHandler=alphaP.print, restricted=True) prepStacks(looper, alpha, connect=False) beta_msg_handler = CounterMsgsHandler() beta = ZStack(names[1], ha=genHa(), basedirpath=tdir, msgHandler=beta_msg_handler.handler, restricted=True) prepStacks(looper, beta, connect=False) return alpha, beta
def printers(registry): printersDict = {} for name, ha in registry.items(): printersDict[name] = Printer(name) return printersDict