Example #1
0
def test_check_dependency_linkage(default_os_env):
    code1 = Code(add2)
    code2 = Code(print_x)
    code3 = Code(add3)
    code4 = Code(linear)
    code1 >> [code2, code3]
    code3 >> code4
    assert code3.serial_number in code4.dependency
    assert code3.id in code4.parents
    assert code4.id in code3.children
    assert set(code2.dependency.keys()) == set(code3.dependency.keys())
    assert not code4.dependency[code3.serial_number].arg
    code4.receive('c') << code3
    assert code4.dependency[code3.serial_number].arg == 'c'
    code3.receive('b') << code1
    assert code3.dependency[code1.serial_number].arg == 'b'
    assert not code2.dependency[code1.serial_number].arg
    code3 // code4
    assert len(set(code4.dependency)) == 0
    assert code3.id not in code4.parents
    assert code4.id not in code3.children
    code1 // [code2, code3]
    assert len(code2.dependency.keys()) == len(code3.dependency.keys()) == 0
    assert len(code1.children) == 0
    assert len(code2.parents) == 0 and len(code3.parents) == 0
Example #2
0
def test_argpack_codepack_execution(default_os_env):
    code1 = Code(add2)
    code2 = Code(mul2)
    code1 >> code2
    code2.receive('b') << code1
    codepack = CodePack(id='argpack_test', code=code1, subscribe=code2)
    argpack = codepack.make_argpack()
    snapshot = codepack.to_snapshot(argpack=argpack)
    assert isinstance(snapshot.argpack, dict)
    assert snapshot.argpack['add2']['a'] is None
    assert snapshot.argpack['add2']['b'] is None
    assert snapshot.argpack['mul2']['a'] is None
    assert 'b' not in snapshot.argpack['mul2']
    snapshot2 = CodePackSnapshot.from_dict(snapshot.to_dict())
    assert isinstance(snapshot2.argpack, dict)
    assert snapshot2.argpack['add2']['a'] is None
    assert snapshot2.argpack['add2']['b'] is None
    assert snapshot2.argpack['mul2']['a'] is None
    assert 'b' not in snapshot2.argpack['mul2']
    argpack['add2'](a=3, b=2)
    argpack['mul2'](a=2)
    snapshot3 = codepack.to_snapshot(argpack=argpack)
    assert snapshot3.argpack['add2']['a'] == 3
    assert snapshot3.argpack['add2']['b'] == 2
    assert snapshot3.argpack['mul2']['a'] == 2
    snapshot4 = CodePackSnapshot.from_dict(snapshot3.to_dict())
    assert snapshot4.argpack['add2']['a'] == 3
    assert snapshot4.argpack['add2']['b'] == 2
    assert snapshot4.argpack['mul2']['a'] == 2
    codepack2 = CodePack.from_snapshot(snapshot4)
    assert codepack2(argpack=snapshot4.argpack) == 10
Example #3
0
def test_memory_worker_run_snapshot_with_image(mock_docker_client):
    worker = Default.get_employee('worker')
    assert isinstance(worker.messenger, MemoryMessenger)
    code = Code(add2, image='dummy')
    assert code.get_state() == 'UNKNOWN'
    sn = worker.run_snapshot(code.to_snapshot(kwargs={'a': 3, 'b': 5}))
    assert sn == code.serial_number
    default_config_dir = Config.get_default_config_dir()
    script_dir = os.path.join(default_config_dir, 'scripts')
    mock_docker_client.return_value.containers.run.assert_called_once_with(
        auto_remove=True,
        command=[
            'python', 'run_snapshot.py',
            '%s.json' % sn, '-p', '.', '-l', 'worker-logger'
        ],
        dns=['8.8.8.8'],
        environment=['CODEPACK_LOGGER_LOG_DIR=/usr/logs'],
        image='dummy',
        name=id(worker.docker_manager),
        volumes=[
            '%s:/usr/src/codepack' % script_dir,
            '%s:/usr/logs' % os.path.abspath(Config.get_log_dir())
        ],
        working_dir='/usr/src/codepack')
    worker.stop()
