From 1acab7994675db5fc17a7d242d7de0304efb6987 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 26 Jul 2019 16:45:59 +0200 Subject: [PATCH] =?utf8?q?=E2=9A=A1=EF=B8=8F=20Only=20update=20configurati?= =?utf8?q?on=20once=20per=20second?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/database/memory/MemoryDatabase.kt | 9 ++++++--- .../pterodactylus/sone/database/memory/MemoryDatabaseTest.kt | 10 +++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.kt b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.kt index 6fce7e0..a9459e8 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.kt +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.kt @@ -21,7 +21,7 @@ import com.google.common.base.Preconditions.checkNotNull import com.google.common.collect.HashMultimap import com.google.common.collect.Multimap import com.google.common.collect.TreeMultimap -import com.google.common.util.concurrent.AbstractService +import com.google.common.util.concurrent.* import com.google.inject.Inject import com.google.inject.Singleton import net.pterodactylus.sone.data.Album @@ -70,6 +70,7 @@ class MemoryDatabase @Inject constructor(private val configuration: Configuratio private val soneImages: Multimap = HashMultimap.create() private val memoryBookmarkDatabase = MemoryBookmarkDatabase(this, configurationLoader) private val memoryFriendDatabase = MemoryFriendDatabase(configurationLoader) + private val saveRateLimiter: RateLimiter = RateLimiter.create(1.0) override val soneLoader get() = this::getSone @@ -82,8 +83,10 @@ class MemoryDatabase @Inject constructor(private val configuration: Configuratio override val bookmarkedPosts get() = memoryBookmarkDatabase.bookmarkedPosts override fun save() { - saveKnownPosts() - saveKnownPostReplies() + if (saveRateLimiter.tryAcquire()) { + saveKnownPosts() + saveKnownPostReplies() + } } override fun doStart() { diff --git a/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.kt b/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.kt index 1536da4..4d7a105 100644 --- a/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.kt @@ -38,7 +38,7 @@ import kotlin.test.* */ class MemoryDatabaseTest { - private val configuration = mock() + private val configuration = deepMock() private val memoryDatabase = MemoryDatabase(configuration) private val sone = mock() @@ -409,6 +409,14 @@ class MemoryDatabaseTest { assertThat(configuration.getStringValue("KnownReplies/1/ID").value, equalTo(null)) } + @Test + @Dirty("the rate limiter should be mocked") + fun `saving the database twice in a row only saves it once`() { + memoryDatabase.save() + memoryDatabase.save() + verify(configuration.getStringValue("KnownPosts/0/ID"), times(1)).value = null + } + } private const val SONE_ID = "sone" -- 2.7.4