xref: /aosp_15_r20/external/mobly-snippet-lib/examples/ex5_schedule_rpc/README.md (revision ae5b1ec8a57d9cd6259556f14d3f2bb4f9fb3a85)
1# Scheduling RPCs Example
2
3This example shows you how to use `scheduleRpc` which is built into
4Mobly snippet lib to handle RPC scheduling.
5
6## Why this is needed?
7
8Some tests may need a snippet RPC to execute when the snippet client is unable
9to reach the device, e.g., performing test actions while USB is disconnected.
10For example, for battery testing (with Monsoon devices), we may want to measure
11power consumed during certain test actions (e.g., phone calls). However
12a Monsoon device turns off USB during battery data measurement, and a regular
13snippet RPC won't work when the client is not connected to the device.
14Therefore, prior to starting the Monsoon measurement we need to schedule a phone
15call RPC prior to soccur during the measurement period.
16
17In this scenario, the test steps would be:
18
191. Schedule the `makePhoneCall('123456')` to execute after (e.g., 10 seconds):
20
21        s.scheduleRpc('makePhoneCall', 10000, ['123456'])
22
232. Start a Monsoon device to collect battery data, while simultaneously USB is
24   turned off.
253. After 10 seconds, the phone call starts while USB is off.
264. Finally, after the phone call is finished, Monsoon data collection completes
27   and USB is re-enabled.
285. The test retrieves any cached events or data from the device.
29
30
31
32See the source code ExampleScheduleRpcSnippet.java for details.
33
34## Running the example code
35
36This folder contains a fully working example of a standalone snippet apk.
37
381.  Compile the example
39
40        ./gradlew examples:ex5_schedule_rpc:assembleDebug
41
421.  Install the apk on your phone
43
44        adb install -r ./examples/ex5_schedule_rpc/build/outputs/apk/debug/ex5_schedule_rpc-debug.apk
45
461.  Use `snippet_shell` from mobly to trigger `tryEvent()`:
47
48        snippet_shell.py com.google.android.mobly.snippet.example5
49
50        >>> callback = s.scheduleRpc('makeToast', 5000, ['message'])
51
52        Wait for the message to show up on the screen (sync RPC call)
53
54        >>> callback.waitAndGet('makeToast').data
55        {u'callback': u'null', u'error': u'null', u'result': u'OK', u'id': u'0'}
56
57        >>> callback = s.scheduleRpc('asyncMakeToast', 5000, ['message'])
58
59        Wait for the message to show up on the screen (async RPC call)
60
61        >>> callback.waitAndGet('asyncMakeToast').data
62        {u'callback': u'1-1', u'error': u'null', u'result': u'null', u'id': u'0'}
63