Skip to content

Commit ecee021

Browse files
committed
#337 Moved NoTaskHttpConnection to ProtocolLoadTest instead of removing it alltogether instead of removing it so we keep on testing it in the future
1 parent 4491b93 commit ecee021

File tree

5 files changed

+178
-0
lines changed

5 files changed

+178
-0
lines changed

Diff for: src/Nest/Domain/Connection/ConnectionStatus.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Runtime.CompilerServices;
45
using System.Text;
56
using System.Text.RegularExpressions;
67
using System.IO;
78
using System.Net;
89

10+
11+
912
namespace Nest
1013
{
1114
public class ConnectionStatus

Diff for: src/Nest/Properties/InternalsVisibleTo.cs

+1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
using System.Runtime.InteropServices;
1616
[assembly: InternalsVisibleTo("Nest.Tests.Unit")]
1717
[assembly: InternalsVisibleTo("Nest.Tests.Integration")]
18+
[assembly: InternalsVisibleTo("ProtocolLoadTest")]
1819

1920

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
using System;
2+
using System.IO;
3+
using System.Net;
4+
using System.Text;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using Nest;
8+
using Nest.Domain.Connection;
9+
10+
namespace ProtocolLoadTest
11+
{
12+
public class AsyncRequestOperation : TaskCompletionSource<ConnectionStatus>, IDisposable
13+
{
14+
private readonly HttpWebRequest m_request;
15+
private readonly string m_requestData;
16+
private readonly IConnectionSettings m_connectionSettings;
17+
private ConnectionStatusTracer m_tracer;
18+
private WebResponse m_response;
19+
private Stream m_responseStream;
20+
21+
public AsyncRequestOperation(HttpWebRequest request, string requestData, IConnectionSettings connectionSettings, ConnectionStatusTracer tracer)
22+
{
23+
m_request = request;
24+
m_requestData = requestData;
25+
m_connectionSettings = connectionSettings;
26+
m_tracer = tracer;
27+
Start();
28+
}
29+
30+
private void Start()
31+
{
32+
if (this.m_requestData != null)
33+
WriteRequestDataAsync();
34+
else
35+
GetResponseAsync();
36+
}
37+
38+
private void WriteRequestDataAsync()
39+
{
40+
this.m_request.BeginGetRequestStream(this.Monitor(ar =>
41+
{
42+
var r = this.m_request.EndGetRequestStream(ar);
43+
var buffer = Encoding.UTF8.GetBytes(this.m_requestData);
44+
r.BeginWrite(buffer, 0, buffer.Length, this.Monitor(writeIar =>
45+
{
46+
r.EndWrite(writeIar);
47+
GetResponseAsync();
48+
}), null);
49+
}), null);
50+
}
51+
52+
private void GetResponseAsync()
53+
{
54+
this.m_request.BeginGetResponse(this.Monitor(iarResponse =>
55+
{
56+
m_response = m_request.EndGetResponse(iarResponse);
57+
m_responseStream = m_response.GetResponseStream();
58+
59+
var buffer = new byte[8192];
60+
var result = new MemoryStream(buffer.Length);
61+
ReadResponseStreamAsync(this.m_responseStream, buffer, result);
62+
63+
}), null);
64+
}
65+
66+
private void ReadResponseStreamAsync(Stream stream, byte[] buffer, MemoryStream result)
67+
{
68+
stream.BeginRead(buffer, 0, buffer.Length, this.Monitor(iar =>
69+
{
70+
var bytes = stream.EndRead(iar);
71+
if (bytes == 0)
72+
{
73+
Done(result);
74+
return;
75+
}
76+
77+
result.Write(buffer, 0, bytes);
78+
ReadResponseStreamAsync(stream, buffer, result);
79+
80+
}), null);
81+
}
82+
83+
private void Done(ConnectionStatus connectionStatus)
84+
{
85+
m_tracer.SetResult(connectionStatus);
86+
TrySetResult(connectionStatus);
87+
Dispose();
88+
}
89+
90+
private void Done(Stream result)
91+
{
92+
result.Position = 0;
93+
var reader = new StreamReader(result);
94+
Done(new ConnectionStatus(reader.ReadToEnd())
95+
{
96+
Request = this.m_requestData,
97+
RequestUrl = this.m_request.RequestUri.ToString(),
98+
RequestMethod = this.m_request.Method
99+
});
100+
101+
}
102+
103+
private AsyncCallback Monitor(AsyncCallback callback)
104+
{
105+
return ar =>
106+
{
107+
try
108+
{
109+
callback(ar);
110+
}
111+
catch (WebException webException)
112+
{
113+
var connectionStatus = new ConnectionStatus(webException)
114+
{
115+
Request = this.m_requestData,
116+
RequestUrl = this.m_request.RequestUri.ToString(),
117+
RequestMethod = this.m_request.Method
118+
};
119+
m_connectionSettings.ConnectionStatusHandler(connectionStatus);
120+
Done(connectionStatus);
121+
}
122+
catch (Exception e)
123+
{
124+
TrySetException(e);
125+
Dispose();
126+
}
127+
};
128+
}
129+
130+
public void Dispose()
131+
{
132+
Dispose(ref m_response);
133+
Dispose(ref m_responseStream);
134+
Dispose(ref m_tracer);
135+
}
136+
137+
private static void Dispose<T>(ref T disposable) where T : class, IDisposable
138+
{
139+
var d = Interlocked.Exchange(ref disposable, null);
140+
if (d != null)
141+
d.Dispose();
142+
}
143+
}
144+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
 using System;
2+
using System.IO;
3+
using System.Net;
4+
using System.Reflection;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Nest.Domain.Connection;
8+
9+
namespace Nest
10+
{
11+
public class NoTaskHttpConnection : Connection
12+
{
13+
public NoTaskHttpConnection(IConnectionSettings settings) : base(settings)
14+
{
15+
}
16+
17+
protected virtual Task<ConnectionStatus> DoAsyncRequest(HttpWebRequest request, string data = null)
18+
{
19+
var operation = new AsyncRequestOperation(
20+
request,
21+
data,
22+
_ConnectionSettings,
23+
new ConnectionStatusTracer( this._ConnectionSettings.TraceEnabled ) );
24+
return operation.Task;
25+
}
26+
27+
}
28+
}

Diff for: src/ProtocolLoadTest/ProtocolLoadTest.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
<Reference Include="System.Xml" />
6565
</ItemGroup>
6666
<ItemGroup>
67+
<Compile Include="Connection\AsyncRequestOperation.cs" />
68+
<Compile Include="Connection\NoTasksHttpConnection.cs" />
6769
<Compile Include="TrueAsyncTester.cs" />
6870
<Compile Include="HttpManualAsyncTester.cs" />
6971
<Compile Include="HttpTester.cs" />

0 commit comments

Comments
 (0)