proxygen
RxTest.cpp File Reference
#include <wangle/deprecated/rx/Observer.h>
#include <wangle/deprecated/rx/Subject.h>
#include <gtest/gtest.h>

Go to the source code of this file.

Classes

struct  CO
 

Typedefs

typedef std::unique_ptr< int > MO
 

Functions

static std::unique_ptr< Observer< int > > incrementer (int &counter)
 
 TEST (RxTest, Observe)
 
 TEST (RxTest, ObserveInline)
 
 TEST (RxTest, Subscription)
 
 TEST (RxTest, SubscriptionMove)
 
 TEST (RxTest, SubscriptionOutlivesSubject)
 
 TEST (RxTest, SubscribeDuringCallback)
 
 TEST (RxTest, ObserveDuringCallback)
 
 TEST (RxTest, ObserveInlineDuringCallback)
 
 TEST (RxTest, UnsubscribeDuringCallback)
 
 TEST (RxTest, SubscribeUnsubscribeDuringCallback)
 
static MO makeMO ()
 
template<typename T >
static ObserverPtr< TmakeMOObserver ()
 
 TEST (RxTest, MoveOnlyRvalue)
 
template<typename T >
static ObserverPtr< TmakeCOObserver ()
 
 TEST (RxTest, CopyOnly)
 

Typedef Documentation

typedef std::unique_ptr<int> MO

Definition at line 158 of file RxTest.cpp.

Function Documentation

static std::unique_ptr<Observer<int> > incrementer ( int &  counter)
static

Definition at line 23 of file RxTest.cpp.

References wangle::Observer< T >::create().

Referenced by TEST().

23  {
24  return Observer<int>::create([&] (int) {
25  counter++;
26  });
27 }
std::atomic< int > counter
template<typename T >
static ObserverPtr<T> makeCOObserver ( )
static

Definition at line 186 of file RxTest.cpp.

References wangle::Observer< T >::create(), and T.

186  {
187  return Observer<T>::create([](const T&) {});
188 }
#define T(v)
Definition: http_parser.c:233
static MO makeMO ( )
static

Definition at line 159 of file RxTest.cpp.

Referenced by TEST().

159 { return std::make_unique<int>(1); }
template<typename T >
static ObserverPtr<T> makeMOObserver ( )
static

Definition at line 161 of file RxTest.cpp.

References wangle::Observer< T >::create(), EXPECT_EQ, and T.

161  {
162  return Observer<T>::create([](const T& mo) {
163  EXPECT_EQ(1, *mo);
164  });
165 }
#define T(v)
Definition: http_parser.c:233
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( RxTest  ,
Observe   
)

Definition at line 29 of file RxTest.cpp.

References count, EXPECT_EQ, incrementer(), wangle::Observable< T >::observe(), and wangle::Subject< T >::onNext().

29  {
30  Subject<int> subject;
31  auto count = 0;
32  subject.observe(incrementer(count));
33  subject.onNext(1);
34  EXPECT_EQ(1, count);
35 }
virtual void observe(ObserverPtr< T > observer)
Definition: Observable.h:68
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static std::unique_ptr< Observer< int > > incrementer(int &counter)
Definition: RxTest.cpp:23
void onNext(const T &val) override
Definition: Subject.h:30
int * count
TEST ( RxTest  ,
ObserveInline   
)

Definition at line 37 of file RxTest.cpp.

References count, EXPECT_EQ, incrementer(), wangle::Observable< T >::observe(), and wangle::Subject< T >::onNext().

37  {
38  Subject<int> subject;
39  auto count = 0;
40  auto o = incrementer(count).release();
41  subject.observe(o);
42  subject.onNext(1);
43  EXPECT_EQ(1, count);
44  delete o;
45 }
virtual void observe(ObserverPtr< T > observer)
Definition: Observable.h:68
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static std::unique_ptr< Observer< int > > incrementer(int &counter)
Definition: RxTest.cpp:23
void onNext(const T &val) override
Definition: Subject.h:30
int * count
TEST ( RxTest  ,
Subscription   
)

Definition at line 47 of file RxTest.cpp.

