Skip to content

Re-implement Unified Emulator Settings #1802

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

Merged
merged 21 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/ktx/firestore.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ firestore.setFirestoreSettings(settings)
**Kotlin + KTX**
```kotlin
firestore.firestoreSettings = firestoreSettings {
host = "http://10.0.0.2:8080"
host = "http://10.0.2.2:8080"
isSslEnabled = false
isPersistenceEnabled = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@
import com.google.firebase.components.TestComponentOne;
import com.google.firebase.components.TestComponentTwo;
import com.google.firebase.components.TestUserAgentDependentComponent;
import com.google.firebase.emulators.EmulatedServiceSettings;
import com.google.firebase.emulators.EmulatorSettings;
import com.google.firebase.emulators.FirebaseEmulator;
import com.google.firebase.platforminfo.UserAgentPublisher;
import com.google.firebase.testing.FirebaseAppRule;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -420,43 +417,6 @@ public void testDirectBoot_shouldPreserveDataCollectionAfterUnlock() {
assertTrue(firebaseApp.isDataCollectionDefaultEnabled());
}

@Test
public void testEnableEmulators_shouldAllowDoubleSetBeforeAccess() {
Context mockContext = createForwardingMockContext();
FirebaseApp firebaseApp = FirebaseApp.initializeApp(mockContext);

// A developer would call FirebaseDatabase.EMULATOR but we can't introduce that
// dependency for this test.
FirebaseEmulator emulator = FirebaseEmulator.forName("database");

EmulatedServiceSettings databaseSettings = new EmulatedServiceSettings("10.0.2.2", 9000);
EmulatorSettings emulatorSettings =
new EmulatorSettings.Builder().addEmulatedService(emulator, databaseSettings).build();

// Set twice
firebaseApp.enableEmulators(emulatorSettings);
firebaseApp.enableEmulators(emulatorSettings);
}

@Test
public void testEnableEmulators_shouldThrowIfSetAfterAccess() {
Context mockContext = createForwardingMockContext();
FirebaseApp firebaseApp = FirebaseApp.initializeApp(mockContext);

FirebaseEmulator emulator = FirebaseEmulator.forName("database");

EmulatedServiceSettings databaseSettings = new EmulatedServiceSettings("10.0.2.2", 9000);
EmulatorSettings emulatorSettings =
new EmulatorSettings.Builder().addEmulatedService(emulator, databaseSettings).build();
firebaseApp.enableEmulators(emulatorSettings);

// Access (as if from the Database SDK)
firebaseApp.getEmulatorSettings().getServiceSettings(emulator);

// Try to set again
assertThrows(IllegalStateException.class, () -> firebaseApp.enableEmulators(emulatorSettings));
}

/** Returns mock context that forwards calls to targetContext and localBroadcastManager. */
private Context createForwardingMockContext() {
final UserManager spyUserManager = spy(targetContext.getSystemService(UserManager.class));
Expand Down
38 changes: 0 additions & 38 deletions firebase-common/src/main/java/com/google/firebase/FirebaseApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import com.google.firebase.components.ComponentRegistrar;
import com.google.firebase.components.ComponentRuntime;
import com.google.firebase.components.Lazy;
import com.google.firebase.emulators.EmulatorSettings;
import com.google.firebase.events.Publisher;
import com.google.firebase.heartbeatinfo.DefaultHeartBeatInfo;
import com.google.firebase.internal.DataCollectionConfigStorage;
Expand Down Expand Up @@ -111,9 +110,6 @@ public class FirebaseApp {
private final FirebaseOptions options;
private final ComponentRuntime componentRuntime;

private final AtomicBoolean emulatorSettingsFrozen = new AtomicBoolean(false);
private EmulatorSettings emulatorSettings = EmulatorSettings.DEFAULT;

// Default disabled. We released Firebase publicly without this feature, so making it default
// enabled is a backwards incompatible change.
private final AtomicBoolean automaticResourceManagementEnabled = new AtomicBoolean(false);
Expand Down Expand Up @@ -146,20 +142,6 @@ public FirebaseOptions getOptions() {
return options;
}

/**
* Returns the specified {@link EmulatorSettings} or a default.
*
* <p>TODO(samstern): Un-hide this once Firestore, Database, and Functions are implemented
*
* @hide
*/
@NonNull
public EmulatorSettings getEmulatorSettings() {
checkNotDeleted();
emulatorSettingsFrozen.set(true);
return emulatorSettings;
}

@Override
public boolean equals(Object o) {
if (!(o instanceof FirebaseApp)) {
Expand Down Expand Up @@ -323,26 +305,6 @@ public static FirebaseApp initializeApp(
return firebaseApp;
}

/**
* Specify which services should access local emulators for this FirebaseApp instance.
*
* <p>For example, if the {@link EmulatorSettings} contain {@link
* com.google.firebase.emulators.EmulatedServiceSettings} for {@link FirebaseDatabase#EMULATOR},
* then calls to Cloud Firestore will communicate with the emulator rather than production.
*
* <p>TODO(samstern): Un-hide this once Firestore, Database, and Functions are implemented
*
* @param emulatorSettings the emulator settings for all services.
* @hide
*/
public void enableEmulators(@NonNull EmulatorSettings emulatorSettings) {
checkNotDeleted();
Preconditions.checkState(
!this.emulatorSettingsFrozen.get(),
"Cannot enable emulators after Firebase SDKs have already been used.");
this.emulatorSettings = emulatorSettings;
}

/**
* Deletes the {@link FirebaseApp} and all its data. All calls to this {@link FirebaseApp}
* instance will throw once it has been called.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
/**
* Settings to connect a single Firebase service to a local emulator.
*
* <p>TODO(samstern): Un-hide this once Firestore, Database, and Functions are implemented
*
* @see EmulatorSettings
* @hide
*/
public final class EmulatedServiceSettings {
Expand Down

This file was deleted.

This file was deleted.

2 changes: 2 additions & 0 deletions firebase-database/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Unreleased

- [changed] Added internal HTTP header to the WebChannel connection.
- [feature] Realtime Database now supports connecting to a local emulator via
`FirebaseDatabase#useEmulator()`

