Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
43 changes: 43 additions & 0 deletions spanner-ado-net/spanner-ado-net-tests/ReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,49 @@ public async Task Exception_thrown_from_ExecuteReaderAsync([Values(PrepareOrNot.
Fixture.SpannerMock.AddOrUpdateStatementResult(sql, StatementResult.CreateException(new RpcException(new Status(StatusCode.Internal, "test"))));
Assert.That(async () => await cmd.ExecuteReaderAsync(), Throws.Exception.TypeOf<SpannerDbException>());
}

[NonParallelizable]
[Test]
public async Task Exception_thrown_from_Read_and_ReadAsync([Values(true, false)] bool asyncRead)
Comment thread
olavloite marked this conversation as resolved.
{
var oldStyle = Environment.GetEnvironmentVariable("SPANNER_ADO_COMMUNICATION_STYLE");
Environment.SetEnvironmentVariable("SPANNER_ADO_COMMUNICATION_STYLE", "ServerStreaming");
SpannerPool.CloseSpannerLib();
try
{
const string sql = "SELECT Id FROM test_table";
Fixture.SpannerMock.AddOrUpdateStatementResult(sql, StatementResult.CreateSingleColumnResultSet(new V1.Type { Code = TypeCode.Int64 }, "Id", 1L, 2L));

var executionTime = ExecutionTime.StreamException(
new RpcException(new Status(StatusCode.Internal, "test error")),
1,
new System.Collections.Concurrent.BlockingCollection<int>());
executionTime.AlwaysAllowWrite();
Fixture.SpannerMock.AddOrUpdateExecutionTime(nameof(MockSpannerService.ExecuteStreamingSql), executionTime);

await using var conn = await OpenConnectionAsync();
await using var cmd = new SpannerCommand(sql, conn);
await using var reader = await cmd.ExecuteReaderAsync();

if (asyncRead)
{
Assert.That(await reader.ReadAsync(), Is.True);
Assert.That(reader.GetInt64(0), Is.EqualTo(1L));
Assert.That(async () => await reader.ReadAsync(), Throws.Exception.TypeOf<SpannerDbException>());
}
else
{
Assert.That(reader.Read(), Is.True);
Assert.That(reader.GetInt64(0), Is.EqualTo(1L));
Assert.That(() => reader.Read(), Throws.Exception.TypeOf<SpannerDbException>());
}
}
finally
{
Environment.SetEnvironmentVariable("SPANNER_ADO_COMMUNICATION_STYLE", oldStyle);
SpannerPool.CloseSpannerLib();
}
}

[Ignore("Require multi-statement support")]
[Test]
Expand Down
55 changes: 38 additions & 17 deletions spanner-ado-net/spanner-ado-net/SpannerDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,20 @@ public override void Close()

public override bool Read()
{
if (!InternalRead())
try
{
_hasReadData = true;
_currentRow = LibRows.Next();
if (!InternalRead())
{
_hasReadData = true;
_currentRow = LibRows.Next();
}
_hasData = _hasData || _currentRow != null;
return _currentRow != null;
}
catch (SpannerException exception)
{
throw SpannerDbException.TranslateException(exception);
}
Comment thread
olavloite marked this conversation as resolved.
_hasData = _hasData || _currentRow != null;
return _currentRow != null;
}

public override async Task<bool> ReadAsync(CancellationToken cancellationToken)
Expand Down Expand Up @@ -184,8 +191,15 @@ private bool InternalRead()

private bool CheckForRows()
{
_tempRow ??= LibRows.Next();
return _tempRow != null;
try
{
_tempRow ??= LibRows.Next();
return _tempRow != null;
}
catch (SpannerException exception)
{
throw SpannerDbException.TranslateException(exception);
}
Comment thread
olavloite marked this conversation as resolved.
}

public override DataTable? GetSchemaTable()
Expand Down Expand Up @@ -916,18 +930,25 @@ public override bool IsDBNull(int ordinal)

public override bool NextResult()
{
CheckNotClosed();
if (IsSingleRow || IsSingleResult)
try
{
return false;
CheckNotClosed();
if (IsSingleRow || IsSingleResult)
{
return false;
}
_currentRow = null;
_tempRow = null;
_hasData = false;
_hasReadData = false;
_metadata = null;
_stats = null;
return LibRows.NextResultSet();
}
catch (SpannerException exception)
{
throw SpannerDbException.TranslateException(exception);
}
Comment thread
olavloite marked this conversation as resolved.
_currentRow = null;
_tempRow = null;
_hasData = false;
_hasReadData = false;
_metadata = null;
_stats = null;
return LibRows.NextResultSet();
}

public override IEnumerator GetEnumerator()
Expand Down
Loading