Search in sources :

Example 71 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project jetty.project by eclipse.

the class HalfCloseTest method testCompleteClose.

@Test
public void testCompleteClose() throws Exception {
    Server server = new Server();
    ServerConnector connector = new ServerConnector(server, 1, 1);
    connector.setPort(0);
    connector.setIdleTimeout(5000);
    final AtomicInteger opened = new AtomicInteger(0);
    final CountDownLatch closed = new CountDownLatch(1);
    connector.addBean(new Connection.Listener() {

        @Override
        public void onOpened(Connection connection) {
            opened.incrementAndGet();
        }

        @Override
        public void onClosed(Connection connection) {
            closed.countDown();
        }
    });
    server.addConnector(connector);
    TestHandler handler = new TestHandler();
    server.setHandler(handler);
    server.start();
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        client.getOutputStream().write("GET / HTTP/1.0\r\n\r\n".getBytes());
        IO.toString(client.getInputStream());
        assertEquals(1, handler.getHandled());
        assertEquals(1, opened.get());
    }
    assertEquals(true, closed.await(1, TimeUnit.SECONDS));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Connection(org.eclipse.jetty.io.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) Socket(java.net.Socket) Test(org.junit.Test)

Example 72 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project jetty.project by eclipse.

the class HalfCloseTest method testAsyncClose.

@Test
public void testAsyncClose() throws Exception {
    Server server = new Server();
    ServerConnector connector = new ServerConnector(server, 1, 1);
    connector.setPort(0);
    connector.setIdleTimeout(5000);
    final AtomicInteger opened = new AtomicInteger(0);
    final CountDownLatch closed = new CountDownLatch(1);
    connector.addBean(new Connection.Listener() {

        @Override
        public void onOpened(Connection connection) {
            opened.incrementAndGet();
        }

        @Override
        public void onClosed(Connection connection) {
            closed.countDown();
        }
    });
    server.addConnector(connector);
    AsyncHandler handler = new AsyncHandler();
    server.setHandler(handler);
    server.start();
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        client.getOutputStream().write("GET / HTTP/1.0\r\n\r\n".getBytes());
        IO.toString(client.getInputStream());
        assertEquals(1, handler.getHandled());
        assertEquals(1, opened.get());
    }
    assertEquals(true, closed.await(1, TimeUnit.SECONDS));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Connection(org.eclipse.jetty.io.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) Socket(java.net.Socket) Test(org.junit.Test)

Example 73 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project jetty.project by eclipse.

the class SpringXmlConfigurationTest method testNewObject.

@Test
public void testNewObject() throws Exception {
    final String newDefaultValue = "NEW DEFAULT";
    TestConfiguration.VALUE = 71;
    URL url = SpringXmlConfigurationTest.class.getClassLoader().getResource(_configure);
    final AtomicInteger count = new AtomicInteger(0);
    XmlConfiguration configuration = new XmlConfiguration(url) {

        @Override
        public void initializeDefaults(Object object) {
            super.initializeDefaults(object);
            if (object instanceof TestConfiguration) {
                count.incrementAndGet();
                ((TestConfiguration) object).setTestString0(newDefaultValue);
                ((TestConfiguration) object).setTestString1("WILL BE OVERRIDDEN");
            }
        }
    };
    Map<String, String> properties = new HashMap<String, String>();
    properties.put("test", "xxx");
    TestConfiguration nested = new TestConfiguration();
    nested.setTestString0("nested");
    configuration.getIdMap().put("nested", nested);
    configuration.getProperties().putAll(properties);
    TestConfiguration tc = (TestConfiguration) configuration.configure();
    assertEquals(3, count.get());
    assertEquals(newDefaultValue, tc.getTestString0());
    assertEquals(-1, tc.getTestInt0());
    assertEquals("SetValue", tc.getTestString1());
    assertEquals(1, tc.getTestInt1());
    assertEquals(newDefaultValue, tc.getNested().getTestString0());
    assertEquals("nested", tc.getNested().getTestString1());
    assertEquals(newDefaultValue, tc.getNested().getNested().getTestString0());
    assertEquals("deep", tc.getNested().getNested().getTestString1());
    assertEquals("deep", ((TestConfiguration) configuration.getIdMap().get("nestedDeep")).getTestString1());
    assertEquals(2, ((TestConfiguration) configuration.getIdMap().get("nestedDeep")).getTestInt2());
    assertEquals("xxx", tc.getTestString2());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMap(java.util.HashMap) XmlConfiguration(org.eclipse.jetty.xml.XmlConfiguration) URL(java.net.URL) Test(org.junit.Test)

Example 74 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project jetty.project by eclipse.

the class ServerWriteThread method run.

@Override
public void run() {
    final AtomicInteger m = new AtomicInteger();
    try {
        while (m.get() < messageCount) {
            conn.write(new TextFrame().setPayload(message));
            m.incrementAndGet();
            if (slowness > 0) {
                TimeUnit.MILLISECONDS.sleep(slowness);
            }
        }
    } catch (InterruptedException | IOException e) {
        LOG.warn(e);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) IOException(java.io.IOException)

Example 75 with AtomicInteger

use of java.util.concurrent.atomic.AtomicInteger in project jetty.project by eclipse.

the class ClientWriteThread method run.

@Override
public void run() {
    final AtomicInteger m = new AtomicInteger();
    try {
        LOG.debug("Writing {} messages to connection {}", messageCount);
        LOG.debug("Artificial Slowness {} ms", slowness);
        Future<Void> lastMessage = null;
        RemoteEndpoint remote = session.getRemote();
        while (m.get() < messageCount) {
            lastMessage = remote.sendStringByFuture(message + "/" + m.get() + "/");
            m.incrementAndGet();
            if (slowness > 0) {
                TimeUnit.MILLISECONDS.sleep(slowness);
            }
        }
        if (remote.getBatchMode() == BatchMode.ON)
            remote.flush();
        // block on write of last message
        if (lastMessage != null)
            // block on write
            lastMessage.get(2, TimeUnit.MINUTES);
    } catch (Exception e) {
        LOG.warn(e);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7986 Test (org.junit.Test)3775 CountDownLatch (java.util.concurrent.CountDownLatch)1072 ArrayList (java.util.ArrayList)1018 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)849 List (java.util.List)740 IOException (java.io.IOException)719 AtomicReference (java.util.concurrent.atomic.AtomicReference)574 HashMap (java.util.HashMap)499 Map (java.util.Map)460 Test (org.testng.annotations.Test)419 File (java.io.File)337 ExecutorService (java.util.concurrent.ExecutorService)337 Test (org.junit.jupiter.api.Test)334 AtomicLong (java.util.concurrent.atomic.AtomicLong)329 TimeUnit (java.util.concurrent.TimeUnit)323 HashSet (java.util.HashSet)315 Arrays (java.util.Arrays)308 Set (java.util.Set)284 Collections (java.util.Collections)266