Skip to content

Commit

Permalink
fix: set load-on-startup for automatically registered Vaadin servlet (#…
Browse files Browse the repository at this point in the history
…14501)

When using Vite, DevModeInitializer blocks dev-server startup until a
VaadinServlet is deployed because it needs to get the servlet path to use.
If the container lazily loads servlets, Vite will not start until the first
HTTP request for the Vaadin servlet is received.
This change sets load-on-startup feature for automatically deployed Vaadin
servlet, to ensure that the servlet and Vite are loaded on the startup of the
Web application

Part of #14479
  • Loading branch information
mcollovati authored Sep 13, 2022
1 parent 8ce39da commit 814373d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ private VaadinServletCreation createServletIfNotExists(

registration.setAsyncSupported(true);
registration.addMapping(path);
registration.setLoadOnStartup(1);
return VaadinServletCreation.SERVLET_CREATED;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;

public class ServletDeployerTest {
private final ServletDeployer deployer = new ServletDeployer();

private List<String> servletNames;
private List<String> servletMappings;
private List<Integer> servletLoadOnStartup;

private boolean disableAutomaticServletRegistration = false;

Expand Down Expand Up @@ -84,6 +86,7 @@ private static class ComponentWithRoute extends Component {
public void clearCaptures() {
servletNames = new ArrayList<>();
servletMappings = new ArrayList<>();
servletLoadOnStartup = new ArrayList<>();
}

@Test
Expand All @@ -93,6 +96,7 @@ public void automaticallyRegisterTwoServletsWhenNoServletsPresent()

assertMappingsCount(1, 1);
assertMappingIsRegistered(ServletDeployer.class.getName(), "/*");
assertLoadOnStartupSet();
}

@Test
Expand All @@ -114,6 +118,7 @@ public void registeredNonVaadinServlets_vaadinServletsAreRegistered()
singletonList("/test/*"), Collections.emptyMap())));

assertMappingsCount(1, 1);
assertLoadOnStartupSet();
}

@Test
Expand All @@ -126,6 +131,7 @@ public void frontendServletIsNotRegisteredWhenProductionModeIsActive()

assertMappingsCount(1, 1);
assertMappingIsRegistered(ServletDeployer.class.getName(), "/*");
assertLoadOnStartupSet();
}

@Test
Expand All @@ -135,6 +141,7 @@ public void frontendServletIsNotRegistered_whenMainServletIsRegistered()

assertMappingsCount(1, 1);
assertMappingIsRegistered(ServletDeployer.class.getName(), "/*");
assertLoadOnStartupSet();
}

@Test
Expand All @@ -156,6 +163,7 @@ public void servletIsNotRegisteredWhenAnotherHasTheSamePathMapping_frontendServl

assertMappingsCount(1, 1);
assertMappingIsRegistered(ServletDeployer.class.getName(), "/*");
assertLoadOnStartupSet();
}

private void assertMappingsCount(int numServlets, int numMappings) {
Expand Down Expand Up @@ -184,6 +192,21 @@ private void assertMappingIsRegistered(String servletName,
pathIndex, servletNameIndex);
}

private void assertLoadOnStartupSet() {
assertEquals("Servlet loadOnStartup should be invoked only once", 1,
servletLoadOnStartup.size());
assertEquals(
String.format(
"Expected servlet loadOnStartup to be '%d' but was '%d",
1, servletLoadOnStartup.get(0)),
(Integer) 1, servletLoadOnStartup.get(0));
}

private void assertLoadOnStartupNotSet() {
assertTrue("Servlet loadOnStartup should not have been invoked ",
servletLoadOnStartup.isEmpty());
}

@SuppressWarnings({ "unchecked", "rawtypes" })
private ServletContextEvent getContextEvent(
ServletRegistration... servletRegistrations) throws Exception {
Expand All @@ -196,6 +219,8 @@ private ServletContextEvent getContextEvent(
this.servletMappings.addAll(Arrays.asList(mappings));
return Collections.emptySet();
});
Mockito.doAnswer(i -> this.servletLoadOnStartup.add(i.getArgument(0)))
.when(dynamicMock).setLoadOnStartup(ArgumentMatchers.anyInt());

ServletContext contextMock = Mockito.mock(ServletContext.class);

Expand Down

0 comments on commit 814373d

Please sign in to comment.