Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 210 lines (180 sloc) 5.258 kB
e165f52 @txdv Add Pipes!
authored
1 using System;
2 using Libuv;
3 using System.Net;
4 using System.Text;
5
6 using NUnit.Framework;
7
8 namespace Test
9 {
10 [TestFixture]
11 public class PipeTest
12 {
13 [TestCase]
14 public static void Simple()
15 {
16 Simple("PipeTestSimple");
17 }
18
19 public static void Simple(string name)
20 {
21 int close_cb_called = 0;
22 int cl_send_cb_called = 0;
23 int cl_recv_cb_called = 0;
24 int sv_send_cb_called = 0;
25 int sv_recv_cb_called = 0;
26
27 var server = new PipeServer();
28 server.Bind(name);
29 server.Listen((pipe) => {
30 var stream = pipe.Stream;
31 stream.Resume();
32 stream.Read(Encoding.ASCII, (str) => {
33 sv_recv_cb_called++;
34 Assert.AreEqual("PING", str);
35 stream.Write(Encoding.ASCII, "PONG", (s) => { sv_send_cb_called++; });
36
37 pipe.Close(() => { close_cb_called++; });
38 server.Close(() => { close_cb_called++; });
39 });
40 });
41
42 Pipe.Connect(name, (client) => {
43 var stream = client.Stream;
44 stream.Resume();
45 stream.Write(Encoding.ASCII, "PING", (s) => { cl_send_cb_called++; });
46 stream.Read(Encoding.ASCII, (str) => {
47 cl_recv_cb_called++;
48 Assert.AreEqual("PONG", str);
49 client.Close(() => { close_cb_called++; });
50 });
51 });
52
53 Assert.AreEqual(0, close_cb_called);
54 Assert.AreEqual(0, cl_send_cb_called);
55 Assert.AreEqual(0, cl_recv_cb_called);
56 Assert.AreEqual(0, sv_send_cb_called);
57 Assert.AreEqual(0, sv_recv_cb_called);
58
59 Loop.Default.Run();
60
61 Assert.AreEqual(3, close_cb_called);
62 Assert.AreEqual(1, cl_send_cb_called);
63 Assert.AreEqual(1, cl_recv_cb_called);
64 Assert.AreEqual(1, sv_send_cb_called);
65 Assert.AreEqual(1, sv_recv_cb_called);
66
67 #if DEBUG
68 Assert.AreEqual(1, UV.PointerCount);
69 #endif
70 }
71 public static string Times(string str, int times)
72 {
73 StringBuilder sb = new StringBuilder();
74 for (int i = 0; i < times; i++) {
75 sb.Append(str);
76 }
77 return sb.ToString();
78 }
79
80 [TestCase]
81 public static void Stress()
82 {
83 Stress("PipeTestStress");
84 }
85
86 public static void Stress(string name)
87 {
88 for (int j = 0; j < 10; j++) {
89 int times = 10;
90
91 int close_cb_called = 0;
92 int cl_send_cb_called = 0;
93 int cl_recv_cb_called = 0;
94 int sv_send_cb_called = 0;
95 int sv_recv_cb_called = 0;
96
97 var server = new PipeServer();
98 server.Bind(name);
99 server.Listen((pipe) => {
100 var stream = pipe.Stream;
101 stream.Resume();
102 stream.Read(Encoding.ASCII, (str) => {
103 sv_recv_cb_called++;
104 Assert.AreEqual(Times("PING", times), str);
105 for (int i = 0; i < times; i++) {
106 stream.Write(Encoding.ASCII, "PONG", (s) => { sv_send_cb_called++; });
107 }
108 pipe.Close(() => { close_cb_called++; });
109 server.Close(() => { close_cb_called++; });
110 });
111 });
112
113 Pipe.Connect(name, (client) => {
114 var stream = client.Stream;
115 stream.Resume();
116 for (int i = 0; i < times; i++) {
117 stream.Write(Encoding.ASCII, "PING", (s) => { cl_send_cb_called++; });
118 }
119 stream.Read(Encoding.ASCII, (str) => {
120 cl_recv_cb_called++;
121 Assert.AreEqual(Times("PONG", times), str);
122 client.Close(() => { close_cb_called++; });
123 });
124 });
125
126 Assert.AreEqual(0, close_cb_called);
127 Assert.AreEqual(0, cl_send_cb_called);
128 Assert.AreEqual(0, cl_recv_cb_called);
129 Assert.AreEqual(0, sv_send_cb_called);
130 Assert.AreEqual(0, sv_recv_cb_called);
131
132 Loop.Default.Run();
133
134 Assert.AreEqual(3, close_cb_called);
135 Assert.AreEqual(times, cl_send_cb_called);
136 Assert.AreEqual(1, cl_recv_cb_called);
137 Assert.AreEqual(times, sv_send_cb_called);
138 Assert.AreEqual(1, sv_recv_cb_called);
139
140 #if DEBUG
141 Assert.AreEqual(1, UV.PointerCount);
142 #endif
143 }
144 }
145
146 [TestCase]
147 public static void OneSideClose()
148 {
149 OneSideClose("PipeTestOneSidedClose");
150 }
151
152 public static void OneSideClose(string name)
153 {
154 int close_cb_called = 0;
155 int cl_send_cb_called = 0;
156 int cl_recv_cb_called = 0;
157 int sv_send_cb_called = 0;
158 int sv_recv_cb_called = 0;
159
160 var server = new PipeServer();
161 server.Bind(name);
162 server.Listen((pipe) => {
163 var stream = pipe.Stream;
164 stream.Resume();
165 stream.Read(Encoding.ASCII, (str) => {
166 sv_recv_cb_called++;
167 Assert.AreEqual("PING", str);
168 stream.Write(Encoding.ASCII, "PONG", (s) => { sv_send_cb_called++; });
169 pipe.Close(() => { close_cb_called++; });
170 server.Close(() => { close_cb_called++; });
171 });
172 });
173
174 Pipe.Connect(name, (client) => {
175 var stream = client.Stream;
176 stream.Read(Encoding.ASCII, (str) => {
177 cl_recv_cb_called++;
178 Assert.AreEqual("PONG", str);
179 });
180
181 stream.EndOfStream += () => {
182 close_cb_called++;
183 client.Close();
184 };
185 stream.Resume();
186 stream.Write(Encoding.ASCII, "PING", (s) => { cl_send_cb_called++; });
187 });
188
189 Assert.AreEqual(0, close_cb_called);
190 Assert.AreEqual(0, cl_send_cb_called);
191 Assert.AreEqual(0, cl_recv_cb_called);
192 Assert.AreEqual(0, sv_send_cb_called);
193 Assert.AreEqual(0, sv_recv_cb_called);
194
195 Loop.Default.Run();
196
197 Assert.AreEqual(3, close_cb_called);
198 Assert.AreEqual(1, cl_send_cb_called);
199 Assert.AreEqual(1, cl_recv_cb_called);
200 Assert.AreEqual(1, sv_send_cb_called);
201 Assert.AreEqual(1, sv_recv_cb_called);
202
203 #if DEBUG
204 Assert.AreEqual(1, UV.PointerCount);
205 #endif
206 }
207 }
208 }
209
Something went wrong with that request. Please try again.