Search in sources :

Example 11 with Random

use of java.util.Random in project camel by apache.

the class JmxInstrumentationWithConnectorTest method setUp.

@Override
protected void setUp() throws Exception {
    sleepForConnection = 3000;
    registryPort = 30000 + new Random().nextInt(10000);
    log.info("Using port " + registryPort);
    url = "service:jmx:rmi:///jndi/rmi://localhost:" + registryPort + "/jmxrmi/camel";
    // need to explicit set it to false to use non-platform mbs
    System.setProperty(JmxSystemPropertyKeys.USE_PLATFORM_MBS, "false");
    System.setProperty(JmxSystemPropertyKeys.CREATE_CONNECTOR, "true");
    System.setProperty(JmxSystemPropertyKeys.REGISTRY_PORT, "" + registryPort);
    super.setUp();
}
Also used : Random(java.util.Random)

Example 12 with Random

use of java.util.Random in project camel by apache.

the class PipelineConcurrentTest method testConcurrentPipeline.

public void testConcurrentPipeline() throws Exception {
    int total = 200;
    final int group = total / 20;
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(total);
    ExecutorService executor = Executors.newFixedThreadPool(20);
    for (int i = 0; i < 20; i++) {
        final int threadCount = i;
        executor.execute(new Runnable() {

            public void run() {
                int start = threadCount * group;
                for (int i = 0; i < group; i++) {
                    try {
                        // do some random sleep to simulate spread in user activity
                        Thread.sleep(new Random().nextInt(10));
                    } catch (InterruptedException e) {
                    // ignore
                    }
                    template.sendBody(uri, "" + (start + i));
                }
            }
        });
    }
    mock.assertIsSatisfied();
    mock.expectsNoDuplicates(body());
    executor.shutdown();
}
Also used : Random(java.util.Random) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) ExecutorService(java.util.concurrent.ExecutorService) MockEndpoint(org.apache.camel.component.mock.MockEndpoint)

Example 13 with Random

use of java.util.Random in project camel by apache.

the class FileUtil method createNewTempDir.

/**
     * Creates a new temporary directory in the <tt>java.io.tmpdir</tt> directory.
     */
@Deprecated
private static File createNewTempDir() {
    String s = System.getProperty("java.io.tmpdir");
    File checkExists = new File(s);
    if (!checkExists.exists()) {
        throw new RuntimeException("The directory " + checkExists.getAbsolutePath() + " does not exist, please set java.io.tempdir" + " to an existing directory");
    }
    if (!checkExists.canWrite()) {
        throw new RuntimeException("The directory " + checkExists.getAbsolutePath() + " is not writable, please set java.io.tempdir" + " to a writable directory");
    }
    // create a sub folder with a random number
    Random ran = new Random();
    int x = ran.nextInt(1000000);
    File f = new File(s, "camel-tmp-" + x);
    int count = 0;
    // Let us just try 100 times to avoid the infinite loop
    while (!f.mkdir()) {
        count++;
        if (count >= 100) {
            throw new RuntimeException("Camel cannot a temp directory from" + checkExists.getAbsolutePath() + " 100 times , please set java.io.tempdir" + " to a writable directory");
        }
        x = ran.nextInt(1000000);
        f = new File(s, "camel-tmp-" + x);
    }
    return f;
}
Also used : Random(java.util.Random) File(java.io.File)

Example 14 with Random

use of java.util.Random in project camel by apache.

the class AbstractGoogleCalendarTestSupport method createTestCalendar.

@Before
public void createTestCalendar() {
    Calendar calendar = new Calendar();
    Random rand = new Random();
    calendar.setSummary("camel-calendar-" + rand.nextInt());
    calendar.setTimeZone("America/St_Johns");
    this.calendar = requestBody("google-calendar://calendars/insert?inBody=content", calendar);
}
Also used : Random(java.util.Random) Calendar(com.google.api.services.calendar.model.Calendar) Before(org.junit.Before)

Example 15 with Random

use of java.util.Random in project flink by apache.

the class LargeRecordsTest method testHandleMixedLargeRecords.

