Skip to content

Commit 42e5b8a

Browse files
authored
Avoid throwing in ForcedSender.sendBlocking (#4293)
* Make ForcedSender.sendBlocking do nothing, instead of throw, when given a wrong Transport type. This will make it easier to mock Transport in tests. This method is best-effort anyway, so doing nothing instead of throwing is fine. * Log warning when given wrong type.
1 parent 66b067d commit 42e5b8a

File tree

1 file changed

+9
-8
lines changed
  • transport/transport-runtime/src/main/java/com/google/android/datatransport/runtime

1 file changed

+9
-8
lines changed

transport/transport-runtime/src/main/java/com/google/android/datatransport/runtime/ForcedSender.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,24 @@
1919
import androidx.annotation.WorkerThread;
2020
import com.google.android.datatransport.Priority;
2121
import com.google.android.datatransport.Transport;
22+
import com.google.android.datatransport.runtime.logging.Logging;
2223

2324
@Discouraged(
2425
message =
2526
"TransportRuntime is not a realtime delivery system, don't use unless you absolutely must.")
2627
public final class ForcedSender {
28+
private static final String LOG_TAG = "ForcedSender";
29+
30+
@SuppressLint("DiscouragedApi")
2731
@WorkerThread
2832
public static void sendBlocking(Transport<?> transport, Priority priority) {
29-
@SuppressLint("DiscouragedApi")
30-
TransportContext context = getTransportContextOrThrow(transport).withPriority(priority);
31-
TransportRuntime.getInstance().getUploader().logAndUpdateState(context, 1);
32-
}
33-
34-
private static TransportContext getTransportContextOrThrow(Transport<?> transport) {
3533
if (transport instanceof TransportImpl) {
36-
return ((TransportImpl<?>) transport).getTransportContext();
34+
TransportContext context =
35+
((TransportImpl<?>) transport).getTransportContext().withPriority(priority);
36+
TransportRuntime.getInstance().getUploader().logAndUpdateState(context, 1);
37+
} else {
38+
Logging.w(LOG_TAG, "Expected instance of `TransportImpl`, got `%s`.", transport);
3739
}
38-
throw new IllegalArgumentException("Expected instance of TransportImpl.");
3940
}
4041

4142
private ForcedSender() {}

0 commit comments

Comments
 (0)