Skip to content

Commit a542a2c

Browse files
committed
Start SFTP server on an arbitrary free port.
This makes it possible to run multiple tests in parallel because they don't use the same port anymore.
1 parent 3edb85a commit a542a2c

File tree

4 files changed

+134
-49
lines changed

4 files changed

+134
-49
lines changed

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Fake SFTP Server Rule is published under the
1212
if you want to use it with an older version of Java.
1313

1414
I want to thank my former team SAM at ThoughtWorks for using this library and
15-
@crizzis and @OArtyomov for their feature requests.
15+
@crizzis, @OArtyomov and @TheSentinel454 for their feature requests.
1616

1717

1818
## Installation
@@ -23,9 +23,11 @@ Fake SFTP Server Rule is available from
2323
<dependency>
2424
<groupId>com.github.stefanbirkner</groupId>
2525
<artifactId>fake-sftp-server-rule</artifactId>
26-
<version>1.4.0</version>
26+
<version>2.0.0</version>
2727
</dependency>
2828

29+
If you upgrade from a version < 2.x to the newest version please read the last
30+
section of this readme.
2931

3032
## Usage
3133

@@ -56,7 +58,8 @@ password, but you can restrict it to specific pairs.
5658

5759
It is also possible to do this during the test using the same method.
5860

59-
The port of the server is obtained by `sftpServer.getPort()`. You can change it
61+
By default the SFTP server listens on an auto-allocated port. During the test
62+
this port can be obtained by `sftpServer.getPort()`. It can be changed
6063
by calling `setPort(int)`. If you do this from within a test then the server
6164
gets restarted. The time-consuming restart can be avoided by setting the port
6265
immediately after creating the rule.
@@ -197,3 +200,15 @@ CI.
197200
* Commit the modified `pom.xml` and `README.md`.
198201
* Run `mvn clean deploy` with JDK 8.
199202
* Add a tag for the release: `git tag fake-sftp-server-rule-X.X.X`
203+
204+
205+
## Upgrading from 0.x.y or 1.x.y to version >= 2
206+
207+
In older versions the SFTP server listened to port 23454 by default. From
208+
version 2 on it selects an arbitrary free port by default. If your tests fail
209+
after an upgrade you may consider to restore the old behaviour by immediately
210+
setting the old port after creating the rule.
211+
212+
@Rule
213+
public final FakeSftpServerRule sftpServer = new FakeSftpServerRule()
214+
.setPort(23454);

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</parent>
1010

1111
<artifactId>fake-sftp-server-rule</artifactId>
12-
<version>1.4.0</version>
12+
<version>2.0.0</version>
1313
<packaging>jar</packaging>
1414

1515
<name>Fake SFTP Server Rule</name>

