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 @@ -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,10 @@ 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();
this.settings = mergeEmulatorSettings(settings, emulatorSettings);
}

/** Returns the settings used by this {@code FirebaseFirestore} object. */
Expand All @@ -185,6 +202,8 @@ public FirebaseFirestoreSettings getFirestoreSettings() {
public void setFirestoreSettings(@NonNull FirebaseFirestoreSettings settings) {
synchronized (databaseId) {
checkNotNull(settings, "Provided settings must not be null.");
settings = mergeEmulatorSettings(settings, emulatorSettings);

// As a special exception, don't throw if the same settings are passed repeatedly. This
// should make it simpler to get a Firestore instance in an activity.
if (client != null && !this.settings.equals(settings)) {
Expand All @@ -193,6 +212,7 @@ public void setFirestoreSettings(@NonNull FirebaseFirestoreSettings settings) {
+ "You can only call setFirestoreSettings() before calling any other methods on a "
+ "FirebaseFirestore object.");
}

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

private FirebaseFirestoreSettings mergeEmulatorSettings(
@NonNull FirebaseFirestoreSettings settings,
@Nullable EmulatedServiceSettings emulatorSettings) {
if (emulatorSettings == null) {
return settings;
}

if (!FirebaseFirestoreSettings.DEFAULT_HOST.equals(settings.getHost())) {
throw new IllegalStateException(
"Cannot specify the host in FirebaseFirestoreSettings when EmulatedServiceSettings is provided.");
}

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,176 @@
// 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 static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

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
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());

try {
FirebaseFirestore firestore = FirebaseFirestore.getInstance(app);
firestore.setFirestoreSettings(
new FirebaseFirestoreSettings.Builder().setHost("myhost.com").build());
fail("Exception should be thrown");
} catch (Exception e) {
assertTrue(e instanceof IllegalStateException);
assertEquals(
e.getMessage(),
"Cannot specify the host in FirebaseFirestoreSettings when EmulatedServiceSettings is provided.");
}
}

@Test
public void setSettings_repeatedSuccess() {
FirebaseApp app = getApp("setSettings_repeatedSuccess");
samtstern marked this conversation as resolved.
Show resolved Hide resolved
FirebaseFirestore firestore = FirebaseFirestore.getInstance(app);

FirebaseFirestoreSettings settings =
new FirebaseFirestoreSettings.Builder().setHost("myhost.com").setSslEnabled(false).build();
firestore.setFirestoreSettings(settings);

// This should 'start' Firestore
DocumentReference reference = firestore.document("foo/bar");

// Second settings set should pass because the settings are equal
firestore.setFirestoreSettings(settings);
}

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

FirebaseFirestore firestore = FirebaseFirestore.getInstance(app);

FirebaseFirestoreSettings settings =
new FirebaseFirestoreSettings.Builder().setPersistenceEnabled(false).build();
firestore.setFirestoreSettings(settings);

// This should 'start' Firestore
DocumentReference reference = firestore.document("foo/bar");

// Second settings set should pass because the settings are equal
firestore.setFirestoreSettings(settings);
}

@Test
public void setSettings_repeatedFailure() {
FirebaseApp app = getApp("setSettings_repeatedFailure");
FirebaseFirestore firestore = FirebaseFirestore.getInstance(app);

FirebaseFirestoreSettings settings =
new FirebaseFirestoreSettings.Builder().setHost("myhost.com").setSslEnabled(false).build();

FirebaseFirestoreSettings otherSettings =
new FirebaseFirestoreSettings.Builder()
.setHost("otherhost.com")
.setSslEnabled(false)
.build();

firestore.setFirestoreSettings(settings);

// This should 'start' Firestore
DocumentReference reference = firestore.document("foo/bar");

try {
firestore.setFirestoreSettings(otherSettings);
fail("Exception should be thrown");
} catch (Exception e) {
assertTrue(e instanceof IllegalStateException);
assertTrue(
e.getMessage()
.startsWith(
"FirebaseFirestore has already been started and its settings can no longer be changed."));
}
}
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);
}
}