Example #1
0
  private void startThreadedServer(
      final InetSocketAddress serverAddress, final ReadStrategy readStrategy) throws Exception {

    final AtomicBoolean readyToAccept = new AtomicBoolean(false);
    final Runnable runner =
        new Runnable() {
          @Override
          public void run() {
            // startServer();
            try {
              runTestServer(serverAddress, readStrategy, readyToAccept);
            } catch (final Exception e) {
              e.printStackTrace();
            }
          }
        };

    final Thread t = new Thread(runner, "### starter");
    t.setDaemon(true);
    t.start();

    synchronized (readyToAccept) {
      if (!readyToAccept.get()) {
        readyToAccept.wait(4000);
      }
    }
    assertTrue("Not ready to accept?", readyToAccept.get());
    Thread.yield();
    Thread.yield();
    Thread.yield();
    Thread.yield();
  }
  // http://jira.qos.ch/browse/LBCLASSIC-221
  @Test
  public void bufferShouldBeResetBetweenMessages() throws Exception {
    buildSMTPAppender("bufferShouldBeResetBetweenMessages", SYNCHRONOUS);
    smtpAppender.setLayout(buildPatternLayout(DEFAULT_PATTERN));
    smtpAppender.start();
    logger.addAppender(smtpAppender);
    String msg0 = "hello zero";
    logger.debug(msg0);
    logger.error("error zero");

    String msg1 = "hello one";
    logger.debug(msg1);
    logger.error("error one");

    Thread.yield();
    int oldCount = 0;
    int expectedEmailCount = oldCount + 2;
    waitForServerToReceiveEmails(expectedEmailCount);

    MimeMessage[] mma = greenMailServer.getReceivedMessages();
    assertNotNull(mma);
    assertEquals(expectedEmailCount, mma.length);

    MimeMessage mm0 = mma[oldCount];
    MimeMultipart content0 = (MimeMultipart) mm0.getContent();
    String body0 = GreenMailUtil.getBody(content0.getBodyPart(0));

    MimeMessage mm1 = mma[oldCount + 1];
    MimeMultipart content1 = (MimeMultipart) mm1.getContent();
    String body1 = GreenMailUtil.getBody(content1.getBodyPart(0));
    // second body should not contain content from first message
    assertFalse(body1.contains(msg0));
  }
Example #3
0
  @Test
  public void testDualConnections() throws Exception {
    SailConnection con2 = sail.getConnection();
    try {
      assertEquals(0, countAllElements());
      con.begin();
      con.addStatement(painter, RDF.TYPE, RDFS.CLASS);
      con.addStatement(painting, RDF.TYPE, RDFS.CLASS);
      con.addStatement(picasso, RDF.TYPE, painter, context1);
      con.addStatement(guernica, RDF.TYPE, painting, context1);
      con.commit();
      assertEquals(4, countAllElements());
      con2.begin();
      con2.addStatement(RDF.NIL, RDF.TYPE, RDF.LIST);
      String query = "SELECT S, P, O FROM {S} P {O}";
      ParsedTupleQuery tupleQuery =
          QueryParserUtil.parseTupleQuery(QueryLanguage.SERQL, query, null);
      assertEquals(
          5,
          countElements(
              con2.evaluate(
                  tupleQuery.getTupleExpr(), null, EmptyBindingSet.getInstance(), false)));
      Runnable clearer =
          new Runnable() {

            public void run() {
              try {
                con.begin();
                con.clear();
                con.commit();
              } catch (SailException e) {
                throw new RuntimeException(e);
              }
            }
          };
      Thread thread = new Thread(clearer);
      thread.start();
      Thread.yield();
      Thread.yield();
      con2.commit();
      thread.join();
    } finally {
      con2.close();
    }
  }
 @Test
 public void canEcho() throws Exception {
   server.start();
   Thread.yield();
   Socket s = new Socket("localhost", port);
   OutputStream os = s.getOutputStream();
   os.write("echo\n".getBytes());
   String response = new BufferedReader(new InputStreamReader(s.getInputStream())).readLine();
   assertEquals("echo", response);
 }
 // this test fails intermittently on Jenkins.
 @Test
 public void testMultipleTo() throws Exception {
   buildSMTPAppender("testMultipleTo", SYNCHRONOUS);
   smtpAppender.setLayout(buildPatternLayout(DEFAULT_PATTERN));
   // buildSMTPAppender() already added one destination address
   smtpAppender.addTo("Test <*****@*****.**>, [email protected]");
   smtpAppender.start();
   logger.addAppender(smtpAppender);
   logger.debug("testMultipleTo hello");
   logger.error("testMultipleTo en error", new Exception("an exception"));
   Thread.yield();
   int expectedEmailCount = 3;
   waitForServerToReceiveEmails(expectedEmailCount);
   MimeMessage[] mma = greenMailServer.getReceivedMessages();
   assertNotNull(mma);
   assertEquals(expectedEmailCount, mma.length);
 }
  @Test
  public void multipleEchos() throws Exception {
    server.start();
    Thread.yield();

    Socket s1 = new Socket("localhost", port);
    Socket s2 = new Socket("localhost", port);

    s1.getOutputStream().write("echo1\n".getBytes());
    s2.getOutputStream().write("echo2\n".getBytes());

    String response1 = new BufferedReader(new InputStreamReader(s1.getInputStream())).readLine();
    String response2 = new BufferedReader(new InputStreamReader(s2.getInputStream())).readLine();

    assertEquals("echo1", response1);
    assertEquals("echo2", response2);
  }
 private void testSendRequests(TestWorkAt workAt, String operation, JobQueue jobQueue) {
   Message iMessage =
       new Message(
           null,
           true,
           "oid",
           __workAt_td,
           __workAt_td.getMethodDescription(operation),
           true,
           null,
           false,
           null,
           null);
   for (int i = 0; i < TestWorkAt.MESSAGES; ++i) {
     Thread.yield(); // force scheduling
     jobQueue.putJob(new Job(workAt, __iReceiver, iMessage), new Object());
   }
 }
  @Test
  public void multiLineSubjectTruncatedAtFirstNewLine() throws Exception {
    String line1 = "line 1 of subject";
    String subject = line1 + "\nline 2 of subject\n";
    buildSMTPAppender(subject, ASYNCHRONOUS);

    smtpAppender.setLayout(buildPatternLayout(DEFAULT_PATTERN));
    smtpAppender.start();
    logger.addAppender(smtpAppender);
    logger.debug("hello");
    logger.error("en error", new Exception("an exception"));

    Thread.yield();
    waitUntilEmailIsSent();
    waitForServerToReceiveEmails(1);

    MimeMessage[] mma = greenMailServer.getReceivedMessages();
    assertEquals(1, mma.length);
    assertEquals(line1, mma[0].getSubject());
  }
  /**
   * Runs the same model many times and ensures it is not creating many threads which would finish
   * blocking the JVM
   */
  @Test
  public void testModelUsageOfThreads() {

    // first runs after which the reference will be taken
    final int HEATING_COUNT = 2;
    final int TEST_COUNT = 10;

    // heading period
    for (int i = 0; i < HEATING_COUNT; i++) runModel();

    // take reference
    final int activeThreads = Thread.activeCount();
    final Set<Thread> activeThreadsSet = collectCurrentThreads();

    // run many
    for (int i = 0; i < TEST_COUNT; i++) {
      System.err.println("exec " + i + " threads " + Thread.activeCount());
      runModel();
    }

    if (Thread.activeCount() > activeThreads) {
      double nbPerIteration = Thread.activeCount() / TEST_COUNT;
      Set<Thread> threadsNow = collectCurrentThreads();
      threadsNow.removeAll(activeThreadsSet);
      for (Thread t : threadsNow) {
        System.err.println("extra thread: " + t);
        t.stop();
      }
      Thread.yield();
      System.out.println("did cleaning help ? " + Thread.activeCount() + " / " + activeThreads);
      threadsNow = collectCurrentThreads();
      threadsNow.removeAll(activeThreadsSet);
      for (Thread t : threadsNow) {
        System.out.println("extra thread: " + t);
      }
      fail(
          "about "
              + nbPerIteration
              + " novel threads are created at each run; this cannot be used in production");
    }
  }
Example #10
0
 private void throwException() {
   Thread.yield();
   throw new IllegalStateException();
 }