Beispiel #1
0
    def test_close(self):
        journal = DiskJournal(JOURNAL_DIR, CURRENT_DIR)
        self.assertTrue(not journal.file)

        journal.setup()
        self.assertTrue(not journal.file.closed)

        journal.close()
        self.assertTrue(journal.file.closed)
Beispiel #2
0
def init_persistent_system(obj, basedir=None):
    # a system object is needed in order to coopy work
    if not obj:
        raise Exception(CORE_LOG_PREFIX +
                "Must input a valid object if there's no snapshot files")

    # if obj is a class, change obj to an insance
    if isinstance(obj, type):
        obj = obj()

    validate_system(obj)

    # first step is to check basedir argument. if isn't defined
    # coopy will create a directory name based on system class
    if not basedir:
        basedir = fileutils.obj_to_dir_name(obj)

    # convert some string to a valid directory name
    basedir = fileutils.name_to_dir(basedir)

    system_data_path = os.getcwd()

    # check if basedir exists, if not, create it
    try:
        os.listdir(basedir)
    except os.error:
        os.mkdir(basedir)

    # if no snapshot files, create first one with a 'empty' system
    if not fileutils.last_snapshot_file(basedir):
        logger.info(CORE_LOG_PREFIX + "No snapshot files..")
        SnapshotManager(basedir).take_snapshot(obj)

    # measure restore time
    start = datetime.utcnow()
    logger.info(CORE_LOG_PREFIX + "coopy init....")

    # inject clock on system object
    inject(obj, '_clock', RecordClock())
    inject(obj, '_system_data_path', system_data_path)

    # RestoreHelper will recover a system state based on snapshots and
    # transations files extracting actions executed previously and
    # re-executing them again
    obj = restore(obj, basedir)

    end = datetime.utcnow()
    delta = end - start
    logger.info(CORE_LOG_PREFIX + "spent " + str(delta) + "microseconds")

    journal = DiskJournal(basedir, system_data_path)
    journal.setup()

    snapshot_manager = SnapshotManager(basedir)

    return CoopyProxy(obj, [journal], snapshot_manager=snapshot_manager)
Beispiel #3
0
    def test_current_journal_file(self):
        journal = DiskJournal(JOURNAL_DIR, CURRENT_DIR)
        expected_file_name = '%s%s' % (JOURNAL_DIR,
                                      'transaction_000000000000002.log')

        self.assertEquals(expected_file_name,
                          journal.current_journal_file(JOURNAL_DIR).name)

        # test hack! - create next file
        new_file_name = expected_file_name.replace('2','3')
        open(new_file_name, 'wt').close()

        self.assertEquals(new_file_name,
                          journal.current_journal_file(JOURNAL_DIR).name)
Beispiel #4
0
    def test_receive(self):
        import pickle
        class Message(object):
            def __init__(self, value):
                self.value = value
            def __getstate__(self):
                raise pickle.PicklingError()

        message = Message('test message')
        journal = DiskJournal(JOURNAL_DIR, CURRENT_DIR)
        journal.setup()

        self.assertRaises(
                            pickle.PicklingError,
                            journal.receive,
                            (message)
                          )
Beispiel #5
0
    def test_setup(self):
        journal = DiskJournal(JOURNAL_DIR, CURRENT_DIR)
        self.assertEquals(JOURNAL_DIR, journal.basedir)

        journal.setup()
        expected_file_name = '%s%s' % (JOURNAL_DIR,
                                      'transaction_000000000000002.log')
        self.assertEquals(expected_file_name,
                          journal.file.name)

        if six.PY3:
            import pickle
        else:
            import cPickle as pickle
        # test hack
        pickle_class = pickle.Pickler(open(expected_file_name, 'rb'))\
                                                            .__class__
        self.assertTrue(isinstance(journal.pickler, pickle_class))