Skip to content

Emulator Idempotency: Database - Closed, new Revision available. #8764

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

Closed
wants to merge 9 commits into from
Closed
25 changes: 20 additions & 5 deletions packages/database/src/api/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { Provider } from '@firebase/component';
import {
getModularInstance,
createMockUserToken,
//deepEqual, DEDB replace
EmulatorMockTokenOptions,
getDefaultEmulatorHostnameAndPort
} from '@firebase/util';
Expand Down Expand Up @@ -85,11 +86,12 @@ let useRestClient = false;
function repoManagerApplyEmulatorSettings(
repo: Repo,
host: string,
port: number,
port: number, // DEDB remove
tokenProvider?: AuthTokenProvider
): void {
repo.repoInfo_ = new RepoInfo(
`${host}:${port}`,
//host, DDB replace
`${host}:${port}`, // DEDB remove
/* secure= */ false,
repo.repoInfo_.namespace,
repo.repoInfo_.webSocketOnly,
Expand Down Expand Up @@ -350,13 +352,24 @@ export function connectDatabaseEmulator(
): void {
db = getModularInstance(db);
db._checkNotDeleted('useEmulator');
const hostAndPort = `${host}:${port}`;
const repo = db._repoInternal;
/*
if (db._instanceStarted) {
// If the instance has already been started, then silenty fail if this function is called again
// with the same parameters. If the parameters differ then assert.
if (
hostAndPort === db._repoInternal.repoInfo_.host &&
deepEqual(options, repo.repoInfo_.emulatorOptions)
) {
return;
}
fatal(
'Cannot call useEmulator() after instance has already been initialized.'
'connectDatabaseEmulator() cannot alter the emulator configuration after the database instance has started.'
);
}
*/

const repo = db._repoInternal;
let tokenProvider: EmulatorTokenProvider | undefined = undefined;
if (repo.repoInfo_.nodeAdmin) {
if (options.mockUserToken) {
Expand All @@ -374,7 +387,9 @@ export function connectDatabaseEmulator(
}

// Modify the repo to apply emulator settings
repoManagerApplyEmulatorSettings(repo, host, port, tokenProvider);
//repoManagerApplyEmulatorSettings(repo, hostAndPort, tokenProvider); // DDB Replace
repoManagerApplyEmulatorSettings(repo, host, port, tokenProvider); // DDB Remove

}

/**
Expand Down
21 changes: 20 additions & 1 deletion packages/database/src/core/RepoInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
* limitations under the License.
*/

import { assert } from '@firebase/util';
import {
assert,
//EmulatorMockTokenOptions //DEDB replace
} from '@firebase/util';

import { LONG_POLLING, WEBSOCKET } from '../realtime/Constants';

Expand All @@ -28,6 +31,12 @@ import { each } from './util/util';
export class RepoInfo {
private _host: string;
private _domain: string;
/*
//DEDB replace
private _emulatorOptions: {
mockUserToken?: EmulatorMockTokenOptions | string;
};
*/
internalHost: string;

/**
Expand All @@ -50,6 +59,7 @@ export class RepoInfo {
) {
this._host = host.toLowerCase();
this._domain = this._host.substr(this._host.indexOf('.') + 1);
// this._emulatorOptions = {}; //DEDB replace
this.internalHost =
(PersistentStorage.get('host:' + host) as string) || this._host;
}
Expand Down Expand Up @@ -78,6 +88,15 @@ export class RepoInfo {
}
}

/*
//DEDB replace
get emulatorOptions(): {
mockUserToken?: EmulatorMockTokenOptions | string;
} {
return this._emulatorOptions;
}
*/

toString(): string {
let str = this.toURLString();
if (this.persistenceKey) {
Expand Down
44 changes: 44 additions & 0 deletions packages/database/test/exp/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Deferred } from '@firebase/util';
import { expect, use } from 'chai';
import chaiAsPromised from 'chai-as-promised';

//import { connectDatabaseEmulator } from '../../src/api/Database';
import {
child,
get,
Expand All @@ -46,8 +47,10 @@ import { EventAccumulatorFactory } from '../helpers/EventAccumulator';
import {
DATABASE_ADDRESS,
DATABASE_URL,
//EMULATOR_PORT,
getFreshRepo,
getRWRefs,
//isEmulatorActive,
waitFor,
waitUntil,
writeAndValidate
Expand Down Expand Up @@ -138,6 +141,47 @@ describe('Database@exp Tests', () => {
unsubscribe();
});

/*it('can connected to emulator', async () => {
if (isEmulatorActive()) {
const db = getDatabase(defaultApp);
connectDatabaseEmulator(db, 'localhost', parseInt(EMULATOR_PORT, 10));
await get(refFromURL(db, `${DATABASE_ADDRESS}/foo/bar`));
}
});

it('can chnage emulator config before network operations', async () => {
if (isEmulatorActive()) {
const db = getDatabase(defaultApp);
const port = parseInt(EMULATOR_PORT, 10);
connectDatabaseEmulator(db, 'localhost', port + 1);
connectDatabaseEmulator(db, 'localhost', port);
await get(refFromURL(db, `${DATABASE_ADDRESS}/foo/bar`));
}
});

it('can connected to emulator after network operations with same parameters', async () => {
if (isEmulatorActive()) {
const db = getDatabase(defaultApp);
const port = parseInt(EMULATOR_PORT, 10);
connectDatabaseEmulator(db, 'localhost', port);
await get(refFromURL(db, `${DATABASE_ADDRESS}/foo/bar`));
connectDatabaseEmulator(db, 'localhost', port);
}
});

it('cannot connect to emulator after network operations with different parameters', async () => {
if (isEmulatorActive()) {
const db = getDatabase(defaultApp);
const port = parseInt(EMULATOR_PORT, 10);
connectDatabaseEmulator(db, 'localhost', port);
await get(refFromURL(db, `${DATABASE_ADDRESS}/foo/bar`));
expect(() => {
connectDatabaseEmulator(db, 'localhost', 9001);
}).to.throw();
}
});
*/

it('can properly handle unknown deep merges', async () => {
// Note: This test requires `testIndex` to be added as an index.
// Please run `yarn test:setup` to ensure that this gets added.
Expand Down
6 changes: 5 additions & 1 deletion packages/database/test/helpers/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { EventAccumulator } from './EventAccumulator';

// eslint-disable-next-line @typescript-eslint/no-require-imports
export const TEST_PROJECT = require('../../../../config/project.json');
const EMULATOR_PORT = process.env.RTDB_EMULATOR_PORT;
export const EMULATOR_PORT = process.env.RTDB_EMULATOR_PORT;
const EMULATOR_NAMESPACE = process.env.RTDB_EMULATOR_NAMESPACE;
const USE_EMULATOR = !!EMULATOR_PORT;

Expand Down Expand Up @@ -143,3 +143,7 @@ export async function waitUntil(cb: () => boolean, maxRetries = 5) {
}
});
}

export function isEmulatorActive(): boolean {
return USE_EMULATOR;
}
Loading