|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Flat3\RevPi\Tests\Monitors; |
| 4 | + |
| 5 | +use Flat3\RevPi\Monitors\DigitalMonitor; |
| 6 | +use Flat3\RevPi\Tests\TestCase; |
| 7 | +use Flat3\RevPi\Tests\UsesVirtualEnvironment; |
| 8 | + |
| 9 | +class MonitorTest extends TestCase implements UsesVirtualEnvironment |
| 10 | +{ |
| 11 | + /** |
| 12 | + * Helper to advance the event loop n times (simulate time passing). |
| 13 | + */ |
| 14 | + public function test_no_throttle_debounce(): void |
| 15 | + { |
| 16 | + $monitor = new DigitalMonitor; |
| 17 | + $results = []; |
| 18 | + |
| 19 | + $callback = function ($val) use (&$results) { |
| 20 | + $results[] = $val; |
| 21 | + }; |
| 22 | + |
| 23 | + $wrapped = $monitor->wrap($callback); |
| 24 | + |
| 25 | + // Simulate variable changes |
| 26 | + $inputs = [0, 0, 1, 1, 0, 1, 1, 0]; |
| 27 | + foreach ($inputs as $i => $value) { |
| 28 | + if ($monitor->evaluate($value)) { |
| 29 | + $wrapped($value); |
| 30 | + } |
| 31 | + $this->loop(1, 2); |
| 32 | + } |
| 33 | + |
| 34 | + // Only trigger callback on CHANGE (not same value twice), |
| 35 | + // DigitalMonitor returns true for a transition. |
| 36 | + self::assertSame([1, 0, 1, 0], $results); |
| 37 | + } |
| 38 | + |
| 39 | + public function test_fast_debounce(): void |
| 40 | + { |
| 41 | + $monitor = (new DigitalMonitor)->debounce(1); // 1ms debounce |
| 42 | + $results = []; |
| 43 | + $callback = function ($val) use (&$results) { |
| 44 | + $results[] = $val; |
| 45 | + }; |
| 46 | + $wrapped = $monitor->wrap($callback); |
| 47 | + |
| 48 | + $inputs = [0, 1, 0, 1, 0]; |
| 49 | + foreach ($inputs as $value) { |
| 50 | + if ($monitor->evaluate($value)) { |
| 51 | + $wrapped($value); |
| 52 | + } |
| 53 | + $this->loop(1, 2); |
| 54 | + } |
| 55 | + |
| 56 | + // Advance loop to fire debounce once |
| 57 | + $this->loop(2, 2); |
| 58 | + |
| 59 | + self::assertSame([0], $results); |
| 60 | + |
| 61 | + // Debounce with enough time between events so each triggers |
| 62 | + $monitor = (new DigitalMonitor)->debounce(1); |
| 63 | + $results = []; |
| 64 | + $wrapped = $monitor->wrap(function ($v) use (&$results) { |
| 65 | + $results[] = $v; |
| 66 | + }); |
| 67 | + |
| 68 | + $transitions = [0, 1, 0, 1, 0]; |
| 69 | + foreach ($transitions as $value) { |
| 70 | + if ($monitor->evaluate($value)) { |
| 71 | + $wrapped($value); |
| 72 | + } |
| 73 | + $this->loop(2, 2); // Let debounce interval expire |
| 74 | + } |
| 75 | + self::assertSame([1, 0, 1, 0], $results); |
| 76 | + } |
| 77 | + |
| 78 | + public function test_fast_throttle(): void |
| 79 | + { |
| 80 | + $monitor = (new DigitalMonitor)->throttle(2); // 2ms throttle |
| 81 | + $results = []; |
| 82 | + $callback = function ($val) use (&$results) { |
| 83 | + $results[] = $val; |
| 84 | + }; |
| 85 | + $wrapped = $monitor->wrap($callback); |
| 86 | + |
| 87 | + $inputs = [0, 1, 0, 1, 0]; |
| 88 | + |
| 89 | + // Submit all transitions rapidly with 0ms gap, before throttle can expire |
| 90 | + foreach ($inputs as $value) { |
| 91 | + if ($monitor->evaluate($value)) { |
| 92 | + $wrapped($value); |
| 93 | + } |
| 94 | + } |
| 95 | + // Should only fire the first one! |
| 96 | + $this->loop(3, 2); |
| 97 | + |
| 98 | + // Now let throttle window expire, and send another transition |
| 99 | + if ($monitor->evaluate(1)) { |
| 100 | + $wrapped(1); |
| 101 | + } |
| 102 | + $this->loop(3, 2); |
| 103 | + |
| 104 | + self::assertSame([1, 1], $results); |
| 105 | + } |
| 106 | +} |
0 commit comments