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