-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5119 from Shopify/12-17-send_a_list_of_request_id…
…s_to_monorail Send a list of request ids to monorail
- Loading branch information
Showing
7 changed files
with
175 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import {MAX_REQUEST_IDS, requestIdsCollection} from './request-ids.js' | ||
import {describe, test, expect, beforeEach} from 'vitest' | ||
|
||
describe('RequestIDCollection', () => { | ||
beforeEach(() => { | ||
requestIdsCollection.clear() | ||
}) | ||
|
||
test('starts with an empty collection', () => { | ||
expect(requestIdsCollection.getRequestIds()).toEqual([]) | ||
}) | ||
|
||
test('adds request IDs to collection', () => { | ||
// When | ||
requestIdsCollection.addRequestId('request-1') | ||
requestIdsCollection.addRequestId('request-2') | ||
|
||
// Then | ||
expect(requestIdsCollection.getRequestIds()).toEqual(['request-1', 'request-2']) | ||
}) | ||
|
||
test('ignores undefined or null request IDs', () => { | ||
// When | ||
requestIdsCollection.addRequestId(undefined) | ||
requestIdsCollection.addRequestId(null) | ||
requestIdsCollection.addRequestId('request-1') | ||
|
||
// Then | ||
expect(requestIdsCollection.getRequestIds()).toEqual(['request-1']) | ||
}) | ||
|
||
test('limits collection to MAX_REQUEST_IDS', () => { | ||
// When | ||
for (let i = 0; i < MAX_REQUEST_IDS + 20; i++) { | ||
requestIdsCollection.addRequestId(`request-${i}`) | ||
} | ||
|
||
// Then | ||
expect(requestIdsCollection.getRequestIds()).toHaveLength(100) | ||
expect(requestIdsCollection.getRequestIds()[0]).toBe('request-0') | ||
expect(requestIdsCollection.getRequestIds()[99]).toBe('request-99') | ||
}) | ||
|
||
test('clear() removes all request IDs', () => { | ||
// Given | ||
requestIdsCollection.addRequestId('request-1') | ||
requestIdsCollection.addRequestId('request-2') | ||
|
||
// When | ||
requestIdsCollection.clear() | ||
|
||
// Then | ||
expect(requestIdsCollection.getRequestIds()).toEqual([]) | ||
}) | ||
|
||
test('maintains singleton instance', () => { | ||
// Given | ||
requestIdsCollection.addRequestId('request-1') | ||
|
||
// When | ||
const sameInstance = requestIdsCollection | ||
sameInstance.addRequestId('request-2') | ||
|
||
// Then | ||
expect(requestIdsCollection.getRequestIds()).toEqual(['request-1', 'request-2']) | ||
expect(sameInstance.getRequestIds()).toEqual(['request-1', 'request-2']) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
export const MAX_REQUEST_IDS = 100 | ||
|
||
/** | ||
* Manages collection of request IDs during command execution | ||
*/ | ||
class RequestIDCollection { | ||
private static instance: RequestIDCollection | ||
|
||
static getInstance(): RequestIDCollection { | ||
if (!RequestIDCollection.instance) { | ||
RequestIDCollection.instance = new RequestIDCollection() | ||
} | ||
return RequestIDCollection.instance | ||
} | ||
|
||
private requestIds: string[] = [] | ||
|
||
/** | ||
* Add a request ID to the collection | ||
* We only report the first MAX_REQUEST_IDS request IDs. | ||
*/ | ||
addRequestId(requestId: string | undefined | null) { | ||
if (requestId && this.requestIds.length < MAX_REQUEST_IDS) { | ||
this.requestIds.push(requestId) | ||
} | ||
} | ||
|
||
/** | ||
* Get all collected request IDs | ||
*/ | ||
getRequestIds(): string[] { | ||
return this.requestIds | ||
} | ||
|
||
/** | ||
* Clear all stored request IDs | ||
*/ | ||
clear() { | ||
this.requestIds = [] | ||
} | ||
} | ||
|
||
export const requestIdsCollection = RequestIDCollection.getInstance() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters