blob: 54747d29b986fa3b3e965dc60bd048529aa876e8 [file] [log] [blame]
Ang Li73697b32015-12-03 00:41:53 +00001# python3.4
2# Copyright (C) 2014 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may not
5# use this file except in compliance with the License. You may obtain a copy of
6# the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations under
14# the License.
15
16"""
17Basic LE Stress tests.
18"""
19
20import concurrent
21import pprint
22import time
23
24from queue import Empty
25from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
26from acts.test_utils.bt.bt_test_utils import generate_ble_advertise_objects
27from acts.test_utils.bt.bt_test_utils import generate_ble_scan_objects
28from acts.test_utils.bt.bt_test_utils import get_advanced_droid_list
29from acts.test_utils.bt.bt_test_utils import reset_bluetooth
30from acts.test_utils.bt.bt_test_utils import scan_result
31
32class BleStressTest(BluetoothBaseTest):
33 tests = None
34 default_timeout = 10
35
36 def __init__(self, controllers):
37 BluetoothBaseTest.__init__(self, controllers)
38 self.droid_list = get_advanced_droid_list(self.droids, self.eds)
39 self.scn_droid, self.scn_ed = self.droids[0], self.eds[0]
40 self.adv_droid, self.adv_ed = self.droids[1], self.eds[1]
41 self.tests = (
42 "test_loop_scanning_1000",
43 "test_restart_scan_callback_after_bt_toggle",
44 "test_loop_scanning_100_verify_no_hci_timeout",
45 "test_start_le_scan_while_toggling_bt",
46 )
47 if self.droid_list[0]['max_advertisements'] > 0:
48 self.tests = self.tests + ("test_loop_advertising_100",
49 "test_restart_advertise_callback_after_bt_toggle",)
50
51 def bleadvertise_verify_onsuccess_handler(self, event):
52 test_result = True
53 self.log.debug("Verifying onSuccess event")
54 self.log.debug(pprint.pformat(event))
55 return test_result
56
57 @BluetoothBaseTest.bt_test_wrap
58 def test_loop_scanning_1000(self):
59 """Stress start/stop scan instances.
60
61 This test will start and stop scan instances as fast as possible. This
62 will guarantee that the scan instances are properly being cleaned up
63 when the scan is stopped.
64
65 Steps:
66 1. Start a scan instance.
67 2. Stop the scan instance.
68 3. Repeat steps 1-2 1000 times.
69
70 Expected Result:
71 Neither starting or stopping scan instances causes any failures.
72
73 Returns:
74 Pass if True
75 Fail if False
76
77 TAGS: LE, Scanning, Stress
78 Priority: 1
79 """
80 scan_droid, scan_event_dispatcher = self.droid, self.ed
81 test_result = True
82 for _ in range(1000):
83 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
84 scan_droid)
85 self.scn_droid.bleStartBleScan(
86 filter_list, scan_settings, scan_callback)
87 self.scn_droid.bleStopBleScan(scan_callback)
88 return test_result
89
90 @BluetoothBaseTest.bt_test_wrap
91 def test_loop_scanning_100_verify_no_hci_timeout(self):
92 """Stress start/stop scan instances variant.
93
94 This test will start and stop scan instances with a one second timeout
95 in between each iteration. This testcase was added because the specific
96 timing combination caused hci timeouts.
97
98 Steps:
99 1. Start a scan instance.
100 2. Stop the scan instance.
101 3. Sleep for 1 second.
102 4. Repeat steps 1-3 100 times.
103
104 Expected Result:
105 Neither starting or stopping scan instances causes any failures.
106
107 Returns:
108 Pass if True
109 Fail if False
110
111 TAGS: LE, Scanning, Stress
112 Priority: 1
113 """
114 adv_callback, adv_data, adv_settings = generate_ble_advertise_objects(
115 self.adv_droid)
116 self.adv_droid.bleStartBleAdvertising(
117 adv_callback, adv_data, adv_settings)
118 test_result = True
119 for _ in range(100):
120 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
121 self.scn_droid)
122 self.scn_droid.bleStartBleScan(
123 filter_list, scan_settings, scan_callback)
124 self.log.info(self.scn_ed.pop_event(
125 scan_result.format(scan_callback)))
126 self.scn_droid.bleStopBleScan(scan_callback)
127 time.sleep(1)
128 return test_result
129
130 @BluetoothBaseTest.bt_test_wrap
131 def test_loop_advertising_100(self):
132 """Stress start/stop advertising instances.
133
134 This test will start and stop advertising instances as fast as possible.
135
136 Steps:
137 1. Start a advertising instance.
138 2. Find that an onSuccess callback is triggered.
139 3. Stop the advertising instance.
140 4. Repeat steps 1-3 100 times.
141
142 Expected Result:
143 Neither starting or stopping advertising instances causes any failures.
144
145 Returns:
146 Pass if True
147 Fail if False
148
149 TAGS: LE, Advertising, Stress
150 Priority: 1
151 """
152 advertise_droid, advertise_event_dispatcher = self.droid, self.ed
153 test_result = True
154 for _ in range(100):
155 advertise_callback, advertise_data, advertise_settings = generate_ble_advertise_objects(
156 advertise_droid)
157 advertise_droid.bleStartBleAdvertising(
158 advertise_callback, advertise_data, advertise_settings)
159 expected_advertise_event_name = "".join(
160 ["BleAdvertise", str(advertise_callback), "onSuccess"])
161 worker = advertise_event_dispatcher.handle_event(
162 self.bleadvertise_verify_onsuccess_handler, expected_advertise_event_name, (
163 []),
164 self.default_timeout)
165 try:
166 self.log.debug(worker.result(self.default_timeout))
167 except Empty as error:
168 self.log.debug(
169 " ".join(["Test failed with Empty error:", str(error)]))
170 test_result = False
171 except concurrent.futures._base.TimeoutError as error:
172 self.log.debug(
173 " ".join(["Test failed, filtering callback onSuccess never occurred:",
174 str(error)]))
175 test_result = False
176 advertise_droid.bleStopBleAdvertising(advertise_callback)
177 return test_result
178
179 @BluetoothBaseTest.bt_test_wrap
180 def test_restart_advertise_callback_after_bt_toggle(self):
181 """Test to reuse an advertise callback.
182
183 This will verify if advertising objects can be reused after a bluetooth
184 toggle.
185
186 Steps:
187 1. Start a advertising instance.
188 2. Find that an onSuccess callback is triggered.
189 3. Stop the advertising instance.
190 4. Toggle bluetooth off and on.
191 5. Start an advertising instance on the same objects used in step 1.
192 6. Find that an onSuccess callback is triggered.
193
194 Expected Result:
195 Advertisement should start successfully.
196
197 Returns:
198 Pass if True
199 Fail if False
200
201 TAGS: LE, Advertising, Stress
202 Priority: 1
203 """
204 test_result = True
205 advertise_droid, advertise_event_dispatcher = self.droid, self.ed
206 advertise_callback, advertise_data, advertise_settings = generate_ble_advertise_objects(
207 advertise_droid)
208 advertise_droid.bleStartBleAdvertising(
209 advertise_callback, advertise_data, advertise_settings)
210 expected_advertise_event_name = "".join(
211 ["BleAdvertise", str(advertise_callback), "onSuccess"])
212 worker = advertise_event_dispatcher.handle_event(
213 self.bleadvertise_verify_onsuccess_handler, expected_advertise_event_name, (
214 []),
215 self.default_timeout)
216 try:
217 self.log.debug(worker.result(self.default_timeout))
218 except Empty as error:
219 self.log.debug(
220 " ".join(["Test failed with Empty error:", str(error)]))
221 test_result = False
222 except concurrent.futures._base.TimeoutError as error:
223 self.log.debug(
224 " ".join(["Test failed, filtering callback onSuccess never occurred:",
225 str(error)]))
226 test_result = reset_bluetooth([self.droids[0]], [self.eds[0]])
227 if not test_result:
228 return test_result
229 time.sleep(5)
230 advertise_droid.bleStartBleAdvertising(
231 advertise_callback, advertise_data, advertise_settings)
232 worker = advertise_event_dispatcher.handle_event(
233 self.bleadvertise_verify_onsuccess_handler, expected_advertise_event_name, (
234 []),
235 self.default_timeout)
236 try:
237 self.log.debug(worker.result(self.default_timeout))
238 except Empty as error:
239 self.log.debug(
240 " ".join(["Test failed with Empty error:", str(error)]))
241 test_result = False
242 except concurrent.futures._base.TimeoutError as error:
243 self.log.debug(
244 " ".join(["Test failed, filtering callback onSuccess never occurred:",
245 str(error)]))
246 return test_result
247
248 @BluetoothBaseTest.bt_test_wrap
249 def test_restart_scan_callback_after_bt_toggle(self):
250 """Test to reuse an scan callback.
251
252 This will verify if scan objects can be reused after a bluetooth
253 toggle.
254
255 Steps:
256 1. Start a scanning instance.
257 3. Stop the scanning instance.
258 4. Toggle bluetooth off and on.
259 5. Start an scanning instance on the same objects used in step 1.
260
261 Expected Result:
262 Scanner should start successfully.
263
264 Returns:
265 Pass if True
266 Fail if False
267
268 TAGS: LE, Scanning, Stress
269 Priority: 1
270 """
271 test_result = True
272 scan_droid, scan_event_dispatcher = self.droid, self.ed
273 filter_list, scan_settings, scan_callback = generate_ble_scan_objects(
274 scan_droid)
275 self.scn_droid.bleStartBleScan(
276 filter_list, scan_settings, scan_callback)
277 reset_bluetooth([scan_droid], [scan_event_dispatcher])
278 self.scn_droid.bleStartBleScan(
279 filter_list, scan_settings, scan_callback)
280
281 return test_result