Skip to content

Commit 33a44a9

Browse files
authored
[Vertex AI] Fix caching of Vertex AI instances (#14007)
1 parent 32c8083 commit 33a44a9

File tree

3 files changed

+57
-24
lines changed

3 files changed

+57
-24
lines changed

FirebaseVertexAI/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 11.5.0
2+
- [fixed] Fixed an issue where `VertexAI.vertexAI(app: app1)` and
3+
`VertexAI.vertexAI(app: app2)` would return the same instance if their
4+
`location` was the same, including the default `us-central1`. (#14007)
5+
16
# 11.4.0
27
- [feature] Vertex AI in Firebase is now Generally Available (GA) and can be
38
used in production apps. (#13725)

FirebaseVertexAI/Sources/VertexAI.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,12 @@ public class VertexAI {
5555
// Unlock before the function returns.
5656
defer { os_unfair_lock_unlock(&instancesLock) }
5757

58-
if let instance = instances[location] {
58+
let instanceKey = "\(app.name):\(location)"
59+
if let instance = instances[instanceKey] {
5960
return instance
6061
}
6162
let newInstance = VertexAI(app: app, location: location)
62-
instances[location] = newInstance
63+
instances[instanceKey] = newInstance
6364
return newInstance
6465
}
6566

@@ -116,8 +117,8 @@ public class VertexAI {
116117

117118
private let auth: AuthInterop?
118119

119-
/// A map of active `VertexAI` instances for `app`, keyed by model resource names
120-
/// (e.g., "projects/my-project-id/locations/us-central1/publishers/google/models/gemini-pro").
120+
/// A map of active `VertexAI` instances keyed by the `FirebaseApp` name and the `location`, in
121+
/// the format `appName:location`.
121122
private static var instances: [String: VertexAI] = [:]
122123

123124
/// Lock to manage access to the `instances` array to avoid race conditions.

FirebaseVertexAI/Tests/Unit/VertexComponentTests.swift

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,21 @@ import XCTest
2424
class VertexComponentTests: XCTestCase {
2525
static let projectID = "test-project-id"
2626
static let apiKey = "test-api-key"
27+
static let options = {
28+
let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000",
29+
gcmSenderID: "00000000000000000-00000000000-000000000")
30+
options.projectID = VertexComponentTests.projectID
31+
options.apiKey = VertexComponentTests.apiKey
2732

28-
static var app: FirebaseApp?
33+
return options
34+
}()
2935

30-
let location = "test-location"
36+
static let app = {
37+
FirebaseApp.configure(options: options)
38+
return FirebaseApp(instanceWithName: "test", options: options)
39+
}()
3140

32-
override class func setUp() {
33-
super.setUp()
34-
if app == nil {
35-
let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000",
36-
gcmSenderID: "00000000000000000-00000000000-000000000")
37-
options.projectID = VertexComponentTests.projectID
38-
options.apiKey = VertexComponentTests.apiKey
39-
FirebaseApp.configure(options: options)
40-
app = FirebaseApp(instanceWithName: "test", options: options)
41-
}
42-
}
41+
let location = "test-location"
4342

4443
/// Test that the objc class is available for the component system to update the user agent.
4544
func testComponentsBeingRegistered() throws {
@@ -48,27 +47,55 @@ class VertexComponentTests: XCTestCase {
4847

4948
/// Tests that a vertex instance can be created properly.
5049
func testVertexInstanceCreation() throws {
51-
let app = try XCTUnwrap(VertexComponentTests.app)
52-
53-
let vertex = VertexAI.vertexAI(app: app, location: location)
50+
let vertex = VertexAI.vertexAI(app: VertexComponentTests.app, location: location)
5451

5552
XCTAssertNotNil(vertex)
5653
XCTAssertEqual(vertex.projectID, VertexComponentTests.projectID)
5754
XCTAssertEqual(vertex.apiKey, VertexComponentTests.apiKey)
5855
XCTAssertEqual(vertex.location, location)
5956
}
6057

61-
/// Tests that a vertex instances are reused properly.
62-
func testMultipleComponentInstancesCreated() throws {
58+
/// Tests that Vertex instances are reused properly.
59+
func testSameAppAndLocation_instanceReused() throws {
6360
let app = try XCTUnwrap(VertexComponentTests.app)
61+
6462
let vertex1 = VertexAI.vertexAI(app: app, location: location)
6563
let vertex2 = VertexAI.vertexAI(app: app, location: location)
6664

6765
// Ensure they're the same instance.
6866
XCTAssert(vertex1 === vertex2)
67+
}
68+
69+
func testSameAppAndDifferentLocation_newInstanceCreated() throws {
70+
let vertex1 = VertexAI.vertexAI(app: VertexComponentTests.app, location: location)
71+
let vertex2 = VertexAI.vertexAI(app: VertexComponentTests.app, location: "differentLocation")
72+
73+
// Ensure they are different instances.
74+
XCTAssert(vertex1 !== vertex2)
75+
}
76+
77+
func testDifferentAppAndSameLocation_newInstanceCreated() throws {
78+
FirebaseApp.configure(name: "test-2", options: VertexComponentTests.options)
79+
let app2 = FirebaseApp(instanceWithName: "test-2", options: VertexComponentTests.options)
80+
addTeardownBlock { await app2.delete() }
81+
82+
let vertex1 = VertexAI.vertexAI(app: VertexComponentTests.app, location: location)
83+
let vertex2 = VertexAI.vertexAI(app: app2, location: location)
84+
85+
XCTAssert(VertexComponentTests.app != app2)
86+
XCTAssert(vertex1 !== vertex2) // Ensure they are different instances.
87+
}
88+
89+
func testDifferentAppAndDifferentLocation_newInstanceCreated() throws {
90+
FirebaseApp.configure(name: "test-2", options: VertexComponentTests.options)
91+
let app2 = FirebaseApp(instanceWithName: "test-2", options: VertexComponentTests.options)
92+
addTeardownBlock { await app2.delete() }
93+
94+
let vertex1 = VertexAI.vertexAI(app: VertexComponentTests.app, location: location)
95+
let vertex2 = VertexAI.vertexAI(app: app2, location: "differentLocation")
6996

70-
let vertex3 = VertexAI.vertexAI(app: app, location: "differentLocation")
71-
XCTAssert(vertex1 !== vertex3)
97+
XCTAssert(VertexComponentTests.app != app2)
98+
XCTAssert(vertex1 !== vertex2) // Ensure they are different instances.
7299
}
73100

74101
/// Test that vertex instances get deallocated.

0 commit comments

Comments
 (0)