예제 #1
0
def test_timeout_before_command_sent(buffer_connection,
                                     command_output_and_expected_result_ping):
    from moler.cmd.unix.uptime import Uptime
    from moler.cmd.unix.ping import Ping
    command_output, expected_result = command_output_and_expected_result_ping
    ping_cmd = Ping(connection=buffer_connection.moler_connection,
                    prompt="host:.*#",
                    destination="localhost",
                    options="-w 5")
    uptime_cmd = Uptime(connection=buffer_connection.moler_connection,
                        prompt="host:.*#")
    ping_cmd.start(timeout=2)
    time.sleep(0.05)
    buffer_connection.moler_connection.data_received(
        command_output[0].encode("utf-8"), datetime.datetime.now())
    with pytest.raises(CommandTimeout):
        uptime_cmd(timeout=0.2)
    buffer_connection.moler_connection.data_received(
        command_output[1].encode("utf-8"), datetime.datetime.now())
    ping_cmd.await_done(timeout=0.2)
    ping_ret = ping_cmd.result()
    assert ping_ret == expected_result
예제 #2
0
import time
from moler.cmd.unix.ping import Ping
from moler.connection_factory import get_connection

host = 'www.google.com'
terminal = get_connection(io_type='terminal', variant='threaded')
with terminal.open():
    ping_cmd = Ping(connection=terminal.moler_connection,
                    destination=host,
                    options="-w 6")
    print("Start pinging {} ...".format(host))
    ping_cmd.start()
    print("Doing other stuff while pinging {} ...".format(host))
    time.sleep(3)
    ping_stats = ping_cmd.await_done(timeout=4)
    print("ping {}: {}={}, {}={} [{}]".format(host, 'packet_loss',
                                              ping_stats['packet_loss'],
                                              'time_avg',
                                              ping_stats['time_avg'],
                                              ping_stats['time_unit']))
# result:
"""
Start pinging www.google.com ...
Doing other stuff while pinging www.google.com ...
ping www.google.com: packet_loss=0, time_avg=50.000 [ms]
"""