<commit_before>package com.neverwinterdp.server.http.pixel;

import static org.junit.Assert.assertEquals;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.neverwinterdp.netty.http.client.AsyncHttpClient;
import com.neverwinterdp.server.Server;
import com.neverwinterdp.server.shell.Shell;
import com.neverwinterdp.sparkngin.http.NullDevMessageForwarder;

/**
 * Tests the connection between PixelRouteHandler and Sparkngin
 * @author <NAME>
 */
public class HttpServerPixelRouteHandlerSparknginIntegrationUnitTest {
  static {
    System.setProperty("app.dir", "build/cluster") ;
    System.setProperty("log4j.configuration", "file:src/main/resources/log4j.properties") ;
  }
  
  static protected Server httpServer, sparknginServer ;
  static Shell shell ; 
  static int httpport = 8181;
  static int sparkport = 7181;
  
  @BeforeClass
  static public void setup() throws Exception {
    httpServer = Server.create("-Pserver.name=webserver", "-Pserver.roles=webserver") ;
    sparknginServer = Server.create("-Pserver.name=sparkngin", "-Pserver.roles=sparkngin") ;
    shell = new Shell() ;
    shell.getShellContext().connect();
    
    shell.exec(
      "module install " + 
      "  -Psparkngin:forwarder-class=" + NullDevMessageForwarder.class.getName() +
      "  -Psparkngin:http-listen-port="+Integer.toString(sparkport) +
      "  --member-role sparkngin --autostart --module Sparkngin"
    ) ;
    
    shell.exec(
        "module install " +
        " -Phttp:port="+Integer.toString(httpport) +
        " -Phttp:route.names=pixel" +
        " -Phttp:route.pixel.handler=com.neverwinterdp.server.http.pixel.PixelRouteHandler" +
        " -Phttp:route.pixel.path=/pixel" +
        " -Phttp:route.pixel.sparkngin.connect=http://127.0.0.1:"+Integer.toString(sparkport) +
        " --member-name webserver --autostart --module Http"
    ) ;
    Thread.sleep(1000);
  }

  @AfterClass
  static public void teardown() throws Exception {
    shell.close(); 
    httpServer.destroy();
    sparknginServer.destroy() ;
  }
  
  @Test
  public void testContentReturnedMatchesContentServed100Requests() throws Exception {
    PixelCheckResponseHandler handler = new PixelCheckResponseHandler();
    AsyncHttpClient client = new AsyncHttpClient ("127.0.0.1", httpport, handler) ;
    int testCount = 100;
    for(int i = 0; i < testCount; i++) {
      client.get("/pixel");
    }
    Thread.sleep(1000);
    
    //Make sure testCount responses have been received
    assertEquals(testCount, handler.getCount());
    
    //Make sure no failure was caught since ResponseHandler doesn't have error handling<commit_msg>Removing loop<commit_after>package com.neverwinterdp.server.http.pixel;

import static org.junit.Assert.assertEquals;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.neverwinterdp.netty.http.client.AsyncHttpClient;
import com.neverwinterdp.server.Server;
import com.neverwinterdp.server.shell.Shell;
import com.neverwinterdp.sparkngin.http.NullDevMessageForwarder;

/**
 * Tests the connection between PixelRouteHandler and Sparkngin
 * @author <NAME>
 */
public class HttpServerPixelRouteHandlerSparknginIntegrationUnitTest {
  static {
    System.setProperty("app.dir", "build/cluster") ;
    System.setProperty("log4j.configuration", "file:src/main/resources/log4j.properties") ;
  }
  
  static protected Server httpServer, sparknginServer ;
  static Shell shell ; 
  static int httpport = 8181;
  static int sparkport = 7181;
  
  @BeforeClass
  static public void setup() throws Exception {
    httpServer = Server.create("-Pserver.name=webserver", "-Pserver.roles=webserver") ;
    sparknginServer = Server.create("-Pserver.name=sparkngin", "-Pserver.roles=sparkngin") ;
    shell = new Shell() ;
    shell.getShellContext().connect();
    
    shell.exec(
      "module install " + 
      "  -Psparkngin:forwarder-class=" + NullDevMessageForwarder.class.getName() +
      "  -Psparkngin:http-listen-port="+Integer.toString(sparkport) +
      "  --member-role sparkngin --autostart --module Sparkngin"
    ) ;
    
    shell.exec(
        "module install " +
        " -Phttp:port="+Integer.toString(httpport) +
        " -Phttp:route.names=pixel" +
        " -Phttp:route.pixel.handler=com.neverwinterdp.server.http.pixel.PixelRouteHandler" +
        " -Phttp:route.pixel.path=/pixel" +
        " -Phttp:route.pixel.sparkngin.connect=http://127.0.0.1:"+Integer.toString(sparkport) +
        " --member-name webserver --autostart --module Http"
    ) ;
    Thread.sleep(1000);
  }

  @AfterClass
  static public void teardown() throws Exception {
    shell.close(); 
    httpServer.destroy();
    sparknginServer.destroy() ;
  }
  
  @Test
  public void testContentReturnedMatchesContentServed() throws Exception {
    PixelCheckResponseHandler handler = new PixelCheckResponseHandler();
    AsyncHttpClient client = new AsyncHttpClient ("127.0.0.1", httpport, handler) ;
    client.get("/pixel");
    Thread.sleep(1000);
    
    //Make sure testCount responses have been received
    assertEquals(1, handler.getCount());
    
    //Make sure no failure was caught since ResponseHandler doesn't have error handling
{'fraction_non_alphanumeric': 0.12196520741731982, 'fraction_numerical': 0.012999426495889888, 'mean_word_length': 3.6465364120781527, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4829622', 'n_tokens_mistral': 1668, 'n_tokens_neox': 1606, 'n_words': 297}