Dive communication protocol: Unit tests for client and unix domain server#357
Dive communication protocol: Unit tests for client and unix domain server#357elviscapiaq wants to merge 12 commits intogoogle:mainfrom
Conversation
|
Please take a look of this CL. I want your opinion to decide if we add unit tests in this way or maybe using something else like Google Mock? |
6df7e26 to
010ed76
Compare
|
@wangra-google , @footballhead, I fixed the issue for Linux, now you can take a look of this commit. |
|
At a high-level, I think this is a good approach since we should strive to write tests using the real implementation. (Google SWE Handbook, chapter 18.e.i) The main arguments against mocking from that chapter are:
Chapter 18: Test Doubles goes into options that you can consider if testing the real implementation is not feasible. In general, they encourage looking at other options first instead of mocks, e.g. fakes. I'd encourage reading Chapters 16-19 of the SWE handbook |
010ed76 to
136e942
Compare
Thanks for poiting it out. I have read all these good practices and made a commit based on that. |
|
I am restarting this commit applying all the recommendations and after reading the tips about how to use a fake test. I found it very interesting and useful |
|
I fixed the issue as the server and client tests are only for Linux OS. |
|
I addressed the feedback, ready for review. |
72e8e1b to
8e433ae
Compare
|
Ready for review! |
8e433ae to
dd86539
Compare
fc3a2c3 to
2e44e6a
Compare
2e44e6a to
6859a0d
Compare
| /// Connects to an Unix (Local) Domain with abstract namespace. | ||
| sockaddr_un addr = {}; | ||
| addr.sun_family = AF_UNIX; | ||
| // first char is '\0' |
There was a problem hiding this comment.
Let the code speak for itself. Comments should focus on the why, not the what. The what should be obvious from reading the code. If it's not then that's a good sign that it should be refactored
| // first char is '\0' |
| addr.sun_path[0] = '\0'; | ||
| strncpy(addr.sun_path + 1, kTestServerAddress, std::size(kTestServerAddress)); | ||
| if (connect(client_socket, | ||
| (sockaddr*)&addr, |
There was a problem hiding this comment.
Prefer C++ casts since they are more explicit https://google.github.io/styleguide/cppguide.html#Casting
| ASSERT_TRUE(server->Start(kTestServerAddress).ok()); | ||
|
|
||
| absl::StatusOr<std::unique_ptr<SocketConnection>> client_conn = ConnectClient(); | ||
| ASSERT_TRUE(client_conn.ok()); |
There was a problem hiding this comment.
Prefer using the absl_testing::IsOk() matcher since it will print more information if something fails
| ASSERT_TRUE(client_conn.ok()); | |
| #include "absl/status/status_matchers.h" | |
| using ::absl_testing::IsOk; | |
| // ... | |
| ASSERT_THAT(client_conn.ok(), IsOk()); |
| TEST(UnixDomainServerTest, HandShakeSuccess) | ||
| { | ||
| auto server = std::make_unique<UnixDomainServer>(); | ||
| ASSERT_TRUE(server->Start(kTestServerAddress).ok()); |
There was a problem hiding this comment.
Prefer using the absl_testing::IsOk() matcher since it will print more information if something fails
| ASSERT_TRUE(server->Start(kTestServerAddress).ok()); | ||
|
|
||
| absl::StatusOr<std::unique_ptr<SocketConnection>> client_conn = ConnectClient(); | ||
| ASSERT_TRUE(client_conn.ok()); |
There was a problem hiding this comment.
Prefer using the absl_testing::IsOk() matcher since it will print more information if something fails
| HandshakeRequest request; | ||
| request.SetMajorVersion(5); | ||
| request.SetMinorVersion(2); | ||
| ASSERT_TRUE(SendSocketMessage((*client_conn).get(), request).ok()); |
There was a problem hiding this comment.
Prefer using the absl_testing::IsOk() matcher since it will print more information if something fails
| // Client receives response. | ||
| absl::StatusOr<std::unique_ptr<ISerializable>> response_msg = ReceiveSocketMessage( | ||
| (*client_conn).get()); | ||
| ASSERT_TRUE(response_msg.ok()); |
There was a problem hiding this comment.
Prefer using the absl_testing::IsOk() matcher since it will print more information if something fails
| ASSERT_EQ((*response_msg)->GetMessageType(), MessageType::HANDSHAKE_RESPONSE); | ||
| auto* response = dynamic_cast<HandshakeResponse*>((*response_msg).get()); | ||
| ASSERT_NE(response, nullptr); | ||
| EXPECT_EQ(response->GetMajorVersion(), request.GetMajorVersion()); |
There was a problem hiding this comment.
Prefer a custom matcher here
No description provided.