-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
InitServletTest.java
132 lines (116 loc) · 4.67 KB
/
InitServletTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
// ========================================================================
// 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.Response;
import org.eclipse.jetty.client.api.Result;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.server.NetworkConnector;
import org.eclipse.jetty.server.Server;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class InitServletTest
{
public static class DemoServlet extends HttpServlet
{
private static final long INIT_SLEEP = 2000;
private final AtomicInteger initCount = new AtomicInteger();
@Override
public void init() throws ServletException
{
super.init();
try
{
// Make the initialization last a little while.
// Other requests must wait.
Thread.sleep(INIT_SLEEP);
}
catch (InterruptedException e)
{
throw new ServletException(e);
}
initCount.incrementAndGet();
}
@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 final AtomicInteger index = new AtomicInteger();
private final CountDownLatch resultsLatch;
private final int[] results;
public AsyncResponseListener(CountDownLatch resultsLatch, int[] results)
{
this.resultsLatch = resultsLatch;
this.results = results;
}
public void onComplete(Result result)
{
results[index.getAndIncrement()] = result.getResponse().getStatus();
resultsLatch.countDown();
}
}
@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), "/*");
HttpClient client = new HttpClient();
server.addBean(client);
server.start();
try
{
int port = ((NetworkConnector)server.getConnectors()[0]).getLocalPort();
// Expect 2 responses
CountDownLatch resultsLatch = new CountDownLatch(2);
int[] results = new int[2];
AsyncResponseListener l = new AsyncResponseListener(resultsLatch, results);
// Req1: should initialize servlet.
client.newRequest("http://localhost:" + port + "/r1").send(l);
// Need to give 1st request a head start before request2.
Thread.sleep(DemoServlet.INIT_SLEEP / 4);
// Req2: should see servlet fully initialized by request1.
client.newRequest("http://localhost:" + port + "/r2").send(l);
assertTrue(resultsLatch.await(DemoServlet.INIT_SLEEP * 2, TimeUnit.MILLISECONDS));
assertEquals(HttpStatus.OK_200, results[0]);
assertEquals(HttpStatus.OK_200, results[1]);
}
finally
{
server.stop();
}
}
}