Skip to content

Commit c2e1d3d

Browse files
authored
Try to address flaky database transaction tests. (#235)
The test "Transaction without local events (2)" is inherently racey (we're trying to do 4 sets on one connection before the other connection retries the transaction 25 times). And when it fails, it could cause the rest of the tests to fail by not calling restoreHash(). So I've disabled the test and made the restoreHash handling a bit more robust so similar cascading failures can't happen in the future.
1 parent f13188b commit c2e1d3d

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

packages/database/test/transaction.test.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ import firebase from '@firebase/app';
3232
import '../index';
3333

3434
describe('Transaction Tests', function() {
35+
// Tests that use hijackHash() should set restoreHash to the restore function
36+
// and be sure to call it (and set restoreHash back to null) before the test
37+
// exits. In the case that the test fails to do so, we'll log a warning and
38+
// call restoreHash() manually to ensure subsequent tests aren't affected.
39+
let restoreHash = null;
40+
41+
afterEach(() => {
42+
if (restoreHash) {
43+
console.warn("Prior test didn't properly call restoreHash()!");
44+
restoreHash();
45+
restoreHash = null;
46+
}
47+
});
48+
3549
it('New value is immediately visible.', function() {
3650
const node = getRandomNode() as Reference;
3751
node.child('foo').transaction(function() {
@@ -45,7 +59,7 @@ describe('Transaction Tests', function() {
4559
expect(val).to.equal(42);
4660
});
4761

48-
it.skip('Event is raised for new value.', function() {
62+
it('Event is raised for new value.', function() {
4963
const node = getRandomNode() as Reference;
5064
const fooNode = node.child('foo');
5165
const eventHelper = eventTestHelper([[fooNode, ['value', '']]]);
@@ -475,7 +489,7 @@ describe('Transaction Tests', function() {
475489
});
476490

477491
it('Transaction aborts after 25 retries.', function(done) {
478-
const restoreHash = hijackHash(function() {
492+
restoreHash = hijackHash(function() {
479493
return 'duck, duck, goose.';
480494
});
481495

@@ -492,6 +506,7 @@ describe('Transaction Tests', function() {
492506
expect(committed).to.equal(false);
493507
expect(tries).to.equal(25);
494508
restoreHash();
509+
restoreHash = null;
495510
done();
496511
}
497512
);
@@ -526,7 +541,7 @@ describe('Transaction Tests', function() {
526541
const node = getRandomNode() as Reference;
527542
let fooTransactionDone = false;
528543
let barTransactionDone = false;
529-
const restoreHash = hijackHash(function() {
544+
restoreHash = hijackHash(function() {
530545
return 'foobar';
531546
});
532547

@@ -569,6 +584,7 @@ describe('Transaction Tests', function() {
569584
expect(fooTransactionDone).to.equal(true);
570585
expect(barTransactionDone).to.equal(false);
571586
restoreHash();
587+
restoreHash = null;
572588
});
573589

574590
it('Test transaction on wacky unicode data.', function(done) {
@@ -793,11 +809,14 @@ describe('Transaction Tests', function() {
793809

794810
// This test is meant to ensure that with applyLocally=false, while the transaction is outstanding, we continue
795811
// to get events from other clients.
796-
it('Transaction without local events (2)', function(done) {
812+
// TODO(mikelehen): Unfortunately this test is currently flaky. It's inherently a racey test since it's
813+
// trying to do 4 sets before the transaction retries 25 times (and fails), using two different connections.
814+
// Disabling for now until we rework the approach.
815+
it.skip('Transaction without local events (2)', function(done) {
797816
const refPair = getRandomNode(2) as Reference[],
798817
ref1 = refPair[0],
799818
ref2 = refPair[1];
800-
const restoreHash = hijackHash(function() {
819+
restoreHash = hijackHash(function() {
801820
return 'badhash';
802821
});
803822
const SETS = 4;
@@ -818,6 +837,7 @@ describe('Transaction Tests', function() {
818837

819838
if (current === SETS - 1) {
820839
restoreHash();
840+
restoreHash = null;
821841
}
822842
return 'txn result';
823843
},
@@ -856,7 +876,10 @@ describe('Transaction Tests', function() {
856876
}
857877
expect(events[SETS]).to.equal('txn result');
858878

859-
restoreHash();
879+
if (restoreHash) {
880+
restoreHash();
881+
restoreHash = null;
882+
}
860883
done();
861884
});
862885
}

0 commit comments

Comments
 (0)