summaryrefslogtreecommitdiffstats
path: root/simple/simple-http/src/test/java/org/simpleframework/http/core/StopTest.java
blob: 67751b891e7b06340671398978a0c9ba2e678cd8 (plain)
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
package org.simpleframework.http.core;

import java.io.Closeable;
import java.io.InputStream;
import java.io.PrintStream;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.net.InetSocketAddress;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

import junit.framework.TestCase;

import org.simpleframework.common.thread.ConcurrentExecutor;
import org.simpleframework.http.Request;
import org.simpleframework.http.Response;
import org.simpleframework.transport.connect.Connection;
import org.simpleframework.transport.connect.SocketConnection;

public class StopTest extends TestCase {
   
   private static final int ITERATIONS = 20;
   
   public void testStop() throws Exception {
      ThreadDumper dumper = new ThreadDumper();
      
      dumper.start();
      dumper.waitUntilStarted();
      
      ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
      int initialThreads = threadBean.getThreadCount();
      
      for(int i = 0; i < ITERATIONS; i++) {
         try {
            ServerCriteria criteria = createServer();
            InetSocketAddress address = criteria.getAddress();
            Connection connection = criteria.getConnection();
            Client client = createClient(address, String.format("[%s of %s]", i, ITERATIONS));
            
            Thread.sleep(2000); // allow some requests to execute
            connection.close();
            Thread.sleep(100); // ensure client keeps executing
            client.close();
            Thread.sleep(1000); // wait for threads to terminate
         }catch(Exception e) {
            e.printStackTrace();
         }         
         //assertEquals(initialThreads, threadBean.getThreadCount());
      }
      dumper.kill();
   }
  
   public static Client createClient(InetSocketAddress address, String tag) throws Exception {
      ConcurrentExecutor executor = new ConcurrentExecutor(Runnable.class, 20);
      int port = address.getPort();
      Client client = new Client(executor, port, tag);

      client.start();
      return client;
   }
   
   public static ServerCriteria createServer() throws Exception {
      Container container = new Container() {
         public void handle(Request request, Response response) {
            try {
               PrintStream out = response.getPrintStream();
               response.setValue("Content-Type", "text/plain");
               response.setValue("Connection", "close");
               
               out.print("TEST " + new Date());
               response.close();
            }catch(Exception e) {
               e.printStackTrace();
               try {
                  response.close();
               }catch(Exception ex) {
                  ex.printStackTrace();
               }
            }
         }
      };
      ContainerSocketProcessor server = new ContainerSocketProcessor(container);
      Connection connection = new SocketConnection(server);
      InetSocketAddress address = (InetSocketAddress)connection.connect(null); // ephemeral port
      
      return new ServerCriteria(connection, address);
   }
   
   private static class Client extends Thread implements Closeable {
      
      private ConcurrentExecutor executor;
      private RequestTask task;
      private volatile boolean dead;
      
      public Client(ConcurrentExecutor executor, int port, String tag) {
         this.task = new RequestTask(this, port, tag);
         this.executor = executor;
      }
      
      public boolean isDead() {
         return dead;
      }
      
      public void run() {
         try {
            while(!dead) {
               executor.execute(task);
               Thread.sleep(100);
            }
         }catch(Exception e) {
            e.printStackTrace();
         }
      }
      
      public void close() {
         dead = true;
         executor.stop();
      }
   }
   
   private static class RequestTask implements Runnable {
      
      private Client client;
      private String tag;
      private int port;
      
      public RequestTask(Client client, int port, String tag) {
         this.client = client;
         this.port = port;
         this.tag = tag;
      }
      
      public void run() {
         try {
            if(!client.isDead()) {
               URL target = new URL("http://localhost:"+port+"/");
               URLConnection connection = target.openConnection();
               
               // set a timeout
               connection.setConnectTimeout(10000);
               connection.setReadTimeout(10000);
               
               InputStream stream = connection.getInputStream();
               StringBuilder builder = new StringBuilder();
               int octet = 0;
               
               while((octet = stream.read()) != -1) {
                  builder.append((char)octet);
               }
               stream.close();
               System.out.println(tag + " " + Thread.currentThread() + ": " + builder);
            }
         }catch(Exception e) {
            e.printStackTrace();
         } 
      }
   }
   
   private static class ServerCriteria {

      private Connection connection;
      private InetSocketAddress address;
      
      public ServerCriteria(Connection connection, InetSocketAddress address){
         this.connection = connection;
         this.address = address;
      }
      public Connection getConnection() {
         return connection;
      }
      public InetSocketAddress getAddress() {
         return address;
      }
   }
}