support for move-only types
[folly.git] / folly / experimental / wangle / rx / test / RxTest.cpp
1 /*
2  * Copyright 2014 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 #include <folly/experimental/wangle/rx/Observer.h>
18 #include <folly/experimental/wangle/rx/Subject.h>
19 #include <gtest/gtest.h>
20
21 using namespace folly::wangle;
22
23 static std::unique_ptr<Observer<int>> incrementer(int& counter) {
24   return Observer<int>::create([&] (int x) {
25     counter++;
26   });
27 }
28
29 TEST(RxTest, Subscription) {
30   Subject<int> subject;
31   auto count = 0;
32   {
33     auto s = subject.subscribe(incrementer(count));
34     subject.onNext(1);
35   }
36   // The subscription has gone out of scope so no one should get this.
37   subject.onNext(2);
38   EXPECT_EQ(1, count);
39 }
40
41 TEST(RxTest, SubscriptionMove) {
42   Subject<int> subject;
43   auto count = 0;
44   auto s = subject.subscribe(incrementer(count));
45   auto s2 = subject.subscribe(incrementer(count));
46   s2 = std::move(s);
47   subject.onNext(1);
48   Subscription<int> s3(std::move(s2));
49   subject.onNext(2);
50   EXPECT_EQ(2, count);
51 }
52
53 TEST(RxTest, SubscriptionOutlivesSubject) {
54   Subscription<int> s;
55   {
56     Subject<int> subject;
57     s = subject.subscribe(Observer<int>::create([](int){}));
58   }
59   // Don't explode when s is destroyed
60 }
61
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.
65   Subject<int> subject;
66   int outerCount = 0, innerCount = 0;
67   Subscription<int> s1, s2;
68   s1 = subject.subscribe(Observer<int>::create([&] (int x) {
69     outerCount++;
70     s2 = subject.subscribe(incrementer(innerCount));
71   }));
72   subject.onNext(42);
73   subject.onNext(0xDEADBEEF);
74   EXPECT_EQ(2, outerCount);
75   EXPECT_EQ(1, innerCount);
76 }
77
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
81   Subject<int> subject;
82   int count1 = 0, count2 = 0;
83   auto s1 = subject.subscribe(incrementer(count1));
84   auto s2 = subject.subscribe(Observer<int>::create([&] (int x) {
85     count2++;
86     s1.~Subscription();
87   }));
88   subject.onNext(1);
89   subject.onNext(2);
90   EXPECT_EQ(1, count1);
91   EXPECT_EQ(2, count2);
92 }
93
94 TEST(RxTest, SubscribeUnsubscribeDuringCallback) {
95   // A subscriber who was subscribed and unsubscribed in the course of a
96   // callback should not get any updates
97   Subject<int> subject;
98   int outerCount = 0, innerCount = 0;
99   auto s2 = subject.subscribe(Observer<int>::create([&] (int x) {
100     outerCount++;
101     auto s2 = subject.subscribe(incrementer(innerCount));
102   }));
103   subject.onNext(1);
104   subject.onNext(2);
105   EXPECT_EQ(2, outerCount);
106   EXPECT_EQ(0, innerCount);
107 }
108
109 // Move only type
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) {
115     EXPECT_EQ(1, *mo);
116   });
117 }
118
119 TEST(RxTest, MoveOnlyRvalue) {
120   Subject<MO> subject;
121   auto s1 = subject.subscribe(makeMOObserver<MO>());
122   auto s2 = subject.subscribe(makeMOObserver<MO>());
123   auto mo = makeMO();
124   // Can't bind lvalues to rvalue references
125   // subject.onNext(mo);
126   subject.onNext(std::move(mo));
127   subject.onNext(makeMO());
128 }
129
130 // Copy only type
131 struct CO {
132   CO() = default;
133   CO(const CO&) = default;
134   CO(CO&&) = delete;
135 };
136
137 template <typename T>
138 static ObserverPtr<T> makeCOObserver() {
139   return Observer<T>::create([](const T& mo) {});
140 }
141
142 TEST(RxTest, CopyOnly) {
143   Subject<CO> subject;
144   auto s1 = subject.subscribe(makeCOObserver<CO>());
145   CO co;
146   subject.onNext(co);
147 }