Don't clobber existing history entries.

Currently, adding a history to a NetworkStatsCollection.Builder
will overwrite any history that was previously passed in with the
same key. This breaks the importer (which is the primary/only
caller of this code), because the importer re-uses the same
NetworkStatsCollection object to import multiple files.

Instead, simply add any passed-in entries after the ones that
were already there. Require the caller to pass in entries in
order, because NetworkStatsHistory internally assumes that
entris are always sorted.

Bug: 230289468
Test: manually verified this unbreaks the importer
Change-Id: Ic8647ff28fca78d579d5f759f96a864877f8158b
Merged-In: Ic8647ff28fca78d579d5f759f96a864877f8158b
  (pure cherry-picked from ag/18453213)
This commit is contained in:
Lorenzo Colitti
2022-05-18 17:32:37 +09:00
committed by Junyu Lai
parent c62261f140
commit ec2fbb7159
4 changed files with 60 additions and 12 deletions

View File

@@ -27,6 +27,7 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
@ConnectivityModuleTest
@RunWith(JUnit4::class)
@@ -51,12 +52,22 @@ class NetworkStatsHistoryTest {
.build()
statsSingle.assertEntriesEqual(entry1)
assertEquals(DateUtils.HOUR_IN_MILLIS, statsSingle.bucketDuration)
// Verify the builder throws if the timestamp of added entry is not greater than
// that of any previously-added entry.
assertFailsWith(IllegalArgumentException::class) {
NetworkStatsHistory
.Builder(DateUtils.SECOND_IN_MILLIS, /* initialCapacity */ 0)
.addEntry(entry1).addEntry(entry2).addEntry(entry3)
.build()
}
val statsMultiple = NetworkStatsHistory
.Builder(DateUtils.SECOND_IN_MILLIS, /* initialCapacity */ 0)
.addEntry(entry1).addEntry(entry2).addEntry(entry3)
.addEntry(entry3).addEntry(entry1).addEntry(entry2)
.build()
assertEquals(DateUtils.SECOND_IN_MILLIS, statsMultiple.bucketDuration)
statsMultiple.assertEntriesEqual(entry1, entry2, entry3)
statsMultiple.assertEntriesEqual(entry3, entry1, entry2)
}
fun NetworkStatsHistory.assertEntriesEqual(vararg entries: NetworkStatsHistory.Entry) {