summaryrefslogtreecommitdiffstats
path: root/tester/rt/wait.py
blob: a42e5e56f3a0c650028eb48928297230589ad0be (plain) (blame)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# SPDX-License-Identifier: BSD-2-Clause
'''Wait test target.'''

# Copyright (C) 2013-2021 Chris Johns (chrisj@rtems.org)
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import print_function

import datetime
import os
import threading
import time


class wait(object):
    '''RTEMS Testing WAIT base.'''

    # pylint: disable=useless-object-inheritance
    # pylint: disable=too-many-instance-attributes

    def __init__(self, bsp_arch, bsp, trace=False):
        self.trace = trace
        self.lock_trace = False
        self.lock_locked = None
        self.lock = threading.RLock()
        self.bsp = bsp
        self.bsp_arch = bsp_arch
        self._init()

    def _init(self):
        self.output_length = None
        self.console = None
        self.server = None
        self.timeout = None
        self.test_too_long = None
        self.timer = None
        self.waiting = False
        self.seconds = 0

    def _lock(self, msg):
        if self.lock_trace:
            print('|[   LOCK:%s ]|' % (msg))
            self.lock_locked = datetime.datetime.now()
        self.lock.acquire()

    def _unlock(self, msg):
        if self.lock_trace:
            period = datetime.datetime.now() - self.lock_locked
            print('|] UNLOCK:%s [| : %s' % (msg, period))
        self.lock.release()

    def _trace(self, text):
        if log.tracing:
            log.trace('] wait: ' + text)

    def _console(self, text):
        if self.console:
            self.console(text)

    def _kill(self):
        self._lock('_kill')
        self._console('wait: kill')
        self.waiting = False
        self._unlock('_kill')

    def _timeout(self):
        self._kill()
        if self.timeout is not None:
            self.timeout()

    def _test_too_long(self):
        self._kill()
        if self.test_too_long is not None:
            self.test_too_long()

    def _monitor(self, timeout):
        output_length = self.output_length
        period = timeout[0]
        seconds = timeout[1]
        self._console('wait: start: period=%d seconds=%d' % (period, seconds))
        step = 0.5
        period *= step
        seconds *= step
        self.waiting = True
        while self.waiting and period > 0 and seconds > 0:
            self._unlock('_monitor')
            current_length = self.output_length()
            if output_length != current_length:
                period = timeout[0] * step
            output_length = current_length
            if seconds < step:
                seconds = 0
            else:
                seconds -= step
            if period < step:
                step = period
                period = 0
            else:
                period -= step
            time.sleep(step)
            self._lock('_monitor')
        if self.waiting:
            if period == 0:
                self._timeout()
            elif seconds == 0:
                self._test_too_long()
        self._console('wait: finished: period=%d seconds=%d' % (period, seconds))

    def open(self, output_length, console, timeout):
        '''Start to wait'''
        # pylint: disable=too-many-arguments
        self._lock('_open')
        self._init()
        self.output_length = output_length
        self.console = console
        self.timeout = timeout[2]
        self.test_too_long = timeout[3]
        self._monitor(timeout)

    def kill(self):
        '''Kill the test run.'''
        self._kill()

    def target_restart(self, started):
        pass

    def target_reset(self, started):
        pass

    def target_start(self):
        pass

    def target_end(self):
        pass