Skip to content

Commit 2241aca

Browse files
authored
Merge pull request #236 from ali-ince/1.5-add-close-and-closeasync
added IDriver.Close and IDriver.CloseAsync methods.
2 parents 507a6fa + 241b790 commit 2241aca

25 files changed

+544
-180
lines changed

Neo4j.Driver/Neo4j.Driver.Tests/Connector/SocketClientTests.cs

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public async Task ShouldStopClientAndThrowExceptionWhenProtocolErrorOccurs()
208208
var ex = Record.Exception(() => harness.Client.Receive(messageHandler));
209209
ex.Should().BeOfType<ProtocolException>();
210210

211-
harness.MockTcpSocketClient.Verify(x => x.Dispose(), Times.Once);
211+
harness.MockTcpSocketClient.Verify(x => x.Disconnect(), Times.Once);
212212
}
213213
}
214214

@@ -272,21 +272,65 @@ public Neo4jException Error
272272
}
273273
}
274274

275-
public class DisposeMethod
275+
public class DisposeAndStopMethods
276276
{
277+
277278
[Fact]
278-
public async Task ShouldCallDisconnectOnTheTcpSocketClient()
279+
public void ShouldCallDisconnectOnTheTcpSocketClientWhenDisposed()
280+
{
281+
using (var harness = new SocketClientTestHarness(FakeUri))
282+
{
283+
harness.SetupReadStream("00 00 00 01");
284+
harness.SetupWriteStream();
285+
harness.Client.Start();
286+
harness.Client.Dispose();
287+
harness.MockTcpSocketClient.Verify(s => s.Disconnect(), Times.Once);
288+
harness.Client.IsOpen.Should().BeFalse();
289+
}
290+
}
291+
292+
[Fact]
293+
public async Task ShouldCallDisconnectOnTheTcpSocketClientWhenDisposedAsync()
279294
{
280295
using (var harness = new SocketClientTestHarness(FakeUri))
281296
{
282297
harness.SetupReadStream("00 00 00 01");
283298
harness.SetupWriteStream();
284299
await harness.Client.StartAsync();
285300
harness.Client.Dispose();
286-
harness.MockTcpSocketClient.Verify(s => s.Dispose(), Times.Once);
301+
harness.MockTcpSocketClient.Verify(s => s.Disconnect(), Times.Once);
302+
harness.Client.IsOpen.Should().BeFalse();
303+
}
304+
}
305+
306+
[Fact]
307+
public void ShouldCallDisconnectOnTheTcpSocketClientWhenStopped()
308+
{
309+
using (var harness = new SocketClientTestHarness(FakeUri))
310+
{
311+
harness.SetupReadStream("00 00 00 01");
312+
harness.SetupWriteStream();
313+
harness.Client.Start();
314+
harness.Client.Stop();
315+
harness.MockTcpSocketClient.Verify(s => s.Disconnect(), Times.Once);
287316
harness.Client.IsOpen.Should().BeFalse();
288317
}
289318
}
319+
320+
[Fact]
321+
public async Task ShouldCallDisconnectAsyncOnTheTcpSocketClientWhenStoppedAsync()
322+
{
323+
using (var harness = new SocketClientTestHarness(FakeUri))
324+
{
325+
harness.SetupReadStream("00 00 00 01");
326+
harness.SetupWriteStream();
327+
await harness.Client.StartAsync();
328+
await harness.Client.StopAsync();
329+
harness.MockTcpSocketClient.Verify(s => s.DisconnectAsync(), Times.Once);
330+
harness.Client.IsOpen.Should().BeFalse();
331+
}
332+
}
333+
290334
}
291335
}
292336
}

Neo4j.Driver/Neo4j.Driver.Tests/Connector/SocketConnectionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public void StopsTheClient()
132132
var con = NewSocketConnection(mock.Object);
133133

134134
con.Destroy();
135-
mock.Verify(c => c.Dispose(), Times.Once);
135+
mock.Verify(c => c.Stop(), Times.Once);
136136
}
137137
}
138138

