/simple/src/test/java/org/simpleframework/http/core/StopTest.java

https://gitlab.com/UnderSampled/aard2-build · Java · 176 lines · 144 code · 31 blank · 1 comment · 3 complexity · da015f9082aac674251d02fbafb75c2d MD5 · raw file

  1. package org.simpleframework.http.core;
  2. import java.io.Closeable;
  3. import java.io.InputStream;
  4. import java.io.PrintStream;
  5. import java.lang.management.ManagementFactory;
  6. import java.lang.management.ThreadMXBean;
  7. import java.net.InetSocketAddress;
  8. import java.net.URL;
  9. import java.net.URLConnection;
  10. import java.util.Date;
  11. import junit.framework.TestCase;
  12. import org.simpleframework.http.Request;
  13. import org.simpleframework.http.Response;
  14. import org.simpleframework.transport.connect.Connection;
  15. import org.simpleframework.transport.connect.SocketConnection;
  16. import org.simpleframework.util.thread.ConcurrentExecutor;
  17. public class StopTest extends TestCase {
  18. private static final int ITERATIONS = 20;
  19. public void testStop() throws Exception {
  20. ThreadDumper dumper = new ThreadDumper();
  21. dumper.start();
  22. dumper.waitUntilStarted();
  23. ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
  24. int initialThreads = threadBean.getThreadCount();
  25. for(int i = 0; i < ITERATIONS; i++) {
  26. try {
  27. ServerCriteria criteria = createServer();
  28. InetSocketAddress address = criteria.getAddress();
  29. Connection connection = criteria.getConnection();
  30. Client client = createClient(address, String.format("[%s of %s]", i, ITERATIONS));
  31. Thread.sleep(2000); // allow some requests to execute
  32. connection.close();
  33. Thread.sleep(100); // ensure client keeps executing
  34. client.close();
  35. Thread.sleep(1000); // wait for threads to terminate
  36. }catch(Exception e) {
  37. e.printStackTrace();
  38. }
  39. assertEquals(initialThreads, threadBean.getThreadCount());
  40. }
  41. dumper.kill();
  42. }
  43. public static Client createClient(InetSocketAddress address, String tag) throws Exception {
  44. ConcurrentExecutor executor = new ConcurrentExecutor(Runnable.class, 20);
  45. int port = address.getPort();
  46. Client client = new Client(executor, port, tag);
  47. client.start();
  48. return client;
  49. }
  50. public static ServerCriteria createServer() throws Exception {
  51. Container container = new Container() {
  52. public void handle(Request request, Response response) {
  53. try {
  54. PrintStream out = response.getPrintStream();
  55. response.setValue("Content-Type", "text/plain");
  56. response.setValue("Connection", "close");
  57. out.print("TEST " + new Date());
  58. response.close();
  59. }catch(Exception e) {
  60. e.printStackTrace();
  61. try {
  62. response.close();
  63. }catch(Exception ex) {
  64. ex.printStackTrace();
  65. }
  66. }
  67. }
  68. };
  69. ContainerServer server = new ContainerServer(container);
  70. Connection connection = new SocketConnection(server);
  71. InetSocketAddress address = (InetSocketAddress)connection.connect(null); // ephemeral port
  72. return new ServerCriteria(connection, address);
  73. }
  74. private static class Client extends Thread implements Closeable {
  75. private ConcurrentExecutor executor;
  76. private RequestTask task;
  77. private volatile boolean dead;
  78. public Client(ConcurrentExecutor executor, int port, String tag) {
  79. this.task = new RequestTask(this, port, tag);
  80. this.executor = executor;
  81. }
  82. public boolean isDead() {
  83. return dead;
  84. }
  85. public void run() {
  86. try {
  87. while(!dead) {
  88. executor.execute(task);
  89. Thread.sleep(100);
  90. }
  91. }catch(Exception e) {
  92. e.printStackTrace();
  93. }
  94. }
  95. public void close() {
  96. dead = true;
  97. executor.stop();
  98. }
  99. }
  100. private static class RequestTask implements Runnable {
  101. private Client client;
  102. private String tag;
  103. private int port;
  104. public RequestTask(Client client, int port, String tag) {
  105. this.client = client;
  106. this.port = port;
  107. this.tag = tag;
  108. }
  109. public void run() {
  110. try {
  111. if(!client.isDead()) {
  112. URL target = new URL("http://localhost:"+port+"/");
  113. URLConnection connection = target.openConnection();
  114. // set a timeout
  115. connection.setConnectTimeout(10000);
  116. connection.setReadTimeout(10000);
  117. InputStream stream = connection.getInputStream();
  118. StringBuilder builder = new StringBuilder();
  119. int octet = 0;
  120. while((octet = stream.read()) != -1) {
  121. builder.append((char)octet);
  122. }
  123. stream.close();
  124. System.out.println(tag + " " + Thread.currentThread() + ": " + builder);
  125. }
  126. }catch(Exception e) {
  127. e.printStackTrace();
  128. }
  129. }
  130. }
  131. private static class ServerCriteria {
  132. private Connection connection;
  133. private InetSocketAddress address;
  134. public ServerCriteria(Connection connection, InetSocketAddress address){
  135. this.connection = connection;
  136. this.address = address;
  137. }
  138. public Connection getConnection() {
  139. return connection;
  140. }
  141. public InetSocketAddress getAddress() {
  142. return address;
  143. }
  144. }
  145. }