Search in sources :

Example 1 with AtomicLong

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

the class HTTP2CServerTest method testHTTP_2_0_DirectWithoutH2C.

@Test
public void testHTTP_2_0_DirectWithoutH2C() throws Exception {
    AtomicLong fills = new AtomicLong();
    // Remove "h2c", leaving only "http/1.1".
    connector.clearConnectionFactories();
    HttpConnectionFactory connectionFactory = new HttpConnectionFactory() {

        @Override
        public Connection newConnection(Connector connector, EndPoint endPoint) {
            HttpConnection connection = new HttpConnection(getHttpConfiguration(), connector, endPoint, getHttpCompliance(), isRecordHttpComplianceViolations()) {

                @Override
                public void onFillable() {
                    fills.incrementAndGet();
                    super.onFillable();
                }
            };
            return configure(connection, connector, endPoint);
        }
    };
    connector.addConnectionFactory(connectionFactory);
    connector.setDefaultProtocol(connectionFactory.getProtocol());
    // Now send a HTTP/2 direct request, which
    // will have the PRI * HTTP/2.0 preface.
    byteBufferPool = new MappedByteBufferPool();
    generator = new Generator(byteBufferPool);
    ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
    generator.control(lease, new PrefaceFrame());
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        OutputStream output = client.getOutputStream();
        for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
        // We sent a HTTP/2 preface, but the server has no "h2c" connection
        // factory so it does not know how to handle this request.
        InputStream input = client.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
        String responseLine = reader.readLine();
        Assert.assertThat(responseLine, Matchers.containsString(" 426 "));
        while (true) {
            if (reader.read() < 0)
                break;
        }
    }
    // Make sure we did not spin.
    Thread.sleep(1000);
    Assert.assertThat(fills.get(), Matchers.lessThan(5L));
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) InputStreamReader(java.io.InputStreamReader) HttpConnection(org.eclipse.jetty.server.HttpConnection) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) EndPoint(org.eclipse.jetty.io.EndPoint) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteBuffer(java.nio.ByteBuffer) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) AtomicLong(java.util.concurrent.atomic.AtomicLong) BufferedReader(java.io.BufferedReader) Socket(java.net.Socket) Generator(org.eclipse.jetty.http2.generator.Generator) Test(org.junit.Test)

Example 2 with AtomicLong

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

the class SchedulerTest method testLongCancel.

@Test
public void testLongCancel() throws Exception {
    final AtomicLong executed = new AtomicLong();
    Scheduler.Task task = _scheduler.schedule(new Runnable() {

        @Override
        public void run() {
            executed.set(System.currentTimeMillis());
        }
    }, 2000, TimeUnit.MILLISECONDS);
    Thread.sleep(1600);
    Assert.assertTrue(task.cancel());
    Thread.sleep(1000);
    Assert.assertEquals(0, executed.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 3 with AtomicLong

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

the class SchedulerTest method testExecution.

@Test
public void testExecution() throws Exception {
    final AtomicLong executed = new AtomicLong();
    long expected = System.currentTimeMillis() + 1000;
    Scheduler.Task task = _scheduler.schedule(new Runnable() {

        @Override
        public void run() {
            executed.set(System.currentTimeMillis());
        }
    }, 1000, TimeUnit.MILLISECONDS);
    Thread.sleep(1500);
    Assert.assertFalse(task.cancel());
    Assert.assertThat(executed.get(), Matchers.greaterThanOrEqualTo(expected));
    Assert.assertThat(expected - executed.get(), Matchers.lessThan(1000L));
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 4 with AtomicLong

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

the class SchedulerTest method testTwoExecution.

@Test
public void testTwoExecution() throws Exception {
    final AtomicLong executed = new AtomicLong();
    long expected = System.currentTimeMillis() + 1000;
    Scheduler.Task task = _scheduler.schedule(new Runnable() {

        @Override
        public void run() {
            executed.set(System.currentTimeMillis());
        }
    }, 1000, TimeUnit.MILLISECONDS);
    Thread.sleep(1500);
    Assert.assertFalse(task.cancel());
    Assert.assertThat(executed.get(), Matchers.greaterThanOrEqualTo(expected));
    Assert.assertThat(expected - executed.get(), Matchers.lessThan(1000L));
    final AtomicLong executed1 = new AtomicLong();
    long expected1 = System.currentTimeMillis() + 1000;
    Scheduler.Task task1 = _scheduler.schedule(new Runnable() {

        @Override
        public void run() {
            executed1.set(System.currentTimeMillis());
        }
    }, 1000, TimeUnit.MILLISECONDS);
    Thread.sleep(1500);
    Assert.assertFalse(task1.cancel());
    Assert.assertThat(executed1.get(), Matchers.greaterThanOrEqualTo(expected1));
    Assert.assertThat(expected1 - executed1.get(), Matchers.lessThan(1000L));
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 5 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project druid by druid-io.

the class KafkaLookupExtractorFactoryTest method testCacheKeyScramblesDifferentStarts.

@Test
public void testCacheKeyScramblesDifferentStarts() {
    final int n = 1000;
    final KafkaLookupExtractorFactory factory = new KafkaLookupExtractorFactory(cacheManager, TOPIC, DEFAULT_PROPERTIES);
    factory.getMapRef().set(ImmutableMap.<String, String>of());
    final AtomicLong events = factory.getDoubleEventCount();
    final List<byte[]> byteArrays = new ArrayList<>(n);
    for (int i = 0; i < n; ++i) {
        final LookupExtractor extractor = factory.get();
        final byte[] myKey = extractor.getCacheKey();
        // Not terribly efficient.. but who cares
        for (byte[] byteArray : byteArrays) {
            Assert.assertFalse(Arrays.equals(byteArray, myKey));
        }
        byteArrays.add(myKey);
        events.incrementAndGet();
    }
    Assert.assertEquals(n, byteArrays.size());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayList(java.util.ArrayList) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

AtomicLong (java.util.concurrent.atomic.AtomicLong)2292 Test (org.junit.Test)986 ArrayList (java.util.ArrayList)300 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)273 IOException (java.io.IOException)254 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)250 List (java.util.List)222 HashMap (java.util.HashMap)212 Map (java.util.Map)209 CountDownLatch (java.util.concurrent.CountDownLatch)185 AtomicReference (java.util.concurrent.atomic.AtomicReference)174 HashSet (java.util.HashSet)106 Arrays (java.util.Arrays)101 File (java.io.File)99 Test (org.junit.jupiter.api.Test)98 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)95 Set (java.util.Set)94 TimeUnit (java.util.concurrent.TimeUnit)91 Collections (java.util.Collections)88 Random (java.util.Random)85