Skip to content

WIP: Fix issue where emulator connects to the wrong host upon reconnect #5903

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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 firebase-database/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
# limitations under the License.

version=21.0.0
latestReleasedVersion=20.3.1
latestReleasedVersion=20.3.2
android.enableUnitTestBinaryResources=true
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void onCacheHost(String s) {}
IntegrationTestValues.getNamespace(),
/*secure=*/ true);
DatabaseConfig config = IntegrationTestHelpers.newFrozenTestConfig();
Connection conn = new Connection(config.getConnectionContext(), info, null, del, null, "");
Connection conn = new Connection(config.getConnectionContext(), info, null, del, null, "", false);
conn.open();
IntegrationTestHelpers.waitFor(valSemaphore);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public SynchronousConnection(String host) {
RepoInfo repoInfo = parsed.repoInfo;
HostInfo hostInfo = new HostInfo(repoInfo.host, repoInfo.namespace, repoInfo.secure);

connection = new PersistentConnectionImpl(context.getConnectionContext(), hostInfo, this);
connection = new PersistentConnectionImpl(context.getConnectionContext(), hostInfo, this, false);
this.connectSemaphore = new Semaphore(0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,13 @@ public synchronized void setPersistenceCacheSizeBytes(long cacheSizeInBytes) {
* @param port the emulator port (for example, 9000)
*/
public void useEmulator(@NonNull String host, int port) {
System.out.println("Using Emulator");
if (this.repo != null) {
throw new IllegalStateException(
"Cannot call useEmulator() after instance has already been initialized.");
}

this.emulatorSettings = new EmulatedServiceSettings(host, port);
this.config.setUsingEmulator(true);
}

/** @return The semver version for this build of the Firebase Database client */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ public PersistentConnection newPersistentConnection(
ConnectionContext connectionContext,
HostInfo info,
PersistentConnection.Delegate delegate) {

final PersistentConnection connection =
new PersistentConnectionImpl(connectionContext, info, delegate);
new PersistentConnectionImpl(connectionContext, info, delegate, context.isUsingEmulator());

// TODO: Ideally we would remove this listener at some point, but right now
// there's no cleanup path for PersistentConnection (or Repo, etc.). They live forever.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,22 @@ private enum State {
private State state;
private final LogWrapper logger;

private boolean isUsingEmulator;

public Connection(
ConnectionContext context,
HostInfo hostInfo,
String cachedHost,
Delegate delegate,
String optLastSessionId,
String appCheckToken) {
String appCheckToken,
boolean isUsingEmulator) {
long connId = connectionIds++;
this.hostInfo = hostInfo;
this.delegate = delegate;
this.logger = new LogWrapper(context.getLogger(), "Connection", "conn_" + connId);
this.state = State.REALTIME_CONNECTING;
this.isUsingEmulator = isUsingEmulator;
this.conn =
new WebsocketConnection(
context, hostInfo, cachedHost, appCheckToken, this, optLastSessionId);
Expand Down Expand Up @@ -205,7 +209,9 @@ private void onConnectionShutdown(String reason) {
private void onHandshake(Map<String, Object> handshake) {
long timestamp = (Long) handshake.get(SERVER_HELLO_TIMESTAMP);
String host = (String) handshake.get(SERVER_HELLO_HOST);
delegate.onCacheHost(host);
if(!this.isUsingEmulator) {
delegate.onCacheHost(host);
}
String sessionId = (String) handshake.get(SERVER_HELLO_SESSION_ID);

if (state == State.REALTIME_CONNECTING) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,13 @@ private enum ConnectionState {
private long lastWriteTimestamp;
private boolean hasOnDisconnects;

private boolean isUsingEmulator;

public PersistentConnectionImpl(
ConnectionContext context, HostInfo info, final Delegate delegate) {
ConnectionContext context, HostInfo info, final Delegate delegate, boolean isUsingEmulator) {
this.delegate = delegate;
this.context = context;
this.isUsingEmulator = isUsingEmulator;
this.executorService = context.getExecutorService();
this.authTokenProvider = context.getAuthTokenProvider();
this.appCheckTokenProvider = context.getAppCheckTokenProvider();
Expand Down Expand Up @@ -829,7 +832,7 @@ public void openNetworkConnection(String authToken, String appCheckToken) {
this.connectionState = ConnectionState.Connecting;
realtime =
new Connection(
this.context, this.hostInfo, this.cachedHost, this, this.lastSessionId, appCheckToken);
this.context, this.hostInfo, this.cachedHost, this, this.lastSessionId, appCheckToken, this.isUsingEmulator);
realtime.open();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public class Context {

private Platform platform;

protected boolean isUsingEmulator = false;

private Platform getPlatform() {
if (platform == null) {
initializeAndroidPlatform();
Expand All @@ -65,6 +67,10 @@ private synchronized void initializeAndroidPlatform() {
platform = new AndroidPlatform(this.firebaseApp);
}

public boolean isUsingEmulator() {
return this.isUsingEmulator;
}

public boolean isFrozen() {
return frozen;
}
Expand Down Expand Up @@ -139,6 +145,7 @@ public LogWrapper getLogger(String component, String prefix) {
return new LogWrapper(logger, component, prefix);
}

// TODO(mtewani): fill this in instead of repoinfo
public ConnectionContext getConnectionContext() {
return new ConnectionContext(
this.getLogger(),
Expand Down Expand Up @@ -211,8 +218,9 @@ public TokenProvider getAppCheckTokenProvider() {
return this.appCheckTokenProvider;
}


public PersistentConnection newPersistentConnection(
HostInfo info, PersistentConnection.Delegate delegate) {
HostInfo info, PersistentConnection.Delegate delegate) {
return getPlatform().newPersistentConnection(this, this.getConnectionContext(), info, delegate);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ public synchronized void setPersistenceCacheSizeBytes(long cacheSizeInBytes) {
this.cacheSize = cacheSizeInBytes;
}

/**
* @hide
* @param isUsingEmulator
*/
public synchronized void setUsingEmulator(boolean isUsingEmulator) {
this.isUsingEmulator = isUsingEmulator;
}

public synchronized void setFirebaseApp(FirebaseApp app) {
this.firebaseApp = app;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public final class RepoInfo {
public String namespace;
public String internalHost;


@Override
public String toString() {
return "http" + (secure ? "s" : "") + "://" + host;
Expand Down
Loading