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