Skip to content
This repository has been archived by the owner on Sep 8, 2020. It is now read-only.

Commit

Permalink
added test for start()... only caveat is I cannot get rid of the asyn…
Browse files Browse the repository at this point in the history
…c thread
  • Loading branch information
Adam Groszer committed Jan 24, 2010
1 parent a6cd9ff commit 3f669f1
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
59 changes: 59 additions & 0 deletions src/zc/monitor/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,62 @@ Once we're done we can tell the calculator to let us go.

>>> connection.test_input('quit\n')
-> CLOSE

Start server
------------

>>> import time
>>> import zope.testing.loggingsupport, logging
>>> loghandler = zope.testing.loggingsupport.InstalledHandler(
... None, level=logging.INFO)


>>> zc.monitor.start(9644)
True

>>> print loghandler
zc.ngi.async.server INFO
listening on ('', 9644)

>>> zc.monitor.last_listener.close()
>>> zc.monitor.last_listener = None
>>> time.sleep(0.1)



>>> loghandler.clear()

>>> zc.monitor.start(('127.0.0.1', 9644))
True

>>> print loghandler
zc.ngi.async.server INFO
listening on ('127.0.0.1', 9644)

>>> zc.monitor.last_listener.close()
>>> zc.monitor.last_listener = None
>>> time.sleep(0.1)

Trying to rebind to a port in use:

>>> loghandler.clear()

>>> zc.monitor.start(('127.0.0.1', 9644))
True

>>> zc.monitor.start(('127.0.0.1', 9644))
False

>>> print loghandler
zc.ngi.async.server INFO
listening on ('127.0.0.1', 9644)
zc.ngi.async.server WARNING
unable to listen on ('127.0.0.1', 9644)
root WARNING
unable to start zc.monitor server because the address ('127.0.0.1', 9644) is in use.

>>> zc.monitor.last_listener.close()
>>> zc.monitor.last_listener = None
>>> time.sleep(0.1)

>>> loghandler.uninstall()
10 changes: 7 additions & 3 deletions src/zc/monitor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def handle_input(self, connection, data):
def handle_close(self, connection, reason):
pass # Don't care

#testing support
last_listener = None

def start(address):
"""start monitor server.
Expand All @@ -90,14 +92,16 @@ def start(address):
ourAddress = address

try:
zc.ngi.async.listener(ourAddress, Server)
global last_listener
last_listener = zc.ngi.async.listener(ourAddress, Server)
except socket.error, e:
if e.args[0] == errno.EADDRINUSE:
# Don't kill the process just because somebody else has our port.
# This might be a zopectl debug or some other innocuous problem.
logging.warning(
'unable to start zc.monitor server because port %d is in use.',
port)
'unable to start zc.monitor server because the address %s '\
'is in use.',
ourAddress)
return False
else:
raise
Expand Down

0 comments on commit 3f669f1

Please sign in to comment.