Skip to content

Commit

Permalink
emove the loco from DCCppThrottleManagers list of throttle when dispo…
Browse files Browse the repository at this point in the history
…sing of last throttle.
  • Loading branch information
Pugwash1 committed Sep 7, 2020
1 parent f3d6ab5 commit 4694476
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions java/src/jmri/jmrix/dccpp/DCCppThrottleManager.java
Expand Up @@ -167,6 +167,7 @@ public boolean disposeThrottle(jmri.DccThrottle t, jmri.ThrottleListener l) {
tc.getCommandStation().releaseRegister(t.getLocoAddress().getNumber());
if (t instanceof DCCppThrottle) {
DCCppThrottle lnt = (DCCppThrottle) t;
throttles.remove(lnt.getLocoAddress()); // remove from throttles map.
lnt.throttleDispose();
return true;
}
Expand Down
56 changes: 55 additions & 1 deletion java/test/jmri/jmrix/dccpp/DCCppThrottleManagerTest.java
@@ -1,9 +1,17 @@
package jmri.jmrix.dccpp;

import jmri.util.JUnitUtil;
import jmri.DccLocoAddress;
import jmri.DccThrottle;
import jmri.InstanceManager;
import jmri.ThrottleListener;

import org.junit.Assert;
import org.junit.jupiter.api.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* DCCppThrottleManagerTest.java
* <p>
Expand All @@ -14,22 +22,68 @@
*/
public class DCCppThrottleManagerTest extends jmri.managers.AbstractThrottleManagerTestBase {

private DccThrottle throttle;
boolean failedThrottleRequest = false;
DCCppCommandStation cs = null;

@Override
@BeforeEach
public void setUp() {
JUnitUtil.setUp();
DCCppCommandStation cs = new DCCppCommandStation();
cs = new DCCppCommandStation();
cs.setCommandStationMaxNumSlots(12); // the "traditional" value for DCC++
DCCppInterfaceScaffold tc = new DCCppInterfaceScaffold(cs);
tm = new DCCppThrottleManager(new DCCppSystemConnectionMemo(tc));
}

@Test
public void testCreateLnThrottleRunAndRelease() {
ThrottleListener throtListen = new ThrottleListener() {
@Override
public void notifyThrottleFound(DccThrottle t) {
throttle = t;
log.error("created a throttle");
}

@Override
public void notifyFailedThrottleRequest(jmri.LocoAddress address, String reason) {
log.error("Throttle request failed for {} because {}", address, reason);
failedThrottleRequest = true;
}

@Override
public void notifyDecisionRequired(jmri.LocoAddress address, DecisionType question) {
// this is a never-stealing impelementation.
InstanceManager.throttleManagerInstance().cancelThrottleRequest(address, this);
}
};
DccLocoAddress locoAddress = new DccLocoAddress(1203,true);
tm.requestThrottle(1203, throtListen,true);

Assert.assertNotNull("have created a throttle", throttle);
Assert.assertEquals("is DCCppThrottle", throttle.getClass(), jmri.jmrix.dccpp.DCCppThrottle.class);
Assert.assertEquals(true, (((DCCppThrottleManager)tm).throttles.containsKey(locoAddress))); // now you see it
Assert.assertEquals(1,tm.getThrottleUsageCount(locoAddress));
Assert.assertEquals(1, cs.getRegisterNum(1203)); // now you see it
jmri.util.JUnitAppender.assertErrorMessage("created a throttle");

tm.releaseThrottle(throttle, throtListen);
Assert.assertEquals(0,tm.getThrottleUsageCount(locoAddress));
Assert.assertEquals(false, (((DCCppThrottleManager)tm).throttles.containsKey(locoAddress))); //now you dont
Assert.assertEquals(-1, cs.getRegisterNum(1203)); //now you dont

}

@AfterEach
public void tearDown() {
tm =null;
cs = null;
JUnitUtil.resetWindows(false, false);
JUnitUtil.clearShutDownManager(); // put in place because AbstractMRTrafficController implementing subclass was not terminated properly
JUnitUtil.tearDown();

}

private final static Logger log = LoggerFactory.getLogger(DCCppThrottleManagerTest.class);

}

0 comments on commit 4694476

Please sign in to comment.