This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #4299 Add Mechanism to Keep Higher Priority Breadcrumbs if Max…
… Number of Breadcrumb is recorded
- Loading branch information
1 parent
1b7ee68
commit 11166f3
Showing
8 changed files
with
189 additions
and
85 deletions.
There are no files selected for viewing
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
29 changes: 29 additions & 0 deletions
29
components/lib/crash/src/main/java/mozilla/components/lib/crash/BreadcrumbPriorityQueue.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,29 @@ | ||
package mozilla.components.lib.crash | ||
|
||
import java.util.PriorityQueue | ||
import kotlin.collections.ArrayList | ||
|
||
internal class BreadcrumbPriorityQueue( | ||
private val maxSize: Int | ||
) : PriorityQueue<Breadcrumb>() { | ||
@Synchronized | ||
override fun add(element: Breadcrumb?): Boolean { | ||
val result = super.add(element) | ||
if (this.size > maxSize) { | ||
this.poll() | ||
} | ||
return result | ||
} | ||
|
||
@Synchronized | ||
fun toSortedArrayList(): ArrayList<Breadcrumb> { | ||
val breadcrumbsArrayList: ArrayList<Breadcrumb> = arrayListOf() | ||
if (isNotEmpty()) { | ||
breadcrumbsArrayList.addAll(this) | ||
|
||
/* Sort by timestamp before reporting */ | ||
breadcrumbsArrayList.sortBy { it.date } | ||
} | ||
return breadcrumbsArrayList | ||
} | ||
} |
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
121 changes: 121 additions & 0 deletions
121
...nents/lib/crash/src/test/java/mozilla/components/lib/crash/BreadcrumbPriorityQueueTest.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,121 @@ | ||
/* 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 mozilla.components.lib.crash | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import java.lang.Thread.sleep | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class BreadcrumbPriorityQueueTest { | ||
|
||
@Test | ||
fun `Breadcrumb priority queue stores only max number of breadcrumbs`() { | ||
val testMessage = "test_Message" | ||
val testData = hashMapOf("1" to "one", "2" to "two") | ||
val testCategory = "testing_category" | ||
val testLevel = Breadcrumb.Level.CRITICAL | ||
val testType = Breadcrumb.Type.USER | ||
var crashBreadcrumbs = BreadcrumbPriorityQueue(5) | ||
repeat(10) { | ||
crashBreadcrumbs.add(Breadcrumb(testMessage, testData, testCategory, testLevel, testType)) | ||
} | ||
assertEquals(crashBreadcrumbs.size, 5) | ||
|
||
crashBreadcrumbs = BreadcrumbPriorityQueue(10) | ||
repeat(15) { | ||
crashBreadcrumbs.add(Breadcrumb(testMessage, testData, testCategory, testLevel, testType)) | ||
} | ||
assertEquals(crashBreadcrumbs.size, 10) | ||
} | ||
|
||
@Test | ||
fun `Breadcrumb priority queue stores the correct priority`() { | ||
val testMessage = "test_Message" | ||
val testData = hashMapOf("1" to "one", "2" to "two") | ||
val testCategory = "testing_category" | ||
val testType = Breadcrumb.Type.USER | ||
val maxNum = 5 | ||
var crashBreadcrumbs = BreadcrumbPriorityQueue(maxNum) | ||
|
||
repeat(maxNum) { | ||
crashBreadcrumbs.add( | ||
Breadcrumb(testMessage, testData, testCategory, Breadcrumb.Level.DEBUG, testType) | ||
) | ||
} | ||
assertEquals(crashBreadcrumbs.size, maxNum) | ||
|
||
repeat(maxNum) { | ||
crashBreadcrumbs.add( | ||
Breadcrumb(testMessage, testData, testCategory, Breadcrumb.Level.INFO, testType) | ||
) | ||
} | ||
|
||
for (i in 0 until maxNum) { | ||
assertEquals(crashBreadcrumbs.elementAt(i).level, Breadcrumb.Level.INFO) | ||
} | ||
|
||
repeat(maxNum) { | ||
crashBreadcrumbs.add( | ||
Breadcrumb(testMessage, testData, testCategory, Breadcrumb.Level.DEBUG, testType) | ||
) | ||
} | ||
|
||
for (i in 0 until maxNum) { | ||
assertEquals(crashBreadcrumbs.elementAt(i).level, Breadcrumb.Level.INFO) | ||
} | ||
|
||
repeat(maxNum) { | ||
crashBreadcrumbs.add( | ||
Breadcrumb(testMessage, testData, testCategory, Breadcrumb.Level.WARNING, testType) | ||
) | ||
} | ||
|
||
for (i in 0 until maxNum) { | ||
assertEquals(crashBreadcrumbs.elementAt(i).level, Breadcrumb.Level.WARNING) | ||
} | ||
} | ||
|
||
@Test | ||
fun `Breadcrumb priority queue output list result is sorted by time`() { | ||
val testMessage = "test_Message" | ||
val testData = hashMapOf("1" to "one", "2" to "two") | ||
val testCategory = "testing_category" | ||
val testType = Breadcrumb.Type.USER | ||
val maxNum = 10 | ||
var crashBreadcrumbs = BreadcrumbPriorityQueue(maxNum) | ||
|
||
repeat(maxNum) { | ||
crashBreadcrumbs.add( | ||
Breadcrumb(testMessage, testData, testCategory, Breadcrumb.Level.DEBUG, testType) | ||
) | ||
sleep(100) /* make sure time elapsed */ | ||
} | ||
|
||
var result = crashBreadcrumbs.toSortedArrayList() | ||
var time = result[0].date | ||
for (i in 1 until result.size) { | ||
assertTrue(time.before(result[i].date)) | ||
time = result[i].date | ||
} | ||
|
||
repeat(maxNum / 2) { | ||
crashBreadcrumbs.add( | ||
Breadcrumb(testMessage, testData, testCategory, Breadcrumb.Level.INFO, testType) | ||
) | ||
sleep(100) /* make sure time elapsed */ | ||
} | ||
|
||
result = crashBreadcrumbs.toSortedArrayList() | ||
time = result[0].date | ||
for (i in 1 until result.size) { | ||
assertTrue(time.before(result[i].date)) | ||
time = result[i].date | ||
} | ||
} | ||
} |
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