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
private val soneImages: Multimap<String, Image> = HashMultimap.create<String, Image>()
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
override val bookmarkedPosts get() = memoryBookmarkDatabase.bookmarkedPosts
override fun save() {
- saveKnownPosts()
- saveKnownPostReplies()
+ if (saveRateLimiter.tryAcquire()) {
+ saveKnownPosts()
+ saveKnownPostReplies()
+ }
}
override fun doStart() {
*/
class MemoryDatabaseTest {
- private val configuration = mock<Configuration>()
+ private val configuration = deepMock<Configuration>()
private val memoryDatabase = MemoryDatabase(configuration)
private val sone = mock<Sone>()
assertThat(configuration.getStringValue("KnownReplies/1/ID").value, equalTo<Any>(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"