~ubuntu-branches/ubuntu/vivid/dulwich/vivid-proposed

« back to all changes in this revision

Viewing changes to dulwich/tests/compat/server_utils.py

  • Committer: Package Import Robot
  • Author(s): Jelmer Vernooij
  • Date: 2014-04-23 01:41:04 UTC
  • mfrom: (1.5.5)
  • Revision ID: package-import@ubuntu.com-20140423014104-nulhaisomztpfriy
Tags: 0.9.6-1
* New upstream release.
* Allow output to stderr in autopktest.

Show diffs side-by-side

added added

removed removed

Lines of Context:
130
130
        run_git_or_fail(['fetch', self.url(port)] + self.branch_args(),
131
131
                        cwd=self._old_repo.path)
132
132
        # flush the pack cache so any new packs are picked up
133
 
        self._old_repo.object_store._pack_cache = None
 
133
        self._old_repo.object_store._pack_cache_time = 0
134
134
        self.assertReposEqual(self._old_repo, self._new_repo)
135
135
 
136
136
    def test_fetch_from_dulwich_no_op(self):
144
144
        run_git_or_fail(['fetch', self.url(port)] + self.branch_args(),
145
145
                        cwd=self._old_repo.path)
146
146
        # flush the pack cache so any new packs are picked up
147
 
        self._old_repo.object_store._pack_cache = None
 
147
        self._old_repo.object_store._pack_cache_time = 0
148
148
        self.assertReposEqual(self._old_repo, self._new_repo)
149
149
 
150
150
    def test_clone_from_dulwich_empty(self):
234
234
        self.assertReposEqual(clone, self._source_repo)
235
235
 
236
236
 
237
 
class ShutdownServerMixIn:
238
 
    """Mixin that allows serve_forever to be shut down.
239
 
 
240
 
    The methods in this mixin are backported from SocketServer.py in the Python
241
 
    2.6.4 standard library. The mixin is unnecessary in 2.6 and later, when
242
 
    BaseServer supports the shutdown method directly.
243
 
    """
244
 
 
245
 
    def __init__(self):
246
 
        self.__is_shut_down = threading.Event()
247
 
        self.__serving = False
248
 
 
249
 
    def serve_forever(self, poll_interval=0.5):
250
 
        """Handle one request at a time until shutdown.
251
 
 
252
 
        Polls for shutdown every poll_interval seconds. Ignores
253
 
        self.timeout. If you need to do periodic tasks, do them in
254
 
        another thread.
255
 
        """
256
 
        self.__serving = True
257
 
        self.__is_shut_down.clear()
258
 
        while self.__serving:
259
 
            # XXX: Consider using another file descriptor or
260
 
            # connecting to the socket to wake this up instead of
261
 
            # polling. Polling reduces our responsiveness to a
262
 
            # shutdown request and wastes cpu at all other times.
263
 
            r, w, e = select.select([self], [], [], poll_interval)
264
 
            if r:
265
 
                self._handle_request_noblock()
266
 
        self.__is_shut_down.set()
267
 
 
268
 
    serve = serve_forever  # override alias from TCPGitServer
269
 
 
270
 
    def shutdown(self):
271
 
        """Stops the serve_forever loop.
272
 
 
273
 
        Blocks until the loop has finished. This must be called while
274
 
        serve_forever() is running in another thread, or it will deadlock.
275
 
        """
276
 
        self.__serving = False
277
 
        self.__is_shut_down.wait()
278
 
 
279
 
    def handle_request(self):
280
 
        """Handle one request, possibly blocking.
281
 
 
282
 
        Respects self.timeout.
283
 
        """
284
 
        # Support people who used socket.settimeout() to escape
285
 
        # handle_request before self.timeout was available.
286
 
        timeout = self.socket.gettimeout()
287
 
        if timeout is None:
288
 
            timeout = self.timeout
289
 
        elif self.timeout is not None:
290
 
            timeout = min(timeout, self.timeout)
291
 
        fd_sets = select.select([self], [], [], timeout)
292
 
        if not fd_sets[0]:
293
 
            self.handle_timeout()
294
 
            return
295
 
        self._handle_request_noblock()
296
 
 
297
 
    def _handle_request_noblock(self):
298
 
        """Handle one request, without blocking.
299
 
 
300
 
        I assume that select.select has returned that the socket is
301
 
        readable before this function was called, so there should be
302
 
        no risk of blocking in get_request().
303
 
        """
304
 
        try:
305
 
            request, client_address = self.get_request()
306
 
        except socket.error:
307
 
            return
308
 
        if self.verify_request(request, client_address):
309
 
            try:
310
 
                self.process_request(request, client_address)
311
 
            except:
312
 
                self.handle_error(request, client_address)
313
 
                self.close_request(request)
314
 
 
315
 
 
316
237
# TODO(dborowitz): Come up with a better way of testing various permutations of
317
238
# capabilities. The only reason it is the way it is now is that side-band-64k
318
239
# was only recently introduced into git-receive-pack.
325
246
                     if c != 'side-band-64k')
326
247
 
327
248
 
328
 
def ignore_error((e_type, e_value, e_tb)):
 
249
def ignore_error(error):
329
250
    """Check whether this error is safe to ignore."""
 
251
    (e_type, e_value, e_tb) = error
330
252
    return (issubclass(e_type, socket.error) and
331
253
            e_value[0] in (errno.ECONNRESET, errno.EPIPE))
332
254