view tests/badserverext.py @ 33289:abd7dedbaa36

sparse: vendor Facebook-developed extension Facebook has developed an extension to enable "sparse" checkouts - a working directory with a subset of files. This feature is a critical component in enabling repositories to scale to infinite number of files while retaining reasonable performance. It's worth noting that sparse checkout is only one possible solution to this problem: another is virtual filesystems that realize files on first access. But given that virtual filesystems may not be accessible to all users, sparse checkout is necessary as a fallback. Per mailing list discussion at https://www.mercurial-scm.org/pipermail/mercurial-devel/2017-March/095868.html we want to add sparse checkout to the Mercurial distribution via roughly the following mechanism: 1. Vendor extension as-is with minimal modifications (this patch) 2. Refactor extension so it is more clearly experimental and inline with Mercurial practices 3. Move code from extension into core where possible 4. Drop experimental labeling and/or move feature into core after sign-off from narrow clone feature owners This commit essentially copies the sparse extension and tests from revision 71e0a2aeca92a4078fe1b8c76e32c88ff1929737 of the https://bitbucket.org/facebook/hg-experimental repository. A list of modifications made as part of vendoring is as follows: * "EXPERIMENTAL" added to module docstring * Imports were changed to match Mercurial style conventions * "testedwith" value was updated to core Mercurial special value and comment boilerplate was inserted * A "clone_sparse" function was renamed to "clonesparse" to appease the style checker * Paths to the sparse extension in tests reflect built-in location * test-sparse-extensions.t was renamed to test-sparse-fsmonitor.t and references to "simplecache" were removed. The test always skips because it isn't trivial to run it given the way we currently run fsmonitor tests * A double empty line was removed from test-sparse-profiles.t There are aspects of the added code that are obviously not ideal. The goal is to make a minimal number of modifications as part of the vendoring to make it easier to track changes from the original implementation. Refactoring will occur in subsequent patches.
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 01 Jul 2017 10:43:29 -0700
parents c077eac329e2
children fb7897e53d49
line wrap: on
line source

# badserverext.py - Extension making servers behave badly
#
# Copyright 2017 Gregory Szorc <gregory.szorc@gmail.com>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

# no-check-code

"""Extension to make servers behave badly.

This extension is useful for testing Mercurial behavior when various network
events occur.

Various config options in the [badserver] section influence behavior:

closebeforeaccept
   If true, close() the server socket when a new connection arrives before
   accept() is called. The server will then exit.

closeafteraccept
   If true, the server will close() the client socket immediately after
   accept().

closeafterrecvbytes
   If defined, close the client socket after receiving this many bytes.

closeaftersendbytes
   If defined, close the client socket after sending this many bytes.
"""

from __future__ import absolute_import

import socket

from mercurial import(
    registrar,
)

from mercurial.hgweb import (
    server,
)

configtable = {}
configitem = registrar.configitem(configtable)

configitem('badserver', 'closeafteraccept',
    default=False,
)
configitem('badserver', 'closeafterrecvbytes',
    default=0,
)
configitem('badserver', 'closeaftersendbytes',
    default=0,
)
configitem('badserver', 'closebeforeaccept',
    default=False,
)

# We can't adjust __class__ on a socket instance. So we define a proxy type.
class socketproxy(object):
    __slots__ = (
        '_orig',
        '_logfp',
        '_closeafterrecvbytes',
        '_closeaftersendbytes',
    )

    def __init__(self, obj, logfp, closeafterrecvbytes=0,
                 closeaftersendbytes=0):
        object.__setattr__(self, '_orig', obj)
        object.__setattr__(self, '_logfp', logfp)
        object.__setattr__(self, '_closeafterrecvbytes', closeafterrecvbytes)
        object.__setattr__(self, '_closeaftersendbytes', closeaftersendbytes)

    def __getattribute__(self, name):
        if name in ('makefile',):
            return object.__getattribute__(self, name)

        return getattr(object.__getattribute__(self, '_orig'), name)

    def __delattr__(self, name):
        delattr(object.__getattribute__(self, '_orig'), name)

    def __setattr__(self, name, value):
        setattr(object.__getattribute__(self, '_orig'), name, value)

    def makefile(self, mode, bufsize):
        f = object.__getattribute__(self, '_orig').makefile(mode, bufsize)

        logfp = object.__getattribute__(self, '_logfp')
        closeafterrecvbytes = object.__getattribute__(self,
                                                      '_closeafterrecvbytes')
        closeaftersendbytes = object.__getattribute__(self,
                                                      '_closeaftersendbytes')

        return fileobjectproxy(f, logfp,
                               closeafterrecvbytes=closeafterrecvbytes,
                               closeaftersendbytes=closeaftersendbytes)

