/*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2014-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* limitations under the License.
*/
-#include <folly/io/async/AsyncUDPSocket.h>
-#include <folly/io/async/AsyncUDPServerSocket.h>
-#include <folly/io/async/AsyncTimeout.h>
-#include <folly/io/async/EventBase.h>
-#include <folly/SocketAddress.h>
-
-#include <boost/thread/barrier.hpp>
+#include <thread>
+#include <folly/Conv.h>
+#include <folly/SocketAddress.h>
#include <folly/io/IOBuf.h>
+#include <folly/io/async/AsyncTimeout.h>
+#include <folly/io/async/AsyncUDPServerSocket.h>
+#include <folly/io/async/AsyncUDPSocket.h>
+#include <folly/io/async/EventBase.h>
+#include <folly/portability/GMock.h>
#include <folly/portability/GTest.h>
-#include <thread>
-
using folly::AsyncUDPSocket;
using folly::AsyncUDPServerSocket;
using folly::AsyncTimeout;
using folly::EventBase;
using folly::SocketAddress;
using folly::IOBuf;
+using namespace testing;
class UDPAcceptor
: public AsyncUDPServerSocket::Callback {
void start() {
CHECK(evb_->isInEventBaseThread());
- socket_ = folly::make_unique<AsyncUDPServerSocket>(
- evb_,
- 1500);
+ socket_ = std::make_unique<AsyncUDPServerSocket>(evb_, 1500);
try {
socket_->bind(addr_);
evb.loopForever();
});
- auto r = std::make_shared<boost::barrier>(2);
- evb.runInEventBaseThread([r] () {
- r->wait();
- });
- r->wait();
+ evb.waitUntilRunning();
socket_->addListener(&evb, &acceptors_[i]);
threads_.emplace_back(std::move(t));
CHECK(evb_->isInEventBaseThread());
server_ = server;
- socket_ = folly::make_unique<AsyncUDPSocket>(evb_);
+ socket_ = std::make_unique<AsyncUDPSocket>(evb_);
try {
socket_->bind(folly::SocketAddress("127.0.0.1", 0));
TEST(AsyncSocketTest, PingPong) {
folly::EventBase sevb;
UDPServer server(&sevb, folly::SocketAddress("127.0.0.1", 0), 4);
- boost::barrier barrier(2);
// Start event loop in a separate thread
auto serverThread = std::thread([&sevb] () {
});
// Wait for event loop to start
- sevb.runInEventBaseThread([&] () { barrier.wait(); });
- barrier.wait();
+ sevb.waitUntilRunning();
// Start the server
- sevb.runInEventBaseThread([&] () { server.start(); barrier.wait(); });
- barrier.wait();
+ sevb.runInEventBaseThreadAndWait([&]() { server.start(); });
folly::EventBase cevb;
UDPClient client(&cevb);
});
// Wait for event loop to start
- cevb.runInEventBaseThread([&] () { barrier.wait(); });
- barrier.wait();
+ cevb.waitUntilRunning();
// Send ping
cevb.runInEventBaseThread([&] () { client.start(server.address(), 1000); });
// Wait for server thread to joib
serverThread.join();
}
+
+class TestAsyncUDPSocket : public AsyncUDPSocket {
+ public:
+ explicit TestAsyncUDPSocket(EventBase* evb) : AsyncUDPSocket(evb) {}
+
+ MOCK_METHOD3(sendmsg, ssize_t(int, const struct msghdr*, int));
+};