1 #include "../test.h" 2 #include <rxcpp/operators/rx-debounce.hpp> 3 4 using namespace std::chrono; 5 6 SCENARIO("debounce - never", "[debounce][operators]"){ 7 GIVEN("a source"){ 8 auto sc = rxsc::make_test(); 9 auto so = rx::synchronize_in_one_worker(sc); 10 auto w = sc.create_worker(); 11 const rxsc::test::messages<int> on; 12 13 auto xs = sc.make_hot_observable({ 14 on.next(150, 1) 15 }); 16 17 WHEN("values are debounceed"){ 18 19 auto res = w.start( __anonb887d2960102() 20 [so, xs]() { 21 return xs | rxo::debounce(so, milliseconds(10)); 22 } 23 ); 24 25 THEN("the output is empty"){ 26 auto required = std::vector<rxsc::test::messages<int>::recorded_type>(); 27 auto actual = res.get_observer().messages(); 28 REQUIRE(required == actual); 29 } 30 31 THEN("there was 1 subscription/unsubscription to the source"){ 32 auto required = rxu::to_vector({ 33 on.subscribe(200, 1001) 34 }); 35 auto actual = xs.subscriptions(); 36 REQUIRE(required == actual); 37 } 38 } 39 } 40 } 41 42 SCENARIO("debounce - empty", "[debounce][operators]"){ 43 GIVEN("a source"){ 44 auto sc = rxsc::make_test(); 45 auto so = rx::synchronize_in_one_worker(sc); 46 auto w = sc.create_worker(); 47 const rxsc::test::messages<int> on; 48 49 auto xs = sc.make_hot_observable({ 50 on.next(150, 1), 51 on.completed(250) 52 }); 53 54 WHEN("values are debounceed"){ 55 56 auto res = w.start( __anonb887d2960202() 57 [so, xs]() { 58 return xs.debounce(milliseconds(10), so); 59 } 60 ); 61 62 THEN("the output only contains complete message"){ 63 auto required = rxu::to_vector({ 64 on.completed(251) 65 }); 66 auto actual = res.get_observer().messages(); 67 REQUIRE(required == actual); 68 } 69 70 THEN("there was 1 subscription/unsubscription to the source"){ 71 auto required = rxu::to_vector({ 72 on.subscribe(200, 250) 73 }); 74 auto actual = xs.subscriptions(); 75 REQUIRE(required == actual); 76 } 77 78 } 79 } 80 } 81 82 SCENARIO("debounce - no overlap", "[debounce][operators]"){ 83 GIVEN("a source"){ 84 auto sc = rxsc::make_test(); 85 auto so = rx::synchronize_in_one_worker(sc); 86 auto w = sc.create_worker(); 87 const rxsc::test::messages<int> on; 88 89 auto xs = sc.make_hot_observable({ 90 on.next(150, 1), 91 on.next(210, 2), 92 on.next(240, 3), 93 on.completed(300) 94 }); 95 96 WHEN("values are debounceed"){ 97 98 auto res = w.start( __anonb887d2960302() 99 [so, xs]() { 100 return xs.debounce(milliseconds(10), so); 101 } 102 ); 103 104 THEN("the output only contains debounced items sent while subscribed"){ 105 auto required = rxu::to_vector({ 106 on.next(221, 2), 107 on.next(251, 3), 108 on.completed(301) 109 }); 110 auto actual = res.get_observer().messages(); 111 REQUIRE(required == actual); 112 } 113 114 THEN("there was 1 subscription/unsubscription to the source"){ 115 auto required = rxu::to_vector({ 116 on.subscribe(200, 300) 117 }); 118 auto actual = xs.subscriptions(); 119 REQUIRE(required == actual); 120 } 121 122 } 123 } 124 } 125 126 SCENARIO("debounce - overlap", "[debounce][operators]"){ 127 GIVEN("a source"){ 128 auto sc = rxsc::make_test(); 129 auto so = rx::synchronize_in_one_worker(sc); 130 auto w = sc.create_worker(); 131 const rxsc::test::messages<int> on; 132 133 auto xs = sc.make_hot_observable({ 134 on.next(150, 1), 135 on.next(215, 2), 136 on.next(225, 3), 137 on.next(235, 4), 138 on.next(245, 5), 139 on.next(255, 6), 140 on.next(265, 7), 141 on.completed(300) 142 }); 143 144 WHEN("values are debounceed"){ 145 146 auto res = w.start( __anonb887d2960402() 147 [so, xs]() { 148 return xs.debounce(milliseconds(30), so); 149 } 150 ); 151 152 THEN("the output only contains debounced items sent while subscribed"){ 153 auto required = rxu::to_vector({ 154 on.next(296, 7), 155 on.completed(301) 156 }); 157 auto actual = res.get_observer().messages(); 158 REQUIRE(required == actual); 159 } 160 161 THEN("there was 1 subscription/unsubscription to the source"){ 162 auto required = rxu::to_vector({ 163 on.subscribe(200, 300) 164 }); 165 auto actual = xs.subscriptions(); 166 REQUIRE(required == actual); 167 } 168 169 } 170 } 171 } 172 173 SCENARIO("debounce - throw", "[debounce][operators]"){ 174 GIVEN("a source"){ 175 auto sc = rxsc::make_test(); 176 auto so = rx::synchronize_in_one_worker(sc); 177 auto w = sc.create_worker(); 178 const rxsc::test::messages<int> on; 179 180 std::runtime_error ex("debounce on_error from source"); 181 182 auto xs = sc.make_hot_observable({ 183 on.next(150, 1), 184 on.error(250, ex) 185 }); 186 187 WHEN("values are debounceed"){ 188 189 auto res = w.start( __anonb887d2960502() 190 [so, xs]() { 191 return xs.debounce(milliseconds(10), so); 192 } 193 ); 194 195 THEN("the output only contains only error"){ 196 auto required = rxu::to_vector({ 197 on.error(251, ex) 198 }); 199 auto actual = res.get_observer().messages(); 200 REQUIRE(required == actual); 201 } 202 203 THEN("there was 1 subscription/unsubscription to the source"){ 204 auto required = rxu::to_vector({ 205 on.subscribe(200, 250) 206 }); 207 auto actual = xs.subscriptions(); 208 REQUIRE(required == actual); 209 } 210 211 } 212 } 213 } 214