2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <folly/IPAddress.h>
23 #include <folly/IPAddressV6.h>
24 #include <folly/SocketAddress.h>
28 class AsyncServerSocket;
29 class ScopedEventBaseThread;
34 * Binds to an ephemeral port in the ctor but does not listen. Unbinds from the
37 * While an instance is in scope, we know at least one port which is guaranteed
38 * not to be listening - the port the instance binds but does not listen.
40 * Useful for testing server-down cases.
44 * TEST(MyClient, WhenTheServerIsDown_ThrowsServerDownException) {
45 * folly::ScopedBoundPort bound;
46 * MyClient client(bound.getAddress(), 100ms);
47 * EXPECT_THROW(client.getData(), ServerDownException);
50 class ScopedBoundPort {
52 explicit ScopedBoundPort(IPAddress host = IPAddressV6("::1"));
54 SocketAddress getAddress() const;
57 std::unique_ptr<ScopedEventBaseThread> ebth_;
58 std::shared_ptr<AsyncServerSocket> sock_;