Esempio n. 1
0
def test_processes_stats():

    def collect_cb(inf, m, name):
        inf.append(m.stats(name))

    m = Manager()
    m.start()
    testfile, cmd, args, wdir = dummy_cmd()
    testfile1, cmd1, args1, wdir1 = dummy_cmd()
    configa = ProcessConfig("a", cmd, args=args, cwd=wdir)
    m.load(configa)

    time.sleep(0.2)
    infos = []
    infos2 = []
    m.jobs_walk(partial(collect_cb, infos))
    configb = ProcessConfig("b", cmd, args=args, cwd=wdir)
    m.load(configb)
    m.jobs_walk(partial(collect_cb, infos2))
    m.stop()
    m.run()

    assert len(infos) == 1
    assert len(infos2) == 2
    assert infos[0]['name'] == "default.a"
    assert infos2[0]['name'] == "default.a"
    assert infos2[1]['name'] == "default.b"
Esempio n. 2
0
def test_manager_hooks():
    hooks = create_hooks(['create', 'start', 'update', 'stop',
        'delete', 'proc.dummy.start', 'proc.dummy.spawn',
        'proc.dummy.stop', 'proc.dummy.exit'])
    emitted = []
    loop = pyuv.Loop.default_loop()
    s = get_server(loop, emitted)
    s.start()
    m = Manager(loop=loop)
    m.start(apps=[WebHooks(hooks)])
    testfile, cmd, args, wdir = dummy_cmd()
    m.add_process("dummy", cmd, args=args, cwd=wdir, numprocesses=4)
    m.ttin("dummy", 1)
    m.remove_process("dummy")
    time.sleep(0.2)

    def on_stop(manager):
        s.stop()

    m.stop(on_stop)
    m.run()
    assert ('create', 'dummy') in emitted
    assert ('start', 'dummy') in emitted
    assert ('update', 'dummy') in emitted
    assert ('stop', 'dummy') in emitted
    assert ('delete', 'dummy') in emitted
    assert ('proc.dummy.start', 'dummy') in emitted
    assert ('proc.dummy.spawn', 'dummy') in emitted
    assert ('proc.dummy.stop', 'dummy') in emitted
    assert ('proc.dummy.exit', 'dummy') in emitted
Esempio n. 3
0
def test_basic():
    emitted = []
    m = Manager()
    m.start()

    def cb(ev, msg):
        emitted.append((ev, msg['name']))

    # subscribe to all events
    chan = m.subscribe("EVENTS")
    chan.bind('.', cb)

    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir, numprocesses=4)
    m.load(config)
    m.scale("dummy", 1)
    m.unload("dummy")

    time.sleep(0.2)
    m.stop()
    m.run()

    assert ('load', 'default.dummy') in emitted
    assert ('start', 'default.dummy') in emitted
    assert ('update', 'default.dummy') in emitted
    assert ('stop', 'default.dummy') in emitted
    assert ('unload', 'default.dummy') in emitted
Esempio n. 4
0
def test_scalein():
    m = Manager()
    m.start()
    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir, numprocesses=1)
    m.load(config)
    state = m._get_locked_state("dummy")

    assert len(state.running) == 1
    ret = m.scale("dummy", 1)
    assert ret == 2

    time.sleep(0.2)
    assert len(state.running) == 2

    ret = m.scale("dummy", 1)
    assert ret == 3

    time.sleep(0.2)
    assert len(state.running) == 3

    ret = m.scale("dummy", 3)
    assert ret == 6

    time.sleep(0.2)
    assert len(state.running) == 6

    m.stop()
    m.run()
Esempio n. 5
0
def test_process_events():
    emitted = []
    m = Manager()
    m.start()

    def cb(ev, *args):
        emitted.append(ev)

    # subscribe to all events
    chan = m.subscribe("JOB:default.dummy")
    chan.bind_all(cb)

    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir)
    m.load(config)
    m.unload(config)

    time.sleep(0.2)
    m.stop()
    m.run()

    assert 'start' in emitted
    assert 'spawn' in emitted
    assert 'stop' in emitted
    assert 'exit' in emitted
