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!"))
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
480
def __exit__(self, *args):
483
# patch __builtin__.open so we don't have to try to interrupt a hashing
484
bi = __import__('__builtin__')
487
class OpenFaker(object):
488
"""A class to fake open for specific paths"""
489
def __init__(self, paths):
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)
499
open_faker = OpenFaker([testfile])
470
502
receiver = Helper()
472
503
hq = hash_queue.HashQueue(receiver)
473
self.addCleanup(hq.shutdown)
475
# read in small chunks, so we have more iterations
476
hq.hasher.chunk_size = 2**8
505
"""cleanup the mess"""
508
self.addCleanup(cleanup)
477
509
hq.insert(testfile, "mdid")
478
511
# insert it again, to cancel the first one
479
512
reactor.callLater(0.1, hq.insert, testfile, "mdid")
481
# timeout is used by trial, pylint: disable-msg=W0612
482
test_interrupt_current.timeout = 5
484
515
def test_shutdown(self):
485
516
"""Test that the HashQueue shutdown """