Skip to main content
summaryrefslogtreecommitdiffstats
blob: c4188f0d91e26f64547756b33e828ec14f7634a7 (plain) (blame)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//
//  ========================================================================
//  Copyright (c) 1995-2016 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.client;

import java.io.IOException;
import java.net.URI;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

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

import org.eclipse.jetty.http.HttpMethods;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

/**
 * IdleTimeoutTest
 *
 * Warning - this is a slow test. Uncomment the ignore to run it.
 *
 */
public class IdleTimeoutTest
{
    public int _repetitions = 30;
    
    @Ignore
    //@Test
    public void testIdleTimeoutOnBlockingConnector() throws Exception
    {
        final HttpClient client = new HttpClient();
        client.setMaxConnectionsPerAddress(4);
        client.setConnectorType(HttpClient.CONNECTOR_SOCKET);
        client.setTimeout(TimeUnit.SECONDS.toMillis(86400)); // very long timeout on data
        client.setIdleTimeout(500); // very short idle timeout
        client.start();

        final CountDownLatch counter = new CountDownLatch(_repetitions);
        
        Thread runner = new Thread()
        {
            public void run()
            {
                try
                {
                    for (int i=0; i<_repetitions; i++) 
                    {
                        ContentExchange exchange = new ContentExchange();
                        exchange.setURL("http://www.google.com/?i="+i);
                        client.send(exchange);
                        exchange.waitForDone();
                        counter.countDown();
                        System.err.println(counter.getCount());
                        Thread.sleep(1000); //wait long enough for idle timeout to expire   
                    }
                }
                catch (Exception e)
                {
                    Assert.fail(e.getMessage());
                }
            }
        };
       
        runner.start();
        if (!counter.await(80, TimeUnit.SECONDS))
            Assert.fail("Test did not complete in time");
        
    }

    @Test
    public void testConnectionsAreReleasedWhenExpired() throws Exception
    {
        // we need a server that times out and a client with shorter timeout settings, so we need to create new ones
        Server server = new Server();
        Connector connector = new SelectChannelConnector();
        server.addConnector(connector);
        server.setHandler(new AbstractHandler()
        {
            public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
            {
                if (request.getParameter("timeout") != null)
                {
                    try
                    {
                        Thread.sleep(1000);
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
                baseRequest.setHandled(true);
                response.getWriter().write("Hello world");
            }
        });
        server.start();

        HttpClient httpClient = new HttpClient();
        httpClient.setMaxConnectionsPerAddress(1);
        httpClient.setConnectTimeout(200);
        httpClient.setTimeout(200);
        httpClient.setIdleTimeout(200);
        httpClient.start();

        String uriString =  "http://localhost:" + connector.getLocalPort() + "/";

        HttpExchange httpExchange = new HttpExchange();
        httpExchange.setURI(URI.create(uriString).resolve("?timeout=true"));
        httpExchange.setMethod(HttpMethods.GET);
        httpClient.send(httpExchange);
        int status = httpExchange.waitForDone();
        assertThat("First request expired", status, is(8));

        httpExchange = new HttpExchange();
        httpExchange.setURI(URI.create(uriString));
        httpExchange.setMethod(HttpMethods.GET);
        httpClient.send(httpExchange);
        status = httpExchange.waitForDone();
        assertThat("Second request was successful as timeout is not set", status, is(7));
    }
}

Back to the top