-
Notifications
You must be signed in to change notification settings - Fork 1
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 #94 from flotiq/feature/25873-add-internal-throttl…
…e-to-cli-importer #25873 add internal throttle to importer
- Loading branch information
Showing
6 changed files
with
232 additions
and
39 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
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
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,55 @@ | ||
const axios = require('axios'); | ||
const FlotiqApi = require('./../src/flotiq-api'); | ||
|
||
jest.mock('axios'); | ||
|
||
describe('FlotiqApi', () => { | ||
const mockApiUrl = 'https://dummy-api.flotiq.com'; | ||
const mockApiKey = 'dummyApiKey'; | ||
|
||
it('method persistContentObjectBatch should retry when receiving a 429 status', async () => { | ||
// Mock first response from Axios as 429, seconds as 200 | ||
const postMock = jest.fn() | ||
.mockRejectedValueOnce({ response: { status: 429 } }) | ||
.mockResolvedValueOnce({ ok: true }); | ||
|
||
axios.create.mockReturnValue({ | ||
post: postMock, | ||
}); | ||
|
||
const flotiqApi = new FlotiqApi(`${mockApiUrl}/api/v1`, mockApiKey, { | ||
batchSize: 100, | ||
writePerSecondLimit: 5, | ||
}); | ||
|
||
const obj = new Array(100).fill({}); | ||
await flotiqApi.persistContentObjectBatch('mockContentType', obj); | ||
|
||
// Expect first call to be 429, then after retry: success | ||
expect(postMock).toHaveBeenCalledTimes(2); | ||
expect(postMock).toHaveBeenCalledWith(expect.anything(), expect.arrayContaining([{}])); | ||
}); | ||
|
||
it('method patchContentObjectBatch should retry when receiving a 429 status', async () => { | ||
// Mock first response from Axios as 429, seconds as 200 | ||
const patchMock = jest.fn() | ||
.mockRejectedValueOnce({ response: { status: 429 } }) | ||
.mockResolvedValueOnce({ ok: true }); | ||
|
||
axios.create.mockReturnValue({ | ||
patch: patchMock, | ||
}); | ||
|
||
const flotiqApi = new FlotiqApi(`${mockApiUrl}/api/v1`, mockApiKey, { | ||
batchSize: 100, | ||
writePerSecondLimit: 5, | ||
}); | ||
|
||
const obj = new Array(100).fill({}); | ||
await flotiqApi.patchContentObjectBatch('mockContentType', obj); | ||
|
||
// Expect first call to be 429, then after retry: success | ||
expect(patchMock).toHaveBeenCalledTimes(2); | ||
expect(patchMock).toHaveBeenCalledWith(expect.anything(), expect.arrayContaining([{}])); | ||
}); | ||
}); |
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,109 @@ | ||
const fs = require('fs/promises'); | ||
const FlotiqApi = require('./../src/flotiq-api'); | ||
const { mediaImporter } = require('./../src/media'); | ||
|
||
jest.mock('axios'); | ||
jest.mock('fs/promises'); | ||
jest.mock('node-fetch'); | ||
jest.mock('./../src/flotiq-api'); | ||
jest.mock('./../src/logger', () => ({ | ||
info: jest.fn(), | ||
warn: jest.fn(), | ||
error: jest.fn(), | ||
})); | ||
|
||
describe('mediaImporter', () => { | ||
const mockDirectory = '/mock/directory'; | ||
const mockApiUrl = 'https://dummy-api.flotiq.com'; | ||
const mockApiKey = 'dummyApiKey'; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
|
||
FlotiqApi.mockImplementation(() => ({ | ||
fetchContentTypeDefinition: jest.fn().mockResolvedValue([]), | ||
fetchContentTypeDefs: jest.fn().mockResolvedValue([]), | ||
updateContentTypeDefinition: jest.fn(), | ||
fetchContentObjects: jest.fn().mockResolvedValue([]), | ||
patchContentObjectBatch: jest.fn(), | ||
persistContentObjectBatch: jest.fn(), | ||
createOrUpdate: jest.fn().mockResolvedValue({ | ||
ok: true, | ||
json: jest.fn().mockResolvedValue({success:true}) | ||
}), | ||
middleware: { | ||
put: jest.fn(), | ||
delete: jest.fn().mockResolvedValue(undefined) | ||
} | ||
})); | ||
|
||
global.fetch = jest.fn(() => | ||
Promise.resolve({ | ||
status: 404, // fetch should return 404 for importer to send postMedia request | ||
}) | ||
); | ||
|
||
// Mock fs.readFile | ||
fs.readFile.mockResolvedValue( | ||
JSON.stringify([ | ||
{ | ||
id: 'file1', | ||
url: '/image/0x0/dummy_media_id.jpg', | ||
mimeType: 'image/png', | ||
extension: 'png', | ||
fileName: 'file1.png' | ||
} | ||
]) | ||
); | ||
}); | ||
|
||
|
||
|
||
it('should retry on 429 error during media upload', async () => { | ||
const flotiqApi = new FlotiqApi(`${mockApiUrl}/api/v1`, mockApiKey, { | ||
batchSize: 100, | ||
}); | ||
flotiqApi.flotiqApiUrl = mockApiUrl; | ||
|
||
const mockMediaApi = { | ||
post: jest.fn() | ||
.mockRejectedValueOnce({ | ||
response: { | ||
status: 429, | ||
message: 'Too Many Requests' | ||
} | ||
}) | ||
.mockResolvedValueOnce({ | ||
data: { id: 'new-media-id' } | ||
}) | ||
}; | ||
|
||
await mediaImporter(mockDirectory, flotiqApi, mockMediaApi, 1); | ||
|
||
expect(mockMediaApi.post).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('should respect writePerSecondLimit and throttle uploads', async () => { | ||
const flotiqApi = new FlotiqApi(`${mockApiUrl}/api/v1`, mockApiKey, { | ||
batchSize: 100, | ||
}); | ||
flotiqApi.flotiqApiUrl = mockApiUrl; | ||
|
||
const mockMediaApi = { | ||
post: jest.fn() | ||
.mockResolvedValueOnce({ | ||
data: { id: 'new-media-id' } | ||
}) | ||
}; | ||
|
||
const start = Date.now(); | ||
await mediaImporter(mockDirectory, flotiqApi, mockMediaApi, 1); // writePerSecondLimit = 1 | ||
|
||
const end = Date.now(); | ||
const elapsed = end - start; | ||
|
||
expect(mockMediaApi.post).toHaveBeenCalledTimes(1); | ||
// Check that importer respected throttle limit | ||
expect(elapsed).toBeGreaterThanOrEqual(1000); // at least 1 second | ||
}); | ||
}); |