2 * Copyright 2014 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.
17 #include <folly/experimental/wangle/rx/Observer.h>
18 #include <folly/experimental/wangle/rx/Subject.h>
19 #include <gtest/gtest.h>
21 using namespace folly::wangle;
23 static std::unique_ptr<Observer<int>> incrementer(int& counter) {
24 return Observer<int>::create([&] (int x) {
29 TEST(RxTest, Subscription) {
33 auto s = subject.subscribe(incrementer(count));
36 // The subscription has gone out of scope so no one should get this.
41 TEST(RxTest, SubscriptionMove) {
44 auto s = subject.subscribe(incrementer(count));
45 auto s2 = subject.subscribe(incrementer(count));
48 Subscription<int> s3(std::move(s2));
53 TEST(RxTest, SubscriptionOutlivesSubject) {
57 s = subject.subscribe(Observer<int>::create([](int){}));
59 // Don't explode when s is destroyed
62 TEST(RxTest, SubscribeDuringCallback) {
63 // A subscriber who was subscribed in the course of a callback should get
64 // subsequent updates but not the current update.
66 int outerCount = 0, innerCount = 0;
67 Subscription<int> s1, s2;
68 s1 = subject.subscribe(Observer<int>::create([&] (int x) {
70 s2 = subject.subscribe(incrementer(innerCount));
73 subject.onNext(0xDEADBEEF);
74 EXPECT_EQ(2, outerCount);
75 EXPECT_EQ(1, innerCount);
78 TEST(RxTest, UnsubscribeDuringCallback) {
79 // A subscriber who was unsubscribed in the course of a callback should get
80 // the current update but not subsequent ones
82 int count1 = 0, count2 = 0;
83 auto s1 = subject.subscribe(incrementer(count1));
84 auto s2 = subject.subscribe(Observer<int>::create([&] (int x) {
94 TEST(RxTest, SubscribeUnsubscribeDuringCallback) {
95 // A subscriber who was subscribed and unsubscribed in the course of a
96 // callback should not get any updates
98 int outerCount = 0, innerCount = 0;
99 auto s2 = subject.subscribe(Observer<int>::create([&] (int x) {
101 auto s2 = subject.subscribe(incrementer(innerCount));
105 EXPECT_EQ(2, outerCount);
106 EXPECT_EQ(0, innerCount);
110 typedef std::unique_ptr<int> MO;
111 static MO makeMO() { return folly::make_unique<int>(1); }
112 template <typename T>
113 static ObserverPtr<T> makeMOObserver() {
114 return Observer<T>::create([](const T& mo) {
119 TEST(RxTest, MoveOnlyRvalue) {
121 auto s1 = subject.subscribe(makeMOObserver<MO>());
122 auto s2 = subject.subscribe(makeMOObserver<MO>());
124 // Can't bind lvalues to rvalue references
125 // subject.onNext(mo);
126 subject.onNext(std::move(mo));
127 subject.onNext(makeMO());
133 CO(const CO&) = default;
137 template <typename T>
138 static ObserverPtr<T> makeCOObserver() {
139 return Observer<T>::create([](const T& mo) {});
142 TEST(RxTest, CopyOnly) {
144 auto s1 = subject.subscribe(makeCOObserver<CO>());