folly: AsyncSocketException: move implementation to .cpp, refactor
[folly.git] / folly / io / async / test / EventBaseLocalTest.cpp
index 96ff491e6e61eccb31c1a930d1673305f5b4d311..9a001975c143019ac687eef027b6865c3834388a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2017 Facebook, Inc.
+ * Copyright 2004-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.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
 
 #include <folly/io/async/EventBaseLocal.h>
 #include <folly/portability/GTest.h>
@@ -71,7 +53,9 @@ TEST(EventBaseLocalTest, Basic) {
     EXPECT_EQ(dtorCnt, 2); // should dtor a Foo when evb2 destructs
 
   }
-  EXPECT_EQ(dtorCnt, 3); // should dtor a Foo when foo destructs
+  EXPECT_EQ(dtorCnt, 2); // should schedule Foo destructor, when foo destructs
+  evb1.loop();
+  EXPECT_EQ(dtorCnt, 3); // Foo will be destroyed in EventBase loop
 }
 
 TEST(EventBaseLocalTest, getOrCreate) {
@@ -87,3 +71,23 @@ TEST(EventBaseLocalTest, getOrCreate) {
   auto creator = []() { return new int(4); };
   EXPECT_EQ(ints.getOrCreateFn(evb2, creator), 4);
 }
+
+using IntPtr = std::unique_ptr<int>;
+
+TEST(EventBaseLocalTest, getOrCreateNoncopyable) {
+  folly::EventBase evb1;
+  folly::EventBaseLocal<IntPtr> ints;
+
+  EXPECT_EQ(ints.getOrCreate(evb1), IntPtr());
+  EXPECT_EQ(ints.getOrCreate(evb1, std::make_unique<int>(5)), IntPtr());
+
+  folly::EventBase evb2;
+  EXPECT_EQ(*ints.getOrCreate(evb2, std::make_unique<int>(5)), 5);
+}
+
+TEST(EventBaseLocalTest, emplaceNoncopyable) {
+  folly::EventBase evb;
+  folly::EventBaseLocal<IntPtr> ints;
+  ints.emplace(evb, std::make_unique<int>(42));
+  EXPECT_EQ(42, **ints.get(evb));
+}