Skip to content

Commit

Permalink
chore: Add test cases for request and requestManager
Browse files Browse the repository at this point in the history
  • Loading branch information
aarlaud committed May 13, 2020
1 parent 44f6c6e commit 703080c
Show file tree
Hide file tree
Showing 13 changed files with 1,733 additions and 12 deletions.
Empty file added fixtures/.gitkeep
Empty file.
13 changes: 8 additions & 5 deletions src/lib/customErrors/requestManagerErrors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import {ApiError} from './apiError'
import {ApiError,
ApiAuthenticationError,
NotFoundError,
GenericError
} from './apiError'

const requestsManagerErrorOverload = (err: Error, channel: string, requestId: string): Error => {
switch(err?.name){
Expand All @@ -10,7 +14,6 @@ const requestsManagerErrorOverload = (err: Error, channel: string, requestId: st
return new RequestsManagerNotFoundError(err.message, channel, requestId)
case 'Unknown':
return new RequestsManagerGenericError(err.message, channel, requestId)
break;
default:
} return new RequestsManagerGenericError("Unclassified", channel, requestId)
}
Expand All @@ -27,7 +30,7 @@ class RequestsManagerApiError extends ApiError {
}
}

class RequestsManagerApiAuthenticationError extends ApiError {
class RequestsManagerApiAuthenticationError extends ApiAuthenticationError {
channel: string
requestId: string
constructor(message: any, channel: string, requestId: string){
Expand All @@ -39,7 +42,7 @@ class RequestsManagerApiAuthenticationError extends ApiError {
}
}

class RequestsManagerNotFoundError extends ApiError {
class RequestsManagerNotFoundError extends NotFoundError {
channel: string
requestId: string
constructor(message: any, channel: string, requestId: string){
Expand All @@ -51,7 +54,7 @@ class RequestsManagerNotFoundError extends ApiError {
}
}

class RequestsManagerGenericError extends ApiError {
class RequestsManagerGenericError extends GenericError {
channel: string
requestId: string
constructor(message: any, channel: string, requestId: string){
Expand Down
2 changes: 1 addition & 1 deletion src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'source-map-support/register';
import { requestsManager } from './requests/requestsManager'
import { requestsManager } from './request/requestManager'

const run = async () => {
const manager = new requestsManager()
Expand Down
3 changes: 2 additions & 1 deletion src/lib/request/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ interface snykRequest {
verb: string,
url: string,
body?: string,
headers?: Object
headers?: Object,
requestId?: string
}

const makeSnykRequest = async (request: snykRequest) => {
Expand Down
23 changes: 18 additions & 5 deletions src/lib/request/requestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ class requestsManager {
_requestsQueue: LeakyBucketQueue<queuedRequest>
// TODO: Type _events rather than plain obscure object structure
_events: any
_retryCounter: Map<string,number>
_MAX_RETRY_COUNT: number

constructor() {
this._requestsQueue = new LeakyBucketQueue<queuedRequest>({ burstSize: 10, period: 500 });
constructor(burstSize = 10, period = 500, maxRetryCount = 5) {
this._requestsQueue = new LeakyBucketQueue<queuedRequest>({ burstSize: burstSize, period: period });
this._setupQueueExecutors(this._requestsQueue)
this._events = {}
this._retryCounter = new Map()
this._MAX_RETRY_COUNT = maxRetryCount
}

_setupQueueExecutors = (queue: LeakyBucketQueue<queuedRequest>) => {
Expand All @@ -52,7 +56,16 @@ class requestsManager {
this._emit({eventType: eventType.data, channel: request.channel, requestId: requestId, data: response })
} catch (err) {
let overloadedError = requestsManagerError.requestsManagerErrorOverload(err, request.channel, requestId)
this._emit({eventType: eventType.error, channel: request.channel, requestId: requestId, data: overloadedError })
let alreadyRetriedCount = this._retryCounter.get(requestId) || 0
if(alreadyRetriedCount >= this._MAX_RETRY_COUNT){
this._emit({eventType: eventType.error, channel: request.channel, requestId: requestId, data: overloadedError })
} else {
this._retryCounter.set(requestId, alreadyRetriedCount+1)
// Throw it back into the queue
this.requestStream(request.snykRequest,request.channel, request.id)
}


}

}
Expand Down Expand Up @@ -168,8 +181,8 @@ class requestsManager {
})
}

requestStream = (request: snykRequest, channel: string = 'stream'): string => {
let requestId = uuidv4()
requestStream = (request: snykRequest, channel: string = 'stream', id: string = ''): string => {
let requestId = id ? id : uuidv4()
let requestForQueue: queuedRequest = {id: requestId, channel: channel, snykRequest: request}
this._requestsQueue.enqueue(requestForQueue)
if(!this._doesChannelHaveListeners(channel)){
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/apiResponses/general-doc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"what orgs can the current token access?": "https://snyk.io/api/v1/orgs",
"what projects are owned by this org?": "https://snyk.io/api/v1/org/:id/projects",
"test a package for issues": "https://snyk.io/api/v1/test/:packageManager/:packageName/:packageVersion"
}
Loading

0 comments on commit 703080c

Please sign in to comment.