Esempio n. 6
0
def test_flapping():
    m = Manager()
    m.start()
    cmd, args, wdir = crash_cmd()
    flapping = FlappingInfo(attempts=1, window=1, retry_in=0.1, max_retry=2)
    m.add_process("crashing", cmd, args=args, cwd=wdir, flapping=flapping)
    state = m.get_process_state("crashing")

    def cb(handle):
        handle.stop()
        assert state.stopped == True

    t = pyuv.Timer(m.loop)
    t.start(cb, 0.8, 0.8)
    m.add_process("crashing2", cmd, args=args, cwd=wdir)
    state = m.get_process_state("crashing2")

    def cb1(handle):
        handle.stop()
        assert state.stopped == False

    t = pyuv.Timer(m.loop)
    t.start(cb1, 0.8, 0.8)

    m.stop()
    m.run()
Esempio n. 7
0
def test_basic():
    emitted = []
    m = Manager()
    m.start()

    def cb(ev, msg):
        emitted.append((ev, msg['name']))

    # subscribe to all events
    chan = m.subscribe("EVENTS")
    chan.bind('.', cb)

    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir, numprocesses=4)
    m.load(config)
    m.scale("dummy", 1)
    m.unload("dummy")

    time.sleep(0.2)
    m.stop()
    m.run()

    assert ('load', 'default.dummy') in emitted
    assert ('start', 'default.dummy') in emitted
    assert ('update', 'default.dummy') in emitted
    assert ('stop', 'default.dummy') in emitted
    assert ('unload', 'default.dummy') in emitted
Esempio n. 8
0
def test_manager_hooks():
    hooks = create_hooks([
        'create', 'start', 'update', 'stop', 'delete', 'proc.dummy.start',
        'proc.dummy.spawn', 'proc.dummy.stop', 'proc.dummy.exit'
    ])
    emitted = []
    loop = pyuv.Loop.default_loop()
    s = get_server(loop, emitted)
    s.start()
    m = Manager(loop=loop)
    m.start(apps=[WebHooks(hooks)])
    testfile, cmd, args, wdir = dummy_cmd()
    m.add_process("dummy", cmd, args=args, cwd=wdir, numprocesses=4)
    m.ttin("dummy", 1)
    m.remove_process("dummy")
    time.sleep(0.2)

    def on_stop(manager):
        s.stop()

    m.stop(on_stop)
    m.run()
    assert ('create', 'dummy') in emitted
    assert ('start', 'dummy') in emitted
    assert ('update', 'dummy') in emitted
    assert ('stop', 'dummy') in emitted
    assert ('delete', 'dummy') in emitted
    assert ('proc.dummy.start', 'dummy') in emitted
    assert ('proc.dummy.spawn', 'dummy') in emitted
    assert ('proc.dummy.stop', 'dummy') in emitted
    assert ('proc.dummy.exit', 'dummy') in emitted
Esempio n. 9
0
def test_processes_stats():
    def collect_cb(inf, m, name):
        inf.append(m.stats(name))

    m = Manager()
    m.start()
    testfile, cmd, args, wdir = dummy_cmd()
    testfile1, cmd1, args1, wdir1 = dummy_cmd()
    configa = ProcessConfig("a", cmd, args=args, cwd=wdir)
    m.load(configa)

    time.sleep(0.2)
    infos = []
    infos2 = []
    m.jobs_walk(partial(collect_cb, infos))
    configb = ProcessConfig("b", cmd, args=args, cwd=wdir)
    m.load(configb)
    m.jobs_walk(partial(collect_cb, infos2))
    m.stop()
    m.run()

    assert len(infos) == 1
    assert len(infos2) == 2
    assert infos[0]['name'] == "default.a"
    assert infos2[0]['name'] == "default.a"
    assert infos2[1]['name'] == "default.b"
Esempio n. 10
0
def test_process_exit_event():
    emitted = []
    m = Manager()
    m.start()

    def cb(ev, msg):
        emitted.append(msg)

    # subscribe to all events
    m.events.subscribe('job.default.dummy.exit', cb)

    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir)
    m.load(config)
    m.unload("dummy")

    time.sleep(0.2)
    m.stop()
    m.run()

    assert len(emitted) == 1
    assert len(emitted[0]) == 7

    msg = emitted[0]
    assert "exit_status" in msg
    assert msg['once'] == False
