|
| 1 | +package org.monogram.core.database |
| 2 | + |
| 3 | +import androidx.room.Room |
| 4 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 5 | +import androidx.test.platform.app.InstrumentationRegistry |
| 6 | +import kotlinx.coroutines.runBlocking |
| 7 | +import org.junit.Assert.assertEquals |
| 8 | +import org.junit.Assert.assertFalse |
| 9 | +import org.junit.Assert.assertNotNull |
| 10 | +import org.junit.Assert.assertNull |
| 11 | +import org.junit.Assert.assertTrue |
| 12 | +import org.junit.Test |
| 13 | +import org.junit.runner.RunWith |
| 14 | +import org.monogram.core.database.entity.MessageHoleEntity |
| 15 | +import org.monogram.core.database.entity.MessageTombstoneEntity |
| 16 | +import org.monogram.core.database.entity.MetaEntity |
| 17 | +import org.monogram.core.database.entity.PeerEntity |
| 18 | +import org.monogram.core.database.entity.ProfileCommonEntity |
| 19 | +import org.monogram.core.database.entity.ProfileMediaEntity |
| 20 | +import org.monogram.core.database.entity.ProfileMemberEntity |
| 21 | +import org.monogram.core.database.entity.ProfileTabsEntity |
| 22 | +import org.monogram.core.database.entity.UpdateCursorEntity |
| 23 | +import org.monogram.core.models.Chat |
| 24 | +import org.monogram.core.models.Message |
| 25 | +import org.monogram.core.models.MessageId |
| 26 | +import org.monogram.core.models.PeerId |
| 27 | + |
| 28 | +@RunWith(AndroidJUnit4::class) |
| 29 | +class CacheTtlRoomTest { |
| 30 | + @Test |
| 31 | + fun pruneKeepsTheRecentPageAndLeavesCursorsAlone() = runBlocking { |
| 32 | + val context = InstrumentationRegistry.getInstrumentation().targetContext |
| 33 | + val database = Room.inMemoryDatabaseBuilder(context, MonogramDatabase::class.java) |
| 34 | + .allowMainThreadQueries() |
| 35 | + .build() |
| 36 | + val now = 1_800_000_000_000L |
| 37 | + val old = now / 1000 - CACHE_MESSAGE_TTL_SECONDS - 60 |
| 38 | + val recent = now / 1000 - 3_600 |
| 39 | + val warmup = OfflineWarmup(database) |
| 40 | + try { |
| 41 | + val kept = (1..121).map { id -> |
| 42 | + message(7, id, if (id == 121) recent else old) |
| 43 | + } |
| 44 | + val pending = message(7, -1, old, pending = true) |
| 45 | + val failed = message(7, 200, old, failed = true) |
| 46 | + val openChat = (1..30).map { id -> message(9, id, old) } |
| 47 | + run { |
| 48 | + database.messageDao() |
| 49 | + .upsertAll((kept + pending + failed + openChat).map { it.toEntity() }) |
| 50 | + database.messageHoleDao().insert(MessageHoleEntity(7L, 1, Int.MAX_VALUE)) |
| 51 | + database.messageTombstoneDao().insert( |
| 52 | + listOf(MessageTombstoneEntity(7L, 3), MessageTombstoneEntity(7L, 50)), |
| 53 | + ) |
| 54 | + database.peerDao().upsertAll( |
| 55 | + listOf(PeerEntity(7L, "user", "Kept"), PeerEntity(99L, "user", "Stray")), |
| 56 | + ) |
| 57 | + repeat(503) { index -> |
| 58 | + database.metaDao().upsert(MetaEntity("discussion_read:7:${index + 1}", "1")) |
| 59 | + } |
| 60 | + database.updateCursorDao().upsert(UpdateCursorEntity("updates", 4, 5, 6, 7, 0)) |
| 61 | + database.updateCursorDao().upsert( |
| 62 | + UpdateCursorEntity("$KEY_CHANNEL_CURSOR_PREFIX" + "1", 1, 0, 2, 0, 9), |
| 63 | + ) |
| 64 | + database.profileTabsDao().upsert(ProfileTabsEntity(1L, "{}", 0L)) |
| 65 | + database.profileTabsDao().upsert(ProfileTabsEntity(2L, "{}", now)) |
| 66 | + database.profileMembersDao().upsertAll( |
| 67 | + listOf( |
| 68 | + ProfileMemberEntity(1L, 5L, 0, "Old", role = "member"), |
| 69 | + ProfileMemberEntity(2L, 6L, 0, "New", role = "member"), |
| 70 | + ), |
| 71 | + ) |
| 72 | + database.profileMediaDao().upsertAll( |
| 73 | + listOf( |
| 74 | + ProfileMediaEntity(1L, "photo", 1, 0, 7L), |
| 75 | + ProfileMediaEntity(2L, "photo", 1, 0, 7L), |
| 76 | + ), |
| 77 | + ) |
| 78 | + database.profileCommonDao().upsertAll( |
| 79 | + listOf( |
| 80 | + ProfileCommonEntity(1L, 3L, 0, "Old"), |
| 81 | + ProfileCommonEntity(2L, 4L, 0, "New"), |
| 82 | + ), |
| 83 | + ) |
| 84 | + } |
| 85 | + warmup.upsertChats(listOf(Chat(id = PeerId(7), title = "Kept"))) |
| 86 | + warmup.setOpenChat(9L) |
| 87 | + warmup.cacheHistoryPage(PeerId(7), listOf(message(7, 121, recent))) |
| 88 | + warmup.pruneExpiredCache(nowMillis = now, force = true) |
| 89 | + run { |
| 90 | + val dao = database.messageDao() |
| 91 | + assertNull(dao.byIds(7L, listOf(1)).singleOrNull()) |
| 92 | + assertNotNull(dao.byIds(7L, listOf(22)).singleOrNull()) |
| 93 | + assertNotNull(dao.byIds(7L, listOf(121)).singleOrNull()) |
| 94 | + assertTrue(dao.byIds(7L, listOf(-1)).single().pending) |
| 95 | + assertTrue(dao.byIds(7L, listOf(200)).single().failed) |
| 96 | + assertEquals(30, dao.newestConfirmedIds(9L, 100).size) |
| 97 | + assertEquals("Kept", database.chatDao().get(7L)?.title) |
| 98 | + val account = database.updateCursorDao().get("updates") |
| 99 | + assertEquals(4, account?.pts) |
| 100 | + assertEquals(5, account?.qts) |
| 101 | + assertEquals(6, account?.date) |
| 102 | + assertEquals(7, account?.seq) |
| 103 | + val channel = database.updateCursorDao().get("$KEY_CHANNEL_CURSOR_PREFIX" + "1") |
| 104 | + assertEquals(9, channel?.channelPts) |
| 105 | + assertNull(database.profileTabsDao().get(1L)) |
| 106 | + assertNotNull(database.profileTabsDao().get(2L)) |
| 107 | + assertTrue(database.profileMembersDao().list(1L).isEmpty()) |
| 108 | + assertEquals(1, database.profileMembersDao().list(2L).size) |
| 109 | + assertTrue(database.profileMediaDao().list(1L).isEmpty()) |
| 110 | + assertEquals(1, database.profileMediaDao().list(2L).size) |
| 111 | + assertTrue(database.profileCommonDao().list(1L).isEmpty()) |
| 112 | + assertEquals(1, database.profileCommonDao().list(2L).size) |
| 113 | + assertTrue(database.messageTombstoneDao().deletedIds(7L, listOf(3)).isEmpty()) |
| 114 | + assertEquals(listOf(50), database.messageTombstoneDao().deletedIds(7L, listOf(50))) |
| 115 | + assertNotNull(database.peerDao().get(7L)) |
| 116 | + assertNull(database.peerDao().get(99L)) |
| 117 | + assertNull(database.metaDao().get("discussion_read:7:1")) |
| 118 | + assertNotNull(database.metaDao().get("discussion_read:7:503")) |
| 119 | + val holes = database.messageHoleDao().forChat(7L, MessageHoleEntity.SOURCE_HISTORY) |
| 120 | + assertTrue(holes.any { 1 in it.startId..it.endId }) |
| 121 | + assertTrue(holes.none { hole -> (22..121).any { it in hole.startId..hole.endId } }) |
| 122 | + assertFalse(holes.any { 121 in it.startId..it.endId }) |
| 123 | + } |
| 124 | + } finally { |
| 125 | + database.close() |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private fun message( |
| 130 | + chatId: Long, |
| 131 | + id: Int, |
| 132 | + date: Long, |
| 133 | + pending: Boolean = false, |
| 134 | + failed: Boolean = false, |
| 135 | + ) = Message( |
| 136 | + id = MessageId(PeerId(chatId), id), |
| 137 | + senderId = null, |
| 138 | + text = null, |
| 139 | + date = date, |
| 140 | + outgoing = pending || failed, |
| 141 | + pending = pending, |
| 142 | + failed = failed, |
| 143 | + randomId = if (pending) 99L else null, |
| 144 | + ) |
| 145 | +} |
0 commit comments