Skip to content

Commit 929ac40

Browse files
committed
fix android & ios bindings tests
1 parent 4601db9 commit 929ac40

5 files changed

Lines changed: 109 additions & 4 deletions

File tree

sdks/android/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ class ClientTest : BaseInstrumentedTest() {
523523
@Test
524524
fun testsCanSeeKeyPackageStatus() {
525525
val fixtures = runBlocking { createFixtures() }
526-
runBlocking { Client.connectToApiBackend(ClientOptions.Api(XMTPEnvironment.LOCAL, true)) }
526+
runBlocking { Client.connectToApiBackendExclusive(ClientOptions.Api(XMTPEnvironment.LOCAL, true)) }
527527
val inboxState =
528528
runBlocking {
529529
Client
@@ -576,7 +576,7 @@ class ClientTest : BaseInstrumentedTest() {
576576
// @Test
577577
// fun testsCanSeeInvalidKeyPackageStatusOnDev() {
578578
// runBlocking {
579-
// Client.connectToApiBackend(
579+
// Client.connectToApiBackendExclusive(
580580
// ClientOptions.Api(
581581
// XMTPEnvironment.DEV,
582582
// true

sdks/android/library/src/androidTest/java/org/xmtp/android/library/PerformanceTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ class PerformanceTest : BaseInstrumentedTest() {
150150
val time3 = end3.time - start3.time
151151
Log.d("PERF", "Built a client with inboxId in ${time3 / 1000.0}s")
152152

153-
runBlocking { Client.connectToApiBackend(ClientOptions.Api(XMTPEnvironment.DEV, true)) }
153+
runBlocking { Client.connectToApiBackendExclusive(ClientOptions.Api(XMTPEnvironment.DEV, true)) }
154154
val start4 = Date()
155155
runBlocking {
156156
Client.create(

sdks/android/library/src/main/java/org/xmtp/android/library/Client.kt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import uniffi.xmtpv3.FfiXmtpClient
3131
import uniffi.xmtpv3.XmtpApiClient
3232
import uniffi.xmtpv3.applySignatureRequest
3333
import uniffi.xmtpv3.connectToBackend
34+
import uniffi.xmtpv3.connectToBackendExclusive
3435
import uniffi.xmtpv3.createClient
3536
import uniffi.xmtpv3.enterDebugWriter
3637
import uniffi.xmtpv3.exitDebugWriter
@@ -245,6 +246,56 @@ class Client(
245246
}
246247
}
247248

249+
@Deprecated("This function will be removed on d14n cutover. Use connectToApiBackend instead.")
250+
suspend fun connectToApiBackendExclusive(api: ClientOptions.Api): XmtpApiClient {
251+
val cacheKey = api.toCacheKey()
252+
return cacheLock.withLock {
253+
val cached = apiClientCache[cacheKey]
254+
255+
if (cached != null && isConnected(cached)) {
256+
return cached
257+
}
258+
259+
// If not cached or not connected, create a fresh client
260+
val newClient =
261+
connectToBackendExclusive(
262+
api.env.getUrl(),
263+
api.gatewayHost,
264+
FfiClientMode.DEFAULT,
265+
api.appVersion,
266+
null,
267+
null,
268+
)
269+
apiClientCache[cacheKey] = newClient
270+
return@withLock newClient
271+
}
272+
}
273+
274+
@Deprecated("This function will be removed on d14n cutover. Use connectToSyncApiBackend instead.")
275+
suspend fun connectToSyncApiBackendExclusive(api: ClientOptions.Api): XmtpApiClient {
276+
val cacheKey = api.toCacheKey()
277+
return syncCacheLock.withLock {
278+
val cached = syncApiClientCache[cacheKey]
279+
280+
if (cached != null && isConnected(cached)) {
281+
return cached
282+
}
283+
284+
// If not cached or not connected, create a fresh client
285+
val newClient =
286+
connectToBackendExclusive(
287+
api.env.getUrl(),
288+
api.gatewayHost,
289+
FfiClientMode.DEFAULT,
290+
api.appVersion,
291+
null,
292+
null,
293+
)
294+
syncApiClientCache[cacheKey] = newClient
295+
return@withLock newClient
296+
}
297+
}
298+
248299
suspend fun getOrCreateInboxId(
249300
api: ClientOptions.Api,
250301
publicIdentity: PublicIdentity,

sdks/ios/Sources/XMTPiOS/Client.swift

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,60 @@ public final class Client {
478478
return newClient
479479
}
480480

481+
@available(*, deprecated, message: "This function will be removed on d14n cutover. Use connectToApiBackend instead.")
482+
public static func connectToApiBackendExclusive(api: ClientOptions.Api)
483+
async throws
484+
-> XmtpApiClient
485+
{
486+
let cacheKey = ApiCacheKey(api: api).stringValue
487+
488+
// Check for an existing connected client
489+
if let cached = await apiCache.getClient(forKey: cacheKey),
490+
try await isConnected(api: cached)
491+
{
492+
return cached
493+
}
494+
495+
// Either not cached or not connected; create new client
496+
let newClient = try await connectToBackendExclusive(
497+
v3Host: api.env.url,
498+
gatewayHost: api.gatewayHost,
499+
clientMode: FfiClientMode.default,
500+
appVersion: api.appVersion,
501+
authCallback: nil,
502+
authHandle: nil
503+
)
504+
await apiCache.setClient(newClient, forKey: cacheKey)
505+
return newClient
506+
}
507+
508+
@available(*, deprecated, message: "This function will be removed on d14n cutover. Use connectToSyncApiBackend instead.")
509+
public static func connectToSyncApiBackendExclusive(api: ClientOptions.Api)
510+
async throws
511+
-> XmtpApiClient
512+
{
513+
let cacheKey = ApiCacheKey(api: api).stringValue
514+
515+
// Check for an existing connected client
516+
if let cached = await apiCache.getSyncClient(forKey: cacheKey),
517+
try await isConnected(api: cached)
518+
{
519+
return cached
520+
}
521+
522+
// Either not cached or not connected; create new client
523+
let newClient = try await connectToBackendExclusive(
524+
v3Host: api.env.url,
525+
gatewayHost: api.gatewayHost,
526+
clientMode: FfiClientMode.default,
527+
appVersion: api.appVersion,
528+
authCallback: nil,
529+
authHandle: nil
530+
)
531+
await apiCache.setSyncClient(newClient, forKey: cacheKey)
532+
return newClient
533+
}
534+
481535
public static func getOrCreateInboxId(
482536
api: ClientOptions.Api, publicIdentity: PublicIdentity
483537
) async throws -> InboxId {

sdks/ios/Tests/XMTPTests/ClientTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ class ClientTests: XCTestCase {
942942
let fixtures = try await fixtures()
943943
let api = ClientOptions.Api(env: .local, isSecure: XMTPEnvironment.local.isSecure)
944944

945-
try await Client.connectToApiBackend(api: api)
945+
try await Client.connectToApiBackendExclusive(api: api)
946946

947947
guard
948948
let inboxState = try await Client.inboxStatesForInboxIds(

0 commit comments

Comments
 (0)