Esempio n. 11
0
def test_manager_hooks():
    hooks = create_hooks(['load', 'unload', 'start', 'update', 'stop',
        'job.default.dummy.start', 'job.default.dummy.spawn',
        'job.default.dummy.stop', 'job.default.dummy.exit'])
    emitted = []
    loop = pyuv.Loop.default_loop()
    s = get_server(loop, emitted)
    s.start()
    m = Manager(loop=loop)
    m.start(apps=[WebHooks(hooks)])
    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir, numprocesses=1)
    m.load(config)
    m.manage("dummy")
    m.scale("dummy", 1)
    m.unload("dummy")

    t = pyuv.Timer(loop)

    def on_stop(manager):
        t.start(lambda h: s.stop(), 0.4, 0.0)

    m.stop(on_stop)

    m.run()
    assert ('load', 'default.dummy') in emitted
    assert ('start', 'default.dummy') in emitted
    assert ('update', 'default.dummy') in emitted
    assert ('stop', 'default.dummy') in emitted
    assert ('unload', 'default.dummy') in emitted
    assert ('job.default.dummy.start', 'default.dummy') in emitted
    assert ('job.default.dummy.spawn', 'default.dummy') in emitted
    assert ('job.default.dummy.stop', 'default.dummy') in emitted
    assert ('job.default.dummy.exit', 'default.dummy') in emitted
Esempio n. 12
0
def test_scalein():
    m = Manager()
    m.start()
    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir, numprocesses=1)
    m.load(config)
    state = m._get_locked_state("dummy")

    assert len(state.running) == 1
    ret = m.scale("dummy", 1)
    assert ret == 2

    time.sleep(0.2)
    assert len(state.running) == 2

    ret = m.scale("dummy", 1)
    assert ret == 3

    time.sleep(0.2)
    assert len(state.running) == 3


    ret = m.scale("dummy", 3)
    assert ret == 6

    time.sleep(0.2)
    assert len(state.running) == 6

    m.stop()
    m.run()
Esempio n. 13
0
def test_process_events():
    emitted = []
    m = Manager()
    m.start()

    def cb(ev, *args):
        emitted.append(ev)

    # subscribe to all events
    chan = m.subscribe("JOB:default.dummy")
    chan.bind_all(cb)

    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir)
    m.load(config)
    m.unload(config)

    time.sleep(0.2)
    m.stop()
    m.run()

    assert 'start' in emitted
    assert 'spawn' in emitted
    assert 'stop' in emitted
    assert 'exit' in emitted
Esempio n. 14
0
def test_process_exit_event():
    emitted = []
    m = Manager()
    m.start()

    def cb(ev, msg):
        emitted.append(msg)

    # subscribe to all events
    m.events.subscribe('job.default.dummy.exit', cb)

    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir)
    m.load(config)
    m.unload("dummy")

    time.sleep(0.2)
    m.stop()
    m.run()

    assert len(emitted) == 1
    assert len(emitted[0]) == 7

    msg = emitted[0]
    assert "exit_status" in msg
    assert msg['once'] == False
Esempio n. 15
0
def test_ttin():
    m = Manager()
    m.start()
    testfile, cmd, args, wdir = dummy_cmd()
    m.add_process("dummy", cmd, args=args, cwd=wdir, numprocesses=1)
    state = m.get_process_state("dummy")

    assert len(state.running) == 1
    ret = m.ttin("dummy", 1)
    assert ret == 2

    time.sleep(0.2)
    assert len(state.running) == 2

    ret = m.ttin("dummy", 1)
    assert ret == 3

    time.sleep(0.2)
    assert len(state.running) == 3


    ret = m.ttin("dummy", 3)
    assert ret == 6

    time.sleep(0.2)
    assert len(state.running) == 6

    m.stop()
    m.run()
Esempio n. 16
0
def test_events():
    emitted = []
    m = Manager()
    m.start()

    def cb(ev, msg):
        emitted.append((ev, msg['name']))

    # subscribe to all events
    m.on('.', cb)

    testfile, cmd, args, wdir = dummy_cmd()
    m.add_process("dummy", cmd, args=args, cwd=wdir, numprocesses=4)
    m.ttin("dummy", 1)
    m.remove_process("dummy")

    time.sleep(0.2)
    m.stop()
    m.run()


    assert ('create', 'dummy') in emitted
    assert ('start', 'dummy') in emitted
    assert ('update', 'dummy') in emitted
    assert ('stop', 'dummy') in emitted
    assert ('delete', 'dummy') in emitted
