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;
68class NamedPipeTests :
public UnitTest
73 :
UnitTest (
"NamedPipe", UnitTestCategories::networking)
76 void runTest()
override
78 const String pipeName (
"TestPipe");
80 beginTest (
"Pre test cleanup");
83 expect (pipe.createNewPipe (pipeName,
false));
86 beginTest (
"Create pipe");
89 expect (! pipe.isOpen());
91 expect (pipe.createNewPipe (pipeName,
true));
92 expect (pipe.isOpen());
94 expect (pipe.createNewPipe (pipeName,
false));
95 expect (pipe.isOpen());
98 expect (! otherPipe.createNewPipe (pipeName,
true));
99 expect (! otherPipe.isOpen());
102 beginTest (
"Existing pipe");
106 expect (! pipe.openExisting (pipeName));
107 expect (! pipe.isOpen());
109 expect (pipe.createNewPipe (pipeName,
true));
112 expect (otherPipe.openExisting (pipeName));
113 expect (otherPipe.isOpen());
116 int sendData = 4684682;
118 beginTest (
"Receive message created pipe");
121 expect (pipe.createNewPipe (pipeName,
true));
123 WaitableEvent senderFinished;
124 SenderThread sender (pipeName,
false, senderFinished, sendData);
126 sender.startThread();
129 auto bytesRead = pipe.read (&recvData,
sizeof (recvData), 2000);
131 expect (senderFinished.wait (4000));
133 expectEquals (bytesRead, (
int)
sizeof (recvData));
134 expectEquals (sender.result, (
int) sizeof (sendData));
135 expectEquals (recvData, sendData);
138 beginTest (
"Receive message existing pipe");
140 WaitableEvent senderFinished;
141 SenderThread sender (pipeName,
true, senderFinished, sendData);
144 expect (pipe.openExisting (pipeName));
146 sender.startThread();
149 auto bytesRead = pipe.read (&recvData,
sizeof (recvData), 2000);
151 expect (senderFinished.wait (4000));
153 expectEquals (bytesRead, (
int)
sizeof (recvData));
154 expectEquals (sender.result, (
int) sizeof (sendData));
155 expectEquals (recvData, sendData);
158 beginTest (
"Send message created pipe");
161 expect (pipe.createNewPipe (pipeName,
true));
163 WaitableEvent receiverFinished;
164 ReceiverThread receiver (pipeName,
false, receiverFinished);
166 receiver.startThread();
168 auto bytesWritten = pipe.write (&sendData,
sizeof (sendData), 2000);
170 expect (receiverFinished.wait (4000));
172 expectEquals (bytesWritten, (
int)
sizeof (sendData));
173 expectEquals (receiver.result, (
int) sizeof (receiver.recvData));
174 expectEquals (receiver.recvData, sendData);
177 beginTest (
"Send message existing pipe");
179 WaitableEvent receiverFinished;
180 ReceiverThread receiver (pipeName,
true, receiverFinished);
183 expect (pipe.openExisting (pipeName));
185 receiver.startThread();
187 auto bytesWritten = pipe.write (&sendData,
sizeof (sendData), 2000);
189 expect (receiverFinished.wait (4000));
191 expectEquals (bytesWritten, (
int)
sizeof (sendData));
192 expectEquals (receiver.result, (
int) sizeof (receiver.recvData));
193 expectEquals (receiver.recvData, sendData);
199 struct NamedPipeThread :
public Thread
201 NamedPipeThread (
const String& tName,
const String& pName,
202 bool shouldCreatePipe, WaitableEvent& completed)
203 : Thread (tName), pipeName (pName), workCompleted (completed)
205 if (shouldCreatePipe)
206 pipe.createNewPipe (pipeName);
208 pipe.openExisting (pipeName);
217 const String& pipeName;
218 WaitableEvent& workCompleted;
224 struct SenderThread :
public NamedPipeThread
226 SenderThread (
const String& pName,
bool shouldCreatePipe,
227 WaitableEvent& completed,
int sData)
228 : NamedPipeThread (
"NamePipeSender", pName, shouldCreatePipe, completed),
234 result = pipe.write (&sendData,
sizeof (sendData), 2000);
235 workCompleted.signal();
242 struct ReceiverThread :
public NamedPipeThread
244 ReceiverThread (
const String& pName,
bool shouldCreatePipe,
245 WaitableEvent& completed)
246 : NamedPipeThread (
"NamePipeSender", pName, shouldCreatePipe, completed)
251 result = pipe.read (&recvData,
sizeof (recvData), 2000);
252 workCompleted.signal();
259static 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.