-
Notifications
You must be signed in to change notification settings - Fork 18
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
1 parent
c9e4aa6
commit 18ff149
Showing
2 changed files
with
124 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#if NET7_0_OR_GREATER | ||
|
||
using System.Data.Common; | ||
|
||
namespace Ydb.Sdk.Ado; | ||
|
||
public class YdbDataSource : DbDataSource | ||
{ | ||
private readonly YdbConnectionStringBuilder _ydbConnectionStringBuilder; | ||
|
||
public YdbDataSource(YdbConnectionStringBuilder connectionStringBuilder) | ||
{ | ||
_ydbConnectionStringBuilder = connectionStringBuilder; | ||
} | ||
|
||
public YdbDataSource(string connectionString) | ||
{ | ||
_ydbConnectionStringBuilder = new YdbConnectionStringBuilder(connectionString); | ||
} | ||
|
||
public YdbDataSource() | ||
{ | ||
_ydbConnectionStringBuilder = new YdbConnectionStringBuilder(); | ||
} | ||
|
||
protected override YdbConnection CreateDbConnection() | ||
{ | ||
return new YdbConnection(_ydbConnectionStringBuilder); | ||
} | ||
|
||
protected override YdbConnection OpenDbConnection() | ||
{ | ||
var dbConnection = CreateDbConnection(); | ||
try | ||
{ | ||
dbConnection.Open(); | ||
return dbConnection; | ||
} | ||
catch | ||
{ | ||
dbConnection.Close(); | ||
throw; | ||
} | ||
} | ||
|
||
public new YdbConnection CreateConnection() => CreateDbConnection(); | ||
|
||
public new YdbConnection OpenConnection() => OpenDbConnection(); | ||
|
||
public new async ValueTask<YdbConnection> OpenConnectionAsync(CancellationToken cancellationToken = default) | ||
{ | ||
var ydbConnection = CreateDbConnection(); | ||
|
||
try | ||
{ | ||
await ydbConnection.OpenAsync(cancellationToken); | ||
return ydbConnection; | ||
} | ||
catch | ||
{ | ||
await ydbConnection.CloseAsync(); | ||
throw; | ||
} | ||
} | ||
|
||
public override string ConnectionString => _ydbConnectionStringBuilder.ConnectionString; | ||
} | ||
|
||
#endif |
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 @@ | ||
using Xunit; | ||
Check warning on line 1 in src/Ydb.Sdk/tests/Ado/YdbDataSourceTests.cs GitHub Actions / Inspection (./src/YdbSdk.sln)
|
||
using Ydb.Sdk.Ado; | ||
Check warning on line 2 in src/Ydb.Sdk/tests/Ado/YdbDataSourceTests.cs GitHub Actions / Inspection (./src/YdbSdk.sln)
|
||
|
||
#if NET7_0_OR_GREATER | ||
namespace Ydb.Sdk.Tests.Ado; | ||
|
||
[Trait("Category", "Integration")] | ||
public class YdbDataSourceTests | ||
{ | ||
private const int SelectedCount = 100; | ||
|
||
private readonly YdbDataSource _dataSource = new("MaxSessionPool=10"); | ||
|
||
[Fact] | ||
public async Task OpenConnectionAsync_WhenMaxSessionPool10_ReturnOpenConnection() | ||
{ | ||
var tasks = new Task[SelectedCount]; | ||
for (var i = 0; i < SelectedCount; i++) | ||
{ | ||
tasks[i] = Task.Run(async () => | ||
{ | ||
await using var ydbConnection = await _dataSource.OpenConnectionAsync(); | ||
var ydbCommand = new YdbCommand(ydbConnection) { CommandText = "SELECT 1" }; | ||
Assert.Equal(1, await ydbCommand.ExecuteScalarAsync()); | ||
}); | ||
} | ||
|
||
await Task.WhenAll(tasks); | ||
} | ||
|
||
[Fact] | ||
public void CreateCommand_FromDataSource_ReturnDbCommand() | ||
{ | ||
for (var i = 0; i < SelectedCount; i++) | ||
{ | ||
Assert.Equal(1, _dataSource.CreateCommand("SELECT 1;").ExecuteScalar()); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void CreateConnection_FromDataSource_ReturnNotOpenConnection() | ||
{ | ||
using var ydbConnection = _dataSource.CreateConnection(); | ||
ydbConnection.Open(); | ||
Assert.Equal(1, new YdbCommand(ydbConnection) { CommandText = "SELECT 1" }.ExecuteScalar()); | ||
} | ||
|
||
[Fact] | ||
public void OpenConnection_FromDataSource_ReturnOpenConnection() | ||
{ | ||
using var ydbConnection = _dataSource.OpenConnection(); | ||
Assert.Equal(1, new YdbCommand(ydbConnection) { CommandText = "SELECT 1" }.ExecuteScalar()); | ||
} | ||
} | ||
#endif |