blob: 989fa281b404aa296a44d392a7e30a8c20d6d39f [file] [log] [blame]
jdespreze2d5ed72018-03-07 14:48:16 -08001/*
2 * Copyright (C) 2018 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 */
16package com.android.tradefed.suite.checker;
17
Julien Desprez12bb2132018-07-12 15:30:29 -070018import com.android.ddmlib.Log.LogLevel;
jdespreze2d5ed72018-03-07 14:48:16 -080019import com.android.tradefed.device.DeviceNotAvailableException;
20import com.android.tradefed.device.ITestDevice;
21import com.android.tradefed.log.LogUtil.CLog;
Julien Desprez3e4d47d2018-05-23 15:53:40 -070022import com.android.tradefed.suite.checker.StatusCheckerResult.CheckStatus;
jdespreze2d5ed72018-03-07 14:48:16 -080023import com.android.tradefed.util.TimeUtil;
24
25import java.util.Date;
26
27/** Status checker to ensure that the device and host time are kept in sync. */
28public class TimeStatusChecker implements ISystemStatusChecker {
29
Julien Desprez45c1f7f2019-03-01 10:27:25 -080030 private boolean mFailing = false;
31
jdespreze2d5ed72018-03-07 14:48:16 -080032 @Override
Julien Desprez3e4d47d2018-05-23 15:53:40 -070033 public StatusCheckerResult postExecutionCheck(ITestDevice device)
34 throws DeviceNotAvailableException {
jdespreze2d5ed72018-03-07 14:48:16 -080035 long difference = device.getDeviceTimeOffset(new Date());
36 if (difference > 5000L) {
Julien Desprez3e4d47d2018-05-23 15:53:40 -070037 String message =
38 String.format(
39 "Found a difference of '%s' between the host and device clock, "
40 + "resetting the device time.",
41 TimeUtil.formatElapsedTime(difference));
42 CLog.w(message);
Julien Desprez12bb2132018-07-12 15:30:29 -070043 device.logOnDevice(
44 "Tradefed.TimeStatusChecker",
45 LogLevel.VERBOSE,
46 "Module Checker is about to reset the time.");
jdespreze2d5ed72018-03-07 14:48:16 -080047 device.setDate(new Date());
Julien Desprez45c1f7f2019-03-01 10:27:25 -080048 if (mFailing) {
49 // Avoid capturing more bugreport if the issue continues to happen, only the first
50 // module will capture it.
51 CLog.w("TimeStatusChecker is still failing on %s", device.getSerialNumber());
52 return new StatusCheckerResult(CheckStatus.SUCCESS);
53 }
Julien Desprez3e4d47d2018-05-23 15:53:40 -070054 StatusCheckerResult result = new StatusCheckerResult(CheckStatus.FAILED);
55 result.setErrorMessage(message);
Julien Desprez45c1f7f2019-03-01 10:27:25 -080056 mFailing = true;
Julien Desprez3e4d47d2018-05-23 15:53:40 -070057 return result;
jdespreze2d5ed72018-03-07 14:48:16 -080058 }
Julien Desprez45c1f7f2019-03-01 10:27:25 -080059 mFailing = false;
Julien Desprez3e4d47d2018-05-23 15:53:40 -070060 return new StatusCheckerResult(CheckStatus.SUCCESS);
jdespreze2d5ed72018-03-07 14:48:16 -080061 }
62}