Skip to content

Commit 88734fd

Browse files
author
Michael Lehenbauer
authored
Fix flaky RTDB tests due to increment operations. (#2466)
The introduction of tests that try to send increment operations to the backend has resulted in test flakiness. Basically the emulator rejects these as invalid operations and closes the connection. The client automatically reconnects, so _usually_ this doesn't matter. But the SDK intentionally doesn't tolerate disconnects in the middle of a transaction, so if the disconnect happens to happen during a transaction test, then the test will fail. I'm avoiding the problem by isolating the increment tests better (use an isolated instance of RTDB and ensures it remains offline so it doesn't even try to send the requests to the emulator). I've opened b/146657568 to track cleaning this up once the emulator supports increment operations.
1 parent 984c1e4 commit 88734fd

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

packages/database/test/servervalues.test.ts

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

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

5256
node.set(addOne);
5357
});
5458

5559
it('handles increments locally', async () => {
56-
const node = getRandomNode() as Reference;
60+
// TODO(b/146657568): Remove getFreshRepoFromReference() call and goOffline()
61+
// once we have emulator support. We can also await the set() calls.
62+
const node = getFreshRepoFromReference(getRandomNode()) as Reference;
63+
node.database.goOffline();
64+
5765
const addOne = Database.ServerValue._increment(1);
5866

59-
// Must go offline because the latest emulator may not support this server op
60-
// This also means we can't await node operations, which would block the test.
61-
node.database.goOffline();
62-
try {
63-
const values: any[] = [];
64-
const expected: any[] = [];
65-
node.on('value', snap => values.push(snap.val()));
67+
const values: any = [];
68+
const expected: any = [];
69+
node.on('value', snap => values.push(snap.val()));
6670

67-
// null -> increment(x) = x
68-
node.set(addOne);
69-
expected.push(1);
71+
// null -> increment(x) = x
72+
node.set(addOne);
73+
expected.push(1);
7074

71-
// x -> increment(y) = x + y
72-
node.set(5);
73-
node.set(addOne);
74-
expected.push(5);
75-
expected.push(6);
75+
// x -> increment(y) = x + y
76+
node.set(5);
77+
node.set(addOne);
78+
expected.push(5);
79+
expected.push(6);
7680

77-
// str -> increment(x) = x
78-
node.set('hello');
79-
node.set(addOne);
80-
expected.push('hello');
81-
expected.push(1);
81+
// str -> increment(x) = x
82+
node.set('hello');
83+
node.set(addOne);
84+
expected.push('hello');
85+
expected.push(1);
8286

83-
// obj -> increment(x) = x
84-
node.set({ 'hello': 'world' });
85-
node.set(addOne);
86-
expected.push({ 'hello': 'world' });
87-
expected.push(1);
87+
// obj -> increment(x) = x
88+
node.set({ 'hello': 'world' });
89+
node.set(addOne);
90+
expected.push({ 'hello': 'world' });
91+
expected.push(1);
8892

89-
node.off('value');
90-
expect(values).to.deep.equal(expected);
91-
} finally {
92-
node.database.goOnline();
93-
}
93+
node.off('value');
94+
expect(values).to.deep.equal(expected);
9495
});
9596
});

0 commit comments

Comments
 (0)