-
-
Notifications
You must be signed in to change notification settings - Fork 24.7k
Fix MySQLRecordManager DataSource connection churn #6570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jianongHe
wants to merge
2
commits into
FlowiseAI:main
Choose a base branch
from
jianongHe:fix-mysql-record-manager-datasource
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+384
−66
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
packages/components/nodes/recordmanager/MySQLRecordManager/MySQLrecordManager.test.ts
This file contains hidden or 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,138 @@ | ||
| const mockDataSourceInstances: Array<{ | ||
| isInitialized: boolean | ||
| initialize: jest.Mock | ||
| destroy: jest.Mock | ||
| createQueryRunner: jest.Mock | ||
| }> = [] | ||
|
|
||
| const mockQueryRunners: Array<{ | ||
| isReleased: boolean | ||
| manager: { | ||
| query: jest.Mock | ||
| } | ||
| release: jest.Mock | ||
| }> = [] | ||
|
|
||
| jest.mock('typeorm', () => { | ||
| class MockDataSource { | ||
| options: unknown | ||
| isInitialized = false | ||
| initialize = jest.fn(async () => { | ||
| this.isInitialized = true | ||
| return this | ||
| }) | ||
| destroy = jest.fn(async () => { | ||
| this.isInitialized = false | ||
| }) | ||
| createQueryRunner = jest.fn(() => { | ||
| const queryRunner = { | ||
| isReleased: false, | ||
| manager: { | ||
| query: jest.fn(async (query: string) => { | ||
| if (query.includes('UNIX_TIMESTAMP')) { | ||
| return [{ epoch: '123' }] | ||
| } | ||
|
|
||
| if (query.includes('SELECT `key`')) { | ||
| return [{ key: 'a' }] | ||
| } | ||
|
|
||
| return [] | ||
| }) | ||
| }, | ||
| release: jest.fn(async () => { | ||
| queryRunner.isReleased = true | ||
| }) | ||
| } | ||
|
|
||
| mockQueryRunners.push(queryRunner) | ||
| return queryRunner | ||
| }) | ||
|
|
||
| constructor(options: unknown) { | ||
| this.options = options | ||
| mockDataSourceInstances.push(this) | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| DataSource: MockDataSource | ||
| } | ||
| }) | ||
|
|
||
| const { MySQLRecordManager } = require('./MySQLrecordManager') | ||
|
|
||
| const createManager = () => | ||
| new MySQLRecordManager('test_namespace', { | ||
| mysqlOptions: { | ||
| type: 'mysql', | ||
| host: 'localhost', | ||
| port: 3306, | ||
| username: 'user', | ||
| password: 'password', | ||
| database: 'flowise' | ||
| }, | ||
| tableName: 'upsertion_records' | ||
| }) | ||
|
|
||
| describe('MySQLRecordManager connection lifecycle', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks() | ||
| mockDataSourceInstances.length = 0 | ||
| mockQueryRunners.length = 0 | ||
| }) | ||
|
|
||
| it('reuses one initialized DataSource across operations and destroys it only on close', async () => { | ||
| const manager = createManager() | ||
|
|
||
| await expect(manager.exists(['a'])).resolves.toEqual([true]) | ||
| await expect(manager.listKeys()).resolves.toEqual(['a']) | ||
| await expect(manager.getTime()).resolves.toBe(123) | ||
|
|
||
| expect(mockDataSourceInstances).toHaveLength(1) | ||
| expect(mockDataSourceInstances[0].initialize).toHaveBeenCalledTimes(1) | ||
| expect(mockDataSourceInstances[0].destroy).not.toHaveBeenCalled() | ||
| expect(mockDataSourceInstances[0].createQueryRunner).toHaveBeenCalledTimes(3) | ||
| expect(mockQueryRunners).toHaveLength(3) | ||
| mockQueryRunners.forEach((queryRunner) => { | ||
| expect(queryRunner.release).toHaveBeenCalledTimes(1) | ||
| expect(queryRunner.isReleased).toBe(true) | ||
| }) | ||
|
|
||
| await manager.close() | ||
|
|
||
| expect(mockDataSourceInstances[0].destroy).toHaveBeenCalledTimes(1) | ||
| expect(mockDataSourceInstances[0].isInitialized).toBe(false) | ||
| }) | ||
|
|
||
| it('shares the pending DataSource initialization across concurrent operations', async () => { | ||
| const manager = createManager() | ||
|
|
||
| await expect(Promise.all([manager.exists(['a']), manager.listKeys()])).resolves.toEqual([[true], ['a']]) | ||
|
|
||
| expect(mockDataSourceInstances).toHaveLength(1) | ||
| expect(mockDataSourceInstances[0].initialize).toHaveBeenCalledTimes(1) | ||
| expect(mockDataSourceInstances[0].destroy).not.toHaveBeenCalled() | ||
| expect(mockDataSourceInstances[0].createQueryRunner).toHaveBeenCalledTimes(2) | ||
|
|
||
| await manager.close() | ||
| }) | ||
|
|
||
| it('releases query runners when update validation fails', async () => { | ||
| const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => undefined) | ||
| const manager = createManager() | ||
|
|
||
| await expect(manager.update(['a'], { timeAtLeast: 999 })).rejects.toThrow('Time sync issue with database 123 < 999') | ||
|
|
||
| expect(mockDataSourceInstances).toHaveLength(1) | ||
| expect(mockDataSourceInstances[0].destroy).not.toHaveBeenCalled() | ||
| expect(mockQueryRunners).toHaveLength(2) | ||
| mockQueryRunners.forEach((queryRunner) => { | ||
| expect(queryRunner.release).toHaveBeenCalledTimes(1) | ||
| expect(queryRunner.isReleased).toBe(true) | ||
| }) | ||
|
|
||
| await manager.close() | ||
| consoleErrorSpy.mockRestore() | ||
| }) | ||
| }) |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.