Esempio n. 17
0
def test_start_multiple():
    m = Manager()
    m.start()
    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir, numprocesses=2)
    m.load(config)
    state = m._get_locked_state("dummy")
    assert len(state.running) == 2
    m.stop()
    m.run()
Esempio n. 18
0
def test_start_multiple():
    m = Manager()
    m.start()
    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir, numprocesses=2)
    m.load(config)
    state = m._get_locked_state("dummy")
    assert len(state.running) == 2
    m.stop()
    m.run()
Esempio n. 19
0
def test_simple():
    m = Manager()
    m.start()
    assert m.started == True

    def on_stop(manager):
        assert manager.started == False

    m.stop(on_stop)
    m.run()
Esempio n. 20
0
def test_start_multiple():
    m = Manager()
    m.start()
    testfile, cmd, args, wdir = dummy_cmd()
    m.add_process("dummy", cmd, args=args, cwd=wdir, numprocesses=2)
    state = m.get_process_state("dummy")

    assert len(state.running) == 2

    m.stop()

    m.run()
Esempio n. 21
0
def test_start_stop_process():
    m = Manager()
    m.start()
    testfile, cmd, args, wdir = dummy_cmd()
    m.add_process("dummy", cmd, args=args, cwd=wdir)
    state = m.get_process_state("dummy")

    assert len(state.running) == 1

    m.stop_process("dummy")
    assert len(state.running) == 0

    m.stop()
    m.run()
Esempio n. 22
0
def test_start_stop_job():
    res = []
    m = Manager()
    m.start()
    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir)
    m.load(config)
    state = m._get_locked_state("dummy")
    res.append(len(state.running))
    m.stop_job("dummy")
    res.append(len(state.running))
    m.stop()
    m.run()

    assert res == [1, 0]
Esempio n. 23
0
def test_start_stop_job():
    res = []
    m = Manager()
    m.start()
    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir)
    m.load(config)
    state = m._get_locked_state("dummy")
    res.append(len(state.running))
    m.stop_job("dummy")
    res.append(len(state.running))
    m.stop()
    m.run()

    assert res == [1, 0]
Esempio n. 24
0
def test_multiple_handers():
    http_endpoint = HttpEndpoint(uri="%s:%s" % (TEST_HOST, TEST_PORT))
    http_endpoint2 = HttpEndpoint(uri="%s:%s" % (TEST_HOST, TEST_PORT2))
    http_handler = HttpHandler(endpoints=[http_endpoint, http_endpoint2])
    m = Manager()
    m.start(apps=[http_handler])
    time.sleep(0.2)

    s = Server("http://%s:%s" % (TEST_HOST, TEST_PORT), loop=m.loop)
    s2 = Server("http://%s:%s" % (TEST_HOST, TEST_PORT2), loop=m.loop)
    assert TEST_PORT != TEST_PORT2
    assert s.version == __version__
    assert s2.version == __version__

    m.stop()
    m.run()
Esempio n. 25
0
def test_multiple_handers():
    http_endpoint = HttpEndpoint(uri="%s:%s" % (TEST_HOST, TEST_PORT))
    http_endpoint2 = HttpEndpoint(uri="%s:%s" % (TEST_HOST, TEST_PORT2))
    http_handler = HttpHandler(endpoints=[http_endpoint, http_endpoint2])
    m = Manager()
    m.start(apps=[http_handler])
    time.sleep(0.2)

    s = Server("http://%s:%s" % (TEST_HOST, TEST_PORT), loop=m.loop)
    s2 = Server("http://%s:%s" % (TEST_HOST, TEST_PORT2), loop=m.loop)
    assert TEST_PORT != TEST_PORT2
    assert s.version == __version__
    assert s2.version == __version__

    m.stop()
    m.run()
Esempio n. 26
0
def test_numprocesses():
    m = Manager()
    m.start()
    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir, numprocesses=4)
    m.load(config)
    state = m._get_locked_state("dummy")

    assert len(state.running) == 4
    state.numprocesses = 0
    assert state.numprocesses == 0

    m.manage("dummy")
    time.sleep(0.2)
    assert len(state.running) == 0
    m.stop()
    m.run()
Esempio n. 27
0
def test_process_commit():
    emitted = []
    m = Manager()
    m.start()

    def cb(ev, msg):
        emitted.append(msg)

    # subscribe to all events
    m.events.subscribe('job.default.dummy.exit', cb)

    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir, numprocesses=0)
    m.load(config)

    pids0 = m.pids()
    pid = m.commit("dummy", env={"BLAH": "test"})
    process = m.get_process(pid)
    pids1 = m.pids()

    state = m._get_locked_state("dummy")

    assert len(state.running) == 0
    assert state.numprocesses == 0
    assert len(state.running_out) == 1


    m.unload("dummy")

    time.sleep(0.2)
    m.stop()
    m.run()

    assert pids0 == []
    assert pid == 1
    assert process.name == "default.dummy"
    assert process.pid == 1
    assert "BLAH" in process.env
    assert pids1 == [1]

    assert len(emitted) == 1
    assert len(emitted[0]) == 7

    msg = emitted[0]
    assert "exit_status" in msg
    assert msg['once'] == True
Esempio n. 28
0
def test_numprocesses():
    m = Manager()
    m.start()
    testfile, cmd, args, wdir = dummy_cmd()
    m.add_process("dummy", cmd, args=args, cwd=wdir, numprocesses=4)
    state = m.get_process_state("dummy")

    assert len(state.running) == 4
    state.numprocesses = 0
    assert state.numprocesses == 0

    m.manage_process("dummy")
    time.sleep(0.2)
    assert len(state.running) == 0
    m.stop()

    m.run()
Esempio n. 29
0
def test_numprocesses():
    m = Manager()
    m.start()
    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir, numprocesses=4)
    m.load(config)
    state = m._get_locked_state("dummy")

    assert len(state.running) == 4
    state.numprocesses = 0
    assert state.numprocesses == 0

    m.manage("dummy")
    time.sleep(0.2)
    assert len(state.running) == 0
    m.stop()
    m.run()
Esempio n. 30
0
def test_simple_process():
    m = Manager()
    m.start()
    testfile, cmd, args, wdir = dummy_cmd()
    m.add_process("dummy", cmd, args=args, cwd=wdir, start=False)
    state = m.get_process_state("dummy")

    assert state.numprocesses == 1
    assert state.name == "dummy"
    assert state.cmd == cmd
    assert state.settings['args'] == args
    assert state.settings['cwd'] == wdir

    m.remove_process("dummy")
    assert m.get_process_state("dummy") == None
    m.stop()
    m.run()
Esempio n. 31
0
def test_process_commit():
    emitted = []
    m = Manager()
    m.start()

    def cb(ev, msg):
        emitted.append(msg)

    # subscribe to all events
    m.events.subscribe('job.default.dummy.exit', cb)

    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir, numprocesses=0)
    m.load(config)

    pids0 = m.pids()
    pid = m.commit("dummy", env={"BLAH": "test"})
    process = m.get_process(pid)
    pids1 = m.pids()

    state = m._get_locked_state("dummy")

    assert len(state.running) == 0
    assert state.numprocesses == 0
    assert len(state.running_out) == 1

    m.unload("dummy")

    time.sleep(0.2)
    m.stop()
    m.run()

    assert pids0 == []
    assert pid == 1
    assert process.name == "default.dummy"
    assert process.pid == 1
    assert "BLAH" in process.env
    assert pids1 == [1]

    assert len(emitted) == 1
    assert len(emitted[0]) == 7

    msg = emitted[0]
    assert "exit_status" in msg
    assert msg['once'] == True
Esempio n. 32
0
def test_processes_stats():
    m = Manager()
    m.start()
    testfile, cmd, args, wdir = dummy_cmd()
    testfile1, cmd1, args1, wdir1 = dummy_cmd()
    m.add_process("a", cmd, args=args, cwd=wdir)
    time.sleep(0.2)
    infos = list(m.processes_stats())
    m.add_process("b", cmd, args=args, cwd=wdir)
    infos2 = list(m.processes_stats())
    m.stop()
    m.run()

    assert len(infos) == 1
    assert len(infos2) == 2

    assert infos[0]['name'] == "a"
    assert infos2[0]['name'] == "a"
    assert infos2[1]['name'] == "b"
Esempio n. 33
0
def test_process_stats():
    m = Manager()
    m.start()
    testfile, cmd, args, wdir = dummy_cmd()
    m.add_process("dummy", cmd, args=args, cwd=wdir)
    time.sleep(0.2)
    info = m.get_process_stats("dummy")
    info_by_id = m.get_process_stats(1)
    pid = m.running[1].pid
    m.stop()
    m.run()

    assert isinstance(info, dict)
    assert isinstance(info_by_id, dict)
    assert "pid" in info_by_id
    assert info_by_id["pid"] == pid
    assert info['name'] == "dummy"
    assert len(info['stats']) == 1
    assert info['stats'][0]['pid'] == info_by_id['pid']
Esempio n. 34
0
def test_process_id():
    m = Manager()
    m.start()
    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir, numprocesses=4)
    m.load(config)
    state = m._get_locked_state("dummy")

    res = []
    def cb(m, p):
        res.append(p.pid)

    res2 = [p.pid for p in m.list("dummy")]
    m.walk(cb, "dummy")
    m.stop()
    m.run()

    assert res == [1, 2, 3, 4]
    assert res == res2
Esempio n. 35
0
def test_process_id():
    m = Manager()
    m.start()
    testfile, cmd, args, wdir = dummy_cmd()
    m.add_process("dummy", cmd, args=args, cwd=wdir, numprocesses=4)
    state = m.get_process_state("dummy")

    processes = state.list_processes()
    assert isinstance(processes, list)

    p = processes[0]
    assert isinstance(p, Process)
    assert p.id == 1

    p = processes[2]
    assert p.id == 3

    m.stop()
    m.run()
Esempio n. 36
0
def test_process_stats():
    m = Manager()
    m.start()
    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir)
    m.load(config)
    time.sleep(0.2)
    info = m.stats("dummy")
    info_by_id = m.get_process(1).info
    os_pid = m.running[1].os_pid
    m.stop()
    m.run()

    assert isinstance(info, dict)
    assert isinstance(info_by_id, dict)
    assert "os_pid" in info_by_id
    assert info_by_id["os_pid"] == os_pid
    assert info['name'] == "default.dummy"
    assert len(info['stats']) == 1
    assert info['stats'][0]['os_pid'] == info_by_id['os_pid']
Esempio n. 37
0
def test_process_stats():
    m = Manager()
    m.start()
    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir)
    m.load(config)
    time.sleep(0.2)
    info = m.stats("dummy")
    info_by_id = m.get_process(1).info
    os_pid = m.running[1].os_pid
    m.stop()
    m.run()

    assert isinstance(info, dict)
    assert isinstance(info_by_id, dict)
    assert "os_pid" in info_by_id
    assert info_by_id["os_pid"] == os_pid
    assert info['name'] == "default.dummy"
    assert len(info['stats']) == 1
    assert info['stats'][0]['os_pid'] == info_by_id['os_pid']
Esempio n. 38
0
def test_process_id():
    m = Manager()
    m.start()
    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir, numprocesses=4)
    m.load(config)
    state = m._get_locked_state("dummy")

    res = []

    def cb(m, p):
        res.append(p.pid)

    res2 = [p.pid for p in m.list("dummy")]
    m.walk(cb, "dummy")
    m.stop()
    m.run()

    assert res == [1, 2, 3, 4]
    assert res == res2
Esempio n. 39
0
def test_process_events():
    emitted = []
    m = Manager()
    m.start()

    def cb(ev, *args):
        emitted.append(ev)

    # subscribe to all events
    m.on('proc.dummy', cb)

    testfile, cmd, args, wdir = dummy_cmd()
    m.add_process("dummy", cmd, args=args, cwd=wdir)
    m.stop_process("dummy")

    time.sleep(0.2)
    m.stop()
    m.run()

    assert 'proc.dummy.start' in emitted
    assert 'proc.dummy.spawn' in emitted
    assert 'proc.dummy.stop' in emitted
    assert 'proc.dummy.exit' in emitted
Esempio n. 40
0
def test_simple_job():
    m = Manager()
    m.start()
    testfile, cmd, args, wdir = dummy_cmd()

    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir)

    m.load(config, start=False)
    state = m._get_locked_state("dummy")

    assert state.numprocesses == 1
    assert state.name == "default.dummy"
    assert state.cmd == cmd
    assert state.config['args'] == args
    assert state.config['cwd'] == wdir

    m.unload("dummy")

    with pytest.raises(ProcessError):
        m._get_locked_state("dummy")

    m.stop()
    m.run()
Esempio n. 41
0
def test_simple_job():
    m = Manager()
    m.start()
    testfile, cmd, args, wdir = dummy_cmd()

    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir)

    m.load(config, start=False)
    state = m._get_locked_state("dummy")

    assert state.numprocesses == 1
    assert state.name == "default.dummy"
    assert state.cmd == cmd
    assert state.config['args'] == args
    assert state.config['cwd'] == wdir

    m.unload("dummy")

    with pytest.raises(ProcessError):
        m._get_locked_state("dummy")

    m.stop()
    m.run()
Esempio n. 42
0
def test_process_exit_event():
    emitted = []
    m = Manager()
    m.start()

    def cb(ev, msg):
        emitted.append(msg)

    # subscribe to all events
    m.on('proc.dummy.exit', cb)

    testfile, cmd, args, wdir = dummy_cmd()
    m.add_process("dummy", cmd, args=args, cwd=wdir)
    m.stop_process("dummy")

    time.sleep(0.2)
    m.stop()
    m.run()

    assert len(emitted) == 1
    assert len(emitted[0]) == 6

    msg = emitted[0]
    assert "exit_status" in msg
Esempio n. 43
0
def test_process_events():
    emitted = []
    m = Manager()
    m.start()

    def cb(ev, *args):
        emitted.append(ev)

    # subscribe to all events
    m.events.subscribe('job.default.dummy', cb)

    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir)
    m.load(config)
    m.unload("dummy")

    time.sleep(0.2)
    m.stop()
    m.run()

    assert 'job.default.dummy.start' in emitted
    assert 'job.default.dummy.spawn' in emitted
    assert 'job.default.dummy.stop' in emitted
    assert 'job.default.dummy.exit' in emitted
Esempio n. 44
0
def test_process_events():
    emitted = []
    m = Manager()
    m.start()

    def cb(ev, *args):
        emitted.append(ev)

    # subscribe to all events
    m.events.subscribe('job.default.dummy', cb)

    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir)
    m.load(config)
    m.unload("dummy")

    time.sleep(0.2)
    m.stop()
    m.run()

    assert 'job.default.dummy.start' in emitted
    assert 'job.default.dummy.spawn' in emitted
    assert 'job.default.dummy.stop' in emitted
    assert 'job.default.dummy.exit' in emitted
Esempio n. 45
0
def test_manager_hooks():
    hooks = create_hooks([
        'load', 'unload', 'start', 'update', 'stop', 'job.default.dummy.start',
        'job.default.dummy.spawn', 'job.default.dummy.stop',
        'job.default.dummy.exit'
    ])
    emitted = []
    loop = pyuv.Loop.default_loop()
    s = get_server(loop, emitted)
    s.start()
    m = Manager(loop=loop)
    m.start(apps=[WebHooks(hooks)])
    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir, numprocesses=1)
    m.load(config)
    m.manage("dummy")
    m.scale("dummy", 1)
    m.unload("dummy")

    t = pyuv.Timer(loop)

    def on_stop(manager):
        t.start(lambda h: s.stop(), 0.4, 0.0)

    m.stop(on_stop)

    m.run()
    assert ('load', 'default.dummy') in emitted
    assert ('start', 'default.dummy') in emitted
    assert ('update', 'default.dummy') in emitted
    assert ('stop', 'default.dummy') in emitted
    assert ('unload', 'default.dummy') in emitted
    assert ('job.default.dummy.start', 'default.dummy') in emitted
    assert ('job.default.dummy.spawn', 'default.dummy') in emitted
    assert ('job.default.dummy.stop', 'default.dummy') in emitted
    assert ('job.default.dummy.exit', 'default.dummy') in emitted
Esempio n. 46
0
def test_lookup_manager():

    # intiallize the lookupd server
    loop = pyuv.Loop.default_loop()
    r = Registry(loop)
    sock = bind_sockets(LOOKUPD_ADDR)
    io_loop = IOLoop(_loop=loop)
    server = http_server(io_loop, sock, registration_db=r)
    server.start()

    # subscribe to events
    emitted = []

    def cb(event, message):
        emitted.append((event, message))

    r.bind_all(cb)

    # start the manager with the HTTP API
    http_handler = HttpHandler(
        MockConfig(bind=GAFFERD_ADDR,
                   lookupd_addresses=["http://%s" % LOOKUPD_ADDR]))
    m = Manager(loop=loop)
    m.start(apps=[http_handler])

    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir)
    m.load(config)
    m.stop_process(1)
    m.unload("dummy")

    t = pyuv.Timer(loop)

    def do_stop(h):
        server.stop()
        io_loop.close(True)

    def stop_server(m):
        t.start(do_stop, 0.4, 0.0)

    m.stop(stop_server)
    loop.run()

    assert len(emitted) == 7
    actions = [line[0] for line in emitted]
    assert list(actions) == [
        'add_node', 'identify', 'add_job', 'add_process', 'remove_process',
        'remove_job', 'remove_node'
    ]

    assert isinstance(emitted[0][1], GafferNode)
    assert isinstance(emitted[1][1], GafferNode)
    assert isinstance(emitted[2][1], dict)
    assert "job_name" in emitted[2][1]
    assert emitted[2][1]['job_name'] == "default.dummy"
    assert isinstance(emitted[3][1], dict)
    assert "job_name" in emitted[3][1]
    assert emitted[3][1]['job_name'] == "default.dummy"
    assert "pid" in emitted[3][1]
    assert emitted[3][1]['pid'] == 1
    assert isinstance(emitted[4][1], dict)
    assert "job_name" in emitted[4][1]
    assert emitted[4][1]['job_name'] == "default.dummy"
    assert "pid" in emitted[4][1]
    assert emitted[4][1]['pid'] == 1
    assert isinstance(emitted[5][1], dict)
    assert emitted[5][1]['job_name'] == "default.dummy"
    assert isinstance(emitted[6][1], GafferNode)
    assert emitted[6][1].sessions == {}
Esempio n. 47
0
def test_lookup_manager():

    # intiallize the lookupd server
    loop = pyuv.Loop.default_loop()
    r = Registry(loop)
    sock = bind_sockets(LOOKUPD_ADDR)
    io_loop = IOLoop(_loop=loop)
    server = http_server(io_loop, sock, registration_db=r)
    server.start()

    # subscribe to events
    emitted = []
    def cb(event, message):
        emitted.append((event, message))
    r.bind_all(cb)


    # start the manager with the HTTP API
    http_handler = HttpHandler(MockConfig(bind=GAFFERD_ADDR,
            lookupd_addresses=["http://%s" % LOOKUPD_ADDR]))
    m = Manager(loop=loop)
    m.start(apps=[http_handler])

    testfile, cmd, args, wdir = dummy_cmd()
    config = ProcessConfig("dummy", cmd, args=args, cwd=wdir)
    m.load(config)
    m.stop_process(1)
    m.unload("dummy")

    t = pyuv.Timer(loop)

    def do_stop(h):
        server.stop()
        io_loop.close(True)

    def stop_server(m):
        t.start(do_stop, 0.4, 0.0)

    m.stop(stop_server)
    loop.run()

    assert len(emitted) == 7
    actions = [line[0] for line in emitted]
    assert list(actions) == ['add_node', 'identify', 'add_job', 'add_process',
            'remove_process', 'remove_job', 'remove_node']


    assert isinstance(emitted[0][1], GafferNode)
    assert isinstance(emitted[1][1], GafferNode)
    assert isinstance(emitted[2][1], dict)
    assert "job_name" in emitted[2][1]
    assert emitted[2][1]['job_name'] == "default.dummy"
    assert isinstance(emitted[3][1], dict)
    assert "job_name" in emitted[3][1]
    assert emitted[3][1]['job_name'] == "default.dummy"
    assert "pid" in emitted[3][1]
    assert emitted[3][1]['pid'] == 1
    assert isinstance(emitted[4][1], dict)
    assert "job_name" in emitted[4][1]
    assert emitted[4][1]['job_name'] == "default.dummy"
    assert "pid" in emitted[4][1]
    assert emitted[4][1]['pid'] == 1
    assert isinstance(emitted[5][1], dict)
    assert emitted[5][1]['job_name'] == "default.dummy"
    assert isinstance(emitted[6][1], GafferNode)
    assert emitted[6][1].sessions == {}