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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
"""
mbed SDK
Copyright (c) 2011-2016 ARM Limited
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import inspect
from time import time
from inspect import isfunction, ismethod
class BaseHostTestAbstract(object):
""" Base class for each host-test test cases with standard
setup, test and teardown set of functions
"""
name = '' # name of the host test (used for local registration)
__event_queue = None # To main even loop
__dut_event_queue = None # To DUT
script_location = None # Path to source file used to load host test
__config = {}
def __notify_prn(self, text):
if self.__event_queue:
self.__event_queue.put(('__notify_prn', text, time()))
def __notify_conn_lost(self, text):
if self.__event_queue:
self.__event_queue.put(('__notify_conn_lost', text, time()))
def __notify_sync_failed(self, text):
if self.__event_queue:
self.__event_queue.put(('__notify_sync_failed', text, time()))
def __notify_dut(self, key, value):
"""! Send data over serial to DUT """
if self.__dut_event_queue:
self.__dut_event_queue.put((key, value, time()))
def notify_complete(self, result=None):
"""! Notify main even loop that host test finished processing
@param result True for success, False failure. If None - no action in main even loop
"""
if self.__event_queue:
self.__event_queue.put(('__notify_complete', result, time()))
def reset_dut(self, value):
"""
Reset device under test
:return:
"""
if self.__event_queue:
self.__event_queue.put(('__reset_dut', value, time()))
def reset(self):
"""
Reset the device under test and continue running the host test
:return:
"""
if self.__event_queue:
self.__event_queue.put(("__reset", "0", time()))
def notify_conn_lost(self, text):
"""! Notify main even loop that there was a DUT-host test connection error
@param consume If True htrun will process (consume) all remaining events
"""
self.__notify_conn_lost(text)
def log(self, text):
"""! Send log message to main event loop """
self.__notify_prn(text)
def send_kv(self, key, value):
"""! Send Key-Value data to DUT """
self.__notify_dut(key, value)
def setup_communication(self, event_queue, dut_event_queue, config={}):
"""! Setup queues used for IPC """
self.__event_queue = event_queue # To main even loop
self.__dut_event_queue = dut_event_queue # To DUT
self.__config = config
def get_config_item(self, name):
"""
Return test config
:param name:
:return:
"""
return self.__config.get(name, None)
def setup(self):
"""! Setup your tests and callbacks """
raise NotImplementedError
def result(self):
"""! Returns host test result (True, False or None) """
raise NotImplementedError
def teardown(self):
"""! Blocking always guaranteed test teardown """
raise NotImplementedError
def event_callback(key):
"""
Decorator for defining a event callback method. Adds a property attribute "event_key" with value as the passed key.
:param key:
:return:
"""
def decorator(func):
func.event_key = key
return func
return decorator
class HostTestCallbackBase(BaseHostTestAbstract):
def __init__(self):
BaseHostTestAbstract.__init__(self)
self.__callbacks = {}
self.__restricted_callbacks = [
'__coverage_start',
'__testcase_start',
'__testcase_finish',
'__testcase_summary',
'__exit',
'__exit_event_queue'
]
self.__consume_by_default = [
'__coverage_start',
'__testcase_start',
'__testcase_finish',
'__testcase_count',
'__testcase_name',
'__testcase_summary',
'__rxd_line',
]
self.__assign_default_callbacks()
self.__assign_decorated_callbacks()
def __callback_default(self, key, value, timestamp):
"""! Default callback """
#self.log("CALLBACK: key=%s, value=%s, timestamp=%f"% (key, value, timestamp))
pass
def __default_end_callback(self, key, value, timestamp):
"""
Default handler for event 'end' that gives test result from target.
This callback is not decorated as we don't know then in what order this
callback would be registered. We want to let users over write this callback.
Hence it should be registered before registering user defined callbacks.
:param key:
:param value:
:param timestamp:
:return:
"""
self.notify_complete(value == 'success')
def __assign_default_callbacks(self):
"""! Assigns default callback handlers """
for key in self.__consume_by_default:
self.__callbacks[key] = self.__callback_default
# Register default handler for event 'end' before assigning user defined callbacks to let users over write it.
self.register_callback('end', self.__default_end_callback)
def __assign_decorated_callbacks(self):
"""
It looks for any callback methods decorated with @event_callback
Example:
Define a method with @event_callback decorator like:
@event_callback('<event key>')
def event_handler(self, key, value, timestamp):
do something..
:return:
"""
for name, method in inspect.getmembers(self, inspect.ismethod):
key = getattr(method, 'event_key', None)
if key:
self.register_callback(key, method)
def register_callback(self, key, callback, force=False):
"""! Register callback for a specific event (key: event name)
@param key String with name of the event
@param callback Callable which will be registstered for event "key"
@param force God mode
"""
# Non-string keys are not allowed
if type(key) is not str:
raise TypeError("event non-string keys are not allowed")
# And finally callback should be callable
if not callable(callback):
raise TypeError("event callback should be callable")
# Check if callback has all three required parameters (key, value, timestamp)
# When callback is class method should have 4 arguments (self, key, value, timestamp)
if ismethod(callback):
arg_count = callback.__code__.co_argcount
if arg_count != 4:
err_msg = "callback 'self.%s('%s', ...)' defined with %d arguments"% (callback.__name__, key, arg_count)
err_msg += ", should have 4 arguments: self.%s(self, key, value, timestamp)"% callback.__name__
raise TypeError(err_msg)
# When callback is just a function should have 3 arguments func(key, value, timestamp)
if isfunction(callback):
arg_count = callback.__code__.co_argcount
if arg_count != 3:
err_msg = "callback '%s('%s', ...)' defined with %d arguments"% (callback.__name__, key, arg_count)
err_msg += ", should have 3 arguments: %s(key, value, timestamp)"% callback.__name__
raise TypeError(err_msg)
if not force:
# Event starting with '__' are reserved
if key.startswith('__'):
raise ValueError("event key starting with '__' are reserved")
# We predefined few callbacks you can't use
if key in self.__restricted_callbacks:
raise ValueError("we predefined few callbacks you can't use e.g. '%s'"% key)
self.__callbacks[key] = callback
def get_callbacks(self):
return self.__callbacks
def setup(self):
pass
def result(self):
pass
def teardown(self):
pass
class BaseHostTest(HostTestCallbackBase):
__BaseHostTest_Called = False
def base_host_test_inited(self):
""" This function will check if BaseHostTest ctor was called
Call to BaseHostTest is required in order to force required
interfaces implementation.
@return Returns True if ctor was called (ok behaviour)
"""
return self.__BaseHostTest_Called
def __init__(self):
HostTestCallbackBase.__init__(self)
self.__BaseHostTest_Called = True
|