Fix lost callbacks due to time drift between server and emulator

Reviewed By: yungsters

Differential Revision: D3434921

fbshipit-source-id: ec82374a8ed322d99beadac78a415f952ceb3ec8
This commit is contained in:
Krishna Monian 2016-06-15 13:13:09 -07:00 committed by Facebook Github Bot 0
parent f236667a17
commit 12a87b6674
6 changed files with 27 additions and 12 deletions

View File

@ -4,20 +4,21 @@ android_library(
name = 'testing',
srcs = glob(['**/*.java']),
deps = [
react_native_target('java/com/facebook/react:react'),
react_native_target('java/com/facebook/react/bridge:bridge'),
react_native_target('java/com/facebook/react/common:common'),
react_native_target('java/com/facebook/react/modules/core:core'),
react_native_target('java/com/facebook/react/modules/debug:debug'),
react_native_target('java/com/facebook/react/devsupport:devsupport'),
react_native_target('java/com/facebook/react/shell:shell'),
react_native_target('java/com/facebook/react/uimanager:uimanager'),
react_native_dep('libraries/soloader/java/com/facebook/soloader:soloader'),
react_native_dep('third-party/android/support/v4:lib-support-v4'),
react_native_dep('third-party/java/infer-annotations:infer-annotations'),
react_native_dep('third-party/java/jsr-305:jsr-305'),
react_native_dep('third-party/java/junit:junit'),
react_native_dep('third-party/java/mockito:mockito'),
react_native_dep('third-party/java/testing-support-lib:runner'),
react_native_target('java/com/facebook/react/bridge:bridge'),
react_native_target('java/com/facebook/react/common:common'),
react_native_target('java/com/facebook/react/devsupport:devsupport'),
react_native_target('java/com/facebook/react/modules/core:core'),
react_native_target('java/com/facebook/react/modules/debug:debug'),
react_native_target('java/com/facebook/react/shell:shell'),
react_native_target('java/com/facebook/react/uimanager:uimanager'),
react_native_target('java/com/facebook/react:react'),
],
visibility = [
'PUBLIC',

View File

@ -30,10 +30,12 @@ import com.facebook.react.bridge.SoftAssertions;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.common.ApplicationHolder;
import com.facebook.react.common.futures.SimpleSettableFuture;
import com.facebook.react.devsupport.DevSupportManager;
import com.facebook.react.modules.core.Timing;
import com.facebook.soloader.SoLoader;
import static org.mockito.Mockito.*;
/**
* Use this class for writing integration tests of catalyst. This class will run all JNI call
@ -137,7 +139,7 @@ public abstract class ReactIntegrationTestCase extends AndroidTestCase {
new Runnable() {
@Override
public void run() {
Timing timing = new Timing(getContext());
Timing timing = new Timing(getContext(), mock(DevSupportManager.class));
simpleSettableFuture.set(timing);
}
});

View File

@ -80,7 +80,7 @@ import com.facebook.systrace.Systrace;
new AndroidInfoModule(),
new DeviceEventManagerModule(catalystApplicationContext, mHardwareBackBtnHandler),
new ExceptionsManagerModule(mReactInstanceManager.getDevSupportManager()),
new Timing(catalystApplicationContext),
new Timing(catalystApplicationContext, mReactInstanceManager.getDevSupportManager()),
new SourceCodeModule(mReactInstanceManager.getSourceUrl()),
uiManagerModule,
new JSCHeapCapture(catalystApplicationContext),

View File

@ -30,6 +30,7 @@ import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.common.SystemClock;
import com.facebook.react.devsupport.DevSupportManager;
import com.facebook.react.uimanager.ReactChoreographer;
/**
@ -38,6 +39,8 @@ import com.facebook.react.uimanager.ReactChoreographer;
public final class Timing extends ReactContextBaseJavaModule implements LifecycleEventListener,
OnExecutorUnregisteredListener {
private final DevSupportManager mDevSupportManager;
private static class Timer {
private final ExecutorToken mExecutorToken;
@ -112,8 +115,9 @@ public final class Timing extends ReactContextBaseJavaModule implements Lifecycl
private @Nullable ReactChoreographer mReactChoreographer;
private boolean mFrameCallbackPosted = false;
public Timing(ReactApplicationContext reactContext) {
public Timing(ReactApplicationContext reactContext, DevSupportManager devSupportManager) {
super(reactContext);
mDevSupportManager = devSupportManager;
// We store timers sorted by finish time.
mTimers = new PriorityQueue<Timer>(
11, // Default capacity: for some reason they don't expose a (Comparator) constructor
@ -213,6 +217,12 @@ public final class Timing extends ReactContextBaseJavaModule implements Lifecycl
final int duration,
final double jsSchedulingTime,
final boolean repeat) {
// If the times on the server and device have drifted throw an exception to warn the developer
// that things might not work or results may not be accurate. This is required only for
// developer builds.
if (mDevSupportManager.getDevSupportEnabled() && Math.abs(jsSchedulingTime - System.currentTimeMillis()) > 1000) {
throw new RuntimeException("System and emulator/device times have drifted. Please correct this by running adb shell \"date `date +%m%d%H%M%Y.%S`\" on your dev machine");
}
// Adjust for the amount of time it took for native to receive the timer registration call
long adjustedDuration = (long) Math.max(
0,

View File

@ -6,6 +6,7 @@ robolectric3_test(
name = 'modules',
srcs = glob(['**/*.java']),
deps = [
react_native_dep('java/com/facebook/catalyst/js/react-native-github/ReactAndroid/src/main/java/com/facebook/react/devsupport:devsupport'),
react_native_dep('libraries/fbcore/src/test/java/com/facebook/powermock:powermock'),
react_native_dep('third-party/java/fest:fest'),
react_native_dep('third-party/java/junit:junit'),

View File

@ -16,6 +16,7 @@ import com.facebook.react.bridge.ExecutorToken;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.CatalystInstance;
import com.facebook.react.bridge.JavaOnlyArray;
import com.facebook.react.devsupport.DevSupportManager;
import com.facebook.react.uimanager.ReactChoreographer;
import com.facebook.react.common.SystemClock;
import com.facebook.react.modules.core.JSTimersExecution;
@ -92,7 +93,7 @@ public class TimingModuleTest {
eq(ReactChoreographer.CallbackType.TIMERS_EVENTS),
any(Choreographer.FrameCallback.class));
mTiming = new Timing(reactContext);
mTiming = new Timing(reactContext, mock(DevSupportManager.class));
mJSTimersMock = mock(JSTimersExecution.class);
mExecutorTokenMock = mock(ExecutorToken.class);
when(reactContext.getJSModule(mExecutorTokenMock, JSTimersExecution.class)).thenReturn(mJSTimersMock);