Skip to content

Commit 6a2242f

Browse files
added tests
1 parent 515b33c commit 6a2242f

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

src/Ydb.Sdk/test/Ydb.Sdk.Ado.Tests/YdbDataSourceTests.cs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ await _dataSource.ExecuteAsync(ydbConnection =>
181181
}
182182

183183
[Fact]
184-
public async Task ExecuteAsync_CancelsBetweenRetries()
184+
public async Task ExecuteAsync_WhenCancelsBetweenRetries_Throws()
185185
{
186186
using var cts = new CancellationTokenSource();
187187
var attempt = 0;
@@ -201,4 +201,49 @@ await _dataSource.ExecuteAsync(async (_, _) =>
201201

202202
Assert.Equal(1, attempt);
203203
}
204+
205+
[Theory]
206+
[InlineData(10)]
207+
[InlineData(20)]
208+
[InlineData(30)]
209+
public async Task ExecuteInTransactionAsync_WhenTLI_ThenRetriesUntilSuccess(int concurrentJob)
210+
{
211+
var tableName = $"Table_TLI_{Random.Shared.Next()}";
212+
await using (var ydbConnection = await CreateOpenConnectionAsync())
213+
{
214+
await new YdbCommand(ydbConnection)
215+
{
216+
CommandText = $"CREATE TABLE {tableName} (id Int32, count Int32, PRIMARY KEY (id));"
217+
}.ExecuteNonQueryAsync();
218+
219+
await new YdbCommand(ydbConnection)
220+
{ CommandText = $"INSERT INTO {tableName} (id, count) VALUES (1, 0);" }.ExecuteNonQueryAsync();
221+
}
222+
223+
var tasks = new List<Task>();
224+
for (var i = 0; i < concurrentJob; i++)
225+
{
226+
tasks.Add(_dataSource.ExecuteInTransactionAsync(async ydbConnection =>
227+
{
228+
var count = (int)(await new YdbCommand(ydbConnection)
229+
{ CommandText = $"SELECT count FROM {tableName} WHERE id = 1" }.ExecuteScalarAsync())!;
230+
231+
await new YdbCommand(ydbConnection)
232+
{
233+
CommandText = $"UPDATE {tableName} SET count = @count + 1 WHERE id = 1",
234+
Parameters = { new YdbParameter { Value = count, ParameterName = "count" } }
235+
}.ExecuteNonQueryAsync();
236+
}));
237+
}
238+
239+
await Task.WhenAll(tasks);
240+
241+
await using (var ydbConnection = await CreateOpenConnectionAsync())
242+
{
243+
Assert.Equal(concurrentJob, await new YdbCommand(ydbConnection)
244+
{ CommandText = $"SELECT count FROM {tableName} WHERE id = 1" }.ExecuteScalarAsync());
245+
246+
await new YdbCommand(ydbConnection) { CommandText = $"DROP TABLE {tableName}" }.ExecuteNonQueryAsync();
247+
}
248+
}
204249
}

0 commit comments

Comments
 (0)