Example #4
0
def test_load_code_from_storage_service_with_id(default_os_env):
    code1 = Code(add2)
    code1.save()
    code2 = Code(id='add2', serial_number='1234')
    assert code1.source == code2.source
    assert code1(1, 2) == code2(1, 2)
    assert code1.serial_number != code2.serial_number
    assert code2.serial_number == '1234'
Example #5
0
def test_argpack_input(default_os_env):
    code1 = Code(add2)
    code2 = Code(mul2)
    code1 >> code2
    code2.receive('b') << code1
    codepack = CodePack(id='argpack_test', code=code1, subscribe=code2)
    argpack = codepack.make_argpack()
    argpack['add2'](a=3, b=2)
    with pytest.raises(TypeError):
        argpack['mul2'](c=5)
    argpack['mul2'](a=2)
    assert argpack['add2']['a'] == 3
    assert argpack['add2']['b'] == 2
    assert argpack['mul2']['a'] == 2
    assert codepack(argpack=argpack) == 10
Example #6
0
def test_memory_storage_jobstore_codepack_snapshot():
    code = Code(hello)
    codepack = CodePack('codepack_test', code=code, subscribe=code)
    argpack = codepack.make_argpack()
    argpack['hello']['name'] = 'CodePack'
    job_id = 'job_test'
    storage = MemoryStorage(item_type=StorableJob, key='id')
    jobstore = JobStore(storage=storage)
    scheduler = Scheduler(jobstore=jobstore, blocking=False)
    scheduler.add_codepack(job_id=job_id,
                           codepack=codepack,
                           argpack=argpack,
                           trigger='interval',
                           seconds=30)
    scheduler.start()
    assert scheduler.is_running()
    assert codepack.id not in storage.memory
    assert job_id in storage.memory
    job = storage.memory[job_id].to_dict()
    for k in [
            '_id', 'trigger', 'codepack', 'snapshot', 'last_run_time',
            'next_run_time'
    ]:
        assert k in job
    assert job['_id'] == job_id
    assert job['trigger'] == 'interval[0:00:30]'
    assert job['codepack'] == codepack.id
    assert job['snapshot'] == codepack.serial_number
    scheduler.remove_job(job_id)
    assert len(storage.memory) == 0
    scheduler.stop()
Example #7
0
def test_memory_storage_service(default_os_env):
    storage = MemoryStorage(item_type=Code)
    mss = StorageService(storage=storage)
    mss.storage.init()
    code1 = Code(hello, storage_service=mss)
    code2 = Code(add2, storage_service=mss)
    code1.save()
    assert len(storage.memory) == 1
    assert code1.id in storage.memory
    assert code2.id not in storage.memory
    code3 = mss.load(code1.id)
    assert code1.id == code3.id
    assert code1.source == code3.source
    assert code1("CodePack") == code3("CodePack")
    mss.remove(code3.id)
    assert len(storage.memory) == 0
Example #8
0
def test_file_storage_jobstore_codepack_snapshot(testdir):
    code = Code(hello)
    codepack = CodePack('codepack_test', code=code, subscribe=code)
    argpack = codepack.make_argpack()
    argpack['hello']['name'] = 'CodePack'
    job_id = 'job_test'
    storage = FileStorage(item_type=StorableJob,
                          key='id',
                          path='testdir/scheduler/')
    jobstore = JobStore(storage=storage)
    scheduler = Scheduler(jobstore=jobstore, blocking=False)
    scheduler.add_codepack(job_id=job_id,
                           codepack=codepack,
                           argpack=argpack,
                           trigger='interval',
                           seconds=30)
    scheduler.start()
    assert scheduler.is_running()
    assert not storage.exist(key=codepack.id)
    assert storage.exist(key=job_id)
    job = storage.load(key=job_id, to_dict=True)
    for k in [
            '_id', 'trigger', 'codepack', 'snapshot', 'last_run_time',
            'next_run_time'
    ]:
        assert k in job
    assert job['_id'] == job_id
    assert job['trigger'] == 'interval[0:00:30]'
    assert job['codepack'] == codepack.id
    assert job['snapshot'] == codepack.serial_number
    scheduler.remove_job(job_id)
    assert len(storage.list_all()) == 0
    scheduler.stop()
Example #9
0
def test_add_dependency(default_os_env):
    dependency = [{'id': 'test1', 'serial_number': '1234', 'arg': None}, {'id': 'test2', 'serial_number': '5678', 'arg': 'a'}]
    code = Code(add2, dependency=dependency)
    assert '1234' in code.dependency and '5678' in code.dependency
    assert code.dependency['5678'].arg == 'a'
    assert not code.dependency['1234'].arg
    tmp = code.dependency.get_args()
    assert len(tmp) == 1 and 'a' in tmp
Example #10
0
def test_from_dict(default_os_env):
    d = {'_id': 'test', 'source': "def plus1(x):\n  return x + 1"}
    code = Code.from_dict(d)
    assert code.id == 'test'
    assert code.function.__name__ == 'plus1'
    ret = code(x=5)
    assert ret == 6
    assert code.get_state() == 'TERMINATED'
Example #11
0
def test_memory_worker_run_snapshot_with_env(mock_subprocess_run):
    worker = Default.get_employee('worker')
    assert isinstance(worker.messenger, MemoryMessenger)
    code = Code(add2, env='test_env', image='dummy')
    assert code.get_state() == 'UNKNOWN'
    sn = worker.run_snapshot(code.to_snapshot(kwargs={'a': 3, 'b': 5}))
    assert sn == code.serial_number
    default_config_dir = Config.get_default_config_dir()
    script_dir = os.path.join(default_config_dir, 'scripts')
    mock_subprocess_run.assert_called_once_with([
        os.path.join(worker.interpreter_manager.path, 'test_env', 'bin',
                     'python'),
        os.path.join(script_dir, 'run_snapshot.py'),
        os.path.join(script_dir, '%s.json' % sn), '-p', script_dir, '-l',
        'worker-logger'
    ])
    worker.stop()
Example #12
0
def test_mongo_code_snapshot_service_search_and_remove(default_os_env,
                                                       fake_mongodb):
    db = 'test'
    collection = 'snapshot'
    storage = MongoStorage(item_type=CodeSnapshot,
                           mongodb=fake_mongodb,
                           db=db,
                           collection=collection)
    mss = SnapshotService(storage=storage)
    code1 = Code(add2)
    code2 = Code(add3)
    code1 >> code2
    code2(1, 2, 3)
    snapshot1 = CodeSnapshot(code1)
    snapshot2 = CodeSnapshot(code2)
    mss.save(snapshot=snapshot1)
    mss.save(snapshot=snapshot2)
    assert fake_mongodb[db][collection].count_documents({}) == 2
    loaded = mss.load(
        [snapshot1.serial_number, snapshot2.serial_number, '1234'])
    assert len(loaded) == 2
    assert loaded[0]['state'] == 'UNKNOWN'
    assert loaded[1]['state'] == 'WAITING'
    loaded = mss.load(
        [snapshot1.serial_number, snapshot2.serial_number, '1234'],
        projection=['state'])
    for x in loaded:
        assert set(x.keys()) == {'state', 'serial_number'}
    search_result = mss.search(key='state', value='WAITING')
    assert len(search_result) == 1
    assert search_result[0] == snapshot2.to_dict()
    search_result = mss.search(key='state',
                               value='WAITING',
                               projection=['state'])
    assert len(search_result) == 1
    assert len(search_result[0].keys()) == 2
    assert search_result[0]['state'] == 'WAITING'
    assert search_result[0]['serial_number'] == code2.serial_number
    mss.remove(snapshot2.serial_number)
    loaded = mss.load(
        [snapshot1.serial_number, snapshot2.serial_number, '1234'])
    assert len(loaded) == 1
    search_result = mss.search(key='state',
                               value='WAITING',
                               projection=['state'])
    assert len(search_result) == 0
