Skip to content

Commit

Permalink
feat(bdd): use random port if 8234 is not available
Browse files Browse the repository at this point in the history
closes #283
  • Loading branch information
klieber committed Sep 16, 2020
1 parent 31862a5 commit 9ac60fb
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 28 deletions.
14 changes: 12 additions & 2 deletions src/main/java/com/github/searls/jasmine/mojo/ServerMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package com.github.searls.jasmine.mojo;

import com.github.searls.jasmine.config.ImmutableServerConfiguration;
import com.github.searls.jasmine.config.JasmineConfiguration;
import com.github.searls.jasmine.config.ServerConfiguration;
import com.github.searls.jasmine.io.RelativizesFilePaths;
Expand Down Expand Up @@ -84,10 +85,19 @@ public ServerMojo(MavenProject mavenProject,
}

@Override
public void run(ServerConfiguration serverConfiguration,
public void run(ServerConfiguration requestedServerConfiguration,
JasmineConfiguration jasmineConfiguration) throws Exception {
ServerManager serverManager = serverManagerFactory.create();
serverManager.start(serverConfiguration.getServerPort(), resourceHandlerConfigurator.createHandler(jasmineConfiguration));
int assignedPort = serverManager.start(
requestedServerConfiguration.getServerPort(),
resourceHandlerConfigurator.createHandler(jasmineConfiguration)
);

ServerConfiguration serverConfiguration = ImmutableServerConfiguration.builder()
.from(requestedServerConfiguration)
.serverPort(assignedPort)
.build();

LOGGER.info(this.buildServerInstructions(serverConfiguration, jasmineConfiguration));
openBrowser(serverConfiguration);
serverManager.join();
Expand Down
23 changes: 20 additions & 3 deletions src/main/java/com/github/searls/jasmine/server/ServerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -22,10 +22,17 @@
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class ServerManager {

private static final Logger LOGGER = LoggerFactory.getLogger(ServerManager.class);

private static final int ANY_PORT = 0;
public static final String FAILED_TO_BIND = "Failed to bind";

private final ServerConnector connector;

Expand All @@ -40,7 +47,17 @@ public int start(Handler handler) throws Exception {
public int start(int port, Handler handler) throws Exception {
connector.setPort(port);
connector.getServer().setHandler(handler);
connector.getServer().start();
try {
connector.getServer().start();
} catch (IOException e) {
if (ANY_PORT != port && e.getMessage().contains(FAILED_TO_BIND)) {
LOGGER.debug("Unable to start on port {}. Trying a random port.", port, e);
connector.setPort(ANY_PORT);
connector.getServer().start();
} else {
throw e;
}
}

return connector.getLocalPort();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/instructions.template
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Jasmine testing environment started! You can run your specs as you develop by visiting
this URL in a web browser:

%s
%s

The server will monitor these two directories for scripts that you add, remove, and change:

Expand Down
33 changes: 11 additions & 22 deletions src/test/java/com/github/searls/jasmine/mojo/ServerMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -19,6 +19,7 @@
*/
package com.github.searls.jasmine.mojo;

import com.github.searls.jasmine.config.ImmutableServerConfiguration;
import com.github.searls.jasmine.config.JasmineConfiguration;
import com.github.searls.jasmine.config.ServerConfiguration;
import com.github.searls.jasmine.io.RelativizesFilePaths;
Expand All @@ -28,7 +29,6 @@
import com.github.searls.jasmine.server.ServerManagerFactory;
import org.apache.maven.project.MavenProject;
import org.eclipse.jetty.server.Handler;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
Expand All @@ -48,21 +48,12 @@ public class ServerMojoTest {
@Mock
private MavenProject mavenProject;

@Mock
private ServerConfiguration serverConfiguration;

@Mock
private JasmineConfiguration jasmineConfiguration;

@Mock
private RelativizesFilePaths relativizesFilePaths;

@Mock
private File baseDir;

@Mock
private File targetDir;

@Mock
private File sourceDir;

Expand Down Expand Up @@ -90,9 +81,8 @@ public class ServerMojoTest {
@InjectMocks
private ServerMojo subject;

@Before
public void before() throws Exception {
when(serverConfiguration.getServerPort()).thenReturn(PORT);
@Test
public void testRunMojo() throws Exception {

when(jasmineConfiguration.getSources()).thenReturn(sources);
when(jasmineConfiguration.getSpecs()).thenReturn(specs);
Expand All @@ -101,18 +91,17 @@ public void before() throws Exception {
when(specs.getDirectory()).thenReturn(specDir);
when(serverManagerFactory.create()).thenReturn(serverManager);
when(configurator.createHandler(jasmineConfiguration)).thenReturn(handler);
when(serverManager.start(PORT, handler)).thenReturn(PORT);

ServerConfiguration serverConfiguration = ImmutableServerConfiguration.builder()
.uriScheme("http")
.serverHostname("localhost")
.serverPort(PORT)
.build();

subject.run(serverConfiguration, jasmineConfiguration);
}

@Test
public void startsTheServer() throws Exception {
verify(serverManager).start(PORT, handler);
}

@Test
public void joinsTheServer() throws Exception {
verify(serverManager).join();
}
}

0 comments on commit 9ac60fb

Please sign in to comment.