Skip to content

Commit 88fb6b1

Browse files
authored
Merge pull request #185 from backtrace-labs/fix-anr-settings-timout
Fix default timeout in BacktraceANRSettings
2 parents 8ef7d57 + a8db39b commit 88fb6b1

3 files changed

Lines changed: 102 additions & 13 deletions

File tree

backtrace-library/src/main/java/backtraceio/library/anr/BacktraceANRSettings.java

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,80 @@
22

33
import backtraceio.library.watchdog.OnApplicationNotRespondingEvent;
44

5+
6+
/**
7+
* Configuration settings for Application Not Responding (ANR) detection.
8+
* This class allows customization of ANR monitoring behavior, such as the timeout duration
9+
* and callback events.
10+
*/
511
public class BacktraceANRSettings {
6-
private int timeout = 0;
7-
private boolean debug = false;
8-
private OnApplicationNotRespondingEvent onApplicationNotRespondingEvent = null;
12+
/**
13+
* Default timeout value in milliseconds
14+
*/
15+
public final static int DEFAULT_ANR_TIMEOUT = 5000;
16+
17+
/**
18+
* The timeout in milliseconds after which an ANR is reported if the main thread is blocked.
19+
*/
20+
private final int timeout;
21+
22+
/**
23+
* Flag to enable or disable additional debug logging for ANR detection.
24+
* When true, more verbose logging related to ANR monitoring might be produced.
25+
*/
26+
private final boolean debug;
927

10-
public BacktraceANRSettings() { }
28+
/**
29+
* Callback interface to be invoked when an Application Not Responding event is detected.
30+
* This allows custom handling of ANR events.
31+
*/
32+
private final OnApplicationNotRespondingEvent onApplicationNotRespondingEvent;
33+
34+
/**
35+
* Default constructor.
36+
* Initializes ANR settings with default values.
37+
*/
38+
public BacktraceANRSettings() {
39+
this(DEFAULT_ANR_TIMEOUT, null, false);
40+
}
1141

42+
/**
43+
* Constructs ANR settings with specified parameters.
44+
*
45+
* @param timeout The timeout in milliseconds for ANR detection.
46+
* @param onApplicationNotRespondingEvent The callback to be invoked when an ANR is detected.
47+
* Can be null if no custom callback is needed.
48+
* @param debug True to enable debug logging for ANR detection, false otherwise.
49+
*/
1250
public BacktraceANRSettings(int timeout, OnApplicationNotRespondingEvent onApplicationNotRespondingEvent, boolean debug) {
13-
super();
14-
this.debug = debug;
15-
this.onApplicationNotRespondingEvent = onApplicationNotRespondingEvent;
1651
this.timeout = timeout;
52+
this.onApplicationNotRespondingEvent = onApplicationNotRespondingEvent;
53+
this.debug = debug;
1754
}
1855

56+
/**
57+
* Gets the configured ANR timeout in milliseconds.
58+
*
59+
* @return The timeout in milliseconds.
60+
*/
1961
public int getTimeout() {
2062
return timeout;
2163
}
2264

65+
/**
66+
* Checks if debug logging for ANR detection is enabled.
67+
*
68+
* @return True if debug logging is enabled, false otherwise.
69+
*/
2370
public boolean isDebug() {
2471
return debug;
2572
}
2673

74+
/**
75+
* Gets the configured callback for ANR events.
76+
*
77+
* @return The {@link OnApplicationNotRespondingEvent} callback, or null if none is set.
78+
*/
2779
public OnApplicationNotRespondingEvent getOnApplicationNotRespondingEvent() {
2880
return onApplicationNotRespondingEvent;
2981
}

backtrace-library/src/main/java/backtraceio/library/watchdog/BacktraceANRHandlerWatchdog.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import backtraceio.library.BacktraceClient;
1010
import backtraceio.library.anr.BacktraceANRHandler;
11+
import backtraceio.library.anr.BacktraceANRSettings;
1112
import backtraceio.library.logger.BacktraceLogger;
1213

1314

@@ -19,11 +20,6 @@ public class BacktraceANRHandlerWatchdog extends Thread implements BacktraceANRH
1920

2021
private final static String LOG_TAG = BacktraceANRHandlerWatchdog.class.getSimpleName();
2122

22-
/**
23-
* Default timeout value in milliseconds
24-
*/
25-
private final static int DEFAULT_ANR_TIMEOUT = 5000;
26-
2723
/**
2824
* Current Backtrace client instance which will be used to send information about exception
2925
*/
@@ -60,7 +56,7 @@ public class BacktraceANRHandlerWatchdog extends Thread implements BacktraceANRH
6056
* @param client current Backtrace client instance which will be used to send information about exception
6157
*/
6258
public BacktraceANRHandlerWatchdog(BacktraceClient client) {
63-
this(client, DEFAULT_ANR_TIMEOUT);
59+
this(client, BacktraceANRSettings.DEFAULT_ANR_TIMEOUT);
6460
}
6561

6662
/**
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package backtraceio.library.anr;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertNull;
6+
7+
import org.junit.Test;
8+
9+
import backtraceio.library.watchdog.BacktraceWatchdogTimeoutException;
10+
import backtraceio.library.watchdog.OnApplicationNotRespondingEvent;
11+
12+
public class BacktraceANRSettingsTest {
13+
14+
@Test
15+
public void defaultConstructor() {
16+
// WHEN
17+
BacktraceANRSettings settings = new BacktraceANRSettings();
18+
// THEN
19+
assertEquals(BacktraceANRSettings.DEFAULT_ANR_TIMEOUT, settings.getTimeout());
20+
assertNull(settings.getOnApplicationNotRespondingEvent());
21+
assertFalse(settings.isDebug());
22+
}
23+
24+
@Test
25+
public void paramsConstructor() {
26+
// GIVEN
27+
int timeout = 2000;
28+
boolean debug = true;
29+
OnApplicationNotRespondingEvent event = exception -> {
30+
31+
};
32+
// WHEN
33+
BacktraceANRSettings settings = new BacktraceANRSettings(timeout, event, debug);
34+
35+
// THEN
36+
assertEquals(timeout, settings.getTimeout());
37+
assertEquals(event, settings.getOnApplicationNotRespondingEvent());
38+
assertEquals(debug, settings.isDebug());
39+
}
40+
41+
}

0 commit comments

Comments
 (0)