Skip to content

Commit a878d07

Browse files
authored
Mila/dynamically set target backend in test (#769)
1 parent 1793ca8 commit a878d07

File tree

3 files changed

+82
-23
lines changed

3 files changed

+82
-23
lines changed

.github/workflows/integration_tests.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,26 @@ jobs:
377377
name: 'firebase_unity_sdk.zip'
378378
workflow: 'build_starter.yml'
379379
run_id: ${{ github.event.inputs.packaged_sdk_run_id }}
380+
- name: Set up Node (16)
381+
uses: actions/setup-node@v3
382+
with:
383+
node-version: 16.x
384+
- name: Setup java for Firestore emulator
385+
uses: actions/setup-java@v3
386+
with:
387+
distribution: 'temurin'
388+
java-version: '17'
389+
- name: Setup Firestore Emulator
390+
uses: nick-invision/retry@v2
391+
with:
392+
shell: bash
393+
timeout_minutes: 5
394+
max_attempts: 3
395+
command: npm install -g firebase-tools
380396
- name: Run Playmode (in editor mode) integration tests
381397
shell: bash
382398
run: |
399+
firebase emulators:start --only firestore &
383400
if [[ -n "${{ github.event.inputs.packaged_sdk_run_id }}" ]]; then
384401
unzip -q firebase_unity_sdk.zip -d ~/Downloads/
385402
else
@@ -466,11 +483,31 @@ jobs:
466483
with:
467484
path: testapps
468485
name: ${{ steps.matrix_info.outputs.artifact_path }}
486+
- name: Set up Node (16)
487+
if: ${{ matrix.test_device == 'github_runner' }}
488+
uses: actions/setup-node@v3
489+
with:
490+
node-version: 16.x
491+
- name: Setup java for Firestore emulator
492+
if: ${{ matrix.test_device == 'github_runner' }}
493+
uses: actions/setup-java@v3
494+
with:
495+
distribution: 'temurin'
496+
java-version: '17'
497+
- name: Setup Firestore Emulator
498+
if: ${{ matrix.test_device == 'github_runner' }}
499+
uses: nick-invision/retry@v2
500+
with:
501+
shell: bash
502+
timeout_minutes: 5
503+
max_attempts: 3
504+
command: npm install -g firebase-tools
469505
- name: Run Desktop integration tests
470506
if: ${{ matrix.test_device == 'github_runner' }}
471507
timeout-minutes: 30
472508
shell: bash
473509
run: |
510+
firebase emulators:start --only firestore &
474511
python scripts/gha/desktop_tester.py --testapp_dir testapps \
475512
--logfile_name "${{ steps.matrix_info.outputs.info }}"
476513
- name: Run Mobile integration tests on real device via FTL

firestore/testapp/Assets/Firebase/Sample/Firestore/UIHandler.cs

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,34 @@ public class UIHandler : MonoBehaviour {
6060
// Previously completed task.
6161
protected Task previousTask;
6262

63+
private enum Backend {
64+
Production = 0,
65+
Emulator = 1
66+
// Nightly and stage can be added later.
67+
}
68+
69+
private Backend TargetBackend = GetTargetBackend();
70+
// For local testing purpose, manually modify the port number if Firestore
71+
// emulator is running at a different port.
72+
private string EmulatorPort = "8080";
73+
74+
private static Backend GetTargetBackend() {
75+
// Set Firestore emulator as default backend.
76+
Backend targetBackend = Backend.Emulator;
77+
78+
// Use production backend if not running on Unity editor or standalone platform (Mac OS X,
79+
// Windows or Linux).
80+
#if !UNITY_EDITOR && !UNITY_STANDALONE
81+
targetBackend = Backend.Production;
82+
// Set custom script `RUN_AGAINST_PRODUCTION` to run tests against the productions. Refer to
83+
// unity documents for guidance:https://docs.unity3d.com/Manual/CustomScriptingSymbols.html
84+
#elif RUN_AGAINST_PRODUCTION
85+
targetBackend = Backend.Production;
86+
#endif
87+
88+
return targetBackend;
89+
}
90+
6391
/**
6492
* Compares two objects for deep equality.
6593
*/
@@ -227,18 +255,23 @@ protected internal FirebaseFirestore TestFirestore(FirebaseApp app, string datab
227255
return firestore;
228256
}
229257

258+
// Check if Firestore emulator is the target backend.
259+
protected bool IsUsingFirestoreEmulator() {
260+
return (TargetBackend == Backend.Emulator);
261+
}
262+
230263
// Update the `Settings` of a Firestore instance to run tests against the production or
231264
// Firestore emulator backend.
232265
protected internal void SetTargetBackend(FirebaseFirestore db) {
233266
string targetHost = GetTargetHost();
234-
267+
235268
// Avoid updating `Settings` if not required. No changes are allowed to be made to the
236269
// settings of a <c>FirebaseFirestore</c> instance if it has invoked any non-static method.
237270
/// Attempting to do so will result in an exception.
238271
if (db.Settings.Host == targetHost) {
239272
return;
240273
}
241-
274+
242275
db.Settings.Host = targetHost;
243276
// Emulator does not support ssl.
244277
db.Settings.SslEnabled = IsUsingFirestoreEmulator() ? false : true;
@@ -252,20 +285,10 @@ private string GetTargetHost() {
252285
#else
253286
string localHost = "localhost";
254287
#endif // UNITY_ANDROID
255-
256-
// Use FIRESTORE_EMULATOR_PORT if it is set to non empty string, otherwise use the
257-
// default port.
258-
string port = Environment.GetEnvironmentVariable("FIRESTORE_EMULATOR_PORT") ?? "8080";
259-
return localHost + ":" + port;
288+
return localHost + ":" + EmulatorPort;
260289
}
261-
262-
return FirebaseFirestore.DefaultInstance.Settings.Host;
263-
}
264290

265-
// Check if the `USE_FIRESTORE_EMULATOR` environment variable is set regardsless
266-
// of its value.
267-
protected internal bool IsUsingFirestoreEmulator() {
268-
return (Environment.GetEnvironmentVariable("USE_FIRESTORE_EMULATOR") != null);
291+
return FirebaseFirestore.DefaultInstance.Settings.Host;
269292
}
270293

271294
// Cancel the currently running operation.

firestore/testapp/Assets/Firebase/Sample/Firestore/UIHandlerAutomated.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ protected override void Start() {
115115
TestDocumentSnapshotDoubleIncrementBehavior,
116116
TestDocumentSnapshotServerTimestampBehavior,
117117
TestSnapshotMetadataEqualsAndGetHashCode,
118-
TestAuthIntegration,
119118
TestDocumentListen,
120119
TestDocumentListenWithMetadataChanges,
121120
TestQueryListen,
@@ -170,21 +169,19 @@ protected override void Start() {
170169
TestTerminateAppWithMultiDB,
171170
TestRestartCustomFirestore,
172171
};
172+
173+
// Set the list of tests to run against Production only.
174+
Func<Task>[] testsToRunAgainstProductionOnly = {
175+
// While running on CI, this test case passes only if it is tested against the production.
176+
TestAuthIntegration,
177+
};
173178

174179
// For local development convenience, populate `testFilter` with the tests that you would like
175180
// to run (instead of running the entire suite).
176181
Func<Task>[] testFilter = {
177182
// THIS LIST MUST BE EMPTY WHEN CHECKED INTO SOURCE CONTROL!
178183
};
179184

180-
/*
181-
* THIS MUST BE COMMENTED OUT WHEN CHECKED INTO SOURCE CONTROL!
182-
*
183-
* To run tests against Firestore emulator locally, set `USE_FIRESTORE_EMULATOR` to "true".
184-
* To switch back to run against prod, set it back to null.
185-
*/
186-
// Environment.SetEnvironmentVariable("USE_FIRESTORE_EMULATOR", "true");
187-
188185
// Unity "helpfully" adds stack traces whenever you call Debug.Log. Unfortunately, these stack
189186
// traces are basically useless, since the good parts are always truncated. (See comments on
190187
// LogInBatches.) So just disable them.
@@ -204,6 +201,8 @@ protected override void Start() {
204201
if (IsUsingFirestoreEmulator()) {
205202
Debug.Log("Running tests against Firestore Emulator.");
206203
tests = tests.Concat(testsToRunAgainstFirestoreEmulatorOnly).ToArray();
204+
} else {
205+
tests = tests.Concat(testsToRunAgainstProductionOnly).ToArray();
207206
}
208207

209208
testRunner = AutomatedTestRunner.CreateTestRunner(

0 commit comments

Comments
 (0)