This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
15278 detekt rule runblocking #15942
Merged
MarcLeclair
merged 19 commits into
mozilla-mobile:master
from
MarcLeclair:15278-detekt-rule-runblocking
Nov 2, 2020
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
266786b
For #15278: added CoroutineManager to count runBlocking calls
MarcLeclair da7b1ed
For #15278: Added actual detekt rule for runblocking and its config t…
MarcLeclair 1214bb6
For #15278: Added unit test for RunblockingCounter
MarcLeclair 3fd69e2
For #15278: renamed StrictModeStartupSuppressionCountTest.kt to Perfo…
MarcLeclair d526980
Lint fix
MarcLeclair d1db99f
For #15278: made runblocking a Long to prevent overflow
MarcLeclair 5f1c747
For #15278: fixed MozRunblocking name, description and moved RunBlock…
MarcLeclair c277469
For #15278:Renamed MozillaRunblockingCheck to MozillaRunBlockingCheck
MarcLeclair de6a96d
For #15278: Added setup for unit test, since it failed without restti…
MarcLeclair 745e9af
For #15278: Fixed naming for RunBlocking lint check
MarcLeclair da0f4c6
For #15278: removed changes made to test to use runBlockingIncrement
MarcLeclair bb5636d
For #15728: added test exclusion for runBlocking check
MarcLeclair a121a9b
For #15278: changed null check and added Synchronized to count setter
MarcLeclair 8d63ebc
For #15278: fix for nits
MarcLeclair 36fc3e1
For #15278: added StartupExcessiveResourceUseTest to CODEOWNERS
MarcLeclair 676fb6b
For #15278: fixed for nits
MarcLeclair 5f2da80
For #15278: Moved increment function to extension function and fixed …
MarcLeclair b265a37
For #15278: Added tests for Atomic Integer extension and nit fix
MarcLeclair e0be45d
merged master
MarcLeclair File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.fenix.ext | ||
|
||
import java.util.concurrent.atomic.AtomicInteger | ||
|
||
/** | ||
* Increases an AtomicInteger safely. | ||
*/ | ||
fun AtomicInteger.getAndIncrementNoOverflow() { | ||
var prev: Int | ||
var next: Int | ||
do { | ||
prev = this.get() | ||
next = if (prev == Integer.MAX_VALUE) prev else prev + 1 | ||
} while (!this.compareAndSet(prev, next)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
app/src/main/java/org/mozilla/fenix/perf/RunBlockingCounter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
// This class implements the alternative ways to invoke runBlocking with some | ||
// monitoring by wrapping the raw methods. This lint check tells us not to use the raw | ||
// methods so we suppress the check. | ||
@file:Suppress("MozillaRunBlockingCheck") | ||
|
||
package org.mozilla.fenix.perf | ||
|
||
import androidx.annotation.VisibleForTesting | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.runBlocking | ||
import org.mozilla.fenix.ext.getAndIncrementNoOverflow | ||
import java.util.concurrent.atomic.AtomicInteger | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
/** | ||
* Counts the number of runBlocking calls made | ||
*/ | ||
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) | ||
object RunBlockingCounter { | ||
val count = AtomicInteger(0) | ||
} | ||
|
||
/** | ||
* Wrapper around `runBlocking`. RunBlocking seems to be a "fix-all" to return values to the thread | ||
* where the coroutine is called. The official doc explains runBlocking: "Runs a new coroutine and | ||
* blocks the current thread interruptibly until its completion`. This can block our main thread | ||
* which could lead to significant jank. This wrapper aims to count the number of runBlocking call | ||
* to try to limit them as much as possible to encourage alternatives solutions whenever this function | ||
* might be needed. | ||
*/ | ||
fun <T> runBlockingIncrement( | ||
context: CoroutineContext? = null, | ||
action: suspend CoroutineScope.() -> T | ||
): T { | ||
RunBlockingCounter.count.getAndIncrementNoOverflow() | ||
return if (context != null) { | ||
runBlocking(context) { action() } | ||
} else { | ||
runBlocking { action() } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you read the comment? 😝 Can you add this to code owners?