Skip to content

Commit

Permalink
Revert "Issue #4351 Found simpler test"
Browse files Browse the repository at this point in the history
This reverts commit 5bc6593.
  • Loading branch information
janbartel committed Nov 27, 2019
1 parent 5bc6593 commit a6cdec5
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 89 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
//
// ========================================================================
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//

package org.eclipse.jetty.servlet;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.client.api.Result;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.server.NetworkConnector;
import org.eclipse.jetty.server.Server;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.array;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class InitServletTest
{
public static class DemoServlet extends HttpServlet
{
AtomicInteger initCount = new AtomicInteger();

@Override
public void init() throws ServletException
{
super.init();
try
{
//Make the initialization last a little while so
//other request can run
Thread.sleep(5000);
}
catch (InterruptedException e)
{
throw new ServletException(e);
}
initCount.addAndGet(1);
}

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
//Check that the init() method has been totally finished (by another request) before
//the servlet service() method is called.
if (initCount.get() != 1)
{
resp.sendError(500, "Servlet not initialized!");
}
}
}

private static class AsyncResponseListener implements Response.CompleteListener
{

private CountDownLatch resultsLatch;
private Integer[] results;
private AtomicInteger index = new AtomicInteger();

public AsyncResponseListener(CountDownLatch resultsLatch, Integer[] results)
{
this.resultsLatch = resultsLatch;
this.results = results;
}

public void onComplete(Result result)
{
results[index.getAndAdd(1)] = result.getResponse().getStatus();
resultsLatch.countDown();
}

public void awaitCompletion() throws InterruptedException
{
assertTrue(resultsLatch.await(60L, TimeUnit.SECONDS));
}
}

@Test
public void testServletInitialization() throws Exception
{
Server server = new Server(0);
ServletContextHandler context = new ServletContextHandler(server, "/");
server.setHandler(context);
//add a lazily instantiated servlet
context.addServlet(new ServletHolder(DemoServlet.class), "/*");
server.start();
int port = ((NetworkConnector)server.getConnectors()[0]).getLocalPort();
HttpClient client = new HttpClient();
try
{
client.start();

//Expect 2 responses
CountDownLatch resultsLatch = new CountDownLatch(2);
Integer[] results = new Integer[2];
AsyncResponseListener l = new AsyncResponseListener(resultsLatch, results);

//req1: should initialize servlet
Request r1 = client.newRequest("http://localhost:" + port + "/r1");
r1.method(HttpMethod.GET).send(l);

//Need to give 1st request a head start before request2
Thread.sleep(1000);

//req2: should see servlet fully initialized by request1
Request r2 = client.newRequest("http://localhost:" + port + "/r2");
r2.method(HttpMethod.GET).send(l);

l.awaitCompletion();

assertThat(results, is(array(equalTo(HttpStatus.OK_200), equalTo(HttpStatus.OK_200))));
}
finally
{
client.stop();
server.stop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,10 @@

import java.util.Collections;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServlet;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.util.MultiException;
import org.eclipse.jetty.util.log.StacklessLogging;
Expand All @@ -48,89 +42,6 @@ public class ServletHolderTest
{
public static class FakeServlet extends HttpServlet
{
public AtomicInteger initCount = new AtomicInteger();

@Override
public void init() throws ServletException
{
super.init();
try
{
//Make the initialization last a little while so
//other request can run
Thread.sleep(1000);
}
catch (InterruptedException e)
{
throw new ServletException(e);
}
initCount.addAndGet(1);
}
}

@Test
public void testServletInitAtomic() throws Exception
{
Server server = new Server();
ServletContextHandler context = new ServletContextHandler();
final ServletHolder holder = new ServletHolder(Source.EMBEDDED);
holder.setHeldClass(FakeServlet.class);
holder.setName("Fake");
context.addServlet(holder, "/fake/*");
server.setHandler(context);
server.start();

final AtomicInteger t1Count = new AtomicInteger();
final AtomicInteger t2Count = new AtomicInteger();
final CountDownLatch t1Latch = new CountDownLatch(1);
final CountDownLatch t2Latch = new CountDownLatch(1);

//Test that 2 calls to holder.getServlet are atomic - only
//one should call servlet.init() and the other should be
//held waiting until that fully finishes.
Thread t1 = new Thread()
{
public void run()
{
try
{
FakeServlet s = (FakeServlet)holder.getServlet();
t1Count.set(s.initCount.get());
t1Latch.countDown();
}
catch (ServletException e)
{
fail(e);
}
}
};

Thread t2 = new Thread()
{
public void run()
{
try
{
FakeServlet s = (FakeServlet)holder.getServlet();
t2Count.set(s.initCount.get());
t2Latch.countDown();
}
catch (ServletException e)
{
fail(e);
}
}
};

t1.start();
Thread.sleep(500);
t2.start();

t1Latch.await(10L, TimeUnit.SECONDS);
t2Latch.await(10L, TimeUnit.SECONDS);

assertEquals(1, t1Count.get());
assertEquals(1, t2Count.get());
}

@Test
Expand Down

0 comments on commit a6cdec5

Please sign in to comment.