# We can't adjust __class__ on socket._fileobject, so define a proxy.
class fileobjectproxy(object):
    __slots__ = (
        '_orig',
        '_logfp',
        '_closeafterrecvbytes',
        '_closeaftersendbytes',
    )

    def __init__(self, obj, logfp, closeafterrecvbytes=0,
                 closeaftersendbytes=0):
        object.__setattr__(self, '_orig', obj)
        object.__setattr__(self, '_logfp', logfp)
        object.__setattr__(self, '_closeafterrecvbytes', closeafterrecvbytes)
        object.__setattr__(self, '_closeaftersendbytes', closeaftersendbytes)

    def __getattribute__(self, name):
        if name in ('read', 'readline', 'write', '_writelog'):
            return object.__getattribute__(self, name)

        return getattr(object.__getattribute__(self, '_orig'), name)

    def __delattr__(self, name):
        delattr(object.__getattribute__(self, '_orig'), name)

    def __setattr__(self, name, value):
        setattr(object.__getattribute__(self, '_orig'), name, value)

    def _writelog(self, msg):
        msg = msg.replace('\r', '\\r').replace('\n', '\\n')

        object.__getattribute__(self, '_logfp').write(msg)
        object.__getattribute__(self, '_logfp').write('\n')
        object.__getattribute__(self, '_logfp').flush()

    def read(self, size=-1):
        remaining = object.__getattribute__(self, '_closeafterrecvbytes')

        # No read limit. Call original function.
        if not remaining:
            result = object.__getattribute__(self, '_orig').read(size)
            self._writelog('read(%d) -> (%d) (%s) %s' % (size,
                                                           len(result),
                                                           result))
            return result

        origsize = size

        if size < 0:
            size = remaining
        else:
            size = min(remaining, size)

        result = object.__getattribute__(self, '_orig').read(size)
        remaining -= len(result)

        self._writelog('read(%d from %d) -> (%d) %s' % (
            size, origsize, len(result), result))

        object.__setattr__(self, '_closeafterrecvbytes', remaining)

        if remaining <= 0:
            self._writelog('read limit reached, closing socket')
            self._sock.close()
            # This is the easiest way to abort the current request.
            raise Exception('connection closed after receiving N bytes')

        return result

    def readline(self, size=-1):
        remaining = object.__getattribute__(self, '_closeafterrecvbytes')

        # No read limit. Call original function.
        if not remaining:
            result = object.__getattribute__(self, '_orig').readline(size)
            self._writelog('readline(%d) -> (%d) %s' % (
                size, len(result), result))
            return result

        origsize = size

        if size < 0:
            size = remaining
        else:
            size = min(remaining, size)

        result = object.__getattribute__(self, '_orig').readline(size)
        remaining -= len(result)

        self._writelog('readline(%d from %d) -> (%d) %s' % (
            size, origsize, len(result), result))

        object.__setattr__(self, '_closeafterrecvbytes', remaining)

        if remaining <= 0:
            self._writelog('read limit reached; closing socket')
            self._sock.close()
            # This is the easiest way to abort the current request.
            raise Exception('connection closed after receiving N bytes')

        return result

    def write(self, data):
        remaining = object.__getattribute__(self, '_closeaftersendbytes')

        # No byte limit on this operation. Call original function.
        if not remaining:
            self._writelog('write(%d) -> %s' % (len(data), data))
            result = object.__getattribute__(self, '_orig').write(data)
            return result

        if len(data) > remaining:
            newdata = data[0:remaining]
        else:
            newdata = data

        remaining -= len(newdata)

        self._writelog('write(%d from %d) -> (%d) %s' % (
            len(newdata), len(data), remaining, newdata))

        result = object.__getattribute__(self, '_orig').write(newdata)

        object.__setattr__(self, '_closeaftersendbytes', remaining)

        if remaining <= 0:
            self._writelog('write limit reached; closing socket')
            self._sock.close()
            raise Exception('connection closed after sending N bytes')

        return result

def extsetup(ui):
    # Change the base HTTP server class so various events can be performed.
    # See SocketServer.BaseServer for how the specially named methods work.
    class badserver(server.MercurialHTTPServer):
        def __init__(self, ui, *args, **kwargs):
            self._ui = ui
            super(badserver, self).__init__(ui, *args, **kwargs)

            # Need to inherit object so super() works.
            class badrequesthandler(self.RequestHandlerClass, object):
                def send_header(self, name, value):
                    # Make headers deterministic to facilitate testing.
                    if name.lower() == 'date':
                        value = 'Fri, 14 Apr 2017 00:00:00 GMT'
                    elif name.lower() == 'server':
                        value = 'badhttpserver'

                    return super(badrequesthandler, self).send_header(name,
                                                                      value)

            self.RequestHandlerClass = badrequesthandler

        # Called to accept() a pending socket.
        def get_request(self):
            if self._ui.configbool('badserver', 'closebeforeaccept'):
                self.socket.close()

                # Tells the server to stop processing more requests.
                self.__shutdown_request = True

                # Simulate failure to stop processing this request.
                raise socket.error('close before accept')

            if self._ui.configbool('badserver', 'closeafteraccept'):
                request, client_address = super(badserver, self).get_request()
                request.close()
                raise socket.error('close after accept')

            return super(badserver, self).get_request()

        # Does heavy lifting of processing a request. Invokes
        # self.finish_request() which calls self.RequestHandlerClass() which
        # is a hgweb.server._httprequesthandler.
        def process_request(self, socket, address):
            # Wrap socket in a proxy if we need to count bytes.
            closeafterrecvbytes = self._ui.configint('badserver',
                                                     'closeafterrecvbytes')
            closeaftersendbytes = self._ui.configint('badserver',
                                                     'closeaftersendbytes')

            if closeafterrecvbytes or closeaftersendbytes:
                socket = socketproxy(socket, self.errorlog,
                                     closeafterrecvbytes=closeafterrecvbytes,
                                     closeaftersendbytes=closeaftersendbytes)

            return super(badserver, self).process_request(socket, address)

    server.MercurialHTTPServer = badserver