Adds writer test case for RCU
[folly.git] / folly / io / async / test / ScopedBoundPort.h
1 /*
2  * Copyright 2016-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
17 #pragma once
18
19 #include <memory>
20 #include <utility>
21
22 #include <folly/IPAddress.h>
23 #include <folly/IPAddressV6.h>
24 #include <folly/SocketAddress.h>
25
26 namespace folly {
27
28 class AsyncServerSocket;
29 class ScopedEventBaseThread;
30
31 /***
32  *  ScopedBoundPort
33  *
34  *  Binds to an ephemeral port in the ctor but does not listen. Unbinds from the
35  *  port in the dtor.
36  *
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.
39  *
40  *  Useful for testing server-down cases.
41  *
42  *  Example:
43  *
44  *    TEST(MyClient, WhenTheServerIsDown_ThrowsServerDownException) {
45  *      folly::ScopedBoundPort bound;
46  *      MyClient client(bound.getAddress(), 100ms);
47  *      EXPECT_THROW(client.getData(), ServerDownException);
48  *    }
49  */
50 class ScopedBoundPort {
51  public:
52   explicit ScopedBoundPort(IPAddress host = IPAddressV6("::1"));
53   ~ScopedBoundPort();
54   SocketAddress getAddress() const;
55
56  private:
57   std::unique_ptr<ScopedEventBaseThread> ebth_;
58   std::shared_ptr<AsyncServerSocket> sock_;
59 };
60 } // namespace folly