1 #include "../test.h" 2 #include "rxcpp/operators/rx-repeat.hpp" 3 4 5 SCENARIO("repeat, basic test", "[repeat][operators]"){ 6 GIVEN("cold observable of 3 ints."){ 7 auto sc = rxsc::make_test(); 8 auto w = sc.create_worker(); 9 const rxsc::test::messages<int> on; 10 11 auto xs = sc.make_cold_observable({ 12 on.next(100, 1), 13 on.next(150, 2), 14 on.next(200, 3), 15 on.completed(250) 16 }); 17 18 WHEN("infinite repeat is launched"){ 19 20 auto res = w.start( __anonf37af5d60102() 21 [&]() { 22 return xs 23 | rxo::repeat() 24 // forget type to workaround lambda deduction bug on msvc 2013 25 | rxo::as_dynamic(); 26 } 27 ); 28 29 THEN("the output contains 3 sets of ints"){ 30 auto required = rxu::to_vector({ 31 on.next(300, 1), 32 on.next(350, 2), 33 on.next(400, 3), 34 on.next(550, 1), 35 on.next(600, 2), 36 on.next(650, 3), 37 on.next(800, 1), 38 on.next(850, 2), 39 on.next(900, 3) 40 }); 41 auto actual = res.get_observer().messages(); 42 REQUIRE(required == actual); 43 } 44 45 THEN("there was 4 subscriptions and 4 unsubscriptions to the ints"){ 46 auto required = rxu::to_vector({ 47 on.subscribe(200, 450), 48 on.subscribe(450, 700), 49 on.subscribe(700, 950), 50 on.subscribe(950, 1000) 51 }); 52 auto actual = xs.subscriptions(); 53 REQUIRE(required == actual); 54 } 55 } 56 } 57 } 58 59 SCENARIO("repeat, 0 times case", "[repeat][operators]"){ 60 GIVEN("cold observable of 3 ints."){ 61 auto sc = rxsc::make_test(); 62 auto w = sc.create_worker(); 63 const rxsc::test::messages<int> on; 64 65 auto xs = sc.make_cold_observable({ 66 on.next(100, 1), 67 on.next(150, 2), 68 on.next(200, 3), 69 on.completed(250) 70 }); 71 72 WHEN("repeat zero times is launched"){ 73 74 auto res = w.start( __anonf37af5d60202() 75 [&]() { 76 return xs 77 | rxo::repeat(0) 78 // forget type to workaround lambda deduction bug on msvc 2013 79 | rxo::as_dynamic(); 80 } 81 ); 82 83 THEN("the output should be empty"){ 84 auto required = rxu::to_vector({ 85 on.completed(200) 86 }); 87 auto actual = res.get_observer().messages(); 88 REQUIRE(required == actual); 89 } 90 91 THEN("no subscriptions in repeat(0) variant that skips on.next()"){ 92 auto required = std::vector<rxcpp::notifications::subscription>(); 93 auto actual = xs.subscriptions(); 94 REQUIRE(required == actual); 95 } 96 } 97 } 98 } 99 100 SCENARIO("repeat, infinite observable test", "[repeat][operators]"){ 101 GIVEN("cold observable of 3 ints that never completes."){ 102 auto sc = rxsc::make_test(); 103 auto w = sc.create_worker(); 104 const rxsc::test::messages<int> on; 105 106 auto xs = sc.make_cold_observable({ 107 on.next(100, 1), 108 on.next(150, 2), 109 on.next(200, 3) 110 }); 111 112 WHEN("infinite repeat is launched"){ 113 114 auto res = w.start( __anonf37af5d60302() 115 [&]() { 116 return xs 117 .repeat() 118 // forget type to workaround lambda deduction bug on msvc 2013 119 .as_dynamic(); 120 } 121 ); 122 123 THEN("the output contains a set of ints"){ 124 auto required = rxu::to_vector({ 125 on.next(300, 1), 126 on.next(350, 2), 127 on.next(400, 3) 128 }); 129 auto actual = res.get_observer().messages(); 130 REQUIRE(required == actual); 131 } 132 133 THEN("there was 1 subscription and 1 unsubscription to the ints"){ 134 auto required = rxu::to_vector({ 135 on.subscribe(200, 1000) 136 }); 137 auto actual = xs.subscriptions(); 138 REQUIRE(required == actual); 139 } 140 } 141 } 142 } 143 144 SCENARIO("repeat, error test", "[repeat][operators]"){ 145 GIVEN("cold observable of 3 ints followed by an error."){ 146 auto sc = rxsc::make_test(); 147 auto w = sc.create_worker(); 148 const rxsc::test::messages<int> on; 149 150 std::runtime_error ex("repeat on_error from source"); 151 152 auto xs = sc.make_cold_observable({ 153 on.next(100, 1), 154 on.next(150, 2), 155 on.next(200, 3), 156 on.error(250, ex) 157 }); 158 159 WHEN("infinite repeat is launched"){ 160 161 auto res = w.start( __anonf37af5d60402() 162 [&]() { 163 return xs 164 .repeat() 165 // forget type to workaround lambda deduction bug on msvc 2013 166 .as_dynamic(); 167 } 168 ); 169 170 THEN("the output contains a set of ints and an error"){ 171 auto required = rxu::to_vector({ 172 on.next(300, 1), 173 on.next(350, 2), 174 on.next(400, 3), 175 on.error(450, ex) 176 }); 177 auto actual = res.get_observer().messages(); 178 REQUIRE(required == actual); 179 } 180 181 THEN("there was 1 subscription and 1 unsubscription to the ints"){ 182 auto required = rxu::to_vector({ 183 on.subscribe(200, 450) 184 }); 185 auto actual = xs.subscriptions(); 186 REQUIRE(required == actual); 187 } 188 } 189 } 190 } 191 192 SCENARIO("countable repeat, basic test", "[repeat][operators]"){ 193 GIVEN("cold observable of 3 ints."){ 194 auto sc = rxsc::make_test(); 195 auto w = sc.create_worker(); 196 const rxsc::test::messages<int> on; 197 198 auto xs = sc.make_cold_observable({ 199 on.next(5, 1), 200 on.next(10, 2), 201 on.next(15, 3), 202 on.completed(20) 203 }); 204 205 WHEN("repeat of 3 iterations is launched"){ 206 207 auto res = w.start( __anonf37af5d60502() 208 [&]() { 209 return xs 210 .repeat(3) 211 // forget type to workaround lambda deduction bug on msvc 2013 212 .as_dynamic(); 213 } 214 ); 215 216 THEN("the output contains 3 sets of ints"){ 217 auto required = rxu::to_vector({ 218 on.next(205, 1), 219 on.next(210, 2), 220 on.next(215, 3), 221 on.next(225, 1), 222 on.next(230, 2), 223 on.next(235, 3), 224 on.next(245, 1), 225 on.next(250, 2), 226 on.next(255, 3), 227 on.completed(260) 228 }); 229 auto actual = res.get_observer().messages(); 230 REQUIRE(required == actual); 231 } 232 233 THEN("there was 3 subscriptions and 3 unsubscriptions to the ints"){ 234 auto required = rxu::to_vector({ 235 on.subscribe(200, 220), 236 on.subscribe(220, 240), 237 on.subscribe(240, 260) 238 }); 239 auto actual = xs.subscriptions(); 240 REQUIRE(required == actual); 241 } 242 } 243 } 244 } 245 246 SCENARIO("countable repeat, dispose test", "[repeat][operators]"){ 247 GIVEN("cold observable of 3 ints."){ 248 auto sc = rxsc::make_test(); 249 auto w = sc.create_worker(); 250 const rxsc::test::messages<int> on; 251 252 auto xs = sc.make_cold_observable({ 253 on.next(5, 1), 254 on.next(10, 2), 255 on.next(15, 3), 256 on.completed(20) 257 }); 258 259 WHEN("repeat of 3 iterations is launched"){ 260 261 auto res = w.start( __anonf37af5d60602() 262 [&]() { 263 return xs 264 .repeat(3) 265 // forget type to workaround lambda deduction bug on msvc 2013 266 .as_dynamic(); 267 }, 268 231 269 ); 270 271 THEN("the output contains less than 2 full sets of ints"){ 272 auto required = rxu::to_vector({ 273 on.next(205, 1), 274 on.next(210, 2), 275 on.next(215, 3), 276 on.next(225, 1), 277 on.next(230, 2), 278 }); 279 auto actual = res.get_observer().messages(); 280 REQUIRE(required == actual); 281 } 282 283 THEN("there was 2 subscriptions and 2 unsubscriptions to the ints"){ 284 auto required = rxu::to_vector({ 285 on.subscribe(200, 220), 286 on.subscribe(220, 231) 287 }); 288 auto actual = xs.subscriptions(); 289 REQUIRE(required == actual); 290 } 291 } 292 } 293 } 294 295 SCENARIO("countable repeat, infinite observable test", "[repeat][operators]"){ 296 GIVEN("cold observable of 3 ints that never completes."){ 297 auto sc = rxsc::make_test(); 298 auto w = sc.create_worker(); 299 const rxsc::test::messages<int> on; 300 301 auto xs = sc.make_cold_observable({ 302 on.next(100, 1), 303 on.next(150, 2), 304 on.next(200, 3) 305 }); 306 307 WHEN("infinite repeat is launched"){ 308 309 auto res = w.start( __anonf37af5d60702() 310 [&]() { 311 return xs 312 .repeat(3) 313 // forget type to workaround lambda deduction bug on msvc 2013 314 .as_dynamic(); 315 } 316 ); 317 318 THEN("the output contains a set of ints"){ 319 auto required = rxu::to_vector({ 320 on.next(300, 1), 321 on.next(350, 2), 322 on.next(400, 3) 323 }); 324 auto actual = res.get_observer().messages(); 325 REQUIRE(required == actual); 326 } 327 328 THEN("there was 1 subscription and 1 unsubscription to the ints"){ 329 auto required = rxu::to_vector({ 330 on.subscribe(200, 1000) 331 }); 332 auto actual = xs.subscriptions(); 333 REQUIRE(required == actual); 334 } 335 } 336 } 337 } 338 339 SCENARIO("countable repeat, error test", "[repeat][operators]"){ 340 GIVEN("cold observable of 3 ints followed by an error."){ 341 auto sc = rxsc::make_test(); 342 auto w = sc.create_worker(); 343 const rxsc::test::messages<int> on; 344 345 std::runtime_error ex("repeat on_error from source"); 346 347 auto xs = sc.make_cold_observable({ 348 on.next(100, 1), 349 on.next(150, 2), 350 on.next(200, 3), 351 on.error(250, ex) 352 }); 353 354 WHEN("infinite repeat is launched"){ 355 356 auto res = w.start( __anonf37af5d60802() 357 [&]() { 358 return xs 359 .repeat(3) 360 // forget type to workaround lambda deduction bug on msvc 2013 361 .as_dynamic(); 362 } 363 ); 364 365 THEN("the output contains a set of ints and an error"){ 366 auto required = rxu::to_vector({ 367 on.next(300, 1), 368 on.next(350, 2), 369 on.next(400, 3), 370 on.error(450, ex) 371 }); 372 auto actual = res.get_observer().messages(); 373 REQUIRE(required == actual); 374 } 375 376 THEN("there was 1 subscription and 1 unsubscription to the ints"){ 377 auto required = rxu::to_vector({ 378 on.subscribe(200, 450) 379 }); 380 auto actual = xs.subscriptions(); 381 REQUIRE(required == actual); 382 } 383 } 384 } 385 } 386