1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.compatibility.common.util; 18 19 import com.android.ddmlib.Log.LogLevel; 20 import com.android.tradefed.device.DeviceNotAvailableException; 21 import com.android.tradefed.device.ITestDevice; 22 import com.android.tradefed.log.LogUtil.CLog; 23 import com.android.tradefed.result.ITestInvocationListener; 24 import com.android.tradefed.result.InputStreamSource; 25 import com.android.tradefed.result.LogDataType; 26 import com.android.tradefed.util.RunUtil; 27 28 /** 29 * Utility functions related to device state monitoring during compatibility test. 30 */ 31 public class MonitoringUtils { 32 33 private static final long CONNECTIVITY_CHECK_TIME_MS = 60 * 1000; 34 private static final long CONNECTIVITY_CHECK_INTERVAL_MS = 5 * 1000; 35 checkDeviceConnectivity(ITestDevice device)36 public static boolean checkDeviceConnectivity(ITestDevice device) 37 throws DeviceNotAvailableException { 38 long start = System.currentTimeMillis(); 39 while (System.currentTimeMillis() - start < CONNECTIVITY_CHECK_TIME_MS) { 40 if (device.checkConnectivity()) { 41 CLog.i("Connectivity: passed check."); 42 return true; 43 } else { 44 CLog.logAndDisplay(LogLevel.INFO, 45 "Connectivity check failed, retrying in %dms", 46 CONNECTIVITY_CHECK_INTERVAL_MS); 47 RunUtil.getDefault().sleep(CONNECTIVITY_CHECK_INTERVAL_MS); 48 } 49 } 50 return false; 51 } 52 checkDeviceConnectivity(ITestDevice device, ITestInvocationListener listener, String tag)53 public static void checkDeviceConnectivity(ITestDevice device, ITestInvocationListener listener, 54 String tag) throws DeviceNotAvailableException { 55 if (!checkDeviceConnectivity(device)) { 56 CLog.w("Connectivity: check failed."); 57 InputStreamSource bugSource = device.getBugreport(); 58 listener.testLog(String.format("bugreport-connectivity-%s", tag), 59 LogDataType.TEXT, bugSource); 60 bugSource.cancel(); 61 } 62 } 63 } 64