|
1 | 1 | package io.sentry |
2 | 2 |
|
| 3 | +import com.google.common.truth.Truth.assertThat |
| 4 | +import java.io.StringReader |
3 | 5 | import java.util.Date |
4 | 6 | import java.util.concurrent.CountDownLatch |
5 | 7 | import java.util.concurrent.Executors |
@@ -365,6 +367,60 @@ class BreadcrumbTest { |
365 | 367 | } |
366 | 368 | } |
367 | 369 |
|
| 370 | + @Test |
| 371 | + fun `breadcrumbs sharing a timestamp keep the order they were recorded in`() { |
| 372 | + val timestamp = Date(1_600_000_000_000) |
| 373 | + val first = Breadcrumb(timestamp).apply { message = "first" } |
| 374 | + val second = Breadcrumb(timestamp).apply { message = "second" } |
| 375 | + val third = Breadcrumb(timestamp).apply { message = "third" } |
| 376 | + |
| 377 | + val sorted = listOf(third, first, second).sorted().map { it.message } |
| 378 | + |
| 379 | + assertThat(sorted).containsExactly("first", "second", "third").inOrder() |
| 380 | + } |
| 381 | + |
| 382 | + @Test |
| 383 | + fun `a deserialized breadcrumb is ordered by its own timestamp, not by when it was parsed`() { |
| 384 | + val live = Breadcrumb(Date(1_600_000_000_000)).apply { message = "live" } |
| 385 | + val restored = |
| 386 | + Breadcrumb.fromMap( |
| 387 | + mapOf( |
| 388 | + Breadcrumb.JsonKeys.TIMESTAMP to DateUtils.getTimestamp(Date(1_500_000_000_000)), |
| 389 | + Breadcrumb.JsonKeys.MESSAGE to "restored", |
| 390 | + ), |
| 391 | + SentryOptions(), |
| 392 | + ) |
| 393 | + |
| 394 | + val sorted = listOf(live, restored).sorted().map { it.message } |
| 395 | + |
| 396 | + assertThat(sorted).containsExactly("restored", "live").inOrder() |
| 397 | + } |
| 398 | + |
| 399 | + @Test |
| 400 | + fun `a breadcrumb read back from JSON is ordered by its own timestamp, not by when it was parsed`() { |
| 401 | + val live = Breadcrumb(Date(1_600_000_000_000)).apply { message = "live" } |
| 402 | + val json = |
| 403 | + """{"timestamp":"${DateUtils.getTimestamp(Date(1_500_000_000_000))}","message":"restored"}""" |
| 404 | + val restored = |
| 405 | + Breadcrumb.Deserializer() |
| 406 | + .deserialize(JsonObjectReader(StringReader(json)), NoOpLogger.getInstance()) |
| 407 | + |
| 408 | + val sorted = listOf(live, restored).sorted().map { it.message } |
| 409 | + |
| 410 | + assertThat(sorted).containsExactly("restored", "live").inOrder() |
| 411 | + } |
| 412 | + |
| 413 | + @Test |
| 414 | + fun `cloning a breadcrumb keeps its position among breadcrumbs sharing its timestamp`() { |
| 415 | + val timestamp = Date(1_600_000_000_000) |
| 416 | + val first = Breadcrumb(timestamp).apply { message = "first" } |
| 417 | + val second = Breadcrumb(timestamp).apply { message = "second" } |
| 418 | + |
| 419 | + val sorted = listOf(second, Breadcrumb(first)).sorted().map { it.message } |
| 420 | + |
| 421 | + assertThat(sorted).containsExactly("first", "second").inOrder() |
| 422 | + } |
| 423 | + |
368 | 424 | class TestKey(val id: Long) { |
369 | 425 | override fun toString(): String = id.toString() |
370 | 426 | } |
|
0 commit comments