Skip to content

Fix flaky RTDB tests due to increment operations. #2466

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 1 commit into from
Dec 20, 2019
Merged
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
67 changes: 34 additions & 33 deletions packages/database/test/servervalues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

import { expect } from 'chai';
import { getRandomNode } from './helpers/util';
import { getFreshRepoFromReference, getRandomNode } from './helpers/util';
import { Database } from '../src/api/Database';
import { Reference } from '../src/api/Reference';
import { nodeFromJSON } from '../src/core/snap/nodeFromJSON';
Expand Down Expand Up @@ -46,50 +46,51 @@ describe('ServerValue tests', () => {
// Ensure that increments don't explode when the SyncTree must return a null
// node (i.e. ChildrenNode.EMPTY_NODE) because there is not yet any synced
// data.
const node = getRandomNode() as Reference;
// TODO(b/146657568): Remove getFreshRepoFromReference() call and goOffline()
// once we have emulator support. We can also await the set() call.
const node = getFreshRepoFromReference(getRandomNode()) as Reference;
node.database.goOffline();

const addOne = Database.ServerValue._increment(1);

node.set(addOne);
});

it('handles increments locally', async () => {
const node = getRandomNode() as Reference;
// TODO(b/146657568): Remove getFreshRepoFromReference() call and goOffline()
// once we have emulator support. We can also await the set() calls.
const node = getFreshRepoFromReference(getRandomNode()) as Reference;
node.database.goOffline();

const addOne = Database.ServerValue._increment(1);

// Must go offline because the latest emulator may not support this server op
// This also means we can't await node operations, which would block the test.
node.database.goOffline();
try {
const values: any[] = [];
const expected: any[] = [];
node.on('value', snap => values.push(snap.val()));
const values: any = [];
const expected: any = [];
node.on('value', snap => values.push(snap.val()));

// null -> increment(x) = x
node.set(addOne);
expected.push(1);
// null -> increment(x) = x
node.set(addOne);
expected.push(1);

// x -> increment(y) = x + y
node.set(5);
node.set(addOne);
expected.push(5);
expected.push(6);
// x -> increment(y) = x + y
node.set(5);
node.set(addOne);
expected.push(5);
expected.push(6);

// str -> increment(x) = x
node.set('hello');
node.set(addOne);
expected.push('hello');
expected.push(1);
// str -> increment(x) = x
node.set('hello');
node.set(addOne);
expected.push('hello');
expected.push(1);

// obj -> increment(x) = x
node.set({ 'hello': 'world' });
node.set(addOne);
expected.push({ 'hello': 'world' });
expected.push(1);
// obj -> increment(x) = x
node.set({ 'hello': 'world' });
node.set(addOne);
expected.push({ 'hello': 'world' });
expected.push(1);

node.off('value');
expect(values).to.deep.equal(expected);
} finally {
node.database.goOnline();
}
node.off('value');
expect(values).to.deep.equal(expected);
});
});