2 * Copyright 2015 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/wangle/deprecated/rx/Observer.h>
18 #include <folly/wangle/deprecated/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, Observe) {
32 subject.observe(incrementer(count));
37 TEST(RxTest, ObserveInline) {
40 auto o = incrementer(count).release();
47 TEST(RxTest, Subscription) {
51 auto s = subject.subscribe(incrementer(count));
54 // The subscription has gone out of scope so no one should get this.
59 TEST(RxTest, SubscriptionMove) {
62 auto s = subject.subscribe(incrementer(count));
63 auto s2 = subject.subscribe(incrementer(count));
66 Subscription<int> s3(std::move(s2));
71 TEST(RxTest, SubscriptionOutlivesSubject) {
75 s = subject.subscribe(Observer<int>::create([](int){}));
77 // Don't explode when s is destroyed
80 TEST(RxTest, SubscribeDuringCallback) {
81 // A subscriber who was subscribed in the course of a callback should get
82 // subsequent updates but not the current update.
84 int outerCount = 0, innerCount = 0;
85 Subscription<int> s1, s2;
86 s1 = subject.subscribe(Observer<int>::create([&] (int x) {
88 s2 = subject.subscribe(incrementer(innerCount));
91 subject.onNext(0xDEADBEEF);
92 EXPECT_EQ(2, outerCount);
93 EXPECT_EQ(1, innerCount);
96 TEST(RxTest, ObserveDuringCallback) {
98 int outerCount = 0, innerCount = 0;
99 subject.observe(Observer<int>::create([&] (int x) {
101 subject.observe(incrementer(innerCount));
104 subject.onNext(0xDEADBEEF);
105 EXPECT_EQ(2, outerCount);
106 EXPECT_EQ(1, innerCount);
109 TEST(RxTest, ObserveInlineDuringCallback) {
110 Subject<int> subject;
111 int outerCount = 0, innerCount = 0;
112 auto innerO = incrementer(innerCount).release();
113 auto outerO = Observer<int>::create([&] (int x) {
115 subject.observe(innerO);
117 subject.observe(outerO);
119 subject.onNext(0xDEADBEEF);
120 EXPECT_EQ(2, outerCount);
121 EXPECT_EQ(1, innerCount);
126 TEST(RxTest, UnsubscribeDuringCallback) {
127 // A subscriber who was unsubscribed in the course of a callback should get
128 // the current update but not subsequent ones
129 Subject<int> subject;
130 int count1 = 0, count2 = 0;
131 auto s1 = subject.subscribe(incrementer(count1));
132 auto s2 = subject.subscribe(Observer<int>::create([&] (int x) {
138 EXPECT_EQ(1, count1);
139 EXPECT_EQ(2, count2);
142 TEST(RxTest, SubscribeUnsubscribeDuringCallback) {
143 // A subscriber who was subscribed and unsubscribed in the course of a
144 // callback should not get any updates
145 Subject<int> subject;
146 int outerCount = 0, innerCount = 0;
147 auto s2 = subject.subscribe(Observer<int>::create([&] (int x) {
149 auto s2 = subject.subscribe(incrementer(innerCount));
153 EXPECT_EQ(2, outerCount);
154 EXPECT_EQ(0, innerCount);
158 typedef std::unique_ptr<int> MO;
159 static MO makeMO() { return folly::make_unique<int>(1); }
160 template <typename T>
161 static ObserverPtr<T> makeMOObserver() {
162 return Observer<T>::create([](const T& mo) {
167 TEST(RxTest, MoveOnlyRvalue) {
169 auto s1 = subject.subscribe(makeMOObserver<MO>());
170 auto s2 = subject.subscribe(makeMOObserver<MO>());
172 // Can't bind lvalues to rvalue references
173 // subject.onNext(mo);
174 subject.onNext(std::move(mo));
175 subject.onNext(makeMO());
181 CO(const CO&) = default;
185 template <typename T>
186 static ObserverPtr<T> makeCOObserver() {
187 return Observer<T>::create([](const T& mo) {});
190 TEST(RxTest, CopyOnly) {
192 auto s1 = subject.subscribe(makeCOObserver<CO>());