Skip to content

Commit

Permalink
Fix flaky BroadcastReceiverSystemInfoProvider test
Browse files Browse the repository at this point in the history
  • Loading branch information
0xnm committed Dec 12, 2022
1 parent b627277 commit 18942b2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import android.os.PowerManager
import com.datadog.android.core.internal.receiver.ThreadSafeReceiver
import com.datadog.android.core.internal.utils.sdkLogger
import com.datadog.android.log.internal.utils.debugWithTelemetry
import kotlin.math.roundToInt

internal class BroadcastReceiverSystemInfoProvider(
private val buildSdkVersionProvider: BuildSdkVersionProvider = DefaultBuildSdkVersionProvider()
Expand Down Expand Up @@ -79,7 +80,9 @@ internal class BroadcastReceiverSystemInfoProvider(
val scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, DEFAULT_BATTERY_SCALE)
val pluggedStatus = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, BATTERY_UNPLUGGED)
val batteryStatus = SystemInfo.BatteryStatus.fromAndroidStatus(status)
val batteryLevel = (level * DEFAULT_BATTERY_SCALE) / scale

@Suppress("UnsafeThirdPartyFunctionCall") // Not a NaN here
val batteryLevel = ((level * DEFAULT_BATTERY_SCALE.toFloat()) / scale).roundToInt()
val onExternalPowerSource = pluggedStatus in PLUGGED_IN_STATUS_VALUES
val batteryFullOrCharging = batteryStatus in batteryFullOrChargingStatus
systemInfo = systemInfo.copy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import org.mockito.junit.jupiter.MockitoSettings
import org.mockito.quality.Strictness
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import kotlin.math.roundToInt

@Extensions(
ExtendWith(MockitoExtension::class),
Expand Down Expand Up @@ -129,7 +130,7 @@ internal class BroadcastReceiverSystemInfoProviderTest {
whenever(mockBuildSdkVersionProvider.version()) doReturn Build.VERSION_CODES.LOLLIPOP

val batteryIntent: Intent = mock()
val scaledLevel = (level * scale) / 100
val scaledLevel = ((level * scale) / 100f).roundToInt()
whenever(batteryIntent.getIntExtra(eq(BatteryManager.EXTRA_STATUS), any()))
.doReturn(status.androidStatus())
whenever(batteryIntent.getIntExtra(eq(BatteryManager.EXTRA_PLUGGED), any()))
Expand Down Expand Up @@ -166,7 +167,7 @@ internal class BroadcastReceiverSystemInfoProviderTest {
whenever(mockBuildSdkVersionProvider.version()) doReturn Build.VERSION_CODES.KITKAT

val batteryIntent: Intent = mock()
val scaledLevel = (level * scale) / 100
val scaledLevel = ((level * scale) / 100f).roundToInt()
whenever(batteryIntent.getIntExtra(eq(BatteryManager.EXTRA_STATUS), any()))
.doReturn(status.androidStatus())
whenever(batteryIntent.getIntExtra(eq(BatteryManager.EXTRA_PLUGGED), any()))
Expand Down Expand Up @@ -221,7 +222,7 @@ internal class BroadcastReceiverSystemInfoProviderTest {
@IntForgery(min = 50, max = 10000) scale: Int
) {
// Given
val scaledLevel = (level * scale) / 100
val scaledLevel = ((level * scale) / 100f).roundToInt()
whenever(mockIntent.getIntExtra(eq(BatteryManager.EXTRA_STATUS), any()))
.doReturn(status.androidStatus())
whenever(mockIntent.getIntExtra(eq(BatteryManager.EXTRA_LEVEL), any())) doReturn scaledLevel
Expand Down Expand Up @@ -337,7 +338,7 @@ internal class BroadcastReceiverSystemInfoProviderTest {
@IntForgery(min = 50, max = 10000) scale: Int
) {
// Given
val scaledLevel = (level * scale) / 100
val scaledLevel = ((level * scale) / 100f).roundToInt()
val batteryIntent: Intent = mock()
whenever(
batteryIntent.getIntExtra(
Expand Down Expand Up @@ -371,7 +372,7 @@ internal class BroadcastReceiverSystemInfoProviderTest {
@IntForgery(min = 50, max = 10000) scale: Int
) {
// Given
val scaledLevel = (level * scale) / 100
val scaledLevel = ((level * scale) / 100f).roundToInt()
val batteryIntent: Intent = mock()
whenever(
batteryIntent.getIntExtra(
Expand Down Expand Up @@ -409,7 +410,7 @@ internal class BroadcastReceiverSystemInfoProviderTest {
@IntForgery(min = 50, max = 10000) scale: Int
) {
// Given
val scaledLevel = (level * scale) / 100
val scaledLevel = ((level * scale) / 100f).roundToInt()
val batteryIntent: Intent = mock()
whenever(batteryIntent.getIntExtra(eq(BatteryManager.EXTRA_SCALE), any())) doReturn scale
whenever(batteryIntent.getIntExtra(eq(BatteryManager.EXTRA_LEVEL), any())) doReturn
Expand All @@ -435,7 +436,7 @@ internal class BroadcastReceiverSystemInfoProviderTest {
@IntForgery(min = 50, max = 10000) scale: Int
) {
// Given
val scaledLevel = (level * scale) / 100
val scaledLevel = ((level * scale) / 100f).roundToInt()
val batteryIntent: Intent = mock()
whenever(batteryIntent.getIntExtra(eq(BatteryManager.EXTRA_SCALE), any())) doReturn scale
whenever(batteryIntent.getIntExtra(eq(BatteryManager.EXTRA_LEVEL), any())) doReturn
Expand Down

0 comments on commit 18942b2

Please sign in to comment.