Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: test/CommunicationTest.cpp

Issue 4882650246414336: Issue 2005 - Refactor working with strings in InputBuffer and OutputBuffer
Patch Set: Created Feb. 17, 2015, 12:36 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« src/shared/Communication.h ('K') | « src/shared/Communication.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/CommunicationTest.cpp
diff --git a/test/CommunicationTest.cpp b/test/CommunicationTest.cpp
index fb9d4c34f35220fdaab146f480a15be2e9fff40d..875bbddaab9a292536f70aea71f79e96d98bdb60 100644
--- a/test/CommunicationTest.cpp
+++ b/test/CommunicationTest.cpp
@@ -17,114 +17,206 @@
#include <gtest/gtest.h>
+#include <thread>
+#include <chrono>
+#include <memory>
#include "../src/shared/AutoHandle.h"
#include "../src/shared/Communication.h"
const std::wstring pipeName(L"\\\\.\\pipe\\adblockplustests");
-namespace
+TEST(CommunicationTest, PipeReadWrite)
Eric 2015/02/17 18:15:13 As long as we're improving the unit tests, can we
sergei 2015/02/18 14:13:06 Actually I would not like to improve tests in that
Eric 2015/02/18 18:12:47 OK. I am most concerned with eliminating the expli
{
- DWORD WINAPI CreatePipe(LPVOID param)
+ auto serverThread = std::thread([]
{
Communication::Pipe pipe(pipeName, Communication::Pipe::MODE_CREATE);
- return 0;
+ });
+ std::this_thread::sleep_for(std::chrono::milliseconds(100));
+ ASSERT_NO_THROW(Communication::Pipe pipe(pipeName, Communication::Pipe::MODE_CONNECT));
+ serverThread.join();
+}
+
+TEST(CommunicationTest, SendReceivePipe)
+{
+ std::unique_ptr<Communication::Pipe> server;
+ auto serverThread = std::thread([&server]
Eric 2015/02/17 18:15:13 Is it necessary to use a separate thread to initia
+ {
+ server.reset(new Communication::Pipe(pipeName, Communication::Pipe::MODE_CREATE));
+ });
+ std::this_thread::sleep_for(std::chrono::milliseconds(100));
Eric 2015/02/17 18:15:13 I myself have had trouble with the Communication u
+ Communication::Pipe client(pipeName, Communication::Pipe::MODE_CONNECT);
+ // send to the server
+ {
+ Communication::OutputBuffer message;
+ message << Communication::ProcType::PROC_IS_WHITELISTED_URL
+ << std::string("This is a test message") << 64ll << 32 << true << false;
+ client.WriteMessage(message);
+ }
+ // receive on the server and send back
+ {
+ Communication::OutputBuffer response;
+ Communication::InputBuffer message = server->ReadMessage();
+ Communication::ProcType proc;
+ message >> proc;
+ EXPECT_EQ(Communication::ProcType::PROC_IS_WHITELISTED_URL, proc);
+ response << Communication::ProcType::PROC_IS_FIRST_RUN_ACTION_NEEDED;
+
Eric 2015/02/17 18:15:13 Each of these individual tests on disparate variab
sergei 2015/02/18 14:13:06 The tests of each type are below, it's not a testi
Eric 2015/02/18 18:12:47 I don't want to belabor the point here, but they'r
+ std::string text;
+ message >> text;
+ EXPECT_EQ("This is a test message", text);
+ response << std::string("Response");
+
+ int64_t integer64;
+ message >> integer64;
+ EXPECT_EQ(64ll, integer64);
+ response << 46ll;
+
+ int32_t integer32;
+ message >> integer32;
+ EXPECT_EQ(32, integer32);
+ response << 23;
+
+ bool boolean;
+ message >> boolean;
+ EXPECT_TRUE(boolean);
+ response << false;
+
+ message >> boolean;
+ EXPECT_FALSE(boolean);
+ response << true;
+
+ server->WriteMessage(response);
}
- DWORD WINAPI ReceiveSend(LPVOID param)
+ // receive on the client
{
- Communication::Pipe pipe(pipeName, Communication::Pipe::MODE_CREATE);
+ Communication::InputBuffer message = client.ReadMessage();
+ Communication::ProcType proc;
+ message >> proc;
+ EXPECT_EQ(Communication::ProcType::PROC_IS_FIRST_RUN_ACTION_NEEDED, proc);
- Communication::InputBuffer message = pipe.ReadMessage();
+ std::string text;
+ message >> text;
+ EXPECT_EQ("Response", text);
- std::string stringValue;
- std::wstring wstringValue;
- int64_t int64Value;
- int32_t int32Value;
- bool boolValue;
- message >> stringValue >> wstringValue >> int64Value >> int32Value >> boolValue;
+ int64_t integer64;
+ message >> integer64;
+ EXPECT_EQ(46ll, integer64);
- stringValue += " Received";
- wstringValue += L" \u043f\u0440\u0438\u043d\u044f\u0442\u043e";
- int64Value += 1;
- int32Value += 2;
- boolValue = !boolValue;
+ int32_t integer32;
+ message >> integer32;
+ EXPECT_EQ(23, integer32);
- Communication::OutputBuffer response;
- response << stringValue << wstringValue << int64Value << int32Value << boolValue;
- pipe.WriteMessage(response);
+ bool boolean;
+ message >> boolean;
+ EXPECT_FALSE(boolean);
- return 0;
+ message >> boolean;
+ EXPECT_TRUE(boolean);
}
+ serverThread.join();
}
-TEST(CommunicationTest, ConnectPipe)
+namespace
{
- AutoHandle thread(CreateThread(0, 0, CreatePipe, 0, 0, 0));
+ template<typename TestedType, typename AnotherType>
+ void SendReceiveTypes(TestedType value, AnotherType anotherTypeValue)
Eric 2015/02/17 18:15:13 You're combining two kinds of test here. 1) Ensure
sergei 2015/02/18 14:13:06 Before the everything was in one test, would you m
Eric 2015/02/18 18:12:47 That's fine.
+ {
+ Communication::OutputBuffer message;
+ message << anotherTypeValue << value;
+ Communication::InputBuffer response(message.Get());
Eric 2015/02/17 18:15:13 I don't particularly care for Get() in the current
sergei 2015/02/18 14:13:06 I find it as overkill here. On 2015/02/17 18:15:1
+ TestedType received;
+ ASSERT_ANY_THROW(response >> received);
+ AnotherType anotherTypeValueReceived;
+ response >> anotherTypeValueReceived;
+ response >> received;
+ EXPECT_EQ(value, received);
+ }
+}
- Sleep(100);
+TEST(CommunicationTest, TypeProc)
+{
+ SendReceiveTypes(Communication::ProcType::PROC_GET_ELEMHIDE_SELECTORS, std::string("non-ProcType"));
+}
- ASSERT_NO_THROW(Communication::Pipe pipe(pipeName, Communication::Pipe::MODE_CONNECT));
+TEST(CommunicationTest, TypeString)
+{
+ int32_t nonStringTypeValue = 10;
+ SendReceiveTypes(std::string("stringValue"), nonStringTypeValue);
}
-TEST(CommunicationTest, SendReceive)
+TEST(CommunicationTest, SendWString_ReceiveWString)
Eric 2015/02/17 18:15:13 Probably better to spell out "Wide" here and below
sergei 2015/02/18 14:13:06 done.
{
- AutoHandle thread(CreateThread(0, 0, ReceiveSend, 0, 0, 0));
+ int32_t nonStringTypeValue = 10;
+ SendReceiveTypes(std::wstring(L"stringValue"), nonStringTypeValue);
+}
- Sleep(100);
+TEST(CommunicationTest, SendString_ReceiveWString)
+{
+ Communication::OutputBuffer message;
+ message << std::string("test");
+ Communication::InputBuffer response(message.Get());
+ std::wstring received;
+ response >> received;
+ EXPECT_EQ(L"test", received);
+}
- Communication::Pipe pipe(pipeName, Communication::Pipe::MODE_CONNECT);
+TEST(CommunicationTest, SendWString_ReceiveString)
+{
+ Communication::OutputBuffer message;
+ message << std::wstring(L"test");
+ Communication::InputBuffer response(message.Get());
+ std::string received;
+ response >> received;
+ EXPECT_EQ("test", received);
+}
+TEST(CommunicationTest, SendStrings_ReceiveWStrings)
+{
Communication::OutputBuffer message;
- message << std::string("Foo") << std::wstring(L"Bar") << int64_t(9876543210L) << int32_t(5) << true;
- pipe.WriteMessage(message);
-
- Communication::InputBuffer response = pipe.ReadMessage();
-
- std::string stringValue;
- std::wstring wstringValue;
- int64_t int64Value;
- int32_t int32Value;
- bool boolValue;
-
- ASSERT_ANY_THROW(response >> wstringValue);
- ASSERT_ANY_THROW(response >> int64Value);
- ASSERT_ANY_THROW(response >> int32Value);
- ASSERT_ANY_THROW(response >> boolValue);
-
- response >> stringValue >> wstringValue;
-
- ASSERT_ANY_THROW(response >> stringValue);
- ASSERT_ANY_THROW(response >> wstringValue);
- ASSERT_ANY_THROW(response >> int32Value);
- ASSERT_ANY_THROW(response >> boolValue);
-
- response >> int64Value >> int32Value >> boolValue;
-
- ASSERT_ANY_THROW(response >> stringValue);
- ASSERT_ANY_THROW(response >> wstringValue);
- ASSERT_ANY_THROW(response >> int64Value);
- ASSERT_ANY_THROW(response >> int32Value);
- ASSERT_ANY_THROW(response >> boolValue);
-
- ASSERT_EQ("Foo Received", stringValue);
- ASSERT_EQ(L"Bar \u043f\u0440\u0438\u043d\u044f\u0442\u043e", wstringValue);
- ASSERT_EQ(9876543211L, int64Value);
- ASSERT_EQ(7, int32Value);
- ASSERT_FALSE(boolValue);
+ std::vector<std::string> strings;
+ strings.emplace_back("test0");
+ strings.emplace_back("test1");
+ message << strings;
+ Communication::InputBuffer response(message.Get());
+ std::vector<std::wstring> received;
+ response >> received;
+ ASSERT_EQ(2u, received.size());
+ EXPECT_EQ(L"test0", received[0]);
+ EXPECT_EQ(L"test1", received[1]);
}
-void SendReceiveStrings(const std::vector<std::string>& src)
+TEST(CommunicationTest, TypeInt64)
+{
+ SendReceiveTypes<int64_t>(10, std::string("non-ProcType"));
+}
+
+TEST(CommunicationTest, TypeInt32)
+{
+ SendReceiveTypes<int32_t>(10, std::string("non-ProcType"));
+}
+
+TEST(CommunicationTest, TypeBool)
+{
+ SendReceiveTypes<bool>(true, std::string("non-ProcType"));
+ SendReceiveTypes<bool>(false, std::string("non-ProcType"));
+}
+
+namespace
{
- Communication::OutputBuffer outputBuffer;
- outputBuffer << src;
- Communication::InputBuffer inputBuffer(outputBuffer.Get());
- std::vector<std::string> dst;
- inputBuffer >> dst;
- auto dstSize = dst.size();
- ASSERT_EQ(dstSize, src.size());
- for (auto i = 0; i < dstSize; ++i)
+ void SendReceiveStrings(const std::vector<std::string>& src)
Eric 2015/02/17 18:15:13 We should test narrow-wide conversions here as wel
sergei 2015/02/18 14:13:06 added
{
- EXPECT_EQ(dst[i], src[i]);
+ Communication::OutputBuffer outputBuffer;
+ outputBuffer << src;
+ Communication::InputBuffer inputBuffer(outputBuffer.Get());
+ std::vector<std::string> dst;
+ inputBuffer >> dst;
+ auto dstSize = dst.size();
+ ASSERT_EQ(dstSize, src.size());
+ for (auto i = 0u; i < dstSize; ++i)
+ {
+ EXPECT_EQ(dst[i], src[i]);
+ }
}
}
« src/shared/Communication.h ('K') | « src/shared/Communication.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld