|
| 1 | +package com.instabug.react.example; |
| 2 | + |
| 3 | +import static com.instabug.reactlibrary.utils.InstabugUtil.getMethod; |
| 4 | + |
| 5 | +import com.facebook.react.bridge.Promise; |
| 6 | +import com.facebook.react.bridge.ReactApplicationContext; |
| 7 | +import com.facebook.react.bridge.ReactContextBaseJavaModule; |
| 8 | +import com.facebook.react.bridge.ReactMethod; |
| 9 | +import com.instabug.crash.CrashReporting; |
| 10 | +import com.instabug.crash.models.IBGNonFatalException; |
| 11 | +import com.instabug.library.Feature; |
| 12 | +import com.instabug.reactlibrary.RNInstabugReactnativeModule; |
| 13 | +import com.instabug.reactlibrary.utils.MainThreadHandler; |
| 14 | + |
| 15 | +import org.json.JSONObject; |
| 16 | + |
| 17 | +import java.lang.reflect.InvocationTargetException; |
| 18 | +import java.lang.reflect.Method; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Random; |
| 22 | + |
| 23 | +import javax.annotation.Nonnull; |
| 24 | +import javax.annotation.Nullable; |
| 25 | + |
| 26 | +public class RNInstabugExampleCrashReportingModule extends ReactContextBaseJavaModule { |
| 27 | + |
| 28 | + public RNInstabugExampleCrashReportingModule(ReactApplicationContext reactApplicationContext) { |
| 29 | + super(reactApplicationContext); |
| 30 | + } |
| 31 | + |
| 32 | + @Nonnull |
| 33 | + @Override |
| 34 | + public String getName() { |
| 35 | + return "CrashReportingExampleModule"; |
| 36 | + } |
| 37 | + |
| 38 | + @ReactMethod |
| 39 | + public void sendNativeNonFatal(final String exceptionObject) { |
| 40 | + final IBGNonFatalException exception = new IBGNonFatalException.Builder(new IllegalStateException("Test exception")) |
| 41 | + .build(); |
| 42 | + CrashReporting.report(exception); |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + @ReactMethod |
| 47 | + public void sendNativeFatalCrash() { |
| 48 | + throw new IllegalStateException("Unhandled IllegalStateException from Instabug Test App"); |
| 49 | + } |
| 50 | + |
| 51 | + @ReactMethod |
| 52 | + public void sendANR() { |
| 53 | + try { |
| 54 | + Thread.sleep(20000); |
| 55 | + } catch (InterruptedException e) { |
| 56 | + throw new RuntimeException(e); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @ReactMethod |
| 61 | + public void sendFatalHang() { |
| 62 | + try { |
| 63 | + Thread.sleep(3000); |
| 64 | + } catch (InterruptedException e) { |
| 65 | + throw new RuntimeException(e); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @ReactMethod |
| 70 | + public void sendOOM() { |
| 71 | + oomCrash(); |
| 72 | + } |
| 73 | + |
| 74 | + private void oomCrash() { |
| 75 | + new Thread(() -> { |
| 76 | + List<String> stringList = new ArrayList<>(); |
| 77 | + for (int i = 0; i < 1_000_000; i++) { |
| 78 | + stringList.add(getRandomString(10_000)); |
| 79 | + } |
| 80 | + }).start(); |
| 81 | + } |
| 82 | + |
| 83 | + private String getRandomString(int length) { |
| 84 | + List<Character> charset = new ArrayList<>(); |
| 85 | + for (char ch = 'a'; ch <= 'z'; ch++) { |
| 86 | + charset.add(ch); |
| 87 | + } |
| 88 | + for (char ch = 'A'; ch <= 'Z'; ch++) { |
| 89 | + charset.add(ch); |
| 90 | + } |
| 91 | + for (char ch = '0'; ch <= '9'; ch++) { |
| 92 | + charset.add(ch); |
| 93 | + } |
| 94 | + |
| 95 | + StringBuilder randomString = new StringBuilder(); |
| 96 | + Random random = new Random(); |
| 97 | + for (int i = 0; i < length; i++) { |
| 98 | + char randomChar = charset.get(random.nextInt(charset.size())); |
| 99 | + randomString.append(randomChar); |
| 100 | + } |
| 101 | + |
| 102 | + return randomString.toString(); |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments