Search in sources :

Example 31 with Consumer

use of java.util.function.Consumer in project buck by facebook.

the class RunShTestAndRecordResultStep method execute.

@Override
public StepExecutionResult execute(ExecutionContext context) throws IOException, InterruptedException {
    TestResultSummary summary;
    if (context.getPlatform() == Platform.WINDOWS) {
        // Ignore sh_test on Windows.
        summary = new TestResultSummary(getShortName(), "sh_test", /* type */
        ResultType.SUCCESS, /* duration*/
        0, /* message */
        "sh_test ignored on Windows", /* stacktrace */
        null, /* stdout */
        null, /* stderr */
        null);
    } else {
        ShellStep test = new ShellStep(filesystem.getRootPath()) {

            boolean timedOut = false;

            @Override
            public String getShortName() {
                return pathToShellScript.toString();
            }

            @Override
            protected ImmutableList<String> getShellCommandInternal(ExecutionContext context) {
                return ImmutableList.<String>builder().add(pathToShellScript.toString()).addAll(args).build();
            }

            @Override
            public ImmutableMap<String, String> getEnvironmentVariables(ExecutionContext context) {
                return ImmutableMap.<String, String>builder().put("NO_BUCKD", "1").putAll(env).build();
            }

            @Override
            protected boolean shouldPrintStderr(Verbosity verbosity) {
                // Do not stream this output because we want to capture it.
                return false;
            }

            @Override
            public StepExecutionResult execute(ExecutionContext context) throws IOException, InterruptedException {
                StepExecutionResult executionResult = super.execute(context);
                if (timedOut) {
                    throw new HumanReadableException("Timed out running test: " + testCaseName + ", with exitCode: " + executionResult.getExitCode());
                }
                return executionResult;
            }

            @Override
            protected Optional<Consumer<Process>> getTimeoutHandler(final ExecutionContext context) {
                return Optional.of(process -> timedOut = true);
            }

            @Override
            protected Optional<Long> getTimeout() {
                return testRuleTimeoutMs;
            }

            @Override
            protected boolean shouldPrintStdout(Verbosity verbosity) {
                // Do not stream this output because we want to capture it.
                return false;
            }
        };
        StepExecutionResult executionResult = test.execute(context);
        // Write test result.
        boolean isSuccess = executionResult.isSuccess();
        summary = new TestResultSummary(getShortName(), "sh_test", /* type */
        isSuccess ? ResultType.SUCCESS : ResultType.FAILURE, test.getDuration(), /* message */
        null, /* stacktrace */
        null, test.getStdout(), test.getStderr());
    }
    ObjectMapper mapper = context.getObjectMapper();
    try (OutputStream outputStream = filesystem.newFileOutputStream(pathToTestResultFile)) {
        mapper.writeValue(outputStream, summary);
    }
    // should be zero.
    return StepExecutionResult.SUCCESS;
}
Also used : ExecutionContext(com.facebook.buck.step.ExecutionContext) StepExecutionResult(com.facebook.buck.step.StepExecutionResult) Consumer(java.util.function.Consumer) HumanReadableException(com.facebook.buck.util.HumanReadableException) OutputStream(java.io.OutputStream) TestResultSummary(com.facebook.buck.test.TestResultSummary) Verbosity(com.facebook.buck.util.Verbosity) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 32 with Consumer

use of java.util.function.Consumer in project elasticsearch by elastic.

the class AsyncIOProcessorTests method testRandomFail.

public void testRandomFail() throws InterruptedException {
    AtomicInteger received = new AtomicInteger(0);
    AtomicInteger failed = new AtomicInteger(0);
    AtomicInteger actualFailed = new AtomicInteger(0);
    AsyncIOProcessor<Object> processor = new AsyncIOProcessor<Object>(logger, scaledRandomIntBetween(1, 2024)) {

        @Override
        protected void write(List<Tuple<Object, Consumer<Exception>>> candidates) throws IOException {
            received.addAndGet(candidates.size());
            if (randomBoolean()) {
                failed.addAndGet(candidates.size());
                if (randomBoolean()) {
                    throw new IOException();
                } else {
                    throw new RuntimeException();
                }
            }
        }
    };
    Semaphore semaphore = new Semaphore(Integer.MAX_VALUE);
    final int count = randomIntBetween(1000, 20000);
    Thread[] thread = new Thread[randomIntBetween(3, 10)];
    CountDownLatch latch = new CountDownLatch(thread.length);
    for (int i = 0; i < thread.length; i++) {
        thread[i] = new Thread() {

            @Override
            public void run() {
                try {
                    latch.countDown();
                    latch.await();
                    for (int i = 0; i < count; i++) {
                        semaphore.acquire();
                        processor.put(new Object(), (ex) -> {
                            if (ex != null) {
                                actualFailed.incrementAndGet();
                            }
                            semaphore.release();
                        });
                    }
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }

            ;
        };
        thread[i].start();
    }
    for (int i = 0; i < thread.length; i++) {
        thread[i].join();
    }
    assertTrue(semaphore.tryAcquire(Integer.MAX_VALUE, 10, TimeUnit.SECONDS));
    assertEquals(count * thread.length, received.get());
    assertEquals(actualFailed.get(), failed.get());
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Semaphore(java.util.concurrent.Semaphore) IOException(java.io.IOException) ESTestCase(org.elasticsearch.test.ESTestCase) Tuple(org.elasticsearch.common.collect.Tuple) IOException(java.io.IOException) Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) Consumer(java.util.function.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List)

Example 33 with Consumer

use of java.util.function.Consumer in project neo4j by neo4j.

the class DelayedBufferTest method shouldHandleTheWholeWorkloadShebang.

@Test
public void shouldHandleTheWholeWorkloadShebang() throws Throwable {
    // GIVEN
    final int size = 1_000;
    final long bufferTime = 3;
    VerifyingConsumer consumer = new VerifyingConsumer(size);
    final Clock clock = Clocks.systemClock();
    Supplier<Long> chunkThreshold = clock::millis;
    Predicate<Long> safeThreshold = time -> clock.millis() - bufferTime >= time;
    final DelayedBuffer<Long> buffer = new DelayedBuffer<>(chunkThreshold, safeThreshold, 10, consumer);
    MaintenanceThread maintenance = new MaintenanceThread(buffer, 5);
    Race adders = new Race();
    final int numberOfAdders = 20;
    final byte[] offeredIds = new byte[size];
    for (int i = 0; i < numberOfAdders; i++) {
        final int finalI = i;
        adders.addContestant(new Runnable() {

            @Override
            public void run() {
                for (int j = 0; j < size; j++) {
                    if (j % numberOfAdders == finalI) {
                        buffer.offer(j);
                        offeredIds[j] = 1;
                        parkNanos(MILLISECONDS.toNanos(current().nextInt(2)));
                    }
                }
            }
        });
    }
    // WHEN (multi-threadded) offering of ids
    adders.go();
    // ... ensuring the test is sane itself (did we really offer all these IDs?)
    for (int i = 0; i < size; i++) {
        assertEquals("ID " + i, (byte) 1, offeredIds[i]);
    }
    maintenance.halt();
    buffer.close();
    // THEN
    consumer.assertHaveOnlySeenRange(0, size - 1);
}
Also used : Suppliers.singleton(org.neo4j.function.Suppliers.singleton) ThreadLocalRandom.current(java.util.concurrent.ThreadLocalRandom.current) Predicate(java.util.function.Predicate) Predicates(org.neo4j.function.Predicates) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) LockSupport.parkNanos(java.util.concurrent.locks.LockSupport.parkNanos) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Supplier(java.util.function.Supplier) LockSupport(java.util.concurrent.locks.LockSupport) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Utils.safeCastLongToInt(org.neo4j.unsafe.impl.batchimport.Utils.safeCastLongToInt) Assert.assertFalse(org.junit.Assert.assertFalse) Clock(java.time.Clock) Assert.fail(org.junit.Assert.fail) Clocks(org.neo4j.time.Clocks) Race(org.neo4j.test.Race) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Clock(java.time.Clock) Race(org.neo4j.test.Race) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 34 with Consumer

