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

Add ability to disconnect and reconnect database #248

Merged
merged 4 commits into from
May 29, 2024
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 @@ -3,15 +3,16 @@ package org.xmtp.android.library
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import kotlinx.coroutines.runBlocking
import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Assert.assertThrows
import org.junit.Assert.fail
import org.junit.Test
import org.junit.runner.RunWith
import org.xmtp.android.library.messages.PrivateKeyBuilder
import org.xmtp.android.library.messages.PrivateKeyBundleV1Builder
import org.xmtp.android.library.messages.generate
import org.xmtp.proto.message.contents.PrivateKeyOuterClass
import uniffi.xmtpv3.GenericException
import java.util.concurrent.CompletableFuture
import java.util.concurrent.TimeUnit

Expand Down Expand Up @@ -204,7 +205,7 @@ class ClientTest {
fun testDoesNotCreateAV3Client() {
val fakeWallet = PrivateKeyBuilder()
val client = Client().create(account = fakeWallet)
Assert.assertThrows("Error no V3 client initialized", XMTPException::class.java) {
assertThrows("Error no V3 client initialized", XMTPException::class.java) {
runBlocking {
client.canMessageV3(listOf(client.address))[client.address]?.let { assert(!it) }
}
Expand Down Expand Up @@ -279,4 +280,51 @@ class ClientTest {
fail("Error: $e")
}
}

@Test
fun testCanDropReconnectDatabase() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
val fakeWallet = PrivateKeyBuilder()
val fakeWallet2 = PrivateKeyBuilder()
val boClient =
Client().create(
account = fakeWallet,
options = ClientOptions(
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
enableAlphaMls = true,
appContext = context
)
)
val alixClient =
Client().create(
account = fakeWallet2,
options = ClientOptions(
ClientOptions.Api(XMTPEnvironment.LOCAL, false),
enableAlphaMls = true,
appContext = context
)
)

runBlocking {
boClient.conversations.newGroup(listOf(alixClient.address))
boClient.conversations.syncGroups()
}

runBlocking {
assertEquals(boClient.conversations.listGroups().size, 1)
}

boClient.dropLocalDatabaseConnection()

assertThrows(
"Client error: storage error: Pool needs to reconnect before use",
GenericException::class.java
) { runBlocking { boClient.conversations.listGroups() } }

runBlocking { boClient.reconnectLocalDatabase() }

runBlocking {
assertEquals(boClient.conversations.listGroups().size, 1)
}
}
}
11 changes: 11 additions & 0 deletions library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,17 @@ class Client() {
File(dbPath).delete()
}

@Deprecated(
message = "This function is delicate and should be used with caution. App will error if database not properly reconnected. See: reconnectLocalDatabase()",
)
fun dropLocalDatabaseConnection() {
libXMTPClient?.releaseDbConnection()
}

suspend fun reconnectLocalDatabase() {
libXMTPClient?.dbReconnect() ?: throw XMTPException("Error no V3 client initialized")
}

val privateKeyBundle: PrivateKeyBundle
get() = PrivateKeyBundleBuilder.buildFromV1Key(privateKeyBundleV1)

Expand Down
Loading