|
15 | 15 | import numpy as np |
16 | 16 | import psutil |
17 | 17 | import pytest |
| 18 | +from pytz import UTC |
18 | 19 |
|
19 | 20 | import csp |
20 | 21 | from csp import PushMode, ts |
@@ -415,6 +416,75 @@ def times(x: ts[bool]) -> ts[datetime]: |
415 | 416 | result = csp.run(times(x), starttime=datetime(2020, 2, 7, 9), endtime=timedelta(seconds=10))[0] |
416 | 417 | self.assertEqual([v[0] for v in result], [v[1] for v in result]) |
417 | 418 |
|
| 419 | + def test_csp_in_realtime__historical(self): |
| 420 | + @csp.node |
| 421 | + def is_in_realtime(x: ts[bool]) -> ts[bool]: |
| 422 | + if csp.ticked(x): |
| 423 | + return csp.in_realtime() |
| 424 | + |
| 425 | + @csp.graph |
| 426 | + def g() -> ts[bool]: |
| 427 | + # Symbolic curve of expected booleans |
| 428 | + times = csp.curve( |
| 429 | + bool, |
| 430 | + [(datetime(2025, 12, 24), False)], |
| 431 | + ) |
| 432 | + csp.add_graph_output("in_realtime", is_in_realtime(times)) |
| 433 | + |
| 434 | + outputs = csp.run(g, starttime=datetime(2025, 12, 24), endtime=datetime(2025, 12, 25), realtime=False) |
| 435 | + assert outputs |
| 436 | + assert outputs["in_realtime"][0][1] is False |
| 437 | + |
| 438 | + def test_csp_in_realtime__realtime(self): |
| 439 | + def utc_now() -> datetime: |
| 440 | + return datetime.now(UTC) |
| 441 | + |
| 442 | + @csp.node |
| 443 | + def is_in_realtime(x: ts[bool]) -> ts[bool]: |
| 444 | + if csp.ticked(x): |
| 445 | + return csp.in_realtime() |
| 446 | + |
| 447 | + @csp.graph |
| 448 | + def g() -> ts[bool]: |
| 449 | + # Symbolic curve of expected booleans |
| 450 | + times = csp.curve( |
| 451 | + bool, |
| 452 | + [(timedelta(seconds=10), True)], |
| 453 | + ) |
| 454 | + csp.add_graph_output("in_realtime", is_in_realtime(times)) |
| 455 | + csp.stop_engine(times) |
| 456 | + |
| 457 | + outputs = csp.run(g, starttime=utc_now(), endtime=utc_now() + timedelta(seconds=30), realtime=True) |
| 458 | + assert outputs |
| 459 | + assert outputs["in_realtime"][0][1] is True |
| 460 | + |
| 461 | + @pytest.mark.skipif(not os.environ.get("CSP_ENGINE_FLAKY_TESTS"), reason="potentially flaky test") |
| 462 | + def test_csp_in_realtime(self): |
| 463 | + def utc_now() -> datetime: |
| 464 | + return datetime.now(UTC) |
| 465 | + |
| 466 | + @csp.node |
| 467 | + def is_in_realtime(x: ts[bool]) -> ts[bool]: |
| 468 | + if csp.ticked(x): |
| 469 | + assert csp.in_realtime() == x |
| 470 | + return csp.in_realtime() |
| 471 | + |
| 472 | + @csp.graph |
| 473 | + def g() -> ts[bool]: |
| 474 | + # Symbolic curve of expected booleans |
| 475 | + times = csp.curve( |
| 476 | + bool, |
| 477 | + [(utc_now() - timedelta(seconds=3), False), (utc_now() + timedelta(seconds=3), True)], |
| 478 | + ) |
| 479 | + csp.add_graph_output("in_realtime", is_in_realtime(times)) |
| 480 | + |
| 481 | + outputs = csp.run( |
| 482 | + g, starttime=utc_now() - timedelta(seconds=5), endtime=utc_now() + timedelta(seconds=10), realtime=True |
| 483 | + ) |
| 484 | + assert outputs |
| 485 | + assert outputs["in_realtime"][0][1] is False |
| 486 | + assert outputs["in_realtime"][1][1] is True |
| 487 | + |
418 | 488 | def test_stop_engine(self): |
419 | 489 | @csp.node |
420 | 490 | def stop(x: ts[bool]) -> ts[bool]: |
|
0 commit comments