Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can easily start a server and then access it in a test case. The gevent's <a href="http://bitbucket.org/denis/gevent/src/tip/greentest/" rel="noreferrer">own test suite</a> does exactly that for testing gevent's <a href="http://gevent.org/servers.html" rel="noreferrer">built-in servers</a>.</p> <p>For example:</p> <pre><code>class SimpleServer(gevent.server.StreamServer): def handle(self, socket, address): socket.sendall('hello and goodbye!') class Test(unittest.TestCase): def test(self): server = SimpleServer(('127.0.0.1', 0)) server.start() client = gevent.socket.create_connection(('127.0.0.1', server.server_port)) response = client.makefile().read() assert response == 'hello and goodbye!' server.stop() </code></pre> <p>Using <code>0</code> for the port value means the server will use any available port. After the server is started, the actual value chosen by <code>bind</code> is available as <code>server_port</code> attribute.</p> <p><code>StreamServer</code> supports SSL too, pass <code>keyfile</code> and <code>certfile</code> arguments to the constructor and it will wrap each socket with <code>SSLObject</code> before passing it to your handler.</p> <p>If you don't use <code>StreamServer</code> and your server is based on <a href="http://gevent.org/intro.html#lightweight-pseudothreads" rel="noreferrer">Greenlet</a> then indeed <a href="http://gevent.org/gevent.html#gevent.spawn" rel="noreferrer">spawning</a> it is what you should do. Don't forget to <a href="http://gevent.org/gevent.html#gevent.Greenlet.kill" rel="noreferrer">kill</a> it at the end of the test case.</p> <p>Starting a server and spawning a greenlet are fast operations in <a href="http://gevent.org/" rel="noreferrer">gevent</a>, much faster than creating a new thread or process and you can easily create a new server for each test case. Just don't forget to cleanup as soon as you don't need the server anymore.</p> <p>I believe there's no need to mock any of gevent API, it's much easier just to use it as servers and clients can happily live within the same process.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload