Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unified emulator settings for Firestore #1690

Merged
merged 8 commits into from
Jun 24, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.google.firebase.database.core.utilities.Utilities;
import com.google.firebase.database.core.utilities.Validation;
import com.google.firebase.emulators.EmulatedServiceSettings;
import com.google.firebase.emulators.EmulatorSettings;
import com.google.firebase.emulators.FirebaseEmulator;

/**
Expand All @@ -39,6 +40,13 @@
*/
public class FirebaseDatabase {

/**
* Emulator identifier. See {@link FirebaseApp#enableEmulators(EmulatorSettings)}
*
* <p>TODO(samstern): Un-hide this once Firestore, Database, and Functions are implemented
*
* @hide
*/
public static final FirebaseEmulator EMULATOR = FirebaseEmulator.forName("database");

private static final String SDK_VERSION = BuildConfig.VERSION_NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static FirebaseFirestore newFirebaseFirestore(
asyncQueue,
firebaseApp,
instanceRegistry,
null,
null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.FirebaseApp;
import com.google.firebase.auth.internal.InternalAuthProvider;
import com.google.firebase.emulators.EmulatedServiceSettings;
import com.google.firebase.emulators.EmulatorSettings;
import com.google.firebase.emulators.FirebaseEmulator;
import com.google.firebase.firestore.FirebaseFirestoreException.Code;
import com.google.firebase.firestore.auth.CredentialsProvider;
import com.google.firebase.firestore.auth.EmptyCredentialsProvider;
Expand Down Expand Up @@ -55,6 +58,15 @@
*/
public class FirebaseFirestore {

/**
* Emulator identifier. See {@link FirebaseApp#enableEmulators(EmulatorSettings)}
*
* <p>TODO(samstern): Un-hide this once Firestore, Database, and Functions are implemented
*
* @hide
*/
public static FirebaseEmulator EMULATOR = FirebaseEmulator.forName("firestore");

/**
* Provides a registry management interface for {@code FirebaseFirestore} instances.
*
Expand All @@ -81,6 +93,7 @@ public interface InstanceRegistry {
private FirebaseFirestoreSettings settings;
private volatile FirestoreClient client;
private final GrpcMetadataProvider metadataProvider;
private final EmulatedServiceSettings emulatorSettings;

@NonNull
public static FirebaseFirestore getInstance() {
Expand Down Expand Up @@ -144,7 +157,8 @@ static FirebaseFirestore newInstance(
queue,
app,
instanceRegistry,
metadataProvider);
metadataProvider,
app.getEmulatorSettings().getServiceSettings(EMULATOR));
return firestore;
}

Expand All @@ -157,7 +171,8 @@ static FirebaseFirestore newInstance(
AsyncQueue asyncQueue,
@Nullable FirebaseApp firebaseApp,
InstanceRegistry instanceRegistry,
@Nullable GrpcMetadataProvider metadataProvider) {
@Nullable GrpcMetadataProvider metadataProvider,
@Nullable EmulatedServiceSettings emulatorSettings) {
this.context = checkNotNull(context);
this.databaseId = checkNotNull(checkNotNull(databaseId));
this.userDataReader = new UserDataReader(databaseId);
Expand All @@ -168,8 +183,12 @@ static FirebaseFirestore newInstance(
this.firebaseApp = firebaseApp;
this.instanceRegistry = instanceRegistry;
this.metadataProvider = metadataProvider;
this.emulatorSettings = emulatorSettings;

settings = new FirebaseFirestoreSettings.Builder().build();
this.settings = new FirebaseFirestoreSettings.Builder().build();
if (this.emulatorSettings != null) {
this.settings = mergeEmulatorSettings(settings, emulatorSettings);
samtstern marked this conversation as resolved.
Show resolved Hide resolved
}
}

/** Returns the settings used by this {@code FirebaseFirestore} object. */
Expand All @@ -193,6 +212,11 @@ public void setFirestoreSettings(@NonNull FirebaseFirestoreSettings settings) {
+ "You can only call setFirestoreSettings() before calling any other methods on a "
+ "FirebaseFirestore object.");
}

if (this.emulatorSettings != null) {
samtstern marked this conversation as resolved.
Show resolved Hide resolved
settings = mergeEmulatorSettings(settings, this.emulatorSettings);
}

this.settings = settings;
}
}
Expand All @@ -215,6 +239,23 @@ private void ensureClientConfigured() {
}
}

private FirebaseFirestoreSettings mergeEmulatorSettings(
@NonNull FirebaseFirestoreSettings settings,
@NonNull EmulatedServiceSettings emulatorSettings) {

if (!FirebaseFirestoreSettings.DEFAULT_HOST.equals(settings.getHost())) {
throw new IllegalStateException(
"Cannot change the Firestore host through FirebaseFirestoreSettings when "
samtstern marked this conversation as resolved.
Show resolved Hide resolved
+ "EmulatedServiceSettings are also in effect. "
+ "Make sure to only set the host in one location.");
}
samtstern marked this conversation as resolved.
Show resolved Hide resolved

return new FirebaseFirestoreSettings.Builder(settings)
.setHost(emulatorSettings.getHost() + ":" + emulatorSettings.getPort())
.setSslEnabled(false)
.build();
}

/** Returns the FirebaseApp instance to which this {@code FirebaseFirestore} belongs. */
@NonNull
public FirebaseApp getApp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ public final class FirebaseFirestoreSettings {
*/
public static final long CACHE_SIZE_UNLIMITED = -1;

/** @hide */
public static final String DEFAULT_HOST = "firestore.googleapis.com";

private static final long MINIMUM_CACHE_BYTES = 1 * 1024 * 1024; // 1 MB
private static final long DEFAULT_CACHE_SIZE_BYTES = 100 * 1024 * 1024; // 100 MB
private static final String DEFAULT_HOST = "firestore.googleapis.com";
private static final boolean DEFAULT_TIMESTAMPS_IN_SNAPSHOTS_ENABLED = true;

/** A Builder for creating {@code FirebaseFirestoreSettings}. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.firestore;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import androidx.annotation.NonNull;
import androidx.test.platform.app.InstrumentationRegistry;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.emulators.EmulatedServiceSettings;
import com.google.firebase.emulators.EmulatorSettings;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class FirebaseFirestoreTest {

@Test
public void getInstance_withEmulator() {
FirebaseApp app = getApp("getInstance_withEmulator");

app.enableEmulators(
new EmulatorSettings.Builder()
.addEmulatedService(
FirebaseFirestore.EMULATOR, new EmulatedServiceSettings("10.0.2.2", 8080))
.build());

FirebaseFirestore firestore = FirebaseFirestore.getInstance(app);
FirebaseFirestoreSettings settings = firestore.getFirestoreSettings();

assertEquals(settings.getHost(), "10.0.2.2:8080");
assertFalse(settings.isSslEnabled());
}

@Test
public void getInstance_withEmulator_mergeSettingsSuccess() {
FirebaseApp app = getApp("getInstance_withEmulator_mergeSettingsSuccess");
app.enableEmulators(
new EmulatorSettings.Builder()
.addEmulatedService(
FirebaseFirestore.EMULATOR, new EmulatedServiceSettings("10.0.2.2", 8080))
.build());

FirebaseFirestore firestore = FirebaseFirestore.getInstance(app);
firestore.setFirestoreSettings(
new FirebaseFirestoreSettings.Builder().setPersistenceEnabled(false).build());

FirebaseFirestoreSettings settings = firestore.getFirestoreSettings();

assertEquals(settings.getHost(), "10.0.2.2:8080");
assertFalse(settings.isSslEnabled());
assertFalse(settings.isPersistenceEnabled());
}

@Test(expected = IllegalStateException.class)
samtstern marked this conversation as resolved.
Show resolved Hide resolved
public void getInstance_withEmulator_mergeSettingsFailure() {
FirebaseApp app = getApp("getInstance_withEmulator_mergeSettingsFailure");
app.enableEmulators(
new EmulatorSettings.Builder()
.addEmulatedService(
FirebaseFirestore.EMULATOR, new EmulatedServiceSettings("10.0.2.2", 8080))
.build());

FirebaseFirestore firestore = FirebaseFirestore.getInstance(app);
firestore.setFirestoreSettings(
new FirebaseFirestoreSettings.Builder().setHost("myhost.com").build());
}
samtstern marked this conversation as resolved.
Show resolved Hide resolved

@NonNull
private FirebaseApp getApp(@NonNull String name) {
return FirebaseApp.initializeApp(
InstrumentationRegistry.getInstrumentation().getContext(),
new FirebaseOptions.Builder()
.setApplicationId("appid")
.setApiKey("apikey")
.setProjectId("projectid")
.build(),
name);
}
}