38 currentPipeName = pipeName;
39 return openInternal (pipeName,
false,
false);
44 return pimpl !=
nullptr;
52 currentPipeName = pipeName;
53 return openInternal (pipeName,
true, mustNotExist);
58 return currentPipeName;
67class NamedPipeTests :
public UnitTest
72 :
UnitTest (
"NamedPipe",
"Networking")
75 void runTest()
override
77 const String pipeName (
"TestPipe");
79 beginTest (
"Pre test cleanup");
82 expect (pipe.createNewPipe (pipeName,
false));
85 beginTest (
"Create pipe");
88 expect (! pipe.isOpen());
90 expect (pipe.createNewPipe (pipeName,
true));
91 expect (pipe.isOpen());
93 expect (pipe.createNewPipe (pipeName,
false));
94 expect (pipe.isOpen());
97 expect (! otherPipe.createNewPipe (pipeName,
true));
98 expect (! otherPipe.isOpen());
101 beginTest (
"Existing pipe");
105 expect (! pipe.openExisting (pipeName));
106 expect (! pipe.isOpen());
108 expect (pipe.createNewPipe (pipeName,
true));
111 expect (otherPipe.openExisting (pipeName));
112 expect (otherPipe.isOpen());
115 int sendData = 4684682;
117 beginTest (
"Receive message created pipe");
120 expect (pipe.createNewPipe (pipeName,
true));
122 WaitableEvent senderFinished;
123 SenderThread sender (pipeName,
false, senderFinished, sendData);
125 sender.startThread();
128 auto bytesRead = pipe.read (&recvData,
sizeof (recvData), 2000);
130 expect (senderFinished.wait (4000));
132 expectEquals (bytesRead, (
int)
sizeof (recvData));
133 expectEquals (sender.result, (
int) sizeof (sendData));
134 expectEquals (recvData, sendData);
137 beginTest (
"Receive message existing pipe");
139 WaitableEvent senderFinished;
140 SenderThread sender (pipeName,
true, senderFinished, sendData);
143 expect (pipe.openExisting (pipeName));
145 sender.startThread();
148 auto bytesRead = pipe.read (&recvData,
sizeof (recvData), 2000);
150 expect (senderFinished.wait (4000));
152 expectEquals (bytesRead, (
int)
sizeof (recvData));
153 expectEquals (sender.result, (
int) sizeof (sendData));
154 expectEquals (recvData, sendData);
157 beginTest (
"Send message created pipe");
160 expect (pipe.createNewPipe (pipeName,
true));
162 WaitableEvent receiverFinished;
163 ReceiverThread receiver (pipeName,
false, receiverFinished);
165 receiver.startThread();
167 auto bytesWritten = pipe.write (&sendData,
sizeof (sendData), 2000);
169 expect (receiverFinished.wait (4000));
171 expectEquals (bytesWritten, (
int)
sizeof (sendData));
172 expectEquals (receiver.result, (
int) sizeof (receiver.recvData));
173 expectEquals (receiver.recvData, sendData);
176 beginTest (
"Send message existing pipe");
178 WaitableEvent receiverFinished;
179 ReceiverThread receiver (pipeName,
true, receiverFinished);
182 expect (pipe.openExisting (pipeName));
184 receiver.startThread();
186 auto bytesWritten = pipe.write (&sendData,
sizeof (sendData), 2000);
188 expect (receiverFinished.wait (4000));
190 expectEquals (bytesWritten, (
int)
sizeof (sendData));
191 expectEquals (receiver.result, (
int) sizeof (receiver.recvData));
192 expectEquals (receiver.recvData, sendData);
198 struct NamedPipeThread :
public Thread
200 NamedPipeThread (
const String& threadName,
const String& pName,
201 bool shouldCreatePipe, WaitableEvent& completed)
202 : Thread (threadName), pipeName (pName), workCompleted (completed)
204 if (shouldCreatePipe)
205 pipe.createNewPipe (pipeName);
207 pipe.openExisting (pipeName);
211 const String& pipeName;
212 WaitableEvent& workCompleted;
218 struct SenderThread :
public NamedPipeThread
220 SenderThread (
const String& pName,
bool shouldCreatePipe,
221 WaitableEvent& completed,
int sData)
222 : NamedPipeThread (
"NamePipeSender", pName, shouldCreatePipe, completed),
228 result = pipe.write (&sendData,
sizeof (sendData), 2000);
229 workCompleted.signal();
236 struct ReceiverThread :
public NamedPipeThread
238 ReceiverThread (
const String& pName,
bool shouldCreatePipe,
239 WaitableEvent& completed)
240 : NamedPipeThread (
"NamePipeSender", pName, shouldCreatePipe, completed)
245 result = pipe.read (&recvData,
sizeof (recvData), 2000);
246 workCompleted.signal();
253static NamedPipeTests namedPipeTests;
String getName() const
Returns the last name that was used to try to open this pipe.
bool isOpen() const
True if the pipe is currently open.
bool createNewPipe(const String &pipeName, bool mustNotExist=false)
Tries to create a new pipe.
NamedPipe()
Creates a NamedPipe.
bool openExisting(const String &pipeName)
Tries to open a pipe that already exists.
void close()
Closes the pipe, if it's open.
Automatically locks and unlocks a ReadWriteLock object.
This is a base class for classes that perform a unit test.