summaryrefslogtreecommitdiff
path: root/tests/common/accel_pppd_process.py
blob: c2ee451ed970ce9f364edfa66a93d9b567b24a39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from subprocess import Popen, PIPE
from common import process
from threading import Thread
import time


def accel_pppd_thread_func(accel_pppd_control):
    process = accel_pppd_control["process"]
    print("accel_pppd_thread_func: before communicate")
    (out, err) = process.communicate()
    print(
        "accel_pppd_thread_func: after communicate out=" + str(out) + " err=" + str(err)
    )
    process.wait()
    print("accel_pppd_thread_func: after wait")


def start(accel_pppd, args, accel_cmd, max_wait_time):
    print("accel_pppd_start: begin")
    accel_pppd_process = Popen([accel_pppd] + args, stdout=PIPE, stderr=PIPE)
    accel_pppd_control = {"process": accel_pppd_process}
    accel_pppd_thread = Thread(
        target=accel_pppd_thread_func,
        args=[accel_pppd_control],
    )
    accel_pppd_thread.start()

    # wait until accel-pppd replies to 'show version'
    # accel-pppd needs some time to be accessible
    sleep_time = 0.0
    is_started = False
    while sleep_time < max_wait_time:
        if accel_pppd_process.poll() is not None:  # process is terminated
            print(
                "accel_pppd_start: terminated during 'show version' polling in (sec): "
                + str(sleep_time)
            )
            is_started = False
            break
        (exit, out, err) = process.run([accel_cmd, "show version"])
        if exit != 0:  # does not reply
            time.sleep(0.1)
            sleep_time += 0.1
        else:  # replied
            print("accel_pppd_start: 'show version' replied")
            is_started = True
            break

    return (is_started, accel_pppd_thread, accel_pppd_control)


def end(accel_pppd_thread, accel_pppd_control, accel_cmd, max_wait_time):
    print("accel_pppd_end: begin")
    if accel_pppd_control["process"].poll() is not None: # terminated
        print("accel_pppd_end: already terminated. nothing to do")
        accel_pppd_thread.join() 
        return

    process.run(
        [accel_cmd, "shutdown hard"]
    )  # send shutdown hard command (in coverage mode it helps saving coverage data)
    print("accel_pppd_end: after shutdown hard")

    # wait until accel-pppd is finished
    sleep_time = 0.0
    is_finished = False
    while sleep_time < max_wait_time:
        if accel_pppd_control["process"].poll() is None:  # not terminated yet
            time.sleep(0.01)
            sleep_time += 0.01
            # print("accel_pppd_end: sleep 0.01")
        else:
            is_finished = True
            print(
                "accel_pppd_end: finished via shutdown hard in (sec): "
                + str(sleep_time)
            )
            break

    # accel-pppd is still alive. kill it
    if not is_finished:
        print("accel_pppd_end: kill process: " + str(accel_pppd_control["process"]))
        accel_pppd_control["process"].kill()  # kill -9 if 'shutdown hard' didn't help

    accel_pppd_thread.join()  # wait until thread is finished
    print("accel_pppd_end: end")