src/main/java/com/github/stefanbirkner/fakesftpserver/rule/FakeSftpServerRule.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@
5151
* }
5252
* </pre>
5353
* <p>It is also possible to do this during the test using the same method.
54-
* <p>The port of the server is obtained by
55-
* {@link #getPort() sftpServer.getPort()}. You can change it by calling
56-
* {@link #setPort(int)}. If you do this from within a test then the server gets
57-
* restarted. The time-consuming restart can be avoided by setting the port
58-
* immediately after creating the rule.
54+
* <p>By default the SFTP server listens on an auto-allocated port. During the
55+
* test this port can be obtained by {@link #getPort() sftpServer.getPort()}. It
56+
* can be changed by calling {@link #setPort(int)}. If you do this from within a
57+
* test then the server gets restarted. The time-consuming restart can be
58+
* avoided by setting the port immediately after creating the rule.
5959
* <pre>
6060
* public class TestClass {
6161
* &#064;Rule
@@ -178,18 +178,30 @@ public FileVisitResult postVisitDirectory(
178178
}
179179
};
180180
private final Map<String, String> usernamesAndPasswords = new HashMap<>();
181-
private int port = 23454;
181+
private int port = 0;
182182

183183
private FileSystem fileSystem;
184184
private SshServer server;
185185

186186
/**
187-
* Returns the port of the SFTP server.
187+
* Returns the port of the SFTP server. If the SFTP server listens on an
188+
* auto-allocated port (that means you didn't call {@link #setPort(int)})
189+
* then you can only call this method during the test.
188190
*
189191
* @return the port of the SFTP server.
192+
* @throws IllegalStateException if you call the method outside of a test
193+
* but haven't called {@link #setPort(int)}) before.
190194
*/
191195
public int getPort() {
192-
return port;
196+
if (port == 0)
197+
return getPortFromServer();
198+
else
199+
return port;
200+
}
201+
202+
private int getPortFromServer() {
203+
verifyThatTestIsRunning("call getPort()");
204+
return server.getPort();
193205
}
194206

195207
/**

src/test/java/com/github/stefanbirkner/fakesftpserver/rule/FakeSftpServerRuleTest.java

Lines changed: 95 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -782,24 +782,26 @@ public void existence_of_a_file_cannot_be_checked_after_the_test_is_finished() {
782782
public static class server_shutdown {
783783
@Test
784784
public void after_a_successful_test_SFTP_server_is_shutdown() {
785-
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
785+
FakeSftpServerRule sftpServer = new FakeSftpServerRule()
786+
.setPort(DUMMY_PORT);
786787
executeTestWithRule(
787788
() -> {},
788789
sftpServer
789790
);
790-
assertConnectionToSftpServerNotPossible(sftpServer);
791+
assertConnectionToSftpServerNotPossible(DUMMY_PORT);
791792
}
792793

793794
@Test
794795
public void after_an_erroneous_test_SFTP_server_is_shutdown() {
795-
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
796+
FakeSftpServerRule sftpServer = new FakeSftpServerRule()
797+
.setPort(DUMMY_PORT);
796798
executeTestThatThrowsExceptionWithRule(
797799
() -> {
798800
throw new RuntimeException();
799801
},
800802
sftpServer
801803
);
802-
assertConnectionToSftpServerNotPossible(sftpServer);
804+
assertConnectionToSftpServerNotPossible(DUMMY_PORT);
803805
}
804806

805807
@Test
@@ -823,12 +825,6 @@ public void after_a_test_second_SFTP_server_is_shutdown_when_port_was_changed_du
823825
assertConnectionToSftpServerNotPossible(DUMMY_PORT);
824826
}
825827

826-
private void assertConnectionToSftpServerNotPossible(
827-
FakeSftpServerRule sftpServer
828-
) {
829-
assertConnectionToSftpServerNotPossible(sftpServer.getPort());
830-
}
831-
832828
private void assertConnectionToSftpServerNotPossible(
833829
int port
834830
) {
@@ -845,6 +841,26 @@ private void assertConnectionToSftpServerNotPossible(
845841
}
846842

847843
public static class port_selection {
844+
@Test
845+
public void by_default_two_rules_run_servers_at_different_ports() {
846+
FakeSftpServerRule firstSftpServer = new FakeSftpServerRule();
847+
AtomicInteger portCaptureForFirstServer = new AtomicInteger();
848+
FakeSftpServerRule secondSftpServer = new FakeSftpServerRule();
849+
AtomicInteger portCaptureForSecondServer = new AtomicInteger();
850+
851+
executeTestWithRule(
852+
() -> portCaptureForFirstServer.set(firstSftpServer.getPort()),
853+
firstSftpServer
854+
);
855+
executeTestWithRule(
856+
() -> portCaptureForSecondServer.set(secondSftpServer.getPort()),
857+
secondSftpServer
858+
);
859+
860+
assertThat(portCaptureForFirstServer)
861+
.doesNotHaveValue(portCaptureForSecondServer.get());
862+
}
863+
848864
@Test
849865
public void the_port_can_be_changed_during_the_test() {
850866
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
@@ -906,37 +922,79 @@ public void it_is_not_possible_to_set_a_port_greater_than_65535() {
906922
}
907923
}
908924

925+
@RunWith(Enclosed.class)
909926
public static class port_query {
910-
@Test
911-
public void port_can_be_read_before_the_test() {
912-
FakeSftpServerRule sftpServer = new FakeSftpServerRule()
913-
.setPort(DUMMY_PORT);
914-
int port = sftpServer.getPort();
915-
assertThat(port).isEqualTo(DUMMY_PORT);
916-
}
917927

918-
@Test
919-
public void port_can_be_read_during_the_test() {
920-
AtomicInteger portCapture = new AtomicInteger();
921-
FakeSftpServerRule sftpServer = new FakeSftpServerRule()
922-
.setPort(DUMMY_PORT);
923-
executeTestWithRule(
924-
() -> portCapture.set(sftpServer.getPort()),
925-
sftpServer
926-
);
927-
assertThat(portCapture).hasValue(DUMMY_PORT);
928+
public static class random_port {
929+
@Test
930+
public void cannot_be_read_before_the_test() {
931+
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
932+
assertPortCannotBeRead(sftpServer);
933+
}
934+
935+
@Test
936+
public void can_be_read_during_the_test() {
937+
AtomicInteger portCapture = new AtomicInteger();
938+
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
939+
executeTestWithRule(
940+
() -> portCapture.set(sftpServer.getPort()),
941+
sftpServer
942+
);
943+
assertThat(portCapture).doesNotHaveValue(0);
944+
}
945+
946+
@Test
947+
public void cannot_be_read_after_the_test() {
948+
FakeSftpServerRule sftpServer = new FakeSftpServerRule();
949+
executeTestWithRule(
950+
() -> {},
951+
sftpServer
952+
);
953+
assertPortCannotBeRead(sftpServer);
954+
}
955+
956+
private void assertPortCannotBeRead(FakeSftpServerRule sftpServer) {
957+
assertThatThrownBy(sftpServer::getPort)
958+
.isInstanceOf(IllegalStateException.class)
959+
.hasMessage(
960+
"Failed to call getPort() because test has not been"
961+
+ " started or is already finished."
962+
);
963+
}
928964
}
929965

930-
@Test
931-
public void port_can_be_read_after_the_test() {
932-
FakeSftpServerRule sftpServer = new FakeSftpServerRule()
933-
.setPort(DUMMY_PORT);
934-
executeTestWithRule(
935-
() -> {},
936-
sftpServer
937-
);
938-
int port = sftpServer.getPort();
939-
assertThat(port).isEqualTo(DUMMY_PORT);
966+
public static class specified_port {
967+
@Test
968+
public void can_be_read_before_the_test() {
969+
FakeSftpServerRule sftpServer = new FakeSftpServerRule()
970+
.setPort(DUMMY_PORT);
971+
int port = sftpServer.getPort();
972+
assertThat(port).isEqualTo(DUMMY_PORT);
973+
}
974+
975+
@Test
976+
public void can_be_read_during_the_test() {
977+
AtomicInteger portCapture = new AtomicInteger();
978+
FakeSftpServerRule sftpServer = new FakeSftpServerRule()
979+
.setPort(DUMMY_PORT);
980+
executeTestWithRule(
981+
() -> portCapture.set(sftpServer.getPort()),
982+
sftpServer
983+
);
984+
assertThat(portCapture).hasValue(DUMMY_PORT);
985+
}
986+
987+
@Test
988+
public void can_be_read_after_the_test() {
989+
FakeSftpServerRule sftpServer = new FakeSftpServerRule()
990+
.setPort(DUMMY_PORT);
991+
executeTestWithRule(
992+
() -> {},
993+
sftpServer
994+
);
995+
int port = sftpServer.getPort();
996+
assertThat(port).isEqualTo(DUMMY_PORT);
997+
}
940998
}
941999
}
9421000

0 commit comments

Comments
 (0)