Exemplo n.º 1
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)
Exemplo n.º 2
0
    def test_execute_action(self):
        from coopy.foundation import Action, RecordClock
        from datetime import datetime
        from coopy.utils import inject

        class Dummy(object):
            def __init__(self):
                self.exec_count = 0

            def business_method_noargs(self):
                self.exec_count += 1

            def business_method_args(self, arg):
                self.exec_count += 2

            def business_method_kwargs(self, keyword_arg="test"):
                self.exec_count += 3


        dummy = Dummy()
        # force clock into dummy
        inject(dummy, '_clock', RecordClock())

        action = Action('caller_id',
                        'business_method_noargs',
                        datetime.now(),
                        (),
                        {})

        action.execute_action(dummy)

        self.assertEquals(1, dummy.exec_count)

        action = Action('caller_id',
                        'business_method_args',
                        datetime.now(),
                        ([1]),
                        {})

        action.execute_action(dummy)

        self.assertEquals(3, dummy.exec_count)

        action = Action('caller_id',
                        'business_method_kwargs',
                        datetime.now(),
                        (),
                        {'keyword_arg' : 'test'})

        action.execute_action(dummy)

        self.assertEquals(6, dummy.exec_count)
Exemplo n.º 3
0
def test_prepare_data():
    from coopy.network.network import prepare_data
    from coopy.foundation import RecordClock
    from coopy.utils import inject
    import six
    if six.PY3:
        import pickle
    else:
        import cPickle as pickle
    import zlib

    wiki = Wiki()
    inject(wiki, '_clock', RecordClock())
    wiki.create_page('test', 'test content', None)

    (header, compressed_data) = prepare_data(wiki)

    copy_wiki = pickle.loads(zlib.decompress(compressed_data))

    assert copy_wiki.get_page('test').id == 'test'
    assert copy_wiki.get_page('test').content == 'test content'
Exemplo n.º 4
0
 def mock_clock(self, system, date):
     test_clock = TestClock(date)
     inject(system, '_clock', test_clock)
Exemplo n.º 5
0
 def enable_clock(self, system):
     inject(system, '_clock', RecordClock())