/*
- * Copyright 2017 Facebook, Inc.
+ * Copyright 2015-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>
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) {
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));
+}