~verterok/ubuntuone-client/volumemanager_udfs-2

« back to all changes in this revision

Viewing changes to tests/syncdaemon/test_hashqueue.py

  • Committer: guillermo.gonzalez at canonical
  • Date: 2010-01-04 18:54:17 UTC
  • mfrom: (294.2.6 trunk)
  • Revision ID: guillermo.gonzalez@canonical.com-20100104185417-is0onxq4znx57u0j
merge with trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
import logging
26
26
import threading
27
27
 
 
28
from StringIO import StringIO
28
29
from twisted.trial.unittest import TestCase as TwistedTestCase
29
30
from twisted.internet import defer, reactor
30
31
 
444
445
    def test_interrupt_current(self):
445
446
        """ Test that the hasher correctly interrupts a inprogress task."""
446
447
        # calculate what we should receive
447
 
        testinfo = os.urandom(10000000)
 
448
        testinfo = os.urandom(1000)
448
449
        hasher = content_hash_factory()
449
450
        hasher.hash_object.update(testinfo)
450
451
        testfile = os.path.join(self.test_dir, "testfile")
467
468
                    d.errback(Exception("the hashes are different!"))
468
469
                else:
469
470
                    d.callback(True)
 
471
 
 
472
        class FakeFile(StringIO):
 
473
            """An endless file"""
 
474
            def read(self, size=10):
 
475
                """return random bytes"""
 
476
                return os.urandom(size)
 
477
            # context manager API
 
478
            def __enter__(self):
 
479
                return self
 
480
            def __exit__(self, *args):
 
481
                pass
 
482
 
 
483
        # patch __builtin__.open so we don't have to try to interrupt a hashing
 
484
        bi = __import__('__builtin__')
 
485
        old_open = bi.open
 
486
 
 
487
        class OpenFaker(object):
 
488
            """A class to fake open for specific paths"""
 
489
            def __init__(self, paths):
 
490
                self.paths = paths
 
491
                self.done = False
 
492
            def __call__(self, path, mode='r'):
 
493
                """the custom open implementation"""
 
494
                if self.done or path not in self.paths:
 
495
                    return old_open(path, mode)
 
496
                else:
 
497
                    self.done = True
 
498
                    return FakeFile()
 
499
        open_faker = OpenFaker([testfile])
 
500
        bi.open = open_faker
 
501
 
470
502
        receiver = Helper()
471
 
 
472
503
        hq = hash_queue.HashQueue(receiver)
473
 
        self.addCleanup(hq.shutdown)
474
 
 
475
 
        # read in small chunks, so we have more iterations
476
 
        hq.hasher.chunk_size = 2**8
 
504
        def cleanup():
 
505
            """cleanup the mess"""
 
506
            bi.open = old_open
 
507
            hq.shutdown()
 
508
        self.addCleanup(cleanup)
477
509
        hq.insert(testfile, "mdid")
 
510
 
478
511
        # insert it again, to cancel the first one
479
512
        reactor.callLater(0.1, hq.insert, testfile, "mdid")
480
513
        return d
481
 
    # timeout is used by trial, pylint: disable-msg=W0612
482
 
    test_interrupt_current.timeout = 5
483
514
 
484
515
    def test_shutdown(self):
485
516
        """Test that the HashQueue shutdown """