Skip to content

Commit

Permalink
fix(persister): Handle concurrent find requests (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
offirgolan authored and jasonmit committed Aug 8, 2018
1 parent 8dfa464 commit 0e02414
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
37 changes: 23 additions & 14 deletions packages/@pollyjs/persister/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const CREATOR_NAME = 'Polly.JS';
export default class Persister {
constructor(polly) {
this.polly = polly;
this.cache = new Map();
this.pending = new Map();
this._cache = new Map();
}

static get type() {
Expand Down Expand Up @@ -116,31 +116,40 @@ export default class Persister {
}

async find(recordingId) {
if (this.cache.has(recordingId)) {
return this.cache.get(recordingId);
}
const { _cache: cache } = this;

if (!cache.has(recordingId)) {
const findRecording = async () => {
const recording = await this.findRecording(recordingId);

if (recording) {
this.assert(
`Recording with id '${recordingId}' is invalid. Please delete the recording so a new one can be created.`,
recording.log && recording.log.creator.name === CREATOR_NAME
);

return recording;
} else {
cache.delete(recordingId);

const recording = await this.findRecording(recordingId);
return null;
}
};

if (recording) {
this.assert(
`Recording with id '${recordingId}' is invalid. Please delete the recording so a new one can be created.`,
recording.log && recording.log.creator.name === CREATOR_NAME
);
this.cache.set(recordingId, recording);
cache.set(recordingId, findRecording());
}

return recording;
return cache.get(recordingId);
}

async save(recordingId) {
await this.saveRecording(...arguments);
this.cache.delete(recordingId);
this._cache.delete(recordingId);
}

async delete(recordingId) {
await this.deleteRecording(...arguments);
this.cache.delete(recordingId);
this._cache.delete(recordingId);
}

async findEntry(pollyRequest) {
Expand Down
26 changes: 22 additions & 4 deletions packages/@pollyjs/persister/tests/unit/persister-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Persister from '../../src';
import { timeout } from '@pollyjs/utils';

describe('Unit | Persister', function() {
it('should exist', function() {
Expand All @@ -19,24 +20,37 @@ describe('Unit | Persister', function() {
};

class CustomPersister extends Persister {
findRecording() {
async findRecording() {
callCounts.find++;
await timeout(1);

return recording;
}

saveRecording() {
async saveRecording() {
callCounts.save++;
await timeout(1);
}

deleteRecording() {
async deleteRecording() {
callCounts.delete++;
await timeout(1);
}
}

this.persister = new CustomPersister({});
});

it('should handle concurrent find requests', async function() {
await Promise.all([
this.persister.find('test'),
this.persister.find('test'),
this.persister.find('test')
]);

expect(callCounts.find).to.equal(1);
});

it('caches', async function() {
await this.persister.find('test');
await this.persister.find('test');
Expand All @@ -49,7 +63,11 @@ describe('Unit | Persister', function() {
recording = null;

await this.persister.find('test');
await this.persister.find('test');
await Promise.all([
this.persister.find('test'),
this.persister.find('test'),
this.persister.find('test')
]);
await this.persister.find('test');

expect(callCounts.find).to.equal(3);
Expand Down

0 comments on commit 0e02414

Please sign in to comment.