Skip to content

Commit

Permalink
Add ability to disconnect and reconnect database (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer authored May 29, 2024
1 parent b2a41f4 commit 5ad84f1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
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

0 comments on commit 5ad84f1

Please sign in to comment.