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
40 changes: 39 additions & 1 deletion spanner-ado-net/spanner-ado-net-tests/ConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -555,5 +555,43 @@ public async Task ReadLargeString()
var got = await conn!.ExecuteScalarAsync(sql);
Assert.That(got, Is.EqualTo(value));
}


private class TestLibObject : Google.Cloud.SpannerLib.AbstractLibObject
{
public bool CloseLibObjectCalled { get; private set; }

public TestLibObject(long id) : base(null!, id)
{
}

protected override void CloseLibObject()
{
CloseLibObjectCalled = true;
}

public void CallDispose(bool disposing)
{
Dispose(disposing);
}
}

[Test]
public void Finalizer_DoesNotCallCloseLibObject()
{
var testObj = new TestLibObject(id: 42);

testObj.CallDispose(disposing: false);

Assert.That(testObj.CloseLibObjectCalled, Is.False);
}

[Test]
public void Dispose_CallsCloseLibObject()
{
var testObj = new TestLibObject(id: 42);

testObj.CallDispose(disposing: true);

Assert.That(testObj.CloseLibObjectCalled, Is.True);
}
Comment thread
olavloite marked this conversation as resolved.
}
20 changes: 20 additions & 0 deletions spanner-ado-net/spanner-ado-net-tests/TransactionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -850,4 +850,24 @@ public async Task CommitAsync_PropagatesException_IfConnectionIsClosed()

Assert.ThrowsAsync<ObjectDisposedException>(async () => await transaction.CommitAsync());
}

[Test]
public async Task Finalizer_DoesNotExecuteRollback()
{
await using var connection = new SpannerConnection(ConnectionString);
await connection.OpenAsync();
var transaction = await connection.BeginTransactionAsync();

var initialRollbacks = Fixture.SpannerMock.Requests.OfType<RollbackRequest>().Count();

var disposeMethod = typeof(SpannerTransaction).GetMethod("Dispose",
System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic,
binder: null,
types: new[] { typeof(bool) },
modifiers: null);
disposeMethod!.Invoke(transaction, new[] { (object)false });

var currentRollbacks = Fixture.SpannerMock.Requests.OfType<RollbackRequest>().Count();
Assert.That(currentRollbacks, Is.EqualTo(initialRollbacks));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ internal AbstractLibObject(ISpannerLib spanner, long id)
Id = id;
}

~AbstractLibObject()
{
Dispose(false);
}

protected void MarkDisposed()
{
_disposed = true;
Expand Down Expand Up @@ -78,7 +73,7 @@ protected virtual void Dispose(bool disposing)
}
try
{
if (Id > 0)
if (disposing && Id > 0)
{
CloseLibObject();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ public GrpcLibSpanner(
_clients[i] = new V1.SpannerLib.SpannerLibClient(_channels[i]);
}
}

~GrpcLibSpanner() => Dispose(false);


public void Dispose()
{
Dispose(true);
Expand All @@ -131,11 +129,17 @@ private void Dispose(bool disposing)
}
try
{
foreach (var channel in _channels)
if (disposing)
{
channel.Dispose();
if (_channels != null)
{
foreach (var channel in _channels)
{
channel?.Dispose();
}
}
_server?.Dispose();
}
_server.Dispose();
}
finally
{
Expand Down
14 changes: 12 additions & 2 deletions spanner-ado-net/spanner-ado-net/SpannerLibGrpcServer/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,18 @@ protected virtual void Dispose(bool disposing)
}
try
{
Stop();
_process?.Dispose();
if (disposing)
{
try
{
Stop();
}
catch (Exception)
{
// Ignore exceptions during shutdown/dispose
}
_process?.Dispose();
}
Comment thread
olavloite marked this conversation as resolved.
}
finally
{
Expand Down
Loading