References count, EXPECT_EQ, incrementer(), wangle::Subject< T >::onNext(), s, and wangle::Observable< T >::subscribe().

47  {
48  Subject<int> subject;
49  auto count = 0;
50  {
51  auto s = subject.subscribe(incrementer(count));
52  subject.onNext(1);
53  }
54  // The subscription has gone out of scope so no one should get this.
55  subject.onNext(2);
56  EXPECT_EQ(1, count);
57 }
virtual Subscription< T > subscribe(ObserverPtr< T > observer)
Definition: Observable.h:64
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static std::unique_ptr< Observer< int > > incrementer(int &counter)
Definition: RxTest.cpp:23
void onNext(const T &val) override
Definition: Subject.h:30
int * count
static set< string > s
TEST ( RxTest  ,
SubscriptionMove   
)

Definition at line 59 of file RxTest.cpp.

References count, EXPECT_EQ, incrementer(), folly::gen::move, wangle::Subject< T >::onNext(), s, and wangle::Observable< T >::subscribe().

59  {
60  Subject<int> subject;
61  auto count = 0;
62  auto s = subject.subscribe(incrementer(count));
63  auto s2 = subject.subscribe(incrementer(count));
64  s2 = std::move(s);
65  subject.onNext(1);
67  subject.onNext(2);
68  EXPECT_EQ(2, count);
69 }
virtual Subscription< T > subscribe(ObserverPtr< T > observer)
Definition: Observable.h:64
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
static std::unique_ptr< Observer< int > > incrementer(int &counter)
Definition: RxTest.cpp:23
void onNext(const T &val) override
Definition: Subject.h:30
int * count
static set< string > s
TEST ( RxTest  ,
SubscriptionOutlivesSubject   
)

Definition at line 71 of file RxTest.cpp.

References s, and wangle::Observable< T >::subscribe().

71  {
73  {
74  Subject<int> subject;
75  s = subject.subscribe(Observer<int>::create([](int){}));
76  }
77  // Don't explode when s is destroyed
78 }
virtual Subscription< T > subscribe(ObserverPtr< T > observer)
Definition: Observable.h:64
static set< string > s
TEST ( RxTest  ,
SubscribeDuringCallback   
)

Definition at line 80 of file RxTest.cpp.

References EXPECT_EQ, incrementer(), wangle::Subject< T >::onNext(), and wangle::Observable< T >::subscribe().

80  {
81  // A subscriber who was subscribed in the course of a callback should get
82  // subsequent updates but not the current update.
83  Subject<int> subject;
84  int outerCount = 0, innerCount = 0;
85  Subscription<int> s1, s2;
86  s1 = subject.subscribe(Observer<int>::create([&] (int) {
87  outerCount++;
88  s2 = subject.subscribe(incrementer(innerCount));
89  }));
90  subject.onNext(42);
91  subject.onNext(0xDEADBEEF);
92  EXPECT_EQ(2, outerCount);
93  EXPECT_EQ(1, innerCount);
94 }
virtual Subscription< T > subscribe(ObserverPtr< T > observer)
Definition: Observable.h:64
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static std::unique_ptr< Observer< int > > incrementer(int &counter)
Definition: RxTest.cpp:23
void onNext(const T &val) override
Definition: Subject.h:30
TEST ( RxTest  ,
ObserveDuringCallback   
)

Definition at line 96 of file RxTest.cpp.

References EXPECT_EQ, incrementer(), wangle::Observable< T >::observe(), and wangle::Subject< T >::onNext().

96  {
97  Subject<int> subject;
98  int outerCount = 0, innerCount = 0;
99  subject.observe(Observer<int>::create([&] (int) {
100  outerCount++;
101  subject.observe(incrementer(innerCount));
102  }));
103  subject.onNext(42);
104  subject.onNext(0xDEADBEEF);
105  EXPECT_EQ(2, outerCount);
106  EXPECT_EQ(1, innerCount);
107 }
virtual void observe(ObserverPtr< T > observer)
Definition: Observable.h:68
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static std::unique_ptr< Observer< int > > incrementer(int &counter)
Definition: RxTest.cpp:23
void onNext(const T &val) override
Definition: Subject.h:30
TEST ( RxTest  ,
ObserveInlineDuringCallback   
)

