40 currentPipeName = pipeName;
41 return openInternal (pipeName,
false,
false);
47 return pimpl !=
nullptr;
55 currentPipeName = pipeName;
56 return openInternal (pipeName,
true, mustNotExist);
62 return currentPipeName;
72class NamedPipeTests final :
public UnitTest
77 :
UnitTest (
"NamedPipe", UnitTestCategories::networking)
80 void runTest()
override
82 const auto pipeName =
"TestPipe" + String ((intptr_t) Thread::getCurrentThreadId());
84 beginTest (
"Pre test cleanup");
87 expect (pipe.createNewPipe (pipeName,
false));
90 beginTest (
"Create pipe");
93 expect (! pipe.isOpen());
95 expect (pipe.createNewPipe (pipeName,
true));
96 expect (pipe.isOpen());
98 expect (pipe.createNewPipe (pipeName,
false));
99 expect (pipe.isOpen());
102 expect (! otherPipe.createNewPipe (pipeName,
true));
103 expect (! otherPipe.isOpen());
106 beginTest (
"Existing pipe");
110 expect (! pipe.openExisting (pipeName));
111 expect (! pipe.isOpen());
113 expect (pipe.createNewPipe (pipeName,
true));
116 expect (otherPipe.openExisting (pipeName));
117 expect (otherPipe.isOpen());
120 int sendData = 4684682;
122 beginTest (
"Receive message created pipe");
125 expect (pipe.createNewPipe (pipeName,
true));
127 WaitableEvent senderFinished;
128 SenderThread sender (pipeName,
false, senderFinished, sendData);
130 sender.startThread();
133 auto bytesRead = pipe.read (&recvData,
sizeof (recvData), 2000);
135 expect (senderFinished.wait (4000));
137 expectEquals (bytesRead, (
int)
sizeof (recvData));
138 expectEquals (sender.result, (
int) sizeof (sendData));
139 expectEquals (recvData, sendData);
142 beginTest (
"Receive message existing pipe");
144 WaitableEvent senderFinished;
145 SenderThread sender (pipeName,
true, senderFinished, sendData);
148 expect (pipe.openExisting (pipeName));
150 sender.startThread();
153 auto bytesRead = pipe.read (&recvData,
sizeof (recvData), 2000);
155 expect (senderFinished.wait (4000));
157 expectEquals (bytesRead, (
int)
sizeof (recvData));
158 expectEquals (sender.result, (
int) sizeof (sendData));
159 expectEquals (recvData, sendData);
162 beginTest (
"Send message created pipe");
165 expect (pipe.createNewPipe (pipeName,
true));
167 WaitableEvent receiverFinished;
168 ReceiverThread receiver (pipeName,
false, receiverFinished);
170 receiver.startThread();
172 auto bytesWritten = pipe.write (&sendData,
sizeof (sendData), 2000);
174 expect (receiverFinished.wait (4000));
176 expectEquals (bytesWritten, (
int)
sizeof (sendData));
177 expectEquals (receiver.result, (
int) sizeof (receiver.recvData));
178 expectEquals (receiver.recvData, sendData);
181 beginTest (
"Send message existing pipe");
183 WaitableEvent receiverFinished;
184 ReceiverThread receiver (pipeName,
true, receiverFinished);
187 expect (pipe.openExisting (pipeName));
189 receiver.startThread();
191 auto bytesWritten = pipe.write (&sendData,
sizeof (sendData), 2000);
193 expect (receiverFinished.wait (4000));
195 expectEquals (bytesWritten, (
int)
sizeof (sendData));
196 expectEquals (receiver.result, (
int) sizeof (receiver.recvData));
197 expectEquals (receiver.recvData, sendData);
203 struct NamedPipeThread :
public Thread
205 NamedPipeThread (
const String& tName,
const String& pName,
206 bool shouldCreatePipe, WaitableEvent& completed)
207 : Thread (tName), pipeName (pName), workCompleted (completed)
209 if (shouldCreatePipe)
210 pipe.createNewPipe (pipeName);
212 pipe.openExisting (pipeName);
216 const String& pipeName;
217 WaitableEvent& workCompleted;
223 struct SenderThread final :
public NamedPipeThread
225 SenderThread (
const String& pName,
bool shouldCreatePipe,
226 WaitableEvent& completed,
int sData)
227 : NamedPipeThread (
"NamePipeSender", pName, shouldCreatePipe, completed),
231 ~SenderThread()
override
238 result = pipe.write (&sendData,
sizeof (sendData), 2000);
239 workCompleted.signal();
246 struct ReceiverThread final :
public NamedPipeThread
248 ReceiverThread (
const String& pName,
bool shouldCreatePipe,
249 WaitableEvent& completed)
250 : NamedPipeThread (
"NamePipeSender", pName, shouldCreatePipe, completed)
253 ~ReceiverThread()
override
260 result = pipe.read (&recvData,
sizeof (recvData), 2000);
261 workCompleted.signal();
268static NamedPipeTests namedPipeTests;
bool createNewPipe(const String &pipeName, bool mustNotExist=false)
bool openExisting(const String &pipeName)