Skip to content

Commit 585f099

Browse files
authored
fix(EventRescheduler): catch exception for event service restart (#446)
1 parent 67a3008 commit 585f099

File tree

3 files changed

+48
-100
lines changed

3 files changed

+48
-100
lines changed

event-handler/src/test/java/com/optimizely/ab/android/event_handler/EventReschedulerTest.java renamed to event-handler/src/androidTest/java/com/optimizely/ab/android/event_handler/EventReschedulerTest.java

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
/****************************************************************************
2-
* Copyright 2016,2021, Optimizely, Inc. and contributors *
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-
***************************************************************************/
1+
// Copyright 2016,2021,2023, Optimizely, Inc. and contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
1614

1715
package com.optimizely.ab.android.event_handler;
1816

1917
import android.content.Context;
2018
import android.content.Intent;
19+
import android.net.NetworkInfo;
2120
import android.net.wifi.WifiManager;
2221

2322
import org.junit.Before;
@@ -26,15 +25,23 @@
2625
import org.junit.runner.RunWith;
2726
import org.mockito.runners.MockitoJUnitRunner;
2827
import org.slf4j.Logger;
28+
29+
import static org.mockito.Matchers.any;
30+
import static org.mockito.Matchers.matches;
31+
import static org.mockito.Mockito.doNothing;
32+
import static org.mockito.Mockito.doReturn;
33+
import static org.mockito.Mockito.doThrow;
2934
import static org.mockito.Mockito.mock;
35+
import static org.mockito.Mockito.spy;
3036
import static org.mockito.Mockito.verify;
3137
import static org.mockito.Mockito.when;
3238

39+
import androidx.test.ext.junit.runners.AndroidJUnit4;
40+
3341
/**
3442
* Unit tests for {@link EventRescheduler}
3543
*/
36-
@RunWith(MockitoJUnitRunner.class)
37-
@Ignore
44+
@RunWith(AndroidJUnit4.class)
3845
public class EventReschedulerTest {
3946

4047
private Context context;
@@ -47,8 +54,7 @@ public void setupEventRescheduler() {
4754
context = mock(Context.class);
4855
intent = mock(Intent.class);
4956
logger = mock(Logger.class);
50-
rescheduler = mock(EventRescheduler.class);
51-
rescheduler = new EventRescheduler();
57+
rescheduler = spy(new EventRescheduler());
5258
rescheduler.logger = logger;
5359
}
5460

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

80+
@Test
81+
public void onReceiveWhenRescheduleWithException() {
82+
when(intent.getAction()).thenThrow(new IllegalStateException());
83+
rescheduler.onReceive(context, intent);
84+
verify(logger).warn(matches("WorkScheduler failed to reschedule an event service.*"));
85+
}
86+
7487
@Test
7588
public void onReceiveValidBootComplete() {
7689
when(intent.getAction()).thenReturn(Intent.ACTION_BOOT_COMPLETED);
@@ -88,10 +101,12 @@ public void onReceiveValidPackageReplaced() {
88101
@Test
89102
public void flushOnWifiConnectionIfScheduled() {
90103
final Intent eventServiceIntent = mock(Intent.class);
91-
when(intent.getAction()).thenReturn(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
92-
when(intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)).thenReturn(true);
104+
when(intent.getAction()).thenReturn(WifiManager.WIFI_STATE_CHANGED_ACTION);
105+
NetworkInfo info = mock(NetworkInfo.class);
106+
when(info.isConnected()).thenReturn(true);
107+
when(intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO)).thenReturn(info);
108+
93109
rescheduler.reschedule(context, intent);
94-
verify(context).startService(eventServiceIntent);
95110
verify(logger).info("Preemptively flushing events since wifi became available");
96111
}
97112
}

event-handler/src/main/java/com/optimizely/ab/android/event_handler/EventRescheduler.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************
2-
* Copyright 2016-2021, Optimizely, Inc. and contributors *
2+
* Copyright 2016-2021, 2023 Optimizely, Inc. and contributors *
33
* *
44
* Licensed under the Apache License, Version 2.0 (the "License"); *
55
* you may not use this file except in compliance with the License. *
@@ -62,10 +62,17 @@ public class EventRescheduler extends BroadcastReceiver {
6262
*/
6363
@Override
6464
public void onReceive(Context context, Intent intent) {
65-
if (context != null && intent != null) {
66-
reschedule(context, intent);
67-
} else {
65+
if (context == null || intent == null) {
6866
logger.warn("Received invalid broadcast to event rescheduler");
67+
return;
68+
}
69+
70+
try {
71+
reschedule(context, intent);
72+
} catch (Exception e) {
73+
// Rare exceptions (IllegalStateException: "WorkManager is not initialized properly...") with WorkerScheduler.startService(), probably related to a WorkManager start timing issue.
74+
// Gracefully handled here, and it's safe for those rare cases since event-dispatch service will be scheduled again on next events.
75+
logger.warn("WorkScheduler failed to reschedule an event service: " + e.getMessage());
6976
}
7077
}
7178

event-handler/src/test/java/com/optimizely/ab/android/event_handler/DefaultEventHandlerTest.java

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)