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