Skip to content
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

Fixed CancellationTokenSource Issue #47

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions Agoda.Frameworks.DB.Tests/DbRepositoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -666,28 +666,6 @@ public void QueryMultipleAsync_Retry_Failure()
Assert.IsNotNull(_onQueryCompleteEvents[0].Error);
Assert.IsNotNull(_onQueryCompleteEvents[1].Error);
}

[Test]
public async Task ExecuteReaderAsync_CancellationToken_Cancelled()
{
var cancellationToken = new CancellationTokenSource(TimeSpan.FromDays(1));
cancellationToken.Cancel();
var maxAttemptCount = 2;
Assert.ThrowsAsync<TaskCanceledException>(async () => await _db.ExecuteReaderAsync<string>("mobile_ro", "db.v1.sp_foo", 1,
maxAttemptCount,
cancellationToken.Token,
new IDbDataParameter[]
{
new SqlParameter("@param1", "value1"),
new SqlParameter("@param2", "value2")

}, reader => { return Task.FromResult("");})
);

_dbResources.Verify(x => x.ChooseDb("mobile_ro").UpdateWeight(It.IsAny<string>(), false), Times.Exactly(maxAttemptCount));
}


protected class FakeStoredProc : IStoredProc<string, string>
{
public string DbName => "mobile_ro";
Expand Down
6 changes: 4 additions & 2 deletions Agoda.Frameworks.DB/DbRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,14 +303,16 @@ public Task<T> ExecuteReaderAsync<T>(
string storedProc,
int timeoutSecs,
int maxAttemptCount,
CancellationToken token,
int taskCancellationTimeOutInMilliSecs,
IDbDataParameter[] parameters,
Func<SqlDataReader, Task<T>> callback)
{
return _dbResources.ChooseDb(database).ExecuteAsync(async (connectionStr, _) =>
{
var stopwatch = Stopwatch.StartNew();
Exception error = null;
var cancellationTokenSource = new CancellationTokenSource();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an IDisposable, can we wrap it in using?

cancellationTokenSource.CancelAfter(taskCancellationTimeOutInMilliSecs);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the operation that you want to include in the timer? Only the query execution, or opening the connection as well? If the first then should we initialize this closer to where it's consumed? Or if you want to timeout even if opening the connection takes a long time, then should we pass the token to connection.OpenAsync() as well?

try
{
using (var connection = _generateConnection(connectionStr))
Expand All @@ -332,7 +334,7 @@ public Task<T> ExecuteReaderAsync<T>(
CommandTimeout = timeoutSecs
};
sqlCommand.Parameters.AddRange(parameters);
using (var reader = await sqlCommand.ExecuteReaderAsync(token))
using (var reader = await sqlCommand.ExecuteReaderAsync(cancellationTokenSource.Token))
{
return await callback(reader);
}
Expand Down
2 changes: 1 addition & 1 deletion Agoda.Frameworks.DB/IDbRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ Task<T> ExecuteReaderAsync<T>(
string storedProc,
int timeoutSecs,
int maxAttemptCount,
CancellationToken token,
int taskCancellationTimeOutInMilliSecs,
IDbDataParameter[] parameters,
Func<SqlDataReader, Task<T>> callback);

Expand Down
Loading