diff --git a/src/iidm/NodeBreakerVoltageLevel.cpp b/src/iidm/NodeBreakerVoltageLevel.cpp index 4dd8b852..85521c91 100644 --- a/src/iidm/NodeBreakerVoltageLevel.cpp +++ b/src/iidm/NodeBreakerVoltageLevel.cpp @@ -68,6 +68,10 @@ void NodeBreakerVoltageLevel::attach(Terminal& terminal, bool test) { // create the link terminal <-> graph vertex m_graph.setVertexObject(node, stdcxx::ref(nodeTerminal)); + + getNetwork().getVariantManager().forEachVariant([this]() { + invalidateCache(); + }); } } @@ -125,7 +129,9 @@ void NodeBreakerVoltageLevel::detach(Terminal& terminal) { assert(node < m_graph.getVertexCount()); assert(stdcxx::areSame(m_graph.getVertexObject(node).get(), nodeTerminal)); - invalidateCache(); + getNetwork().getVariantManager().forEachVariant([this]() { + invalidateCache(); + }); // remove the link terminal <-> graph vertex m_graph.setVertexObject(node, stdcxx::ref()); diff --git a/test/iidm/NodeBreakerVoltageLevelTest.cpp b/test/iidm/NodeBreakerVoltageLevelTest.cpp index 5b57adcf..947065dc 100644 --- a/test/iidm/NodeBreakerVoltageLevelTest.cpp +++ b/test/iidm/NodeBreakerVoltageLevelTest.cpp @@ -1186,6 +1186,40 @@ BOOST_AUTO_TEST_CASE(TestRemoveVoltageLevelWithInternalConnectionsIssue) { POWSYBL_ASSERT_THROW(network.getVoltageLevel("S5 10kV"), PowsyblException, "Unable to find to the identifiable 'S5 10kV'"); } +BOOST_AUTO_TEST_CASE(issue318_invalidateCache) { + Network network("test", "test"); + Substation& substation = network.newSubstation() + .setId("S1") + .setCountry(Country::FR) + .setTso("TSO") + .add(); + VoltageLevel& vl1 = substation.newVoltageLevel() + .setId("VL1") + .setTopologyKind(TopologyKind::NODE_BREAKER) + .setNominalV(90.0) + .setLowVoltageLimit(88.0) + .setHighVoltageLimit(96.0) + .add(); + + BusbarSection& bbs1 = vl1.getNodeBreakerView().newBusbarSection() + .setId("BBS1") + .setName("BBS1_NAME") + .setNode(0) + .add(); + + // In that case, the bus cache is initialized, but there is no bus for node 0 + BOOST_CHECK(!bbs1.getTerminal().getBusView().getBus()); + + BusbarSection& bbs2 = vl1.getNodeBreakerView().newBusbarSection() + .setId("BBS2") + .setName("BBS2_NAME") + .setNode(1) + .add(); + + // Without the fix, this call throws an exception (ArrayIndexOutOfBoundsException), but an invalid reference is expected + BOOST_CHECK(!bbs2.getTerminal().getBusView().getBus()); +} + BOOST_AUTO_TEST_SUITE_END() } // namespace iidm