-
Notifications
You must be signed in to change notification settings - Fork 617
Unified emulator settings for Database #1672
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
Changes from 9 commits
9ef8a4b
9d4be7d
1ca25d4
f39c11f
0f931bb
fb262d3
2762764
24b8f3a
4dd3c23
f7e2832
4ef9919
a2a4cd6
4e1373f
7f173e0
6d1e179
2d67b78
8a51a73
b072f54
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,8 @@ | |
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.emulators.FirebaseEmulator; | ||
import com.google.firebase.events.Publisher; | ||
import com.google.firebase.heartbeatinfo.DefaultHeartBeatInfo; | ||
import com.google.firebase.internal.DataCollectionConfigStorage; | ||
|
@@ -109,6 +111,7 @@ public class FirebaseApp { | |
private final String name; | ||
private final FirebaseOptions options; | ||
private final ComponentRuntime componentRuntime; | ||
private EmulatorSettings emulatorSettings = EmulatorSettings.getDefault(); | ||
|
||
// Default disabled. We released Firebase publicly without this feature, so making it default | ||
// enabled is a backwards incompatible change. | ||
|
@@ -142,6 +145,19 @@ 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(); | ||
return emulatorSettings; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (!(o instanceof FirebaseApp)) { | ||
|
@@ -305,6 +321,26 @@ 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 FirebaseEmulator#FIRESTORE}, | ||
* 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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How will this interact with firestore's firestore.terminate();
app.updateFirestoresEmulator();
Firebase.firestore.use(); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for calling this out, I resolved all the other comments since they were straightforward but I will have to look into this one more deeply. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok actually I do not want to allow that sequence of calls. One of the main reasons for this is to make sure that app developers can reason about their emulation settings at a single point before any Firebase service has done any communication. If you call WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point and sgtm. You may want to confirm this behavior with firestore folks though There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do during the Firestore PR. |
||
!this.emulatorSettings.isAccessed(), | ||
"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. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// 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.emulators; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
/** | ||
* 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 class EmulatedServiceSettings { | ||
samtstern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public static final class Builder { | ||
|
||
private final String host; | ||
private final int port; | ||
|
||
/** | ||
* Create a new EmulatedServiceSettings builder. | ||
* | ||
* @param host the host where the local emulator is running. If you want to access 'localhost' | ||
* from an Android Emulator use '10.0.2.2' instead. | ||
* @param port the port where the local emulator is running. | ||
*/ | ||
public Builder(@NonNull String host, int port) { | ||
samtstern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.host = host; | ||
this.port = port; | ||
} | ||
|
||
@NonNull | ||
public EmulatedServiceSettings build() { | ||
return new EmulatedServiceSettings(this.host, this.port); | ||
} | ||
} | ||
|
||
public final String host; | ||
public final int port; | ||
samtstern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private EmulatedServiceSettings(@NonNull String host, int port) { | ||
this.host = host; | ||
this.port = port; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// 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.emulators; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import com.google.firebase.components.Preconditions; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
/** | ||
* Settings that control which Firebase services should access a local emulator, rather than | ||
* production. | ||
* | ||
* <p>TODO(samstern): Un-hide this once Firestore, Database, and Functions are implemented | ||
* | ||
* @see com.google.firebase.FirebaseApp#enableEmulators(EmulatorSettings) | ||
* @hide | ||
*/ | ||
public class EmulatorSettings { | ||
samtstern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** Empty emulator settings to be used as an internal default */ | ||
public static EmulatorSettings getDefault() { | ||
return new EmulatorSettings.Builder().build(); | ||
} | ||
|
||
public static final class Builder { | ||
|
||
private final Map<FirebaseEmulator, EmulatedServiceSettings> settingsMap = new HashMap<>(); | ||
|
||
/** Constructs an empty builder. */ | ||
public Builder() {} | ||
|
||
/** | ||
* Specify the emulator settings for a single service. | ||
* | ||
* @param emulator the emulated service. | ||
* @param settings the emulator settings. | ||
* @return the builder, for chaining. | ||
*/ | ||
@NonNull | ||
public Builder addEmulatedService( | ||
@NonNull FirebaseEmulator emulator, @NonNull EmulatedServiceSettings settings) { | ||
Preconditions.checkState( | ||
!settingsMap.containsKey(emulator), | ||
"Cannot call addEmulatedService twice for " + emulator.toString()); | ||
this.settingsMap.put(emulator, settings); | ||
return this; | ||
} | ||
|
||
@NonNull | ||
public EmulatorSettings build() { | ||
return new EmulatorSettings(new HashMap<>(settingsMap)); | ||
} | ||
} | ||
|
||
private final AtomicBoolean accessed = new AtomicBoolean(false); | ||
private final Map<FirebaseEmulator, EmulatedServiceSettings> settingsMap; | ||
|
||
private EmulatorSettings(@NonNull Map<FirebaseEmulator, EmulatedServiceSettings> settingsMap) { | ||
this.settingsMap = Collections.unmodifiableMap(settingsMap); | ||
} | ||
|
||
/** | ||
* Determine if any Firebase SDK has already accessed the emulator settings. When true, attempting | ||
* to change the settings should throw an error. | ||
* | ||
* @hide | ||
*/ | ||
public boolean isAccessed() { | ||
samtstern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return accessed.get(); | ||
} | ||
|
||
/** | ||
* Fetch the emulation settings for a single Firebase service. Once this method has been called | ||
* {@link #isAccessed()} will return true. | ||
* | ||
* @hide | ||
*/ | ||
@Nullable | ||
public EmulatedServiceSettings getServiceSettings(@NonNull FirebaseEmulator emulator) { | ||
accessed.set(true); | ||
samtstern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (settingsMap.containsKey(emulator)) { | ||
return settingsMap.get(emulator); | ||
} | ||
|
||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// 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.emulators; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
/** | ||
* Identifier Firebase services that can be emulated using the Firebase Emulator Suite. | ||
* | ||
* <p>TODO(samstern): Un-hide this once Firestore, Database, and Functions are implemented | ||
* | ||
* @see com.google.firebase.FirebaseApp#enableEmulators(EmulatorSettings) | ||
* @see EmulatorSettings | ||
* @see EmulatedServiceSettings | ||
* @hide | ||
*/ | ||
public class FirebaseEmulator { | ||
samtstern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public final String name; | ||
samtstern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Only to be called by SDKs which support emulators in order to make constants. | ||
* | ||
* @hide | ||
*/ | ||
@NonNull | ||
public static FirebaseEmulator forName(String name) { | ||
return new FirebaseEmulator(name); | ||
} | ||
|
||
private FirebaseEmulator(String name) { | ||
this.name = name; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: now that settings are immutable, consider sharing the default instance across app instances.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah good call!