-
Notifications
You must be signed in to change notification settings - Fork 317
Fix flaky connection pool tests for FIFO ordering #3751
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
mdaigle
wants to merge
4
commits into
main
Choose a base branch
from
dev/mdaigle/fix-flaky-connection-pool-tests
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.
+32
−16
Open
Changes from all commits
Commits
Show all changes
4 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
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -281,7 +281,6 @@ out DbConnectionInternal? recycledConnection | |||||
| } | ||||||
|
|
||||||
| [Fact] | ||||||
| [ActiveIssue("https://github.com/dotnet/SqlClient/issues/3730")] | ||||||
| public async Task GetConnectionMaxPoolSize_ShouldRespectOrderOfRequest() | ||||||
| { | ||||||
| // Arrange | ||||||
|
|
@@ -308,29 +307,31 @@ out DbConnectionInternal? internalConnection | |||||
| Assert.NotNull(internalConnection); | ||||||
| } | ||||||
|
|
||||||
| // Use ManualResetEventSlim to synchronize the tasks | ||||||
| // and force the request queueing order. | ||||||
| using ManualResetEventSlim mresQueueOrder = new(); | ||||||
| using CountdownEvent allRequestsQueued = new(2); | ||||||
| // Use multiple ManualResetEventSlim to ensure proper ordering | ||||||
| using ManualResetEventSlim firstTaskReady = new(false); | ||||||
| using ManualResetEventSlim secondTaskReady = new(false); | ||||||
| using ManualResetEventSlim startRequests = new(false); | ||||||
|
|
||||||
| // Act | ||||||
| var recycledTask = Task.Run(() => | ||||||
| { | ||||||
| mresQueueOrder.Set(); | ||||||
| allRequestsQueued.Signal(); | ||||||
| firstTaskReady.Set(); | ||||||
| startRequests.Wait(); | ||||||
| pool.TryGetConnection( | ||||||
| new SqlConnection(""), | ||||||
| new SqlConnection("Timeout=5000"), | ||||||
| null, | ||||||
| new DbConnectionOptions("", null), | ||||||
| out DbConnectionInternal? recycledConnection | ||||||
| ); | ||||||
| return recycledConnection; | ||||||
| }); | ||||||
|
|
||||||
| var failedTask = Task.Run(() => | ||||||
| { | ||||||
| // Force this request to be second in the queue. | ||||||
| mresQueueOrder.Wait(); | ||||||
| allRequestsQueued.Signal(); | ||||||
| secondTaskReady.Set(); | ||||||
| startRequests.Wait(); | ||||||
| // Add a small delay to ensure this request comes after the first | ||||||
| Thread.Sleep(50); | ||||||
| pool.TryGetConnection( | ||||||
| new SqlConnection("Timeout=1"), | ||||||
| null, | ||||||
|
|
@@ -340,7 +341,20 @@ out DbConnectionInternal? failedConnection | |||||
| return failedConnection; | ||||||
| }); | ||||||
|
|
||||||
| allRequestsQueued.Wait(); | ||||||
| // Wait for both tasks to be ready before starting the requests | ||||||
| firstTaskReady.Wait(); | ||||||
| secondTaskReady.Wait(); | ||||||
|
|
||||||
| // Use SpinWait to ensure both tasks are actually waiting | ||||||
|
||||||
| // Use SpinWait to ensure both tasks are actually waiting | |
| // Wait briefly to ensure both tasks are waiting on startRequests |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think making these async introduced a deadlock. In some conditions, they'll hang on to threads and prevent future async operations from going through. I'm going to revert these changes other than the SpinWait -> Thread.Sleep()