-
-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
176 additions
and
83 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
nebula-logger/core/main/logger-engine/lwc/logger/__tests__/loggerServiceTaskQueue.test.js
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,59 @@ | ||
import { LoggerServiceTaskQueue } from '../loggerServiceTaskQueue'; | ||
|
||
describe('logger service task queue tests', () => { | ||
afterEach(async () => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('correctly resolves a single async task', async () => { | ||
const input = 'some string'; | ||
const asyncFunction = async input => { | ||
const output = 'Your input was: ' + input; | ||
return output; | ||
}; | ||
const loggerServiceTaskQueue = new LoggerServiceTaskQueue(); | ||
|
||
loggerServiceTaskQueue.enqueueTask(asyncFunction, input); | ||
const processedTasks = await loggerServiceTaskQueue.processTaskQueue(); | ||
|
||
expect(processedTasks.length).toEqual(1); | ||
expect(processedTasks[0].fn).toEqual(asyncFunction); | ||
expect(processedTasks[0].args.length).toEqual(1); | ||
expect(processedTasks[0].args[0]).toEqual(input); | ||
expect(processedTasks[0].output).toEqual('Your input was: ' + input); | ||
}); | ||
|
||
it('correctly processes sync and async tasks in order', async () => { | ||
const pendingTasks = []; | ||
for (let i = 0; i < 3; i++) { | ||
const task = { | ||
isAsync: false, | ||
fn: () => { | ||
return i; | ||
}, | ||
args: [i] | ||
}; | ||
// Make one task, somewhere in the middle of the queue, an async task | ||
if (i == 1) { | ||
task.isAsync = true; | ||
task.fn = async () => { | ||
return i; | ||
}; | ||
} | ||
pendingTasks.push(task); | ||
} | ||
const loggerServiceTaskQueue = new LoggerServiceTaskQueue(); | ||
|
||
pendingTasks.forEach(task => { | ||
loggerServiceTaskQueue.enqueueTask(task.fn, task.args); | ||
}); | ||
const processedTasks = await loggerServiceTaskQueue.processTaskQueue(); | ||
|
||
expect(processedTasks.length).toEqual(3); | ||
const expectedCombinedOutput = '012'; | ||
const actualCombinedOutput = processedTasks.reduce((accumulator, currentTask) => { | ||
return accumulator + currentTask.output; | ||
}, ''); | ||
expect(actualCombinedOutput).toEqual(expectedCombinedOutput); | ||
}); | ||
}); |
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
30 changes: 30 additions & 0 deletions
30
nebula-logger/core/main/logger-engine/lwc/logger/loggerServiceTaskQueue.js
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,30 @@ | ||
export class LoggerServiceTaskQueue { | ||
#isProcessing = false; | ||
#taskQueue = []; | ||
|
||
enqueueTask(fn, ...args) { | ||
this.#taskQueue.push({ fn, args }); | ||
} | ||
|
||
async processTaskQueue() { | ||
if (this.#isProcessing) { | ||
return; | ||
} | ||
|
||
this.#isProcessing = true; | ||
|
||
const processedTasks = []; | ||
while (this.#taskQueue.length > 0) { | ||
const task = this.#taskQueue.shift(); | ||
task.output = task.fn(...task.args); | ||
if (task.output instanceof Promise) { | ||
task.output = await task.output; | ||
} | ||
processedTasks.push(task); | ||
} | ||
|
||
this.#isProcessing = false; | ||
|
||
return processedTasks; | ||
} | ||
} |
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