Example #13
0
def test_memory_supervisor_run_code():
    supervisor = Default.get_employee('supervisor')
    assert isinstance(supervisor.messenger, MemoryMessenger)
    assert supervisor.messenger.topic == 'codepack'
    assert supervisor.messenger.queues['codepack'].empty()
    code = Code(add2)
    sn = supervisor.run_code(code=code, args=(3, ), kwargs={'b': 5})
    assert sn == code.serial_number
    assert not supervisor.messenger.queues['codepack'].empty(
    ) and supervisor.messenger.queues['codepack'].qsize() == 1
    item = supervisor.messenger.queues['codepack'].get(block=False)
    assert item == code.to_snapshot(args=(3, ),
                                    kwargs={
                                        'b': 5
                                    },
                                    timestamp=item['timestamp']).to_dict()
    supervisor.close()
Example #14
0
def test_to_db_and_from_db(default_os_env, fake_mongodb):
    db = 'test'
    collection = 'codes'
    test_id = 'add2'
    try:
        code1 = Code(add2)
        assert test_id == code1.id
        code1.to_db(mongodb=fake_mongodb, db=db, collection=collection)
        assert fake_mongodb[db][collection].find_one({'_id': code1.id})
        code2 = Code.from_db(id=code1.id, mongodb=fake_mongodb, db=db, collection=collection)
        assert code1.id == code2.id
        assert code1.function.__name__ == code2.function.__name__
        assert code1.description == code2.description
        assert code1.serial_number != code2.serial_number
        assert code1(1, 3) == code2(1, 3)
    finally:
        if fake_mongodb[db][collection].count_documents({'_id': test_id}) > 0:
            fake_mongodb[db][collection].delete_one({'_id': test_id})
Example #15
0
def test_mongo_storage_service(default_os_env, fake_mongodb):
    db = 'test'
    collection = 'codes'
    storage = MongoStorage(item_type=Code, mongodb=fake_mongodb, db=db, collection=collection)
    mss = StorageService(storage=storage)
    assert mss.storage.item_type == Code
    code1 = Code(hello, storage_service=mss)
    code2 = Code(add2, storage_service=mss)
    code1.save()
    assert storage.mongodb[db][collection].count_documents({'_id': code1.id}) == 1
    assert storage.mongodb[db][collection].count_documents({'_id': code2.id}) == 0
    assert storage.item_type == Code
    code3 = mss.load(code1.id)
    assert code1.id == code3.id
    assert code1.source.strip() == code3.source.strip()
    assert code1("CodePack") == code3("CodePack")
    mss.remove(code3.id)
    assert storage.mongodb[db][collection].count_documents({'_id': code1.id}) == 0
Example #16
0
def test_argpack_str(default_os_env):
    code1 = Code(add2)
    code2 = Code(mul2)
    code1 >> code2
    code2.receive('b') << code1
    codepack = CodePack(id='argpack_test', code=code1, subscribe=code2)
    argpack = codepack.make_argpack()
    assert argpack.__str__(
    ) == 'ArgPack(id: argpack_test, args: {add2(a=None, b=None), mul2(a=None)})'
    argpack['add2'](a=3, b=2)
    with pytest.raises(TypeError):
        argpack['mul2'](c=5)
    argpack['mul2'](a=2)
    assert argpack['add2']['a'] == 3
    assert argpack['add2']['b'] == 2
    assert argpack['mul2']['a'] == 2
    assert argpack.__str__(
    ) == 'ArgPack(id: argpack_test, args: {add2(a=3, b=2), mul2(a=2)})'
    assert codepack(argpack=argpack) == 10
Example #17
0
def test_file_code_snapshot_service_search_and_remove(
        default_os_env, testdir_snapshot_service):
    storage = FileStorage(item_type=CodeSnapshot,
                          path=testdir_snapshot_service)
    fss = SnapshotService(storage=storage)
    code1 = Code(add2)
    code2 = Code(add3)
    code1 >> code2
    code2(1, 2, 3)
    snapshot1 = CodeSnapshot(code1)
    snapshot2 = CodeSnapshot(code2)
    fss.save(snapshot=snapshot1)
    fss.save(snapshot=snapshot2)
    assert len(os.listdir(testdir_snapshot_service)) == 2
    loaded = fss.load(
        [snapshot1.serial_number, snapshot2.serial_number, '1234'])
    assert len(loaded) == 2
    assert loaded[0]['state'] == 'UNKNOWN'
    assert loaded[1]['state'] == 'WAITING'
    loaded = fss.load(
        [snapshot1.serial_number, snapshot2.serial_number, '1234'],
        projection=['state'])
    for x in loaded:
        assert set(x.keys()) == {'state', 'serial_number'}
    search_result = fss.search(key='state', value='WAITING')
    assert len(search_result) == 1
    assert search_result[0] == snapshot2.to_dict()
    search_result = fss.search(key='state',
                               value='WAITING',
                               projection=['state'])
    assert len(search_result) == 1
    assert len(search_result[0].keys()) == 2
    assert search_result[0]['state'] == 'WAITING'
    assert search_result[0]['serial_number'] == code2.serial_number
    fss.remove(snapshot2.serial_number)
    loaded = fss.load(
        [snapshot1.serial_number, snapshot2.serial_number, '1234'])
    assert len(loaded) == 1
    search_result = fss.search(key='state',
                               value='WAITING',
                               projection=['state'])
    assert len(search_result) == 0
Example #18
0
def test_dependency_error_propagation(default_os_env):
    code1 = Code(add2)
    code2 = Code(mul2)
    code3 = Code(add3)
    code1 >> code3
    code2 >> code3
    assert code3.get_state() == 'UNKNOWN'
    with pytest.raises(TypeError):
        code1()
    assert code1.get_state() == 'ERROR'
    ret = code3(1, 2, 3)
    assert ret is None
    assert code3.get_state() == 'WAITING'
Example #19
0
def test_no_config():
    code = Code(add2)
    assert 'delivery' in code.service and isinstance(code.service['delivery'],
                                                     DeliveryService)
    assert 'snapshot' in code.service and isinstance(code.service['snapshot'],
                                                     SnapshotService)
    assert 'storage' in code.service and isinstance(code.service['storage'],
                                                    StorageService)
    assert isinstance(code.service['delivery'].storage, MemoryStorage)
    assert isinstance(code.service['snapshot'].storage, MemoryStorage)
    assert isinstance(code.service['storage'].storage, MemoryStorage)
Example #20
0
def test_file_storage_service(default_os_env, testdir_storage_service):
    filepath = testdir_storage_service
    storage = FileStorage(item_type=Code, path=filepath)
    fss = StorageService(storage=storage)
    assert storage.path == filepath
    code1 = Code(hello, storage_service=fss)
    code2 = Code(add2, storage_service=fss)
    code1.save()
    assert not os.path.isfile(Code.get_path(key=code1.id))
    assert os.path.isfile(Code.get_path(key=code1.id, path=filepath))
    assert not os.path.isfile(Code.get_path(key=code2.id, path=filepath))
    code3 = fss.load(code1.id)
    assert code1.id == code3.id
    assert code1.source.strip() == code3.source.strip()
    assert code1("CodePack") == code3("CodePack")
    fss.remove(code3.id)
    assert not os.path.isfile(Code.get_path(key=code1.id, path=filepath))
Example #21
0
def test_if_default_services_have_single_instance_for_each_service(
        testdir_snapshot_service):
    os.environ['CODEPACK_DELIVERY_SOURCE'] = 'memory'
    os.environ['CODEPACK_CODESNAPSHOT_SOURCE'] = 'file'
    os.environ['CODEPACK_CODESNAPSHOT_PATH'] = testdir_snapshot_service
    os.environ['CODEPACK_CODE_SOURCE'] = 'mongodb'
    os.environ['CODEPACK_CODE_DB'] = 'test'
    os.environ['CODEPACK_CODE_COLLECTION'] = 'codes'
    try:
        code1 = Code(add2)
        code2 = Code(add3)
        assert isinstance(code1.service['delivery'], DeliveryService)
        assert isinstance(code1.service['snapshot'], SnapshotService)
        assert isinstance(code1.service['storage'], StorageService)
        assert isinstance(code2.service['delivery'], DeliveryService)
        assert isinstance(code2.service['snapshot'], SnapshotService)
        assert isinstance(code2.service['storage'], StorageService)

        assert isinstance(code1.service['delivery'].storage, MemoryStorage)
        assert isinstance(code1.service['snapshot'].storage, FileStorage)
        assert isinstance(code1.service['storage'].storage, MongoStorage)
        assert isinstance(code2.service['delivery'].storage, MemoryStorage)
        assert isinstance(code2.service['snapshot'].storage, FileStorage)
        assert isinstance(code2.service['storage'].storage, MongoStorage)

        assert (code1.service['delivery']) == (code2.service['delivery'])
        assert (code1.service['snapshot']) == (code2.service['snapshot'])
        assert (code1.service['storage']) == (code2.service['storage'])

        assert id(code1.service['delivery']) == id(code2.service['delivery'])
        assert id(code1.service['snapshot']) == id(code2.service['snapshot'])
        assert id(code1.service['storage']) == id(code2.service['storage'])
    finally:
        for env in [
                'CODEPACK_DELIVERY_SOURCE', 'CODEPACK_CODESNAPSHOT_SOURCE',
                'CODEPACK_CODESNAPSHOT_PATH', 'CODEPACK_CODE_SOURCE',
                'CODEPACK_CODE_DB', 'CODEPACK_CODE_COLLECTION'
        ]:
            os.environ.pop(env, None)
Example #22
0
def test_memory_code_snapshot_service_save_and_load(default_os_env):
    storage = MemoryStorage(item_type=CodeSnapshot)
    mss = SnapshotService(storage=storage)
    mss.storage.init()
    code = Code(add2)
    snapshot = CodeSnapshot(code)
    mss.save(snapshot=snapshot)
    assert len(storage.memory) == 1
    assert mss.load(snapshot.serial_number) == snapshot.to_dict()
    loaded = mss.load(snapshot.serial_number, projection=['state'])
    assert set(loaded.keys()) == {'serial_number', 'state'}
    assert 'state' in loaded
    assert loaded['state'] == 'UNKNOWN'
    assert 'serial_number' in loaded
    assert loaded['serial_number'] == snapshot.serial_number
Example #23
0
def test_memory_worker_run_snapshot_with_nothing():
    worker = Default.get_employee('worker')
    assert isinstance(worker.messenger, MemoryMessenger)
    code = Code(add2)
    assert code.get_state() == 'UNKNOWN'
    sn = worker.run_snapshot(code.to_snapshot(kwargs={'a': 3, 'b': 5}))
    assert sn == code.serial_number
    assert code.get_state() == 'TERMINATED'
    assert code.get_result() == 8
    worker.stop()
