proxygen
FileRegionTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
20 
21 #ifdef SPLICE_F_NONBLOCK
22 using namespace folly;
23 using namespace wangle;
24 using namespace testing;
25 
26 struct FileRegionTest : public Test {
27  FileRegionTest() {
28  // Connect
30  socket->connect(&ccb, server.getAddress(), 30);
31 
32  // Accept the connection
33  acceptedSocket = server.acceptAsync(&evb);
34  acceptedSocket->setReadCB(&rcb);
35 
36  // Create temp file
37  char path[] = "/tmp/AsyncSocketTest.WriteFile.XXXXXX";
38  fd = mkostemp(path, O_RDWR);
39  EXPECT_TRUE(fd > 0);
40  EXPECT_EQ(0, unlink(path));
41  }
42 
43  ~FileRegionTest() override {
44  // Close up shop
45  close(fd);
46  acceptedSocket->close();
47  socket->close();
48  }
49 
50  TestServer server;
51  EventBase evb;
52  std::shared_ptr<AsyncSocket> socket;
53  std::shared_ptr<AsyncSocket> acceptedSocket;
54  ConnCallback ccb;
55  ReadCallback rcb;
56  int fd;
57 };
58 
59 TEST_F(FileRegionTest, Basic) {
60  const size_t count = 1000000000; // 1 GB
61  std::unique_ptr<uint8_t[]> zeroBuf = std::make_unique<uint8_t[]>(count);
62  write(fd, zeroBuf.get(), count);
63 
64  FileRegion fileRegion(fd, 0, count);
65  auto f = fileRegion.transferTo(socket);
66  try {
67  f.getVia(&evb);
68  } catch (std::exception& e) {
69  LOG(FATAL) << exceptionStr(e);
70  }
71 
72  // Let the reads run to completion
73  socket->shutdownWrite();
74  evb.loopIgnoreKeepAlive();
75 
76  ASSERT_EQ(rcb.state, STATE_SUCCEEDED);
77 
78  size_t receivedBytes = 0;
79  for (auto& buf : rcb.buffers) {
80  receivedBytes += buf.length;
81  ASSERT_EQ(memcmp(buf.buffer, zeroBuf.get(), buf.length), 0);
82  }
83  ASSERT_EQ(receivedBytes, count);
84 }
85 
86 TEST_F(FileRegionTest, Repeated) {
87  const size_t count = 1000000;
88  std::unique_ptr<uint8_t[]> zeroBuf = std::make_unique<uint8_t[]>(count);
89  write(fd, zeroBuf.get(), count);
90 
91  int sendCount = 1000;
92 
93  FileRegion fileRegion(fd, 0, count);
94  std::vector<Future<Unit>> fs;
95  for (int i = 0; i < sendCount; i++) {
96  fs.push_back(fileRegion.transferTo(socket));
97  }
98  auto f = collect(fs);
99  ASSERT_NO_THROW(f.getVia(&evb));
100 
101  // Let the reads run to completion
102  socket->shutdownWrite();
103  evb.loopIgnoreKeepAlive();
104 
105  ASSERT_EQ(rcb.state, STATE_SUCCEEDED);
106 
107  size_t receivedBytes = 0;
108  for (auto& buf : rcb.buffers) {
109  receivedBytes += buf.length;
110  }
111  ASSERT_EQ(receivedBytes, sendCount*count);
112 }
113 #endif
auto f
void write(const T &in, folly::io::Appender &appender)
Definition: Types-inl.h:112
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
fbstring exceptionStr(const std::exception &e)
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
NetworkSocket socket(int af, int type, int protocol)
Definition: NetOps.cpp:412
TEST_F(AsyncSSLSocketWriteTest, write_coalescing1)
int * count
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
static std::shared_ptr< AsyncSocket > newSocket(EventBase *evb)
Definition: AsyncSocket.h:281
Future< std::vector< typename std::iterator_traits< InputIterator >::value_type::value_type > > collect(InputIterator first, InputIterator last)
Definition: Future-inl.h:1536
#define ASSERT_NO_THROW(statement)
Definition: gtest.h:1851
int close(NetworkSocket s)
Definition: NetOps.cpp:90