Skip to content

Commit

Permalink
fix: lint error
Browse files Browse the repository at this point in the history
  • Loading branch information
alkatrivedi committed Nov 26, 2024
1 parent d7f0752 commit 814ce81
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/multiplexed-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class MultiplexedSession
* @returns {Promise<void>} A Promise that resolves when the session has been successfully created and assigned, an event
* `mux-session-available` will be emitted to signal that the session is ready.
*
* In case of error, we will be emitting the error along with the erorr event.
* In case of error, an error will get emitted along with the erorr event.
*
* @private
*/
Expand Down
118 changes: 55 additions & 63 deletions test/multiplexed-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,12 @@ import * as assert from 'assert';
import {beforeEach, afterEach, describe, it} from 'mocha';
import * as events from 'events';
import * as sinon from 'sinon';
import {grpc} from 'google-gax';
import {grpc, Status} from 'google-gax';
import {Database} from '../src/database';
import {Session} from '../src/session';
import {MultiplexedSession} from '../src/multiplexed-session';
import {Transaction} from '../src/transaction';

class FakeTransaction {
options;
constructor(options?) {
this.options = options;
}
async begin(): Promise<void> {}
}
import { FakeTransaction } from './session-pool';

Check failure on line 26 in test/multiplexed-session.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `·FakeTransaction·` with `FakeTransaction`

describe('MultiplexedSession', () => {
let multiplexedSession;
Expand Down Expand Up @@ -106,82 +99,76 @@ describe('MultiplexedSession', () => {
assert.strictEqual(_createSessionStub.callCount, 1);
});

it('should start housekeeping', async () => {
await multiplexedSession.createSession();
assert.strictEqual(_maintainStub.callCount, 1);
it('should start housekeeping', done => {
multiplexedSession.createSession();
setImmediate(() => {
try {
assert.strictEqual(_maintainStub.callCount, 1);
done();
} catch (err) {
done(err);
}
});
});

it('should not trigger unhandled promise rejection', () => {
it('should not propagate instance and database not found errors for Multiplexed Session', () => {
const multiplexedSession = new MultiplexedSession(DATABASE);
const error = {
code: grpc.status.PERMISSION_DENIED,
message: 'spanner.sessions.create',
code: Status.NOT_FOUND,
message: 'Database not found',
} as grpc.ServiceError;

sandbox.restore();
sandbox.stub(multiplexedSession, '_createSession').rejects(error);

const originalRejection = process.listeners('unhandledRejection').pop();
if (originalRejection) {
process.removeListener('unhandledRejection', originalRejection!);
}

process.once('unhandledRejection', err => {
assert.ifError(err);
});

multiplexedSession.createSession();

if (originalRejection) {
process.listeners('unhandledRejection').push(originalRejection!);
try {
multiplexedSession.createSession();
} catch (e) {
shouldNotBeCalled();
}
});

it('should not trigger unhandled promise rejection when default credentials not set', () => {
it('should not propagate errors for Multiplexed Session when default credentials not set', () => {
const multiplexedSession = new MultiplexedSession(DATABASE);
const error = {
message: 'Could not load the default credentials',
} as grpc.ServiceError;

sandbox.restore();
sandbox.stub(multiplexedSession, '_createSession').rejects(error);

const originalRejection = process.listeners('unhandledRejection').pop();
if (originalRejection) {
process.removeListener('unhandledRejection', originalRejection!);
}

process.once('unhandledRejection', err => {
assert.ifError(err);
});

multiplexedSession.createSession();

if (originalRejection) {
process.listeners('unhandledRejection').push(originalRejection!);
try {
multiplexedSession.createSession();
} catch (e) {
shouldNotBeCalled();
}
});

it('should not trigger unhandled promise rejection when projectId not set', () => {
it('should not propagate errors for Multiplexed Session when projectId not set', () => {
const multiplexedSession = new MultiplexedSession(DATABASE);
const error = {
code: Status.NOT_FOUND,
message: 'Unable to detect a Project Id in the current environment',
} as grpc.ServiceError;

sandbox.restore();
sandbox.stub(multiplexedSession, '_createSession').rejects(error);

const originalRejection = process.listeners('unhandledRejection').pop();
if (originalRejection) {
process.removeListener('unhandledRejection', originalRejection!);
try {
multiplexedSession.createSession();
} catch (e) {
shouldNotBeCalled();
}
});

process.once('unhandledRejection', err => {
assert.ifError(err);
it('should propagate errors for Multiplexed Session which gets emitted', async () => {
const multiplexedSession = new MultiplexedSession(DATABASE);
const fakeError = new Error();
sandbox.stub(multiplexedSession, '_createSession').rejects(fakeError);
const errorPromise = new Promise<void>((resolve, reject) => {
multiplexedSession.once('error', err => {
if (err === fakeError) {
resolve();
} else {
reject(new Error('Unexpected error emitted'));
}
});
});

multiplexedSession.createSession();

if (originalRejection) {
process.listeners('unhandledRejection').push(originalRejection!);
}
await errorPromise;
});
});

Expand Down Expand Up @@ -310,17 +297,22 @@ describe('MultiplexedSession', () => {
});

it('should remove the available listener', async () => {

const promise = multiplexedSession._getSession();

setTimeout(() => multiplexedSession.emit('mux-session-available'), 100);

assert.strictEqual(multiplexedSession.listenerCount('mux-session-available'), 1);
assert.strictEqual(
multiplexedSession.listenerCount('mux-session-available'),
1
);

try {
await promise;
} finally {
assert.strictEqual(multiplexedSession.listenerCount('mux-session-available'), 0);
assert.strictEqual(
multiplexedSession.listenerCount('mux-session-available'),
0
);
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/session-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function FakePQueue(options) {

FakePQueue.default = FakePQueue;

class FakeTransaction {
export class FakeTransaction {
options;
constructor(options?) {
this.options = options;
Expand Down

0 comments on commit 814ce81

Please sign in to comment.