Skip to content

Commit 8dcdc0d

Browse files
committed
Revert "Add IAsyncReplicationServer/IAsyncReplicationResponse, update ReplicationService, Middleware, and TestServer to support async replication with conditional sync/async DI."
This reverts commit 349621f.
1 parent 349621f commit 8dcdc0d

12 files changed

Lines changed: 49 additions & 176 deletions

File tree

src/Lucene.Net.Replicator/Http/ReplicationService.cs

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace Lucene.Net.Replicator.Http
4949
/// <para/>
5050
/// @lucene.experimental
5151
/// </remarks>
52-
public class ReplicationService : IReplicationService, IAsyncReplicationService // LUCENENET specific: added interface so we can mock easier.
52+
public class ReplicationService : IReplicationService // LUCENENET specific: added interface so we can mock easier.
5353
{
5454
/// <summary>
5555
/// Actions supported by the <see cref="ReplicationService"/>.
@@ -121,14 +121,13 @@ private static string ExtractRequestParam(IReplicationRequest request, string pa
121121
return param;
122122
}
123123

124-
// Shared internal logic (generic on response)
125-
private async Task ExecuteReplicationInternal<TResponse>(
124+
// method to avoid code duplication in sync and async Perform methods
125+
private async Task ExecuteReplicationAsync(
126126
IReplicationRequest request,
127-
TResponse response,
127+
IReplicationResponse response,
128128
Func<Stream, Task> copyStreamFunc,
129129
Func<SessionToken, Task> writeTokenFunc,
130130
Func<Task> flushFunc)
131-
where TResponse : IBaseReplicationResponse
132131
{
133132
string[] pathElements = GetPathElements(request);
134133
if (pathElements.Length != 2)
@@ -184,28 +183,6 @@ private async Task ExecuteReplicationInternal<TResponse>(
184183
}
185184
}
186185

187-
// For sync
188-
private async Task ExecuteReplicationAsync(
189-
IReplicationRequest request,
190-
IReplicationResponse response,
191-
Func<Stream, Task> copyStreamFunc,
192-
Func<SessionToken, Task> writeTokenFunc,
193-
Func<Task> flushFunc)
194-
{
195-
await ExecuteReplicationInternal(request, response, copyStreamFunc, writeTokenFunc, flushFunc);
196-
}
197-
198-
// For async
199-
private async Task ExecuteReplicationAsync(
200-
IReplicationRequest request,
201-
IAsyncReplicationResponse response,
202-
Func<Stream, Task> copyStreamFunc,
203-
Func<SessionToken, Task> writeTokenFunc,
204-
Func<Task> flushFunc)
205-
{
206-
await ExecuteReplicationInternal(request, response, copyStreamFunc, writeTokenFunc, flushFunc);
207-
}
208-
209186
// LUCENENET specific - copy method not used
210187

211188
/// <summary>
@@ -245,7 +222,7 @@ public virtual void Perform(IReplicationRequest request, IReplicationResponse re
245222
/// <exception cref="InvalidOperationException">Thrown when required parameters are missing or invalid.</exception>
246223
public virtual Task PerformAsync(
247224
IReplicationRequest request,
248-
IAsyncReplicationResponse response,
225+
IReplicationResponse response,
249226
CancellationToken cancellationToken = default)
250227
{
251228
return ExecuteReplicationAsync(

src/Lucene.Net.Replicator/Support/Http/Abstractions/IAsyncReplicationResponse.cs

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/Lucene.Net.Replicator/Support/Http/Abstractions/IAsyncReplicationService.cs

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/Lucene.Net.Replicator/Support/Http/Abstractions/IBaseReplicationResponse.cs

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/Lucene.Net.Replicator/Support/Http/Abstractions/IReplicationResponse.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,28 @@ namespace Lucene.Net.Replicator.Http.Abstractions
2828
/// .NET Specific Abstraction
2929
/// </remarks>
3030
//Note: LUCENENET specific
31-
public interface IReplicationResponse: IBaseReplicationResponse
31+
public interface IReplicationResponse
3232
{
33+
/// <summary>
34+
/// Gets or sets the http status code of the response.
35+
/// </summary>
36+
int StatusCode { get; set; }
37+
38+
/// <summary>
39+
/// The response content.
40+
/// </summary>
41+
Stream Body { get; }
42+
3343
/// <summary>
3444
/// Flushes the reponse to the underlying response stream.
3545
/// </summary>
3646
void Flush();
47+
48+
/// <summary>
49+
/// Flushes the response to the underlying response stream asynchronously.
50+
/// </summary>
51+
/// <param name="cancellationToken">Optional cancellation token.</param>
52+
/// <returns>A task representing the asynchronous operation.</returns>
53+
Task FlushAsync(CancellationToken cancellationToken = default);
3754
}
3855
}

src/Lucene.Net.Replicator/Support/Http/Abstractions/IReplicationService.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,15 @@ public interface IReplicationService
3232
/// </summary>
3333
/// <exception cref="InvalidOperationException">required parameters are missing</exception>
3434
void Perform(IReplicationRequest request, IReplicationResponse response);
35+
36+
/// <summary>
37+
/// Executes the replication task asynchronously.
38+
/// </summary>
39+
/// <param name="request">The replication request.</param>
40+
/// <param name="response">The replication response.</param>
41+
/// <param name="cancellationToken">Optional cancellation token.</param>
42+
/// <returns>A task representing the asynchronous operation.</returns>
43+
Task PerformAsync(IReplicationRequest request, IReplicationResponse response, CancellationToken cancellationToken = default);
44+
3545
}
3646
}

src/Lucene.Net.Tests.Replicator/Http/ReplicationServlet.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace Lucene.Net.Replicator.Http
3232

3333
public class ReplicationServlet
3434
{
35-
public void Configure(IApplicationBuilder app, IAsyncReplicationService service, ReplicatorTestCase.MockErrorConfig mockErrorConfig)
35+
public void Configure(IApplicationBuilder app, IReplicationService service, ReplicatorTestCase.MockErrorConfig mockErrorConfig)
3636
{
3737
// Middleware to throw an exception conditionally from our test server.
3838
app.Use(async (context, next) =>
@@ -63,9 +63,9 @@ public void Configure(IApplicationBuilder app, IAsyncReplicationService service,
6363
public class ReplicationServiceMiddleware
6464
{
6565
private readonly RequestDelegate next;
66-
private readonly IAsyncReplicationService service;
66+
private readonly IReplicationService service;
6767

68-
public ReplicationServiceMiddleware(RequestDelegate next, IAsyncReplicationService service)
68+
public ReplicationServiceMiddleware(RequestDelegate next, IReplicationService service)
6969
{
7070
this.next = next ?? throw new ArgumentNullException(nameof(next));
7171
this.service = service ?? throw new ArgumentNullException(nameof(service));

src/Lucene.Net.Tests.Replicator/ReplicatorTestCase.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,15 @@ public class MockErrorConfig
8181
#if FEATURE_ASPNETCORE_TESTHOST
8282
public partial class ReplicatorTestCase
8383
{
84-
public static TestServer NewHttpServer(ReplicationService service, MockErrorConfig mockErrorConfig, bool useSynchronousIO, bool useStartupClass)
84+
public static TestServer NewHttpServer(IReplicationService service, MockErrorConfig mockErrorConfig, bool useSynchronousIO, bool useStartupClass)
8585
{
8686
if (useStartupClass)
8787
{
8888
var builder = new WebHostBuilder()
8989
.ConfigureServices(container =>
9090
{
91-
// Register concrete services
92-
container.AddSingleton<IAsyncReplicationService>(service);
93-
container.AddSingleton<IReplicationService>(service);
94-
container.AddSingleton(mockErrorConfig);
91+
container.AddSingleton(service);
92+
container.AddSingleton(mockErrorConfig);
9593
});
9694

9795
if (useSynchronousIO)
@@ -113,8 +111,7 @@ public static TestServer NewHttpServer(ReplicationService service, MockErrorCon
113111
.ConfigureServices(container =>
114112
{
115113
container.AddRouting();
116-
container.AddSingleton<IAsyncReplicationService>(service);
117-
container.AddSingleton<IReplicationService>(service);
114+
container.AddSingleton(service);
118115
container.AddSingleton(mockErrorConfig);
119116
if (useSynchronousIO)
120117
{
@@ -219,7 +216,7 @@ public async Task InvokeAsync(HttpContext context)
219216
public partial class ReplicatorTestCase
220217
{
221218
// LUCENENET: This uses HttpListener from System.Net to test the service where ASP.NET Core is not supported.
222-
public static TestServer NewHttpServer(ReplicationService service, MockErrorConfig mockErrorConfig, bool useSynchronousIO, bool useStartupClass)
219+
public static TestServer NewHttpServer(IReplicationService service, MockErrorConfig mockErrorConfig, bool useSynchronousIO, bool useStartupClass)
223220
{
224221
return new TestServer(service, mockErrorConfig, useSynchronousIO);
225222
}

src/Lucene.Net.Tests.Replicator/Support/Net/HttpListenerReplicationResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace Lucene.Net.Replicator.Net
2727
/// A concrete implementation of <see cref="IReplicationResponse"/> for supporting
2828
/// <see cref="HttpListener"/>.
2929
/// </summary>
30-
public class HttpListenerReplicationResponse : IReplicationResponse, IAsyncReplicationResponse
30+
public class HttpListenerReplicationResponse : IReplicationResponse
3131
{
3232
private readonly HttpListenerResponse _response;
3333

src/Lucene.Net.Tests.Replicator/Support/Net/TestServer.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Text;
77
using System.Threading;
88
using System.Threading.Tasks;
9-
using Lucene.Net.Replicator.Http;
109

1110
namespace Lucene.Net.Replicator.Net
1211
{
@@ -35,9 +34,7 @@ namespace Lucene.Net.Replicator.Net
3534
public class TestServer : IDisposable
3635
{
3736
private readonly HttpListener _listener;
38-
private readonly IAsyncReplicationService _asyncService;
39-
private readonly IReplicationService _syncService;
40-
37+
private readonly IReplicationService _service;
4138
private readonly ReplicatorTestCase.MockErrorConfig _mockErrorConfig;
4239
private readonly bool _useSynchronousIO;
4340
private readonly CancellationTokenSource _cancellationTokenSource = new();
@@ -46,15 +43,12 @@ public class TestServer : IDisposable
4643
public Uri BaseAddress { get; }
4744

4845
public TestServer(
49-
ReplicationService service, // concrete type implements both sync + async
46+
IReplicationService service,
5047
ReplicatorTestCase.MockErrorConfig mockErrorConfig,
5148
bool useSynchronousIO,
5249
string prefix = "http://localhost:0/")
5350
{
54-
if (service == null) throw new ArgumentNullException(nameof(service));
55-
_asyncService = service;
56-
_syncService = service;
57-
51+
_service = service ?? throw new ArgumentNullException(nameof(service));
5852
_mockErrorConfig = mockErrorConfig ?? throw new ArgumentNullException(nameof(mockErrorConfig));
5953
_useSynchronousIO = useSynchronousIO;
6054

@@ -98,11 +92,11 @@ private async Task ListenLoopAsync(CancellationToken token)
9892

9993
if (_useSynchronousIO)
10094
{
101-
_syncService.Perform(request, response);
95+
_service.Perform(request, response);
10296
}
10397
else
10498
{
105-
await _asyncService.PerformAsync(request, response, token);
99+
await _service.PerformAsync(request, response, token);
106100
}
107101
}
108102
catch

0 commit comments

Comments
 (0)