Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2645 from alvarolobato/JENKINS-28245
[FIX JENKINS-28245] - Allow defining agent ping interval and ping timeout in seconds
- Loading branch information
Showing
4 changed files
with
222 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
core/src/test/java/hudson/slaves/ChannelPingerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package hudson.slaves; | ||
|
||
import static org.mockito.Matchers.eq; | ||
import static org.mockito.Mockito.verify; | ||
import static org.powermock.api.mockito.PowerMockito.mockStatic; | ||
import static org.powermock.api.mockito.PowerMockito.verifyStatic; | ||
|
||
import com.google.common.testing.EqualsTester; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
import hudson.remoting.Channel; | ||
|
||
import java.util.Map; | ||
import java.util.HashMap; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest({ ChannelPinger.class }) | ||
public class ChannelPingerTest { | ||
|
||
@Mock private Channel mockChannel; | ||
|
||
private Map<String, String> savedSystemProperties = new HashMap<String, String>(); | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
MockitoAnnotations.initMocks(this); | ||
mockStatic(ChannelPinger.class); | ||
} | ||
|
||
@Before | ||
public void preserveSystemProperties() throws Exception { | ||
preserveSystemProperty("hudson.slaves.ChannelPinger.pingInterval"); | ||
preserveSystemProperty("hudson.slaves.ChannelPinger.pingIntervalSeconds"); | ||
preserveSystemProperty("hudson.slaves.ChannelPinger.pingTimeoutSeconds"); | ||
} | ||
|
||
@After | ||
public void restoreSystemProperties() throws Exception { | ||
for (Map.Entry<String, String> entry : savedSystemProperties.entrySet()) { | ||
if (entry.getValue() != null) { | ||
System.setProperty(entry.getKey(), entry.getValue()); | ||
} else { | ||
System.clearProperty(entry.getKey()); | ||
} | ||
} | ||
} | ||
|
||
private void preserveSystemProperty(String propertyName) { | ||
savedSystemProperties.put(propertyName, System.getProperty(propertyName)); | ||
System.clearProperty(propertyName); | ||
} | ||
|
||
@Test | ||
public void testDefaults() throws Exception { | ||
ChannelPinger channelPinger = new ChannelPinger(); | ||
channelPinger.install(mockChannel); | ||
|
||
verify(mockChannel).call(eq(new ChannelPinger.SetUpRemotePing(ChannelPinger.PING_TIMEOUT_SECONDS_DEFAULT, | ||
ChannelPinger.PING_INTERVAL_SECONDS_DEFAULT))); | ||
verifyStatic(); | ||
ChannelPinger.setUpPingForChannel(mockChannel, ChannelPinger.PING_TIMEOUT_SECONDS_DEFAULT, | ||
ChannelPinger.PING_INTERVAL_SECONDS_DEFAULT, true); | ||
} | ||
|
||
@Test | ||
public void testFromSystemProperties() throws Exception { | ||
System.setProperty("hudson.slaves.ChannelPinger.pingTimeoutSeconds", "42"); | ||
System.setProperty("hudson.slaves.ChannelPinger.pingIntervalSeconds", "73"); | ||
|
||
ChannelPinger channelPinger = new ChannelPinger(); | ||
channelPinger.install(mockChannel); | ||
|
||
verify(mockChannel).call(new ChannelPinger.SetUpRemotePing(42, 73)); | ||
verifyStatic(); | ||
ChannelPinger.setUpPingForChannel(mockChannel, 42, 73, true); | ||
} | ||
|
||
@Test | ||
public void testFromOldSystemProperty() throws Exception { | ||
System.setProperty("hudson.slaves.ChannelPinger.pingInterval", "7"); | ||
|
||
ChannelPinger channelPinger = new ChannelPinger(); | ||
channelPinger.install(mockChannel); | ||
|
||
verify(mockChannel).call(eq(new ChannelPinger.SetUpRemotePing(ChannelPinger.PING_TIMEOUT_SECONDS_DEFAULT, 420))); | ||
verifyStatic(); | ||
ChannelPinger.setUpPingForChannel(mockChannel, ChannelPinger.PING_TIMEOUT_SECONDS_DEFAULT, 420, true); | ||
} | ||
|
||
@Test | ||
public void testNewSystemPropertyTrumpsOld() throws Exception { | ||
System.setProperty("hudson.slaves.ChannelPinger.pingIntervalSeconds", "73"); | ||
System.setProperty("hudson.slaves.ChannelPinger.pingInterval", "7"); | ||
|
||
ChannelPinger channelPinger = new ChannelPinger(); | ||
channelPinger.install(mockChannel); | ||
|
||
verify(mockChannel).call(eq(new ChannelPinger.SetUpRemotePing(ChannelPinger.PING_TIMEOUT_SECONDS_DEFAULT, 73))); | ||
verifyStatic(); | ||
ChannelPinger.setUpPingForChannel(mockChannel, ChannelPinger.PING_TIMEOUT_SECONDS_DEFAULT, 73, true); | ||
} | ||
|
||
@Test | ||
public void testSetUpRemotePingEquality() { | ||
new EqualsTester() | ||
.addEqualityGroup(new ChannelPinger.SetUpRemotePing(1, 2), new ChannelPinger.SetUpRemotePing(1, 2)) | ||
.addEqualityGroup(new ChannelPinger.SetUpRemotePing(2, 3), new ChannelPinger.SetUpRemotePing(2, 3)) | ||
.testEquals(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters