|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2019 Google Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import * as persistenceHelpers from './persistence_test_helpers'; |
| 19 | +import { expect } from 'chai'; |
| 20 | +import { IndexedDbPersistence } from '../../../src/local/indexeddb_persistence'; |
| 21 | +import { Persistence } from '../../../src/local/persistence'; |
| 22 | +import { PersistencePromise } from '../../../src/local/persistence_promise'; |
| 23 | +import { DbTarget, DbTargetKey } from '../../../src/local/indexeddb_schema'; |
| 24 | +import { TargetId } from '../../../src/core/types'; |
| 25 | + |
| 26 | +let persistence: Persistence; |
| 27 | + |
| 28 | +describe('MemoryTransaction', () => { |
| 29 | + beforeEach(() => { |
| 30 | + return persistenceHelpers.testMemoryEagerPersistence().then(p => { |
| 31 | + persistence = p; |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + genericTransactionTests(); |
| 36 | +}); |
| 37 | + |
| 38 | +describe('IndexedDbTransaction', () => { |
| 39 | + if (!IndexedDbPersistence.isAvailable()) { |
| 40 | + console.warn('No IndexedDB. Skipping IndexedDbTransaction tests.'); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + beforeEach(() => { |
| 45 | + return persistenceHelpers.testIndexedDbPersistence().then(p => { |
| 46 | + persistence = p; |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + afterEach(() => persistence.shutdown()); |
| 51 | + |
| 52 | + genericTransactionTests(); |
| 53 | + |
| 54 | + it('only invokes onCommittedListener once with retries', async () => { |
| 55 | + let runCount = 0; |
| 56 | + let commitCount = 0; |
| 57 | + await persistence.runTransaction( |
| 58 | + 'onCommitted', |
| 59 | + 'readwrite-idempotent', |
| 60 | + txn => { |
| 61 | + const targetsStore = IndexedDbPersistence.getStore< |
| 62 | + DbTargetKey, |
| 63 | + { targetId: TargetId } |
| 64 | + >(txn, DbTarget.store); |
| 65 | + |
| 66 | + txn.addOnCommittedListener(() => { |
| 67 | + ++commitCount; |
| 68 | + }); |
| 69 | + |
| 70 | + expect(commitCount).to.equal(0); |
| 71 | + |
| 72 | + ++runCount; |
| 73 | + if (runCount === 1) { |
| 74 | + // Trigger a unique key violation |
| 75 | + return targetsStore |
| 76 | + .add({ targetId: 1 }) |
| 77 | + .next(() => targetsStore.add({ targetId: 1 })); |
| 78 | + } else { |
| 79 | + return PersistencePromise.resolve(0); |
| 80 | + } |
| 81 | + } |
| 82 | + ); |
| 83 | + |
| 84 | + expect(runCount).to.be.equal(2); |
| 85 | + expect(commitCount).to.be.equal(1); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +function genericTransactionTests(): void { |
| 90 | + it('invokes onCommittedListener when transaction succeeds', async () => { |
| 91 | + let onCommitted = false; |
| 92 | + await persistence.runTransaction( |
| 93 | + 'onCommitted', |
| 94 | + 'readonly-idempotent', |
| 95 | + txn => { |
| 96 | + txn.addOnCommittedListener(() => { |
| 97 | + onCommitted = true; |
| 98 | + }); |
| 99 | + |
| 100 | + expect(onCommitted).to.be.false; |
| 101 | + return PersistencePromise.resolve(); |
| 102 | + } |
| 103 | + ); |
| 104 | + |
| 105 | + expect(onCommitted).to.be.true; |
| 106 | + }); |
| 107 | + |
| 108 | + it('does not invoke onCommittedListener when transaction fails', async () => { |
| 109 | + let onCommitted = false; |
| 110 | + await persistence |
| 111 | + .runTransaction('onCommitted', 'readonly-idempotent', txn => { |
| 112 | + txn.addOnCommittedListener(() => { |
| 113 | + onCommitted = true; |
| 114 | + }); |
| 115 | + |
| 116 | + return PersistencePromise.reject(new Error('Aborted')); |
| 117 | + }) |
| 118 | + .catch(() => {}); |
| 119 | + |
| 120 | + expect(onCommitted).to.be.false; |
| 121 | + }); |
| 122 | +} |
0 commit comments