Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8b822a6644d064031eb4ef75c8accdd48e32e3c9 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//
//  ========================================================================
//  Copyright (c) 1995-2013 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.io.InputStream;
import java.util.ArrayList;

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

import junit.framework.Assert;

import org.eclipse.jetty.http.HttpHeaderValues;
import org.eclipse.jetty.http.HttpHeaders;
import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.bio.SocketConnector;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.junit.Test;

/**
 * A class that attempts to simulate a client communicating with a slow server. It imposes a delay between each handle() call made to the server's handler.
 * 
 * The client sends a binary blob of data to the server, and this blob is then inspected to verify correct transfer.
 */
public class SluggishServerTest
{

    /** msec to wait between reads in the handler -- may need to adjust based on OS/HW/etc. to reproduce bug */
    private final static int READ_DELAY = 5;

    private final static String URL = "http://localhost:";

    /** Stream providing a binary message to send */
    private static class SluggishStream extends InputStream
    {
        private final byte[] request;
        private int pos;

        public SluggishStream(byte[] request)
        {
            this.request = request;
            this.pos = 0;
        }

        @Override
        public int read() throws IOException
        {
            if (pos < request.length)
            {
                int byteVal = request[pos++] & 0xFF;
                return byteVal;
            }
            else
            {
                return -1;
            }
        }

    }

    /** Sends a message containing random binary content to a SluggishHandler */
    private static class SluggishExchange extends HttpExchange
    {
        private byte[] request;

        public SluggishExchange(int port, int count)
        {
            request = new byte[count];
            for (int i=0;i<count;i++)
                request[i]=(byte)('A'+(i%26));
            setURL(URL+port);
            setRequestContentSource(new SluggishStream(request));
            setRequestContentType("application/octet-stream");
            setRequestHeader(HttpHeaders.TRANSFER_ENCODING,HttpHeaderValues.CHUNKED);
        }

        public byte[] getRequestBody()
        {
            return request;
        }

        @Override
        protected void onRequestComplete()
        {
            //System.err.println("REQUEST COMPLETE " + this);
        }

        @Override
        protected void onResponseComplete()
        {
            //System.err.println("RESPONSE COMPLETE " + this);
        }

        @Override
        protected void onResponseStatus(Buffer version, int status, Buffer reason) throws IOException
        {
            //System.err.printf("<<< %s %d %s%n",version,status,reason);
            super.onResponseStatus(version,status,reason);
        }

        @Override
        protected void onResponseHeader(Buffer name, Buffer value) throws IOException
        {
            //System.err.printf("<<< %s: %s%n",name,value);
            super.onResponseHeader(name,value);
        }

        @Override
        protected void onResponseHeaderComplete() throws IOException
        {
            //System.err.printf("<<< --%n");
            super.onResponseHeaderComplete();
        }
        
    }

    /** Receives binary message from a SluggishExchange & stores it for validation */
    private static class SluggishHandler extends AbstractHandler
    {

        private final ArrayList<Byte> accumulatedRequest;

        public SluggishHandler(int requestSize)
        {
            accumulatedRequest = new ArrayList<Byte>(requestSize);
        }

        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
        {
            accumulatedRequest.clear();
            ServletInputStream input = request.getInputStream();
            byte[] buffer = new byte[16384];
            int bytesAvailable;
            while ((bytesAvailable = input.read(buffer,0,buffer.length)) > 0)
            {
                //System.err.println("AVAILABLE FOR READ = " + bytesAvailable);
                for (int n = 0; n < bytesAvailable; ++n)
                {
                    accumulatedRequest.add(buffer[n]);
                }
                try
                {
                    Thread.sleep(READ_DELAY);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
            response.setStatus(HttpServletResponse.SC_OK);
            baseRequest.setHandled(true);
            //System.err.println("HANDLED");
        }

        public byte[] getAccumulatedRequest()
        {
            byte[] buffer = new byte[accumulatedRequest.size()];
            int pos = 0;
            for (Byte b : accumulatedRequest)
            {
                buffer[pos++] = b;
            }
            return buffer;
        }
    }

    private static boolean compareBuffers(byte[] sent, byte[] received)
    {
        if (sent.length != received.length)
        {
            System.err.format("Mismatch in sent/received lengths: sent=%d received=%d\n",sent.length,received.length);
            return false;
        }
        else
        {
            for (int n = 0; n < sent.length; ++n)
            {
                if (sent[n] != received[n])
                {
                    System.err.format("Mismatch at offset %d: request=%d response=%d\n",n,sent[n],received[n]);
                    return false;
                }
            }
        }
        return true;
    }

    @Test
    public void test0() throws Exception
    {
        goSlow(20000,10);    
    }
    
    @Test
    public void test1() throws Exception
    {
        goSlow(200000,5);    
    }
    
    @Test
    public void test2() throws Exception
    {
        goSlow(2000000,2);    
    }
    
    void goSlow(int requestSize,int iterations) throws Exception
    {
        Server server = new Server();
        SocketConnector connector = new SocketConnector();
        server.addConnector(connector);
        SluggishHandler handler = new SluggishHandler(requestSize);
        server.setHandler(handler);
        server.start();
        int port = connector.getLocalPort();

        HttpClient client = new HttpClient();
        client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
        client.setConnectTimeout(5000);
        client.setIdleTimeout(60000);
        client.start();

        try
        {
            for (int i = 0; i < iterations; ++i)
            {
                //System.err.format("-------------- ITERATION %d ------------------\n",i);
                SluggishExchange exchange = new SluggishExchange(port,requestSize);
                long startTime = System.currentTimeMillis();
                client.send(exchange);
                exchange.waitForDone();
                long endTime = System.currentTimeMillis();
                //System.err.println("EXCHANGE STATUS = " + exchange);
                //System.err.println("ELAPSED MSEC = " + (endTime - startTime));
                Assert.assertTrue(compareBuffers(exchange.getRequestBody(),handler.getAccumulatedRequest()));
            }
        }
        finally
        {
            server.stop();
            server.join();
        }
    }
}

Back to the top