Debian Codegraph @

test/core/event_engine/windows/win_socket_test.cc

raw
1 // Copyright 2022 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <grpc/support/port_platform.h>
16
17 #ifdef GPR_WINDOWS
18 #include <gtest/gtest.h>
19
20 #include "absl/status/status.h"
21 #include "absl/time/time.h"
22
23 #include <grpc/grpc.h>
24 #include <grpc/support/log_windows.h>
25
26 #include "src/core/lib/event_engine/common_closures.h"
27 #include "src/core/lib/event_engine/thread_pool.h"
28 #include "src/core/lib/event_engine/windows/iocp.h"
29 #include "src/core/lib/event_engine/windows/win_socket.h"
30 #include "src/core/lib/iomgr/error.h"
31 #include "test/core/event_engine/windows/create_sockpair.h"
32
33 namespace {
34 using ::grpc_event_engine::experimental::AnyInvocableClosure;
35 using ::grpc_event_engine::experimental::CreateSockpair;
36 using ::grpc_event_engine::experimental::IOCP;
37 using ::grpc_event_engine::experimental::ThreadPool;
38 using ::grpc_event_engine::experimental::WinSocket;
39 } // namespace
40
41 class WinSocketTest : public testing::Test {};
42
43 TEST_F(WinSocketTest, ManualReadEventTriggeredWithoutIO) {
44 ThreadPool executor;
45 SOCKET sockpair[2];
46 CreateSockpair(sockpair, IOCP::GetDefaultSocketFlags());
47 WinSocket wrapped_client_socket(sockpair[0], &executor);
48 WinSocket wrapped_server_socket(sockpair[1], &executor);
49 bool read_called = false;
50 AnyInvocableClosure on_read([&read_called]() { read_called = true; });
51 wrapped_client_socket.NotifyOnRead(&on_read);
52 AnyInvocableClosure on_write([] { FAIL() << "No Write expected"; });
53 wrapped_client_socket.NotifyOnWrite(&on_write);
54 ASSERT_FALSE(read_called);
55 wrapped_client_socket.SetReadable();
56 absl::Time deadline = absl::Now() + absl::Seconds(10);
57 while (!read_called) {
58 absl::SleepFor(absl::Milliseconds(42));
59 if (deadline < absl::Now()) {
60 FAIL() << "Deadline exceeded";
61 }
62 }
63 ASSERT_TRUE(read_called);
64 wrapped_client_socket.MaybeShutdown(absl::CancelledError("done"));
65 wrapped_server_socket.MaybeShutdown(absl::CancelledError("done"));
66 executor.Quiesce();
67 }
68
69 TEST_F(WinSocketTest, NotificationCalledImmediatelyOnShutdownWinSocket) {
70 ThreadPool executor;
71 SOCKET sockpair[2];
72 CreateSockpair(sockpair, IOCP::GetDefaultSocketFlags());
73 WinSocket wrapped_client_socket(sockpair[0], &executor);
74 wrapped_client_socket.MaybeShutdown(absl::CancelledError("testing"));
75 bool read_called = false;
76 AnyInvocableClosure closure([&wrapped_client_socket, &read_called] {
77 ASSERT_EQ(wrapped_client_socket.read_info()->bytes_transferred(), 0);
78 ASSERT_EQ(wrapped_client_socket.read_info()->wsa_error(), WSAESHUTDOWN);
79 read_called = true;
80 });
81 wrapped_client_socket.NotifyOnRead(&closure);
82 absl::Time deadline = absl::Now() + absl::Seconds(3);
83 while (!read_called) {
84 absl::SleepFor(absl::Milliseconds(42));
85 if (deadline < absl::Now()) {
86 FAIL() << "Deadline exceeded";
87 }
88 }
89 ASSERT_TRUE(read_called);
90 closesocket(sockpair[1]);
91 executor.Quiesce();
92 }
93
94 int main(int argc, char** argv) {
95 ::testing::InitGoogleTest(&argc, argv);
96 grpc_init();
97 int status = RUN_ALL_TESTS();
98 grpc_shutdown();
99 return status;
100 }
101
102 #else // not GPR_WINDOWS
103 int main(int /* argc */, char** /* argv */) { return 0; }
104 #endif