use of java.util.function.Consumer in project neo4j by neo4j.

the class RecoveryTest method shouldSeeThatACleanDatabaseShouldNotRequireRecovery.

@Test
public void shouldSeeThatACleanDatabaseShouldNotRequireRecovery() throws Exception {
    final PhysicalLogFiles logFiles = new PhysicalLogFiles(directory.directory(), "log", fileSystemRule.get());
    File file = logFiles.getLogFileForVersion(logVersion);
    writeSomeData(file, new Visitor<Pair<LogEntryWriter, Consumer<LogPositionMarker>>, IOException>() {

        @Override
        public boolean visit(Pair<LogEntryWriter, Consumer<LogPositionMarker>> pair) throws IOException {
            LogEntryWriter writer = pair.first();
            Consumer<LogPositionMarker> consumer = pair.other();
            LogPositionMarker marker = new LogPositionMarker();
            // last committed tx
            consumer.accept(marker);
            writer.writeStartEntry(0, 1, 2L, 3L, new byte[0]);
            writer.writeCommitEntry(4L, 5L);
            // check point
            consumer.accept(marker);
            writer.writeCheckPointEntry(marker.newPosition());
            return true;
        }
    });
    LifeSupport life = new LifeSupport();
    Recovery.Monitor monitor = mock(Recovery.Monitor.class);
    try {
        StorageEngine storageEngine = mock(StorageEngine.class);
        final LogEntryReader<ReadableClosablePositionAwareChannel> reader = new VersionAwareLogEntryReader<>();
        LatestCheckPointFinder finder = new LatestCheckPointFinder(logFiles, fileSystemRule.get(), reader);
        TransactionMetadataCache metadataCache = new TransactionMetadataCache(100);
        LogHeaderCache logHeaderCache = new LogHeaderCache(10);
        LogFile logFile = life.add(new PhysicalLogFile(fileSystemRule.get(), logFiles, 50, () -> transactionIdStore.getLastCommittedTransactionId(), logVersionRepository, mock(PhysicalLogFile.Monitor.class), logHeaderCache));
        LogicalTransactionStore txStore = new PhysicalLogicalTransactionStore(logFile, metadataCache, reader);
        life.add(new Recovery(new DefaultRecoverySPI(storageEngine, logFiles, fileSystemRule.get(), logVersionRepository, finder, transactionIdStore, txStore, NO_MONITOR) {

            @Override
            public Visitor<CommittedTransactionRepresentation, Exception> startRecovery() {
                fail("Recovery should not be required");
                // <-- to satisfy the compiler
                return null;
            }
        }, monitor));
        life.start();
        verifyZeroInteractions(monitor);
    } finally {
        life.shutdown();
    }
}
Also used : DefaultRecoverySPI(org.neo4j.kernel.recovery.DefaultRecoverySPI) PhysicalLogicalTransactionStore(org.neo4j.kernel.impl.transaction.log.PhysicalLogicalTransactionStore) LogicalTransactionStore(org.neo4j.kernel.impl.transaction.log.LogicalTransactionStore) StorageEngine(org.neo4j.storageengine.api.StorageEngine) Recovery(org.neo4j.kernel.recovery.Recovery) PhysicalLogFiles(org.neo4j.kernel.impl.transaction.log.PhysicalLogFiles) LogPositionMarker(org.neo4j.kernel.impl.transaction.log.LogPositionMarker) ReadableClosablePositionAwareChannel(org.neo4j.kernel.impl.transaction.log.ReadableClosablePositionAwareChannel) LogFile(org.neo4j.kernel.impl.transaction.log.LogFile) PhysicalLogFile(org.neo4j.kernel.impl.transaction.log.PhysicalLogFile) Consumer(java.util.function.Consumer) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) VersionAwareLogEntryReader(org.neo4j.kernel.impl.transaction.log.entry.VersionAwareLogEntryReader) LogEntryWriter(org.neo4j.kernel.impl.transaction.log.entry.LogEntryWriter) Pair(org.neo4j.helpers.collection.Pair) PhysicalLogicalTransactionStore(org.neo4j.kernel.impl.transaction.log.PhysicalLogicalTransactionStore) LatestCheckPointFinder(org.neo4j.kernel.recovery.LatestCheckPointFinder) CommittedTransactionRepresentation(org.neo4j.kernel.impl.transaction.CommittedTransactionRepresentation) TransactionMetadataCache(org.neo4j.kernel.impl.transaction.log.TransactionMetadataCache) IOException(java.io.IOException) IOException(java.io.IOException) LogFile(org.neo4j.kernel.impl.transaction.log.LogFile) PhysicalLogFile(org.neo4j.kernel.impl.transaction.log.PhysicalLogFile) File(java.io.File) LogHeaderCache(org.neo4j.kernel.impl.transaction.log.LogHeaderCache) PhysicalLogFile(org.neo4j.kernel.impl.transaction.log.PhysicalLogFile) Test(org.junit.Test)

Example 35 with Consumer

use of java.util.function.Consumer in project che by eclipse.

the class ProjectTreeTracker method getDeleteOperation.

private Consumer<String> getDeleteOperation(String endpointId) {
    return it -> {
        timers.add(it);
        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {
                if (timers.contains(it)) {
                    timers.remove(it);
                    ProjectTreeStateUpdateDto params = newDto(ProjectTreeStateUpdateDto.class).withPath(it).withType(DELETED);
                    transmitter.transmitOneToNone(endpointId, OUTGOING_METHOD, params);
                }
            }
        }, 1_000L);
    };
}
Also used : ProjectTreeTrackingOperationDto(org.eclipse.che.api.project.shared.dto.event.ProjectTreeTrackingOperationDto) RequestTransmitter(org.eclipse.che.api.core.jsonrpc.RequestTransmitter) ProjectTreeStateUpdateDto(org.eclipse.che.api.project.shared.dto.event.ProjectTreeStateUpdateDto) HashMap(java.util.HashMap) Timer(java.util.Timer) Singleton(javax.inject.Singleton) CREATED(org.eclipse.che.api.project.shared.dto.event.FileWatcherEventType.CREATED) Inject(javax.inject.Inject) FileWatcherManager(org.eclipse.che.api.vfs.watcher.FileWatcherManager) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) TimerTask(java.util.TimerTask) Collectors.toSet(java.util.stream.Collectors.toSet) Logger(org.slf4j.Logger) Predicate(java.util.function.Predicate) Type(org.eclipse.che.api.project.shared.dto.event.ProjectTreeTrackingOperationDto.Type) Sets.newConcurrentHashSet(com.google.common.collect.Sets.newConcurrentHashSet) DtoFactory.newDto(org.eclipse.che.dto.server.DtoFactory.newDto) Set(java.util.Set) Consumer(java.util.function.Consumer) RequestHandlerConfigurator(org.eclipse.che.api.core.jsonrpc.RequestHandlerConfigurator) Entry(java.util.Map.Entry) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) DELETED(org.eclipse.che.api.project.shared.dto.event.FileWatcherEventType.DELETED) EMPTY_CONSUMER(org.eclipse.che.api.vfs.watcher.FileWatcherManager.EMPTY_CONSUMER) Timer(java.util.Timer) TimerTask(java.util.TimerTask) ProjectTreeStateUpdateDto(org.eclipse.che.api.project.shared.dto.event.ProjectTreeStateUpdateDto)

Aggregations

Consumer (java.util.function.Consumer)193 List (java.util.List)81 Test (org.junit.Test)55 IOException (java.io.IOException)53 ArrayList (java.util.ArrayList)52 Map (java.util.Map)49 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)46 LoggerFactory (org.slf4j.LoggerFactory)41 Logger (org.slf4j.Logger)40 HashMap (java.util.HashMap)36 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)32 File (java.io.File)31 Collections (java.util.Collections)27 Set (java.util.Set)25 Collectors (java.util.stream.Collectors)25 TimeUnit (java.util.concurrent.TimeUnit)24 AtomicReference (java.util.concurrent.atomic.AtomicReference)24 CountDownLatch (java.util.concurrent.CountDownLatch)23 Arrays (java.util.Arrays)21 Supplier (java.util.function.Supplier)21