Skip to content

Commit

Permalink
Added specs testing behavior around client acquiring a connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalBush committed Nov 10, 2017
1 parent fefe9c8 commit 8a33f57
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
67 changes: 67 additions & 0 deletions spec/behavior/client.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const { sinon } = testHelpers;
const Client = require( "src/client" );

describe( "client", () => {
describe( "when connection acquire succeeds", () => {
let pool, result, action;
const expectedConnection = "CONNECTION";
const expectedResult = "RESULT";

before( async () => {
pool = {
acquire: sinon.stub().resolves( expectedConnection ),
release: sinon.stub().resolves()
};
action = sinon.stub().resolves( expectedResult );
const client = new Client( pool );
result = await client.withConnection( action );
} );

it( "should return correct result", () => {
result.should.equal( "RESULT" );
} );

it( "should acquire connection", () => {
pool.acquire.should.be.calledOnce().and.calledWithExactly();
} );

it( "should call action with connection", () => {
action.should.be.calledOnce().and.calledWithExactly( expectedConnection );
} );

it( "should release connection", async () => {
pool.release.should.be.calledOnce().and.calledWithExactly( expectedConnection );
} );
} );

describe( "when connection acquire fails", () => {
let pool, error, action;
const expectedError = new Error( "Boom" );

before( async () => {
pool = {
acquire: sinon.stub().rejects( expectedError ),
release: sinon.stub().resolves()
};
action = sinon.stub().resolves();
const client = new Client( pool );
try {
await client.withConnection( action );
} catch ( e ) {
error = e;
}
} );

it( "should error", () => {
error.should.equal( expectedError );
} );

it( "should acquire connection", () => {
pool.acquire.should.be.calledOnce().and.calledWithExactly();
} );

it( "should not try to release connection", async () => {
pool.release.should.not.be.called();
} );
} );
} );
10 changes: 4 additions & 6 deletions spec/behavior/fileLoader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ describe( "fileLoader", () => {
const fs = {
readFile: sinon.stub()
};
fs.readFile.onCall( 0 ).callsArgWith( 2, null, expected );
fs.readFile.onCall( 1 ).throws( new Error( "you failed" ) );
fs.readFile.callsArgWith( 2, null, expected );

const loader = proxyquire( "src/fileLoader", {
fs
} );

const firstCall = await loader( "A" );
const secondCall = await loader( "A" );
await loader( "A" );
await loader( "A" );

firstCall.should.equal( expected );
secondCall.should.equal( expected );
fs.readFile.should.be.calledOnce();
} );
} );
7 changes: 4 additions & 3 deletions src/fileLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const path = require( "path" );
const callsites = require( "callsites" );

const readFileAsync = promisify( readFile );
const cache = {};
const cache = new Map();

module.exports = async function( relativeFile ) {
const relativeTo = path.dirname( callsites()[ 1 ].getFileName() );
Expand All @@ -15,9 +15,10 @@ module.exports = async function( relativeFile ) {
}
const fileName = path.resolve( relativeTo, relativeFile );

let result = cache[ fileName ];
let result = cache.get( fileName );
if ( !result ) {
result = cache[ fileName ] = await readFileAsync( fileName, "utf8" );
result = await readFileAsync( fileName, "utf8" );
cache.set( fileName, result );
}
return result;
};

0 comments on commit 8a33f57

Please sign in to comment.