Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure LevelDB Iterator is closed #5458

Merged
merged 2 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import com.duckduckgo.app.pixels.remoteconfig.AndroidBrowserConfigFeature
import com.duckduckgo.di.scopes.AppScope
import com.squareup.anvil.annotations.ContributesBinding
import com.squareup.anvil.annotations.ContributesTo
import dagger.Lazy
import dagger.Module
import dagger.Provides
import dagger.SingleInstanceIn
import java.io.File
import java.nio.charset.StandardCharsets
import javax.inject.Inject
import javax.inject.Provider
import kotlinx.coroutines.runBlocking
import org.iq80.leveldb.DB
import org.iq80.leveldb.Options
Expand All @@ -40,7 +40,7 @@ interface WebLocalStorageManager {

@ContributesBinding(AppScope::class)
class DuckDuckGoWebLocalStorageManager @Inject constructor(
private val databaseProvider: Provider<DB>,
private val databaseProvider: Lazy<DB>,
private val androidBrowserConfigFeature: AndroidBrowserConfigFeature,
private val webLocalStorageSettingsJsonParser: WebLocalStorageSettingsJsonParser,
) : WebLocalStorageManager {
Expand All @@ -58,8 +58,8 @@ class DuckDuckGoWebLocalStorageManager @Inject constructor(
Timber.d("WebLocalStorageManager: Allowed domains: $domains")
Timber.d("WebLocalStorageManager: Matching regex: $matchingRegex")

databaseProvider.get().use { db ->
val iterator = db.iterator()
val db = databaseProvider.get()
db.iterator().use { iterator ->
iterator.seekToFirst()

while (iterator.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import com.duckduckgo.app.browser.weblocalstorage.WebLocalStorageSettings
import com.duckduckgo.app.browser.weblocalstorage.WebLocalStorageSettingsJsonParser
import com.duckduckgo.app.pixels.remoteconfig.AndroidBrowserConfigFeature
import com.duckduckgo.feature.toggles.api.Toggle
import javax.inject.Provider
import dagger.Lazy
import kotlinx.coroutines.test.runTest
import org.iq80.leveldb.DB
import org.iq80.leveldb.DBIterator
Expand All @@ -39,7 +39,7 @@ class DuckDuckGoWebLocalStorageManagerTest {

private val mockDB: DB = mock()
private val mockIterator: DBIterator = mock()
private val mockDatabaseProvider: Provider<DB> = mock()
private val mockDatabaseProvider: Lazy<DB> = mock()
private val mockWebLocalStorageSettingsJsonParser: WebLocalStorageSettingsJsonParser = mock()
private val mockAndroidBrowserConfigFeature: AndroidBrowserConfigFeature = mock()
private val mockToggle: Toggle = mock()
Expand Down Expand Up @@ -152,13 +152,13 @@ class DuckDuckGoWebLocalStorageManagerTest {
}

@Test
fun whenClearWebLocalStorageThenDBIsClosed() {
fun whenClearWebLocalStorageThenIteratorIsClosed() {
whenever(mockDB.iterator()).thenReturn(mockIterator)
whenever(mockIterator.hasNext()).thenReturn(false)

testee.clearWebLocalStorage()

verify(mockDB).close()
verify(mockIterator).close()
}

private fun createMockDBEntry(key: ByteArray): MutableMap.MutableEntry<ByteArray, ByteArray> {
Expand Down
Loading