Neo4j.Driver/Neo4j.Driver.Tests/Connector/TcpSocketClientTests.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,16 @@ public TcpSocketClientWithDisposeDetection(SocketSettings socketSettings, ILogge
3636
{
3737
}
3838

39-
protected override void CloseClient()
39+
public override void Disconnect()
4040
{
4141
DisposeCalled = true;
42-
base.CloseClient();
42+
base.Disconnect();
43+
}
44+
45+
public override Task DisconnectAsync()
46+
{
47+
DisposeCalled = true;
48+
return base.DisconnectAsync();
4349
}
4450

4551
public bool DisposeCalled { get; set; }
@@ -108,5 +114,6 @@ public async Task ShouldThrowIOExceptionIfConnTimedOut()
108114
baseException.Message.Should().Be("Failed to connect to server 127.0.0.1:9999 within 0ms.");
109115
}
110116
}
117+
111118
}
112119
}

Neo4j.Driver/Neo4j.Driver.Tests/DriverTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,50 @@ public void ShouldAcceptIfRoutingSchemeWithRoutingContext()
8282
driver.Uri.Host.Should().Be("localhost");
8383
}
8484
}
85+
86+
[Fact]
87+
public void DisposeClosesDriver()
88+
{
89+
var driver = GraphDatabase.Driver("bolt://localhost");
90+
driver.Dispose();
91+
92+
var ex = Record.Exception(() => driver.Session());
93+
ex.Should().NotBeNull();
94+
ex.Should().BeOfType<ObjectDisposedException>();
95+
}
96+
97+
[Fact]
98+
public void CloseClosesDriver()
99+
{
100+
var driver = GraphDatabase.Driver("bolt://localhost");
101+
driver.Close();
102+
103+
var ex = Record.Exception(() => driver.Session());
104+
ex.Should().NotBeNull();
105+
ex.Should().BeOfType<ObjectDisposedException>();
106+
}
107+
108+
[Fact]
109+
public async void CloseAsyncClosesDriver()
110+
{
111+
var driver = GraphDatabase.Driver("bolt://localhost");
112+
await driver.CloseAsync();
113+
114+
var ex = Record.Exception(() => driver.Session());
115+
ex.Should().NotBeNull();
116+
ex.Should().BeOfType<ObjectDisposedException>();
117+
}
118+
119+
[Fact] public async void MultipleCloseAndDisposeIsValidOnDriver()
120+
{
121+
var driver = GraphDatabase.Driver("bolt://localhost");
122+
driver.Close();
123+
await driver.CloseAsync();
124+
driver.Dispose();
125+
126+
var ex = Record.Exception(() => driver.Session());
127+
ex.Should().NotBeNull();
128+
ex.Should().BeOfType<ObjectDisposedException>();
129+
}
85130
}
86131
}

Neo4j.Driver/Neo4j.Driver.Tests/Routing/ClusterConnectionPoolTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public void ShouldRemoveNewlyCreatedPoolnIfDisposeAlreadyCalled()
160160
var exception = Record.Exception(() => pool.Update(new[] {ServerUri}));
161161

162162
// Then
163-
mockedConnectionPool.Verify(x => x.Dispose());
163+
mockedConnectionPool.Verify(x => x.Close());
164164

165165
exception.Should().BeOfType<ObjectDisposedException>();
166166
exception.Message.Should().Contain("Failed to create connections with server");
@@ -199,7 +199,7 @@ public void ShouldRemovedIfExist()
199199
pool.Purge(ServerUri);
200200

201201
// Then
202-
mockedConnectionPool.Verify(x => x.Dispose(), Times.Once);
202+
mockedConnectionPool.Verify(x => x.Close(), Times.Once);
203203
connectionPoolDict.Count.Should().Be(0);
204204
connectionPoolDict.ContainsKey(ServerUri).Should().BeFalse();
205205
}
@@ -237,7 +237,7 @@ public void ShouldRemoveAllAfterDispose()
237237
pool.Dispose();
238238

239239
// Then
240-
mockedConnectionPool.Verify(x => x.Dispose(), Times.Once);
240+
mockedConnectionPool.Verify(x => x.Close(), Times.Once);
241241
connectionPoolDict.Count.Should().Be(0);
242242
connectionPoolDict.ContainsKey(ServerUri).Should().BeFalse();
243243
}

Neo4j.Driver/Neo4j.Driver.Tests/Routing/ClusterDiscoveryManagerTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public void ShouldCarryOutRediscoveryWith32Server(int routerCount, int writerCou
118118
manager.Writers.Count().Should().Be(writerCount);
119119
manager.Routers.Count().Should().Be(routerCount);
120120
manager.ExpireAfterSeconds = 9223372036854775807;
121-
clientMock.Verify(x => x.Dispose(), Times.Once);
121+
clientMock.Verify(x => x.Stop(), Times.Once);
122122
}
123123

124124
[Theory]
@@ -147,7 +147,7 @@ public void ShouldCarryOutRediscovery(int routerCount, int writerCount, int read
147147
manager.Writers.Count().Should().Be(writerCount);
148148
manager.Routers.Count().Should().Be(routerCount);
149149
manager.ExpireAfterSeconds = 9223372036854775807;
150-
clientMock.Verify(x => x.Dispose(), Times.Once);
150+
clientMock.Verify(x => x.Stop(), Times.Once);
151151
}
152152

153153
[Fact]
@@ -174,7 +174,7 @@ public void ShouldServiceUnavailableWhenProcedureNotFound()
174174
// Then
175175
exception.Should().BeOfType<ServiceUnavailableException>();
176176
exception.Message.Should().StartWith("Error when calling `getServers` procedure: ");
177-
messagingClient.ClientMock.Verify(x => x.Dispose(), Times.Once);
177+
messagingClient.ClientMock.Verify(x => x.Stop(), Times.Once);
178178
}
179179

180180
[Fact]
@@ -190,7 +190,7 @@ public void ShouldProtocolErrorWhenNoRecord()
190190
// Then
191191
exception.Should().BeOfType<ProtocolException>();
192192
exception.Message.Should().Be("Error when parsing `getServers` result: Sequence contains no elements.");
193-
clientMock.Verify(x => x.Dispose(), Times.Once);
193+
clientMock.Verify(x => x.Stop(), Times.Once);
194194
}
195195

196196
[Fact]
@@ -211,7 +211,7 @@ public void ShouldProtocolErrorWhenMultipleRecord()
211211
exception.Should().BeOfType<ProtocolException>();
212212
exception.Message.Should()
213213
.Be("Error when parsing `getServers` result: Sequence contains more than one element.");
214-
clientMock.Verify(x => x.Dispose(), Times.Once);
214+
clientMock.Verify(x => x.Stop(), Times.Once);
215215
}
216216

217217
[Fact]
@@ -228,7 +228,7 @@ public void ShouldProtocolErrorWhenRecordUnparsable()
228228
exception.Should().BeOfType<ProtocolException>();
229229
exception.Message.Should()
230230
.Be("Error when parsing `getServers` result: keys (2) does not equal to values (1).");
231-
clientMock.Verify(x => x.Dispose(), Times.Once);
231+
clientMock.Verify(x => x.Stop(), Times.Once);
232232
}
233233

234234
[Fact]
@@ -248,7 +248,7 @@ public void ShouldThrowExceptionIfRouterIsEmpty()
248248
manager.Routers.Count().Should().Be(0);
249249
exception.Should().BeOfType<ProtocolException>();
250250
exception.Message.Should().Contain("0 routers, 2 writers and 1 readers.");
251-
clientMock.Verify(x => x.Dispose(), Times.Once);
251+
clientMock.Verify(x => x.Stop(), Times.Once);
252252
}
253253

254254
[Fact]
@@ -268,7 +268,7 @@ public void ShouldThrowExceptionIfReaderIsEmpty()
268268
manager.Routers.Count().Should().Be(3);
269269
exception.Should().BeOfType<ProtocolException>();
270270
exception.Message.Should().Contain("3 routers, 1 writers and 0 readers.");
271-
clientMock.Verify(x => x.Dispose(), Times.Once);
271+
clientMock.Verify(x => x.Stop(), Times.Once);
272272
}
273273
}
274274

Neo4j.Driver/Neo4j.Driver.Tests/SessionTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using Neo4j.Driver.V1;
2727
using Xunit;
2828
using Record = Xunit.Record;
29+
using TaskExtensions = Neo4j.Driver.Internal.TaskExtensions;
2930

3031
namespace Neo4j.Driver.Tests
3132
{
@@ -530,6 +531,16 @@ public Task<IConnection> AcquireAsync(AccessMode mode)
530531
{
531532
return Task.FromResult(Connection);
532533
}
534+
535+
public void Close()
536+
{
537+
538+
}
539+
540+
public Task CloseAsync()
541+
{
542+
return TaskExtensions.GetCompletedTask();
543+
}
533544
}
534545
}
535546
}

0 commit comments

Comments
 (0)