[Zodb-checkins] CVS: ZODB3/ZEO/tests - zeoserver.py:1.1

Barry Warsaw barry@wooz.org
Thu, 12 Dec 2002 14:03:00 -0500


Update of /cvs-repository/ZODB3/ZEO/tests
In directory cvs.zope.org:/tmp/cvs-serv22037

Added Files:
	zeoserver.py 
Log Message:
Since both the Windows and Unix ZEO tests share the same code, it
makes little sense to call the server script `winserver.py'.  That's
gone now, in favor of this zeoserver.py script.  Changes include:

main(): Accept a -C option pointing to a temp file containing a
ZConfig storage section.  This reads the file and creates the so
described storage, then removes the temp config file.  It creates an
administrative server which opens an `admin' port which is used to
os._exit() this process, with the following protocol.  The first time
the admin port is connected to, an ack character is sent from
zeoserver.  This is necessary to coordinate the tests so that they are
assured the server is running and responding before it tries to do
more.  Otherwise, some tests were vulnerable to timing bugs where the
shutdown connect happened before the server was ready to accept them.

The second connect exits the server process just like before.  Copious
log entries are written.

This script also cleans up after the storages are closed, by calling
the storage's .cleanup() method if it exists (FileStorage and the
Berkeley storages both implement this method).


=== Added File ZODB3/ZEO/tests/zeoserver.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
"""Helper file used to launch a ZEO server cross platform"""

import os
import sys
import errno
import getopt
import random
import socket
import asyncore
import ThreadedAsync

import ZConfig
import zLOG
from ZODB import StorageConfig
import ZEO.StorageServer


def load_storage(fp):
    rootconf = ZConfig.loadfile(fp)
    storageconf = rootconf.getSection('Storage')
    return StorageConfig.createStorage(storageconf)


def cleanup(storage):
    # FileStorage and the Berkeley storages have this method, which deletes
    # all files and directories used by the storage.  This prevents @-files
    # from clogging up /tmp
    try:
        storage.cleanup()
    except AttributeError:
        pass


def log(label, msg, *args):
    zLOG.LOG(label, zLOG.DEBUG, msg % args)


class ZEOTestServer(asyncore.dispatcher):
    """A server for killing the whole process at the end of a test.

    The first time we connect to this server, we write an ack character down
    the socket.  The other end should block on a recv() of the socket so it
    can guarantee the server has started up before continuing on.

    The second connect to the port immediately exits the process, via
    os._exit(), without writing data on the socket.  It does close and clean
    up the storage first.  The other end will get the empty string from its
    recv() which will be enough to tell it that the server has exited.

    I think this should prevent us from ever getting a legitimate addr-in-use
    error.
    """
    __super_init = asyncore.dispatcher.__init__

    def __init__(self, addr, storage):
        self.__super_init()
        self.storage = storage
        # Count down to zero, the number of connects
        self.count = 1
        # For zLOG
        self.label ='zeoserver:%d @ %s' % (os.getpid(), addr)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        # Some ZEO tests attempt a quick start of the server using the same
        # port so we have to set the reuse flag.
        self.set_reuse_addr()
        try:
            self.bind(addr)
        except:
            # We really want to see these exceptions
            import traceback
            traceback.print_exc()
            raise
        self.listen(5)
        self.log('bound and listening')

    def log(self, msg, *args):
        log(self.label, msg, *args)

    def handle_accept(self):
        sock, addr = self.accept()
        self.log('in handle_accept()')
        # When we're done with everything, close the storage.  Do not write
        # the ack character until the storage is finished closing.
        if self.count <= 0:
            self.log('closing the storage')
            self.storage.close()
            cleanup(self.storage)
        if self.count <= 0:
            self.log('exiting')
            os._exit(0)
        self.log('continuing')
        sock.send('X')
        self.count -= 1


def main():
    label = 'zeoserver:%d' % os.getpid()
    log(label, 'starting')
    # We don't do much sanity checking of the arguments, since if we get it
    # wrong, it's a bug in the test suite.
    ro_svr = 0
    configfile = None
    # Parse the arguments and let getopt.error percolate
    opts, args = getopt.getopt(sys.argv[1:], 'rC:')
    for opt, arg in opts:
        if opt == '-r':
            ro_svr = 1
        elif opt == '-C':
            configfile = arg
    # Open the config file and let ZConfig parse the data there.  Then remove
    # the config file, otherwise we'll leave turds.
    fp = open(configfile, 'r')
    storage = load_storage(fp)
    fp.close()
    os.remove(configfile)
    # The rest of the args are hostname, portnum
    zeo_port = int(args[0])
    test_port = zeo_port + 1
    try:
        log(label, 'creating the test server')
        t = ZEOTestServer(('', test_port), storage)
    except socket.error, e:
        if e[0] <> errno.EADDRINUSE: raise
        log(label, 'addr in use, closing and exiting')
        storage.close()
        cleanup(storage)
        sys.exit(2)
    addr = ('', zeo_port)
    log(label, 'creating the storage server')
    serv = ZEO.StorageServer.StorageServer(addr, {'1': storage}, ro_svr)
    log(label, 'entering ThreadedAsync loop')
    ThreadedAsync.loop()


if __name__ == '__main__':
    main()