def t09(): pretty = '%s t9' % __file__ print(pretty) s,p1 = find_free_port() q1 = Pipe() ctr1 = MockControl(p1, 'password', s, [], q1) ctr1.start() s,p2 = find_free_port() q2 = Pipe() ctr2 = MockControl(p2, None, s, [], q2) ctr2.start() rem1 = RemoteControl(('',p1), 'password', timeout=5) rem1.connect_remote(('',p2), None) rem2 = RemoteControl(('',p2), None, timeout=5) rem2.stop(__async__=True) result = True try: msg = q1.get(timeout=1) if msg != ['',p2]: print('FAIL %s: wrong connection lost: %s' % (pretty, msg)) result = False except Exception, e: print('FAIL %s: connection not lost: %s' % (pretty, str(e))) result = False ctr2.terminate()
def t11(): pretty = '%s t11' % __file__ print(pretty) s,p1 = find_free_port() q1 = Pipe() ctr1 = MockControl(p1, 'password', s, [], q1) ctr1.start() s,p2 = find_free_port() q2 = Pipe() ctr2 = MockControl(p2, None, s, [], q2) ctr2.start() # ask ctr1 to create a connection to ctr2 rem1 = RemoteControl(('',p1), 'password', timeout=5) msg = rem1.connect_remote(('',p2), None) if msg != 'pong': print('FAIL %s: bad connection established: %s' % (pretty, msg)) ctr1.terminate() ctr2.terminate() ctr1.join() ctr2.join() return False # tell ctr1 to shut down and wait for out of band ack rem1.stop(__async__=True) try: msg = q1.get(1) except Exception, e: print('FAIL %s: first oob not received: %s' % (pretty, str(e))) ctr1.terminate() ctr2.terminate() return False
def t13(): pretty = '%s t13' % __file__ print(pretty) s,p1 = find_free_port() q1 = Pipe() ctr1 = MockControl(p1, 'pass', s, [], q1) ctr1.start() s,p2 = find_free_port() q2 = Pipe() ctr2 = MockControl(p2, 'secret', s, [], q2) ctr2.start() # ask ctr1 to create a connection to ctr2 rem1 = RemoteControl(('',p1), 'pass', timeout=5) msg = rem1.connect_remote(('',p2), 'secret') if msg != 'pong': print('FAIL %s: bad connection established: %s' % (pretty, msg)) ctr1.terminate() ctr2.terminate() ctr1.join() ctr2.join() return False # stop ctr1 but don't drop the connection to it (rem1 is still in scope and # doesn't get garbage collected or expressely closed) rem1.stop(__async__=True) ctr1.join() # check if ctr2 discovered the connection loss from ctr1 result = True try: msg = q2.get(1) if type(msg) != list: # no way of knowing the exact content beforehand print('FAIL %s: wrong oob received: %s' % (pretty, msg)) result = False except Exception, e: print('FAIL %s: oob not received: %s' % (pretty, str(e))) result = False
def t1(pretty, factory): class Defiant(Control): def handle_SIGTERM(self, signum, frame): pass # go on living @Control.rpc def ping(self): return 'pong', self.pid @Control.rpc def stop(self): Control.stop(self) class Spawner(Control): children = None @Control.rpc def spawn(self): if not self.children: self.children = [] sock, port = find_free_port() d = Defiant(port, None, sock) d.start() self.children.append(d) return [d.pid, port] @Control.rpc def orphanize(self): if not self.children: return None child, self.children = self.children[0], self.children[1:] child.terminate() self.join_later(child) @Control.rpc def list_orphans(self): return [p.pid for p in self.deferred_joins] sock, port = find_free_port() spawner = Spawner(port, None, sock) spawner.start() factory.processes.append(spawner) master = RemoteControl(('', port), None, 1) defiers = [] for i in range(5): pid, port = master.spawn() remote = RemoteControl(('', port), None, 5) defiers.append(remote) orphans = master.list_orphans() if orphans: print('FAIL %s: there are orphans: %s' % (pretty, orphans)) return False for i in range(5): master.orphanize() orphans = master.list_orphans() if len(orphans) != 5: print('FAIL %s: there are not 5 orphans: %s' % (pretty, orphans)) return False for remote in defiers: try: remote.ping() except Exception, e: print('FAIL %s: orphan died too soon: %s' % (pretty, remote)) return False remote.stop(__async__=True)
try: pipe = Pipe() control = MockControl(None, None, sock, [], pipe) control.start() except Exception, e: print('FAIL %s: could not create control: %s' % (pretty, str(e))) return False try: remote = RemoteControl(('',port), None, 1) except Exception, e: print('FAIL %s: could not create remote: %s' % (pretty, str(e))) return False try: remote.stop(__async__=True) control.join() oob = pipe.get(timeout=1) except Exception, e: print('FAIL %s: never got out of band response' % pretty) return False if oob != 'shutdown': print('FAIL %s: wrong oob response: %s' % (pretty, oob)) return False return True # check that two Control objects don't interfere with each other @smoke def t04():