def test_EPIPE_on_connection_during_multisend(): a, b = socket.socketpair() c, d = socket.socketpair() c = Connections('test', 'dummy') c.add(a, 'remote') c.add(c, 'remote2') b.close() # exception should be swallowed on send c.send(c.ALL, 'message')
def start_job(self, work_cb, finished_cb, *args, **kwargs): ''' this is used to start jobs that must be run in a separate process ''' send, recv = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM) recv = IOStream(recv) args = list(args) args.reverse() args.append(send) args.reverse() def work_wrapper(cb): def wrap(sock, *args, **kwargs): results = cb(*args, **kwargs) sock.send(escape.json_encode(results) + "\r\n\r\n") sock.close() return wrap p = multiprocessing.Process(target=work_wrapper(work_cb), args=args, kwargs=kwargs) p.start() def finished_wrapper(self, cb): def wrap(results): results = escape.json_decode(results.strip()) data = cb(results) self.finish(data) return wrap recv.read_until("\r\n\r\n", finished_wrapper(self, finished_cb))
def test_connectionwatcher(): r, w = socket.socketpair() # socket.AF_INET, socket.SOCK_STREAM) g = eventlet.spawn(dummythread) connwatch.spawn(r, weakref.ref(g), TestException) w.close() with eventlet.Timeout(1): g.wait()
def test(): a, b = socket.socketpair() ma = Messaging(a) mb = Messaging(b) assert ma.wait(0) is None mb.send(['foo']) ret = ma.wait() assert ret == ['foo'], ret
def test_EPIPE_on_connection(): a, b = socket.socketpair() c = Connections('test', 'dummy') c.add(a, 'remote') b.close() c.send('remote', 'message') # the bad file-descriptor is silently removed assert len(c.sockets) == 0
def test_error_is_timeout(): s1, _ = socket.socketpair() s1.settimeout(0.01) try: s1.recv(1) except socket.error as e: tests.check_is_timeout(e) else: assert False, 'No timeout, socket.error was not raised'
def test_closed_remote_wait(): a, b = socket.socketpair() ma = Messaging(a) mb = Messaging(b) # send a message for luck. mb.send('message') assert ma.wait() == 'message' b.close() ma.wait()
def test_EBADF_on_connection(): a, b = socket.socketpair() c = Connections('test', 'dummy') c.add(a, 'remote') a.close() # the bad file-descriptor is silently removed so we # will block here on this timeout with eventlet.Timeout(0.1): tools.assert_raises(eventlet.Timeout, c.get) assert len(c.sockets) == 0
def start(self, thread=False): self.loop_in, self.loop_out = socket.socketpair() self.lock = threading.Lock() if thread: assert(self.thread is None) self.thread = threading.Thread(target=self.run) self.thread.setDaemon(True) self.thread.start() else: eventlet.spawn_n(self.run)
def test_closed_socket_wait(): # trigger an EBADF a, b = socket.socketpair() ma = Messaging(a) mb = Messaging(b) # send a message for luck. ma.send('message') assert mb.wait() == 'message' a.close() ma.send('another message')
def test_closed_remote_send(): # trigger an EPIPE a, b = socket.socketpair() ma = Messaging(a) mb = Messaging(b) # send a message for luck. ma.send('message') assert mb.wait() == 'message' b.close() ma.send('another message')
def test_updated_sockets_during_select(): a, b = socket.socketpair() c = Connections('test', 'dummy') c.add(a, 'remote') def updater(c): a, b = socket.socketpair() c.add(a, 'new') d = Connections('test', 'remote_side') d.add(b, 'target') d.send('target', 'message') eventlet.spawn(updater, c) with eventlet.Timeout(1): c.get()
def test_broadcast_to_other(): r, w = socket.socketpair() b1 = Broker('d', 'dummy1') b1.connections.add(r) b2 = Broker('d', 'dummy2') b2.connections.add(w) b1.send(b1.BROADCAST, 'sub', 'message') ret = b2.listen_one(enqueue=False) (bid, eid), sub, message = ret assert bid == 'dummy1' assert sub == 'sub' assert message == 'message'
def start(config, options): server_config = get_config_section_for_name(config, "server", options.name) if "hub_module" in server_config: hubs.use_hub(server_config.import_("hub_module")) if "fork_children" in server_config: if "prefork_listen" in server_config: _toaddr = lambda (host, port): (host, int(port)) toaddr = lambda addrstr: _toaddr(addrstr.split(":", 1)) addrs = map(toaddr, server_config["prefork_listen"].split(",")) for addr in addrs: PREFORK_SOCKETS[addr] = eventlet.listen(addr) children = server_config["fork_children"].split(",") connections = {} for i, j in enumerate(children): for k, l in enumerate(children): if i != k and (i, k) not in connections: a, b = socket.socketpair() connections[(i, k)] = (l, a) connections[(k, i)] = (j, b) for i, child in enumerate(children): print "forking", child pid = os.fork() if pid == 0: # child hub = hubs.get_hub() if hasattr(hub, "poll") and hasattr(hub.poll, "fileno"): # probably epoll which uses a filedescriptor # and thus forking without exec is bad using that # poll instance. hubs.use_hub(hubs.get_default_hub()) myconns = [] for (l, r), s in connections.items(): if l == i: myconns.append(s) else: s[1].close() options.name = child start_single(config, options, myconns) sys.exit(1) while True: try: pid, exitstatus = os.wait() print "childprocess %d died" % pid except OSError, e: if e.errno == errno.ECHILD: sys.exit(0) else: raise except KeyboardInterrupt, e: print "quitting..."
def test_remote_event(): r, w = socket.socketpair() b1 = Broker('d', 'dummy1') b1.connections.add(r) b2 = Broker('d', 'dummy2') b2.connections.add(w) event = b1.send(b1.BROADCAST, 'sub', 'ping') ret = b2.listen_one(enqueue=False) retevt = b2.events.returner_for(ret[0]) retevt.send('pong') with eventlet.Timeout(1): b1.listen_one() b1.process_one() msg = event.wait() assert msg == 'pong', msg
def test(): c1 = Connections('test', 'dummy1') c2 = Connections('test', 'dummy2') r, w = socket.socketpair() c1.add(r, 'dummy2') c2.add(w) c1.send('dummy2', 'test message') with eventlet.Timeout(1): sid, ret = c2.get() assert sid == 'dummy1' assert ret == 'test message' c2.send('dummy1', 'back atcha') with eventlet.Timeout(1): sid, ret = c1.get() assert sid == 'dummy2' assert ret == 'back atcha'
def __init__(self): server_pipe, client_pipe = multiprocessing.Pipe() server_unix_sock, client_unix_sock = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM, 0) if server_unix_sock.family != socket.AF_UNIX: raise ValueError("Only AF_UNIX sockets are allowed") pid = os.fork() if pid == 0: server_pipe.close() server_unix_sock.close() self._pipe = client_pipe self._unix_socket = client_unix_sock else: client_pipe.close() client_unix_sock.close() BaronServer(pid, server_pipe, server_unix_sock) sys.exit(0) # If we get here, somehow, die.
def fork(self): if (self._pipe): #We've already forked. return server_pipe, client_pipe = multiprocessing.Pipe() server_unix_sock, client_unix_sock = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM, 0) if server_unix_sock.family != socket.AF_UNIX: raise ValueError("Only AF_UNIX sockets are allowed") pid = os.fork() if pid == 0: server_pipe.close() server_unix_sock.close() self._pipe = client_pipe self._unix_socket = client_unix_sock else: client_pipe.close() client_unix_sock.close() setproctitle("%s #baron" % getproctitle()) BaronServer(pid, server_pipe, server_unix_sock) sys.exit(0) # If we get here, somehow, die.
def updater(c): a, b = socket.socketpair() c.add(a, 'new') d = Connections('test', 'remote_side') d.add(b, 'target') d.send('target', 'message')
def startLoopback(self): self.loop_in, loop_out = socket.socketpair() eventlet.spawn(self.doCalls, loop_out) self.thread = threading.currentThread()
def setUp(self): self.serv, self.cli = socket.socketpair()
def test_socketpair_select(self): # https://github.com/eventlet/eventlet/pull/25 s1, s2 = socket.socketpair() assert select.select([], [s1], [], 0) == ([], [s1], []) assert select.select([], [s1], [], 0) == ([], [s1], [])