Example #24
0
def test_file_code_snapshot_service_save_and_load(default_os_env,
                                                  testdir_snapshot_service):
    storage = FileStorage(item_type=CodeSnapshot,
                          path=testdir_snapshot_service)
    fss = SnapshotService(storage=storage)
    code = Code(add2)
    snapshot = CodeSnapshot(code)
    fss.save(snapshot=snapshot)
    assert len(os.listdir(testdir_snapshot_service)) == 1
    assert fss.load(snapshot.serial_number) == snapshot.to_dict()
    loaded = fss.load(snapshot.serial_number, projection=['state'])
    assert set(loaded.keys()) == {'serial_number', 'state'}
    assert 'state' in loaded
    assert loaded['state'] == 'UNKNOWN'
    assert 'serial_number' in loaded
    assert loaded['serial_number'] == snapshot.serial_number
Example #25
0
def test_mongo_code_snapshot_service_save_and_load(default_os_env,
                                                   fake_mongodb):
    db = 'test'
    collection = 'snapshot'
    storage = MongoStorage(item_type=CodeSnapshot,
                           mongodb=fake_mongodb,
                           db=db,
                           collection=collection)
    mss = SnapshotService(storage=storage)
    code = Code(add2)
    snapshot = CodeSnapshot(code)
    mss.save(snapshot=snapshot)
    assert fake_mongodb[db][collection].count_documents({}) == 1
    assert mss.load(snapshot.serial_number) == snapshot.to_dict()
    loaded = mss.load(snapshot.serial_number, projection=['state'])
    assert set(loaded.keys()) == {'serial_number', 'state'}
    assert 'state' in loaded
    assert loaded['state'] == 'UNKNOWN'
    assert 'serial_number' in loaded
    assert loaded['serial_number'] == snapshot.serial_number
Example #26
0
def test_some_os_env(fake_mongodb):
    try:
        os.environ['CODEPACK_DELIVERY_SOURCE'] = 'mongodb'
        os.environ['CODEPACK_DELIVERY_DB'] = 'codepack'
        os.environ['CODEPACK_DELIVERY_COLLECTION'] = 'test'
        code = Code(add2)
        assert 'delivery' in code.service and isinstance(
            code.service['delivery'], DeliveryService)
        assert 'snapshot' in code.service and isinstance(
            code.service['snapshot'], SnapshotService)
        assert 'storage' in code.service and isinstance(
            code.service['storage'], StorageService)
        assert isinstance(code.service['delivery'].storage, MongoStorage)
        assert isinstance(code.service['snapshot'].storage, MemoryStorage)
        assert isinstance(code.service['storage'].storage, MemoryStorage)
        assert code.service['delivery'].storage.db == 'codepack'
        assert code.service['delivery'].storage.collection == 'test'
    finally:
        os.environ.pop('CODEPACK_DELIVERY_SOURCE', None)
        os.environ.pop('CODEPACK_DELIVERY_DB', None)
        os.environ.pop('CODEPACK_DELIVERY_COLLECTION', None)
Example #27
0
def test_mongo_storage_jobstore_codepack_snapshot(fake_mongodb):
    code = Code(hello)
    codepack = CodePack('codepack_test', code=code, subscribe=code)
    argpack = codepack.make_argpack()
    argpack['hello']['name'] = 'CodePack'
    job_id = 'job_test'
    db = 'test'
    collection = 'scheduler'
    storage = MongoStorage(item_type=StorableJob,
                           key='id',
                           mongodb=fake_mongodb,
                           db=db,
                           collection=collection)
    jobstore = JobStore(storage=storage)
    scheduler = Scheduler(jobstore=jobstore, blocking=False)
    scheduler.add_codepack(job_id=job_id,
                           codepack=codepack,
                           argpack=argpack,
                           trigger='interval',
                           seconds=30)
    scheduler.start()
    assert scheduler.is_running()
    assert fake_mongodb[db][collection].count_documents({'_id':
                                                         codepack.id}) == 0
    assert fake_mongodb[db][collection].count_documents({'_id': job_id}) == 1
    document = fake_mongodb[db][collection].find_one({'_id': job_id})
    for k in [
            '_id', 'trigger', 'codepack', 'snapshot', 'last_run_time',
            'next_run_time'
    ]:
        assert k in document
    assert document['_id'] == job_id
    assert document['trigger'] == 'interval[0:00:30]'
    assert document['codepack'] == codepack.id
    assert document['snapshot'] == codepack.serial_number
    scheduler.remove_job(job_id)
    assert fake_mongodb[db][collection].count_documents({'_id': job_id}) == 0
    scheduler.stop()
