Skip to content

Commit c338984

Browse files
committed
fix android & ios bindings tests
1 parent 3c8dd06 commit c338984

7 files changed

Lines changed: 119 additions & 8 deletions

File tree

nix/lib/packages/swiftlint.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
unzip,
77
}:
88

9-
stdenv.mkDerivation rec {
9+
stdenv.mkDerivation {
1010
pname = "swiftlint";
1111
version = "0.62.1";
1212

nix/shells/local.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ mkShell (
101101
kotlin
102102
ktlint
103103
jdk17
104+
swiftformat
104105
kotlin-language-server
105106

106107
# Misc dev

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: 53 additions & 2 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
@@ -129,8 +130,8 @@ class Client(
129130
registry
130131
}
131132

132-
private fun ClientOptions.Api.toCacheKey(): String =
133-
"${env.getUrl()}|${appVersion ?: "nil"}|${gatewayHost ?: "nil"}"
133+
private fun ClientOptions.Api.toCacheKey(exclusive: Boolean = false): String =
134+
"${env.getUrl()}|${appVersion ?: "nil"}|${gatewayHost ?: "nil"}|$exclusive"
134135

135136
private val apiClientCache = mutableMapOf<String, XmtpApiClient>()
136137
private val cacheLock = Mutex()
@@ -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(exclusive = true)
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(exclusive = true)
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: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,15 @@ public struct ClientOptions {
133133

134134
struct ApiCacheKey {
135135
let api: ClientOptions.Api
136+
let exclusive: Bool
137+
138+
init(api: ClientOptions.Api, exclusive: Bool = false) {
139+
self.api = api
140+
self.exclusive = exclusive
141+
}
136142

137143
var stringValue: String {
138-
"\(api.env.url)|\(api.appVersion ?? "nil")|\(api.gatewayHost ?? "nil")"
144+
"\(api.env.url)|\(api.appVersion ?? "nil")|\(api.gatewayHost ?? "nil")|\(exclusive)"
139145
}
140146
}
141147

@@ -478,6 +484,59 @@ public final class Client {
478484
return newClient
479485
}
480486

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