Skip to content

Commit

Permalink
Changed the test case to produce a reliable predictable result (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
climategadgets committed May 29, 2024
1 parent 61d0401 commit 809ed8b
Showing 1 changed file with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.sf.dz3r.device.actuator.economizer.v2;

import net.sf.dz3r.device.DeviceState;
import net.sf.dz3r.device.actuator.NullCqrsSwitch;
import net.sf.dz3r.device.actuator.SwitchableHvacDevice;
import net.sf.dz3r.device.actuator.economizer.EconomizerConfig;
Expand All @@ -8,12 +9,13 @@
import net.sf.dz3r.signal.Signal;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Sinks;
import reactor.core.scheduler.Schedulers;

import java.time.Clock;
import java.time.Duration;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicLong;

Expand All @@ -28,15 +30,15 @@ class PidEconomizerTest {
*
* See <a href="https://github.com/home-climate-control/dz/issues/320">#320</a>.
*/
@RepeatedTest(value = 10, failureThreshold = 1)
@Test
void ambientSensorLoss() throws InterruptedException {

var s = new NullCqrsSwitch("test");
var actuator = new NullCqrsSwitch("test");
var hvac = new SwitchableHvacDevice(
Clock.systemUTC(),
"test",
HvacMode.COOLING,
s,
actuator,
false,
null
);
Expand Down Expand Up @@ -76,23 +78,24 @@ void ambientSensorLoss() throws InterruptedException {
.publishOn(Schedulers.boundedElastic())
.subscribe(step -> logger.info("step: {}", step));

var deviceState = new LinkedBlockingQueue<Signal<DeviceState<Boolean>, String>>();

actuator.getFlux().subscribe(deviceState::add);

// Setup complete, let's push data now

// Initially, the economizer is on
// After receiving just the indoor signal (but not ambient), the economizer is off
indoorSink.tryEmitNext(new Signal<>(start, 25.0, "indoor"));
ambientSink.tryEmitNext(new Signal<>(start.plus(Duration.ofMillis(offset.addAndGet(timeStep))), 20.0));
assertThat(deviceState.take().getValue().requested).isFalse();

Thread.sleep(10); // NOSONAR Risks have been assessed and accepted
assertThat(s.getState().requested).isTrue();
// When both indoor and ambient signals are available, it is now on
ambientSink.tryEmitNext(new Signal<>(start.plus(Duration.ofMillis(offset.addAndGet(timeStep))), 20.0));
assertThat(deviceState.take().getValue().requested).isTrue();

// It should turn off now
ambientSink.tryEmitNext(new Signal<>(start.plus(Duration.ofMillis(offset.addAndGet(timeStep))), null, null,Signal.Status.FAILURE_TOTAL, new TimeoutException("oops")));

Thread.sleep(10); // NOSONAR Risks have been assessed and accepted

// As of rev. 139c3ae65438f4a98c90e74dbffe24073e178d20:
// If at any time the I component of stage 1 controller signal is more than 0, this makes its output less than the renderer threshold because the value of -1 is used for signaling,
// and the economizer gets stuck on.
assertThat(s.getState().requested).isFalse();
// ...and it does.
assertThat(deviceState.take().getValue().requested).isFalse();
}
}

0 comments on commit 809ed8b

Please sign in to comment.