3 Copyright (c) 2011-2016 ARM Limited
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
21 from inspect
import isfunction, ismethod
25 """ Base class for each host-test test cases with standard
26 setup, test and teardown set of functions
31 __dut_event_queue =
None
32 script_location =
None
35 def __notify_prn(self, text):
37 self.
__event_queue__event_queue.put((
'__notify_prn', text, time()))
39 def __notify_conn_lost(self, text):
41 self.
__event_queue__event_queue.put((
'__notify_conn_lost', text, time()))
43 def __notify_sync_failed(self, text):
45 self.
__event_queue__event_queue.put((
'__notify_sync_failed', text, time()))
47 def __notify_dut(self, key, value):
48 """! Send data over serial to DUT """
53 """! Notify main even loop that host test finished processing
54 @param result True for success, False failure. If None - no action in main even loop
57 self.
__event_queue__event_queue.put((
'__notify_complete', result, time()))
61 Reset device under test
65 self.
__event_queue__event_queue.put((
'__reset_dut', value, time()))
69 Reset the device under test and continue running the host test
76 """! Notify main even loop that there was a DUT-host test connection error
77 @param consume If True htrun will process (consume) all remaining events
82 """! Send log message to main event loop """
86 """! Send Key-Value data to DUT """
90 """! Setup queues used for IPC """
105 """! Setup your tests and callbacks """
106 raise NotImplementedError
109 """! Returns host test result (True, False or None) """
110 raise NotImplementedError
113 """! Blocking always guaranteed test teardown """
114 raise NotImplementedError
119 Decorator for defining a event callback method. Adds a property attribute "event_key" with value as the passed key.
133 BaseHostTestAbstract.__init__(self)
139 '__testcase_summary',
150 '__testcase_summary',
157 def __callback_default(self, key, value, timestamp):
158 """! Default callback """
162 def __default_end_callback(self, key, value, timestamp):
164 Default handler for event 'end' that gives test result from target.
165 This callback is not decorated as we don't know then in what order this
166 callback would be registered. We want to let users over write this callback.
167 Hence it should be registered before registering user defined callbacks.
174 self.notify_complete(value ==
'success')
176 def __assign_default_callbacks(self):
177 """! Assigns default callback handlers """
178 for key
in self.__consume_by_default:
179 self.__callbacks[key] = self.__callback_default
181 self.register_callback(
'end', self.__default_end_callback)
183 def __assign_decorated_callbacks(self):
185 It looks for any callback methods decorated with @event_callback
188 Define a method with @event_callback decorator like:
190 @event_callback('<event key>')
191 def event_handler(self, key, value, timestamp):
196 for name, method
in inspect.getmembers(self, inspect.ismethod):
197 key = getattr(method,
'event_key',
None)
199 self.register_callback(key, method)
202 """! Register callback for a specific event (key: event name)
203 @param key String with name of the event
204 @param callback Callable which will be registstered for event "key"
205 @param force God mode
209 if type(key)
is not str:
210 raise TypeError(
"event non-string keys are not allowed")
213 if not callable(callback):
214 raise TypeError(
"event callback should be callable")
218 if ismethod(callback):
219 arg_count = six.get_function_code(callback).co_argcount
221 err_msg =
"callback 'self.%s('%s', ...)' defined with %d arguments"% (callback.__name__, key, arg_count)
222 err_msg +=
", should have 4 arguments: self.%s(self, key, value, timestamp)"% callback.__name__
223 raise TypeError(err_msg)
226 if isfunction(callback):
227 arg_count = six.get_function_code(callback).co_argcount
229 err_msg =
"callback '%s('%s', ...)' defined with %d arguments"% (callback.__name__, key, arg_count)
230 err_msg +=
", should have 3 arguments: %s(key, value, timestamp)"% callback.__name__
231 raise TypeError(err_msg)
235 if key.startswith(
'__'):
236 raise ValueError(
"event key starting with '__' are reserved")
240 raise ValueError(
"we predefined few callbacks you can't use e.g. '%s'"% key)
257 class BaseHostTest(HostTestCallbackBase):
259 __BaseHostTest_Called =
False
262 """ This function will check if BaseHostTest ctor was called
263 Call to BaseHostTest is required in order to force required
264 interfaces implementation.
265 @return Returns True if ctor was called (ok behaviour)
270 HostTestCallbackBase.__init__(self)
def get_config_item(self, name)
def __notify_dut(self, key, value)
def notify_conn_lost(self, text)
Notify main even loop that there was a DUT-host test connection error.
def send_kv(self, key, value)
Send Key-Value data to DUT.
def reset_dut(self, value)
def log(self, text)
Send log message to main event loop.
def setup(self)
Setup your tests and callbacks.
def result(self)
Returns host test result (True, False or None)
def __notify_conn_lost(self, text)
def setup_communication(self, event_queue, dut_event_queue, config={})
Setup queues used for IPC.
def notify_complete(self, result=None)
Notify main even loop that host test finished processing.
def __notify_prn(self, text)
def teardown(self)
Blocking always guaranteed test teardown.
bool __BaseHostTest_Called
def base_host_test_inited(self)
def register_callback(self, key, callback, force=False)
Register callback for a specific event (key: event name)
def teardown(self)
Blocking always guaranteed test teardown.
def setup(self)
Setup your tests and callbacks.
def result(self)
Returns host test result (True, False or None)
def __assign_default_callbacks(self)
def __assign_decorated_callbacks(self)