@Test
public void testHandleMixedLargeRecords() {
    try {
        final int NUM_RECORDS = 99;
        final int SEGMENT_SIZE = 32 * 1024;
        final RecordSerializer<SerializationTestType> serializer = new SpanningRecordSerializer<SerializationTestType>();
        final RecordDeserializer<SerializationTestType> deserializer = new AdaptiveSpanningRecordDeserializer<SerializationTestType>();
        final Buffer buffer = new Buffer(MemorySegmentFactory.allocateUnpooledSegment(SEGMENT_SIZE), mock(BufferRecycler.class));
        List<SerializationTestType> originalRecords = new ArrayList<SerializationTestType>((NUM_RECORDS + 1) / 2);
        List<SerializationTestType> deserializedRecords = new ArrayList<SerializationTestType>((NUM_RECORDS + 1) / 2);
        LargeObjectType genLarge = new LargeObjectType();
        Random rnd = new Random();
        for (int i = 0; i < NUM_RECORDS; i++) {
            if (i % 2 == 0) {
                originalRecords.add(new IntType(42));
                deserializedRecords.add(new IntType());
            } else {
                originalRecords.add(genLarge.getRandom(rnd));
                deserializedRecords.add(new LargeObjectType());
            }
        }
        // -------------------------------------------------------------------------------------------------------------
        serializer.setNextBuffer(buffer);
        int numRecordsDeserialized = 0;
        for (SerializationTestType record : originalRecords) {
            // serialize record
            if (serializer.addRecord(record).isFullBuffer()) {
                // buffer is full => move to deserializer
                deserializer.setNextMemorySegment(serializer.getCurrentBuffer().getMemorySegment(), SEGMENT_SIZE);
                // deserialize records, as many complete as there are
                while (numRecordsDeserialized < deserializedRecords.size()) {
                    SerializationTestType next = deserializedRecords.get(numRecordsDeserialized);
                    if (deserializer.getNextRecord(next).isFullRecord()) {
                        assertEquals(originalRecords.get(numRecordsDeserialized), next);
                        numRecordsDeserialized++;
                    } else {
                        break;
                    }
                }
                // move buffers as long as necessary (for long records)
                while (serializer.setNextBuffer(buffer).isFullBuffer()) {
                    deserializer.setNextMemorySegment(serializer.getCurrentBuffer().getMemorySegment(), SEGMENT_SIZE);
                }
                // deserialize records, as many as there are in the last buffer
                while (numRecordsDeserialized < deserializedRecords.size()) {
                    SerializationTestType next = deserializedRecords.get(numRecordsDeserialized);
                    if (deserializer.getNextRecord(next).isFullRecord()) {
                        assertEquals(originalRecords.get(numRecordsDeserialized), next);
                        numRecordsDeserialized++;
                    } else {
                        break;
                    }
                }
            }
        }
        // move the last (incomplete buffer)
        Buffer last = serializer.getCurrentBuffer();
        deserializer.setNextMemorySegment(last.getMemorySegment(), last.getSize());
        serializer.clear();
        // deserialize records, as many as there are in the last buffer
        while (numRecordsDeserialized < deserializedRecords.size()) {
            SerializationTestType next = deserializedRecords.get(numRecordsDeserialized);
            assertTrue(deserializer.getNextRecord(next).isFullRecord());
            assertEquals(originalRecords.get(numRecordsDeserialized), next);
            numRecordsDeserialized++;
        }
        // might be that the last big records has not yet been fully moved, and a small one is missing
        assertFalse(serializer.hasData());
        assertFalse(deserializer.hasUnfinishedData());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : Buffer(org.apache.flink.runtime.io.network.buffer.Buffer) SpillingAdaptiveSpanningRecordDeserializer(org.apache.flink.runtime.io.network.api.serialization.SpillingAdaptiveSpanningRecordDeserializer) AdaptiveSpanningRecordDeserializer(org.apache.flink.runtime.io.network.api.serialization.AdaptiveSpanningRecordDeserializer) ArrayList(java.util.ArrayList) SpanningRecordSerializer(org.apache.flink.runtime.io.network.api.serialization.SpanningRecordSerializer) IntType(org.apache.flink.runtime.io.network.api.serialization.types.IntType) SerializationTestType(org.apache.flink.runtime.io.network.api.serialization.types.SerializationTestType) Random(java.util.Random) LargeObjectType(org.apache.flink.runtime.io.network.serialization.types.LargeObjectType) BufferRecycler(org.apache.flink.runtime.io.network.buffer.BufferRecycler) Test(org.junit.Test)

Aggregations

Random (java.util.Random)4728 Test (org.junit.Test)1273 ArrayList (java.util.ArrayList)602 IOException (java.io.IOException)313 HashMap (java.util.HashMap)242 File (java.io.File)209 List (java.util.List)154 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)151 ByteArrayInputStream (java.io.ByteArrayInputStream)134 HashSet (java.util.HashSet)129 ByteBuffer (java.nio.ByteBuffer)123 Test (org.testng.annotations.Test)121 Path (org.apache.hadoop.fs.Path)116 Map (java.util.Map)106 QuickTest (com.hazelcast.test.annotation.QuickTest)99 ParallelTest (com.hazelcast.test.annotation.ParallelTest)94 CountDownLatch (java.util.concurrent.CountDownLatch)93 Configuration (org.apache.hadoop.conf.Configuration)88 ByteArrayOutputStream (java.io.ByteArrayOutputStream)79 Before (org.junit.Before)78