Skip to content

Commit

Permalink
Added test cases with error signal in the middle (old behavior, NaN) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
climategadgets committed Jun 6, 2024
1 parent ad9199f commit b9be96e
Showing 1 changed file with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,95 @@ void setpointChangeEmitsSignalPid() {

out.dispose();
}

@Test
void errorSignalHysteresis() {

Sinks.Many<Signal<Double, Void>> sink = Sinks.many().unicast().onBackpressureError();
var source = sink.asFlux();

var pc = new HysteresisController<Void>("pc", 20);

var accumulator = new ArrayList<Signal<ProcessController.Status<Double>, Void>>();
var out = pc
.compute(source)
.log()
.subscribe(accumulator::add);

// Emit "change to 1" signal
sink.tryEmitNext(new Signal<>(Instant.now(), 25.0));

// Emit error signal
sink.tryEmitNext(new Signal<>(Instant.now(), null, null, Signal.Status.FAILURE_TOTAL, new IllegalStateException("oops")));

// ???
sink.tryEmitNext(new Signal<>(Instant.now(), 25.0));

// This should make the controller emit a signal since the setpoint now calls for action, but
// in imperative implementation it doesn't; it'll wait till the next incoming signal to do so
pc.setSetpoint(30.0);

sink.tryEmitNext(new Signal<>(Instant.now(), 35.0));

sink.tryEmitComplete();

assertThat(accumulator).hasSize(5);

// Normal signal triggering status change
assertThat(accumulator.get(0).getValue().signal).isEqualTo(1.0);

// Error signal
assertThat(accumulator.get(1).getValue().signal).isNaN();

// Normal again
assertThat(accumulator.get(2).getValue().signal).isEqualTo(1.0);

// Setpoint change
assertThat(accumulator.get(3).getValue().signal).isEqualTo(-1.0);

// PV change again
assertThat(accumulator.get(4).getValue().signal).isEqualTo(1.0);

out.dispose();
}

@Test
void errorSignalPid() {

Sinks.Many<Signal<Double, Void>> sink = Sinks.many().unicast().onBackpressureError();
var source = sink.asFlux();

var pc = new SimplePidController<Void>("pc", 20d, 1, 0, 0, 1.1);

var accumulator = new ArrayList<Signal<ProcessController.Status<Double>, Void>>();
var out = pc
.compute(source)
.log()
.subscribe(accumulator::add);

sink.tryEmitNext(new Signal<>(Instant.now(), 15.0));
sink.tryEmitNext(new Signal<>(Instant.now(), null, null, Signal.Status.FAILURE_TOTAL, new IllegalStateException("oops")));
sink.tryEmitNext(new Signal<>(Instant.now(), 25.0));

pc.setSetpoint(30.0);

sink.tryEmitNext(new Signal<>(Instant.now(), 35.0));

sink.tryEmitComplete();

assertThat(accumulator).hasSize(5);

// PV change
assertThat(accumulator.get(0).getValue().signal).isEqualTo(-5.0);
assertThat(accumulator.get(1).getValue().signal).isNaN();
assertThat(accumulator.get(2).getValue().signal).isEqualTo(5.0);

// Setpoint change
assertThat(accumulator.get(3).getValue().signal).isEqualTo(-5.0);

// PV change again
assertThat(accumulator.get(4).getValue().signal).isEqualTo(5.0);

out.dispose();
}
}

0 comments on commit b9be96e

Please sign in to comment.