Skip to content

Commit

Permalink
Merge pull request #319 from splunk/bugfix/fix_tslint_errors
Browse files Browse the repository at this point in the history
fix(*): fix tslint errors
  • Loading branch information
shakeelmohamed committed Dec 7, 2018
2 parents 3cc9149 + e5d2f5c commit c0907b9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"codecov:integration": "yarn cover:integration && codecov -f ./coverage-integration/lcov.info -F integration",
"cover": "NODE_ENV=coverage nyc yarn test",
"cover:integration": "NODE_ENV=coverage nyc --report-dir coverage-integration yarn test:integration",
"pretest": "yarn build",
"pretest": "yarn tslint && yarn build",
"test": "ts-mocha -p tsconfig.test.json test/unit/*.ts --exclude test/unit/*.d.ts",
"test:integration": "ts-mocha -p tsconfig.test.json --timeout=900000 test/integration/*.ts --exclude test/integration/*.d.ts",
"start:stubserver": "docker run --name=stubserver -d -p 8882:8882 137462835382.dkr.ecr.us-west-1.amazonaws.com/splunk-cloud-sdk-shared-stubby:latest",
Expand Down
19 changes: 11 additions & 8 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ export interface ServiceClientArgs {
defaultTenant?: string;
}

function _sleep(millis: number) : Promise<void> {
const _sleep = (millis: number) : Promise<void> => {
return new Promise(resolve => {
setTimeout(resolve, millis);
})
}
});
};

/**
* This function creates a ResponseHook that will retry requests that receive a 429 (too many requests) response
Expand All @@ -123,21 +123,24 @@ function _sleep(millis: number) : Promise<void> {
* @param onRetry A callback that takes a response and a request. Will be called on every retry attempt.
* @param onFailure A callback that takes a response and a request. Will be called after maxRetries are exhausted.
*/
export function naiveExponentialBackoff({maxRetries = 5, timeout = 100, backoff = 2.0,
onRetry=(response: Response, request: Request) => {},
onFailure=(response: Response, request: Request) => {}} = {}) : ResponseHook {
export function naiveExponentialBackoff({maxRetries = 5,
timeout = 100,
backoff = 2.0,
onRetry=(response: Response, request: Request) => {/**/},
onFailure=(response: Response, request: Request) =>{/**/}
} = {}) : ResponseHook {
const retry : ResponseHook = async (response: Response, request: Request) => {
let retries = 0;
let myResponse = response;
let currentTimeout = timeout;
while (response.status == 429 && retries < maxRetries) {
while (response.status === 429 && retries < maxRetries) {
await _sleep(currentTimeout);
retries += 1;
currentTimeout *= backoff;
myResponse = await fetch(request);
onRetry(myResponse, request);
}
if (response.status == 429) {
if (response.status === 429) {
onFailure(myResponse, request);
}
return myResponse;
Expand Down
28 changes: 14 additions & 14 deletions src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ function* iterateBatches(func: (s: number, b: number) => Promise<object>, batchS
}
}

function _filterObject(template: {[key: string]: any}, propertiesToRemove: Array<string>) : object {
let newObj: {[key: string] : { value: any }} = {};
const _filterObject = (template: {[key: string]: any}, propertiesToRemove: string[]) : object => {
const newObj: {[key: string] : { value: any }} = {};
for (const key in template) {
if (propertiesToRemove.indexOf(key) == -1) {
if (propertiesToRemove.indexOf(key) === -1) {
newObj[key] = template[key];
}
}
return newObj;
}
};

/**
* A base for an easy-to-use search interface
Expand All @@ -41,8 +41,8 @@ export class Search {
private client: SearchService;
public readonly jobId: string;
private isCancelling: boolean;
private _resultObservable?: Observable<any>;
private _statusObservable?: Observable<SearchJob>;
private resultObservableMemo?: Observable<any>;
private statusObservableMemo?: Observable<SearchJob>;
/**
*
* @param searchService
Expand Down Expand Up @@ -133,12 +133,12 @@ export class Search {
* @return An observable that will pass each result object as it is received
*/
public resultObservable = (args: ResultObservableOptions = {}): Observable<any> => {
if (!this._resultObservable) {
if (!this.resultObservableMemo) {
const self = this;
const pollInterval = args.pollInterval || 500;
let filteredArgs = _filterObject(args, ['pollInterval']);
this._resultObservable = Observable.create((observable: any) => {
const promises: Promise<any>[] = [];
const filteredArgs = _filterObject(args, ['pollInterval']);
this.resultObservableMemo = Observable.create((observable: any) => {
const promises: Array<Promise<any>> = [];
self.wait(pollInterval, (job: SearchJob) => {
if (job.resultsAvailable && job.resultsAvailable > 0) { // Passes through arguments, so has the same semantics of offset == window
promises.push(self.getResults(filteredArgs).then(results => observable.next(results)));
Expand All @@ -148,7 +148,7 @@ export class Search {
});
});
}
return this._resultObservable as Observable<any>;
return this.resultObservableMemo as Observable<any>;
}

/**
Expand All @@ -158,13 +158,13 @@ export class Search {
* @return An observable that will periodically poll for status on a job until it is complete
*/
public statusObservable = (updateInterval: number): Observable<SearchJob> => {
if (!this._statusObservable) {
this._statusObservable = new Observable<SearchJob>((o: any) => {
if (!this.statusObservableMemo) {
this.statusObservableMemo = new Observable<SearchJob>((o: any) => {
this.wait(updateInterval, (job: SearchJob) => o.next(job))
.then(() => o.complete(), (err: Error) => o.error(err));
});
}
return this._statusObservable;
return this.statusObservableMemo;
}
}

Expand Down
9 changes: 4 additions & 5 deletions test/unit/base_functionality.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import * as chai from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import 'isomorphic-fetch';
import 'mocha';
import { ServiceClient, naiveExponentialBackoff } from '../../client';
import { naiveExponentialBackoff, ServiceClient, SplunkError } from '../../client';
import config from '../config';
import {SplunkError} from "../../src/client";

const stubbyUrl = `http://${config.stubbyHost}:8882`;
chai.use(chaiAsPromised);
Expand Down Expand Up @@ -114,7 +113,7 @@ describe('Basic client functionality', () => {
it('should allow retry on 429', () => {
let retries = 0;
let failed = false;
s.addResponseHook(naiveExponentialBackoff({maxRetries: 3, onRetry: () => retries += 1, onFailure: () => failed = true}));
s.addResponseHook(naiveExponentialBackoff({ maxRetries: 3, onRetry: () => retries += 1, onFailure: () => failed = true }));
return s.get('api', '/too_many_requests')
.then(httpResponse => {
expect(httpResponse.body).to.haveOwnProperty('foo');
Expand All @@ -129,7 +128,7 @@ describe('Basic client functionality', () => {
const timeout = 50;
const backoff = 2;
const expectedTime = timeout + timeout * backoff + timeout * backoff * backoff;
s.addResponseHook(naiveExponentialBackoff({maxRetries: 3, timeout: timeout, backoff: backoff}));
s.addResponseHook(naiveExponentialBackoff({ timeout, backoff, maxRetries: 3 }));
const start = Date.now();
return s.get('api', '/too_many_requests')
.then(httpResponse => {
Expand All @@ -148,7 +147,7 @@ describe('Basic client functionality', () => {
expect(data.body).to.haveOwnProperty('foo');
expect(data.status).to.equal(200);
});
})
});

it('should handle exceptions', () => {
s.addResponseHook(response => {
Expand Down
3 changes: 2 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"semicolon": true,
"no-unnecessary-callback-wrapper": true,
"object-literal-key-quotes": [true, "consistent-as-needed"],
"no-multi-spaces": true
"no-multi-spaces": true,
"variable-name": [true, "allow-leading-underscore"]
},
"rulesDirectory": []
}

0 comments on commit c0907b9

Please sign in to comment.