Skip to content

Commit

Permalink
fix(EventRescheduler): catch exception for event service restart (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeopt authored Feb 3, 2023
1 parent 67a3008 commit 585f099
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
/****************************************************************************
* Copyright 2016,2021, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/
// Copyright 2016,2021,2023, Optimizely, Inc. and contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.optimizely.ab.android.event_handler;

import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;

import org.junit.Before;
Expand All @@ -26,15 +25,23 @@
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import org.slf4j.Logger;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.matches;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import androidx.test.ext.junit.runners.AndroidJUnit4;

/**
* Unit tests for {@link EventRescheduler}
*/
@RunWith(MockitoJUnitRunner.class)
@Ignore
@RunWith(AndroidJUnit4.class)
public class EventReschedulerTest {

private Context context;
Expand All @@ -47,8 +54,7 @@ public void setupEventRescheduler() {
context = mock(Context.class);
intent = mock(Intent.class);
logger = mock(Logger.class);
rescheduler = mock(EventRescheduler.class);
rescheduler = new EventRescheduler();
rescheduler = spy(new EventRescheduler());
rescheduler.logger = logger;
}

Expand All @@ -71,6 +77,13 @@ public void onReceiveInvalidAction() {
verify(logger).warn("Received unsupported broadcast action to event rescheduler");
}

@Test
public void onReceiveWhenRescheduleWithException() {
when(intent.getAction()).thenThrow(new IllegalStateException());
rescheduler.onReceive(context, intent);
verify(logger).warn(matches("WorkScheduler failed to reschedule an event service.*"));
}

@Test
public void onReceiveValidBootComplete() {
when(intent.getAction()).thenReturn(Intent.ACTION_BOOT_COMPLETED);
Expand All @@ -88,10 +101,12 @@ public void onReceiveValidPackageReplaced() {
@Test
public void flushOnWifiConnectionIfScheduled() {
final Intent eventServiceIntent = mock(Intent.class);
when(intent.getAction()).thenReturn(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
when(intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)).thenReturn(true);
when(intent.getAction()).thenReturn(WifiManager.WIFI_STATE_CHANGED_ACTION);
NetworkInfo info = mock(NetworkInfo.class);
when(info.isConnected()).thenReturn(true);
when(intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO)).thenReturn(info);

rescheduler.reschedule(context, intent);
verify(context).startService(eventServiceIntent);
verify(logger).info("Preemptively flushing events since wifi became available");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2016-2021, Optimizely, Inc. and contributors *
* Copyright 2016-2021, 2023 Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand Down Expand Up @@ -62,10 +62,17 @@ public class EventRescheduler extends BroadcastReceiver {
*/
@Override
public void onReceive(Context context, Intent intent) {
if (context != null && intent != null) {
reschedule(context, intent);
} else {
if (context == null || intent == null) {
logger.warn("Received invalid broadcast to event rescheduler");
return;
}

try {
reschedule(context, intent);
} catch (Exception e) {
// Rare exceptions (IllegalStateException: "WorkManager is not initialized properly...") with WorkerScheduler.startService(), probably related to a WorkManager start timing issue.
// Gracefully handled here, and it's safe for those rare cases since event-dispatch service will be scheduled again on next events.
logger.warn("WorkScheduler failed to reschedule an event service: " + e.getMessage());
}
}

Expand Down

This file was deleted.

0 comments on commit 585f099

Please sign in to comment.