# 19.3.0
- [feature] Added ServerValue.increment() to support atomic field value increments
Expand Down
1 change: 1 addition & 0 deletions firebase-database/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ package com.google.firebase.database {
method public void setLogLevel(@NonNull com.google.firebase.database.Logger.Level);
method public void setPersistenceCacheSizeBytes(long);
method public void setPersistenceEnabled(boolean);
method public void useEmulator(@NonNull String, int);
}

public abstract class GenericTypeIndicator<T> {
Expand Down
2 changes: 1 addition & 1 deletion firebase-database/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

version=19.3.1
version=19.4.0
latestReleasedVersion=19.3.0
android.enableUnitTestBinaryResources=true
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import com.google.firebase.database.core.persistence.MockPersistenceStorageEngine;
import com.google.firebase.database.core.persistence.PersistenceManager;
import com.google.firebase.database.future.WriteFuture;
import com.google.firebase.emulators.EmulatedServiceSettings;
import com.google.firebase.emulators.EmulatorSettings;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -71,19 +69,36 @@ public void getInstanceForAppWithEmulator() {
FirebaseApp app =
appForDatabaseUrl(IntegrationTestValues.getAltNamespace(), "getInstanceForAppWithEmulator");

EmulatedServiceSettings serviceSettings = new EmulatedServiceSettings("10.0.2.2", 9000);
EmulatorSettings emulatorSettings =
new EmulatorSettings.Builder()
.addEmulatedService(FirebaseDatabase.EMULATOR, serviceSettings)
.build();
app.enableEmulators(emulatorSettings);

FirebaseDatabase db = FirebaseDatabase.getInstance(app);
db.useEmulator("10.0.2.2", 9000);

DatabaseReference rootRef = db.getReference();
assertEquals(rootRef.toString(), "http://10.0.2.2:9000");

DatabaseReference urlReference = db.getReferenceFromUrl("https://otherns.firebaseio.com");
assertEquals(urlReference.toString(), "http://10.0.2.2:9000");

DatabaseReference urlReferenceWithPath =
db.getReferenceFromUrl("https://otherns.firebaseio.com/foo");
assertEquals(urlReferenceWithPath.toString(), "http://10.0.2.2:9000/foo");
}

@Test
public void getInstanceForAppWithEmulator_throwsIfSetLate() {
FirebaseApp app =
appForDatabaseUrl(
IntegrationTestValues.getAltNamespace(),
"getInstanceForAppWithEmulator_throwsIfSetLate");

FirebaseDatabase db = FirebaseDatabase.getInstance(app);
DatabaseReference rootRef = db.getReference();

try {
db.useEmulator("10.0.2.2", 9000);
fail("Expected to throw");
} catch (IllegalStateException e) {
// Expected to throw
}
}

@Test
Expand Down
Loading