Example #28
0
def test_code_waiting(default_os_env):
    code1 = Code(add2)
    code2 = Code(add3)
    code1 >> code2
    code2.receive('b') << code1
    code2(3, c=2)
    assert code2.get_state() == 'WAITING'
    snapshot = code2.service['snapshot'].load(
        serial_number=code2.serial_number)
    assert 'args' in snapshot
    assert isinstance(snapshot['args'], list)
    assert len(snapshot['args']) == 1
    assert snapshot['args'][0] == 3
    assert 'kwargs' in snapshot
    assert isinstance(snapshot['kwargs'], dict)
    assert len(snapshot['kwargs']) == 1
    assert 'c' in snapshot['kwargs'].keys()
    assert snapshot['kwargs']['c'] == 2
Example #29
0
def test_code_snapshot_diff(default_os_env):
    timestamp = datetime.now().timestamp()
    code1 = Code(mul2)
    code2 = Code(add2)
    code3 = Code(add3)
    code1 >> code3
    code2 >> code3
    code3.receive('c') << code2
    code3(1, b=2)
    snapshot1 = CodeSnapshot(code3, args=(1, ), kwargs={'b': 2}, timestamp=timestamp)
    snapshot2 = CodeSnapshot.from_dict(snapshot1.to_dict())
    snapshot3 = CodeSnapshot(code3, args=(1, ), kwargs={'b': 3}, timestamp=timestamp + 1)
    assert snapshot1.diff(snapshot2) == dict()
    assert snapshot2.diff(snapshot1) == dict()
    diff = snapshot1.diff(snapshot3)
    assert set(diff.keys()) == {'kwargs', 'timestamp'}
    assert diff['kwargs'] == {'b': 3}
    assert diff['timestamp'] == timestamp + 1
Example #30
0
def test_memory_supervisor_run_codepack():
    supervisor = Default.get_employee('supervisor')
    assert isinstance(supervisor.messenger, MemoryMessenger)
    assert supervisor.messenger.topic == 'codepack'
    assert supervisor.messenger.queues['codepack'].empty()
    code1 = Code(add2)
    code2 = Code(mul2)
    code3 = Code(combination)
    code1 >> code3
    code2 >> code3
    code3.receive('c') << code1
    code3.receive('d') << code2
    codepack = CodePack('test', code=code1, subscribe=code3)
    argpack = codepack.make_argpack()
    argpack['add2'](a=3, b=5)
    argpack['mul2'](a=2, b=3)
    argpack['combination'](a=2, b=4)
    sn = supervisor.run_codepack(codepack=codepack, argpack=argpack)
    assert sn == codepack.serial_number
    assert not supervisor.messenger.queues['codepack'].empty(
    ) and supervisor.messenger.queues['codepack'].qsize() == 3
    items = [
        supervisor.messenger.queues['codepack'].get(block=False)
        for _ in range(3)
    ]
    snapshots = [
        c.to_snapshot(kwargs=argpack[i].to_dict()).to_dict()
        for i, c in codepack.codes.items()
    ]
    for item in items:
        item.pop('timestamp', None)
    for snapshot in snapshots:
        snapshot.pop('timestamp', None)
    assert sorted(items,
                  key=lambda x: x['id']) == sorted(snapshots,
                                                   key=lambda x: x['id'])
    supervisor.close()