1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
using System;
using System.Collections;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using NUnit.Framework;
namespace MonoTests.System.Net.Sockets
{
[TestFixture]
public class SocketConnectAsyncTest
{
Socket serverSocket;
Socket clientSocket;
SocketAsyncEventArgs clientSocketAsyncArgs;
ManualResetEvent readyEvent;
ManualResetEvent mainEvent;
Exception error;
[TestFixtureSetUp]
public void SetUp ()
{
readyEvent = new ManualResetEvent (false);
mainEvent = new ManualResetEvent (false);
}
[TestFixtureTearDown]
public void TearDown ()
{
readyEvent.Close ();
mainEvent.Close ();
}
void StartServer()
{
readyEvent.Reset();
mainEvent.Reset();
ThreadPool.QueueUserWorkItem (_ => DoWork ());
readyEvent.WaitOne ();
}
void StopServer()
{
if (serverSocket != null)
serverSocket.Close ();
}
void DoWork ()
{
serverSocket = new Socket (
AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind (new IPEndPoint (IPAddress.Loopback, 0));
serverSocket.Listen (1);
var async = new SocketAsyncEventArgs ();
async.Completed += (s,e) => OnAccepted (e);
readyEvent.Set ();
if (!serverSocket.AcceptAsync (async))
OnAccepted (async);
}
void OnAccepted (SocketAsyncEventArgs e)
{
var acceptSocket = e.AcceptSocket;
mainEvent.Set ();
}
[Test]
[Category("NotWorking")]
public void Connect ()
{
StartServer();
EndPoint serverEndpoint = serverSocket.LocalEndPoint;
var m = new ManualResetEvent (false);
var e = new SocketAsyncEventArgs ();
clientSocket = new Socket (
AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocketAsyncArgs = new SocketAsyncEventArgs();
clientSocketAsyncArgs.RemoteEndPoint = serverEndpoint;
clientSocketAsyncArgs.Completed += (s,o) => {
if (o.SocketError != SocketError.Success)
error = new SocketException ((int)o.SocketError);
m.Set ();
};
bool res = clientSocket.ConnectAsync(clientSocketAsyncArgs);
if (res) {
if (!m.WaitOne (1500))
throw new TimeoutException ();
}
if (!mainEvent.WaitOne (1500))
throw new TimeoutException ();
if (error != null)
throw error;
m.Reset ();
mainEvent.Reset ();
StopServer();
// Try again to non-listening endpoint, expect error
error = null;
clientSocket = new Socket (
AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocketAsyncArgs = new SocketAsyncEventArgs ();
clientSocketAsyncArgs.RemoteEndPoint = serverEndpoint;
clientSocketAsyncArgs.Completed += (s,o) => {
if (o.SocketError != SocketError.Success)
error = new SocketException ((int)o.SocketError);
m.Set ();
};
res = clientSocket.ConnectAsync (clientSocketAsyncArgs);
if (res) {
if (!m.WaitOne (1500))
throw new TimeoutException ();
}
Assert.IsTrue (error != null, "Connect - no error");
SocketException socketException = (SocketException)error;
Assert.IsTrue(socketException.ErrorCode == (int)SocketError.ConnectionRefused);
m.Reset ();
mainEvent.Reset ();
}
}
}
|