Definition at line 109 of file RxTest.cpp.

References wangle::Observer< T >::create(), EXPECT_EQ, incrementer(), wangle::Observable< T >::observe(), and wangle::Subject< T >::onNext().

109  {
110  Subject<int> subject;
111  int outerCount = 0, innerCount = 0;
112  auto innerO = incrementer(innerCount).release();
113  auto outerO = Observer<int>::create([&] (int) {
114  outerCount++;
115  subject.observe(innerO);
116  }).release();
117  subject.observe(outerO);
118  subject.onNext(42);
119  subject.onNext(0xDEADBEEF);
120  EXPECT_EQ(2, outerCount);
121  EXPECT_EQ(1, innerCount);
122  delete innerO;
123  delete outerO;
124 }
virtual void observe(ObserverPtr< T > observer)
Definition: Observable.h:68
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static std::unique_ptr< Observer< int > > incrementer(int &counter)
Definition: RxTest.cpp:23
void onNext(const T &val) override
Definition: Subject.h:30
TEST ( RxTest  ,
UnsubscribeDuringCallback   
)

Definition at line 126 of file RxTest.cpp.

References EXPECT_EQ, incrementer(), wangle::Subject< T >::onNext(), and wangle::Observable< T >::subscribe().

126  {
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) {
133  count2++;
134  s1.~Subscription();
135  }));
136  subject.onNext(1);
137  subject.onNext(2);
138  EXPECT_EQ(1, count1);
139  EXPECT_EQ(2, count2);
140 }
virtual Subscription< T > subscribe(ObserverPtr< T > observer)
Definition: Observable.h:64
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static std::unique_ptr< Observer< int > > incrementer(int &counter)
Definition: RxTest.cpp:23
void onNext(const T &val) override
Definition: Subject.h:30
TEST ( RxTest  ,
SubscribeUnsubscribeDuringCallback   
)

Definition at line 142 of file RxTest.cpp.

References EXPECT_EQ, incrementer(), wangle::Subject< T >::onNext(), and wangle::Observable< T >::subscribe().

142  {
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) {
148  outerCount++;
149  auto s3 = subject.subscribe(incrementer(innerCount));
150  }));
151  subject.onNext(1);
152  subject.onNext(2);
153  EXPECT_EQ(2, outerCount);
154  EXPECT_EQ(0, innerCount);
155 }
virtual Subscription< T > subscribe(ObserverPtr< T > observer)
Definition: Observable.h:64
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static std::unique_ptr< Observer< int > > incrementer(int &counter)
Definition: RxTest.cpp:23
void onNext(const T &val) override
Definition: Subject.h:30
TEST ( RxTest  ,
MoveOnlyRvalue   
)

Definition at line 167 of file RxTest.cpp.

References makeMO(), folly::gen::move, wangle::Subject< T >::onNext(), and wangle::Observable< T >::subscribe().

167  {
168  Subject<MO> subject;
169  auto s1 = subject.subscribe(makeMOObserver<MO>());
170  auto s2 = subject.subscribe(makeMOObserver<MO>());
171  auto mo = makeMO();
172  // Can't bind lvalues to rvalue references
173  // subject.onNext(mo);
174  subject.onNext(std::move(mo));
175  subject.onNext(makeMO());
176 }
virtual Subscription< T > subscribe(ObserverPtr< T > observer)
Definition: Observable.h:64
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void onNext(const T &val) override
Definition: Subject.h:30
static MO makeMO()
Definition: RxTest.cpp:159
TEST ( RxTest  ,
CopyOnly   
)

Definition at line 190 of file RxTest.cpp.

References GCC61971::co, wangle::Subject< T >::onNext(), and wangle::Observable< T >::subscribe().

190  {
191  Subject<CO> subject;
192  auto s1 = subject.subscribe(makeCOObserver<CO>());
193  CO co;
194  subject.onNext(co);
195 }
virtual Subscription< T > subscribe(ObserverPtr< T > observer)
Definition: Observable.h:64
void onNext(const T &val) override
Definition: Subject.h:30
Definition: RxTest.cpp:179
constexpr auto co