Search in sources :

Example 26 with CallableWithoutResult

use of org.apereo.portal.concurrency.CallableWithoutResult in project uPortal by Jasig.

the class JdbcAuthDaoTest method testConcurrentCreate.

@Test
public void testConcurrentCreate() throws UnknownHostException {
    final String service = "foo";
    // Create & Return
    final String token = this.execute(new Callable<String>() {

        @Override
        public String call() throws Exception {
            final String t = authDao.getAuthToken(service);
            assertEquals(1000, t.length());
            return t;
        }
    });
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            authDao.createToken(service);
        }
    });
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final String t = authDao.getAuthToken(service);
            assertEquals(token, t);
        }
    });
}
Also used : UnknownHostException(java.net.UnknownHostException) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) BasePortalJpaDaoTest(org.apereo.portal.test.BasePortalJpaDaoTest) Test(org.junit.Test)

Example 27 with CallableWithoutResult

use of org.apereo.portal.concurrency.CallableWithoutResult in project uPortal by Jasig.

the class JaxbPortalDataHandlerService method exportAllDataOfType.

@Override
public void exportAllDataOfType(Set<String> typeIds, File directory, BatchExportOptions options) {
    final Queue<ExportFuture<?>> exportFutures = new ConcurrentLinkedQueue<>();
    final boolean failOnError = options != null ? options.isFailOnError() : true;
    // Determine the parent directory to log to
    final File logDirectory = determineLogDirectory(options, "export");
    // Setup reporting file
    final File exportReport = new File(logDirectory, "data-export.txt");
    final PrintWriter reportWriter;
    try {
        reportWriter = new PrintWriter(new BufferedWriter(new FileWriter(exportReport)));
    } catch (IOException e) {
        throw new RuntimeException("Failed to create FileWriter for: " + exportReport, e);
    }
    try {
        for (final String typeId : typeIds) {
            final List<FutureHolder<?>> failedFutures = new LinkedList<>();
            final File typeDir = new File(directory, typeId);
            logger.info("Adding all data of type {} to export queue: {}", typeId, typeDir);
            reportWriter.println(typeId + "," + typeDir);
            final Iterable<? extends IPortalData> dataForType = this.getPortalData(typeId);
            for (final IPortalData data : dataForType) {
                final String dataId = data.getDataId();
                // Check for completed futures on every iteration, needed to fail as fast as
                // possible on an import exception
                final List<FutureHolder<?>> newFailed = waitForFutures(exportFutures, reportWriter, logDirectory, false);
                failedFutures.addAll(newFailed);
                final AtomicLong exportTime = new AtomicLong(-1);
                // Create export task
                Callable<Object> task = new CallableWithoutResult() {

                    @Override
                    protected void callWithoutResult() {
                        exportTime.set(System.nanoTime());
                        try {
                            exportData(typeId, dataId, typeDir);
                        } finally {
                            exportTime.set(System.nanoTime() - exportTime.get());
                        }
                    }
                };
                // Submit the export task
                final Future<?> exportFuture = this.importExportThreadPool.submit(task);
                // Add the future for tracking
                final ExportFuture futureHolder = new ExportFuture(exportFuture, typeId, dataId, exportTime);
                exportFutures.offer(futureHolder);
            }
            final List<FutureHolder<?>> newFailed = waitForFutures(exportFutures, reportWriter, logDirectory, true);
            failedFutures.addAll(newFailed);
            reportWriter.flush();
            if (failOnError && !failedFutures.isEmpty()) {
                throw new RuntimeException(failedFutures.size() + " " + typeId + " entities failed to export.\n" + "\tPer-entity exception logs and a full report can be found in " + // location clickable in some shells
                logDirectory.toURI());
            }
        }
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted while waiting for entities to export", e);
    } finally {
        IOUtils.closeQuietly(reportWriter);
    }
}
Also used : FileWriter(java.io.FileWriter) IOException(java.io.IOException) LinkedList(java.util.LinkedList) PeriodicFlushingBufferedWriter(org.apereo.portal.utils.PeriodicFlushingBufferedWriter) BufferedWriter(java.io.BufferedWriter) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) AtomicLong(java.util.concurrent.atomic.AtomicLong) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 28 with CallableWithoutResult

use of org.apereo.portal.concurrency.CallableWithoutResult in project uPortal by Jasig.

the class JpaPortalEventStoreTest method testStoreSingleEvents.

// Breaks on move to Gradlde
@Ignore
@Test
public void testStoreSingleEvents() throws Exception {
    final DateTime startDate = DateTime.now().minusDays(1);
    final DateTime endDate = DateTime.now().plusDays(1);
    final List<PortalEvent> originalEvents = generateEvents();
    execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            for (final PortalEvent event : originalEvents) {
                portalEventDao.storePortalEvent(event);
            }
        }
    });
    verifyGetEvents(originalEvents, startDate, endDate);
    verifyAggregateEvents(originalEvents, startDate, endDate);
    verifyAggregateEvents(Collections.<PortalEvent>emptyList(), startDate, endDate);
    deleteEvents(originalEvents, startDate, endDate);
    verifyGetEvents(Collections.<PortalEvent>emptyList(), startDate, endDate);
    verifyAggregateEvents(Collections.<PortalEvent>emptyList(), startDate, endDate);
}
Also used : DateTime(org.joda.time.DateTime) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) Ignore(org.junit.Ignore) BaseRawEventsJpaDaoTest(org.apereo.portal.test.BaseRawEventsJpaDaoTest) Test(org.junit.Test)

Example 29 with CallableWithoutResult

use of org.apereo.portal.concurrency.CallableWithoutResult in project uPortal by Jasig.

the class JpaPortalEventStoreTest method verifyAggregateEvents.

protected void verifyAggregateEvents(final List<PortalEvent> originalEvents, final DateTime startDate, final DateTime endDate) {
    execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            // Get all events
            final List<PortalEvent> portalEvents = new LinkedList<PortalEvent>();
            final AtomicReference<DateTime> nextStart = new AtomicReference<DateTime>(startDate);
            // aggregate all events, 5 at a time.
            final int loadSize = 10;
            int startSize;
            do {
                startSize = portalEvents.size();
                portalEventDao.aggregatePortalEvents(nextStart.get(), endDate, loadSize, new Function<PortalEvent, Boolean>() {

                    @Override
                    public Boolean apply(PortalEvent input) {
                        portalEvents.add(input);
                        nextStart.set(input.getTimestampAsDate());
                        return Boolean.TRUE;
                    }
                });
            } while (loadSize + startSize == portalEvents.size());
            assertEquals(originalEvents.size(), portalEvents.size());
            final Iterator<PortalEvent> originalEventItr = originalEvents.iterator();
            final Iterator<PortalEvent> eventItr = portalEvents.iterator();
            while (originalEventItr.hasNext()) {
                assertEquals(originalEventItr.next().getClass(), eventItr.next().getClass());
            }
        }
    });
}
Also used : Function(com.google.common.base.Function) Iterator(java.util.Iterator) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) List(java.util.List) AtomicReference(java.util.concurrent.atomic.AtomicReference) DateTime(org.joda.time.DateTime) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult)

Example 30 with CallableWithoutResult

use of org.apereo.portal.concurrency.CallableWithoutResult in project uPortal by Jasig.

the class JpaPortalEventStoreTest method testStoreBatchEvents.

// Breaks on move to Gradlde
@Ignore
@Test
public void testStoreBatchEvents() throws Exception {
    final DateTime startDate = DateTime.now().minusDays(1);
    final DateTime endDate = DateTime.now().plusDays(1);
    final List<PortalEvent> originalEvents = generateEvents();
    execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final DateTime oldestPortalEventTimestamp = portalEventDao.getOldestPortalEventTimestamp();
            assertNull(oldestPortalEventTimestamp);
            final DateTime newestPortalEventTimestamp = portalEventDao.getNewestPortalEventTimestamp();
            assertNull(newestPortalEventTimestamp);
        }
    });
    execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            portalEventDao.storePortalEvents(originalEvents);
        }
    });
    Collections.sort(originalEvents, new Comparator<PortalEvent>() {

        @Override
        public int compare(PortalEvent o1, PortalEvent o2) {
            return o1.getTimestampAsDate().compareTo(o2.getTimestampAsDate());
        }
    });
    execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final DateTime oldestPortalEventTimestamp = portalEventDao.getOldestPortalEventTimestamp();
            final DateTime newestPortalEventTimestamp = portalEventDao.getNewestPortalEventTimestamp();
            assertNotNull(oldestPortalEventTimestamp);
            assertNotNull(newestPortalEventTimestamp);
            assertEquals(originalEvents.get(0).getTimestampAsDate().getMillis(), oldestPortalEventTimestamp.getMillis());
            assertEquals(originalEvents.get(originalEvents.size() - 1).getTimestampAsDate().getMillis(), newestPortalEventTimestamp.getMillis());
        }
    });
    verifyGetEvents(originalEvents, startDate, endDate);
    verifyAggregateEvents(originalEvents, startDate, endDate);
    verifyAggregateEvents(Collections.<PortalEvent>emptyList(), startDate, endDate);
    deleteEvents(originalEvents, startDate, endDate);
    verifyGetEvents(Collections.<PortalEvent>emptyList(), startDate, endDate);
    verifyAggregateEvents(Collections.<PortalEvent>emptyList(), startDate, endDate);
}
Also used : DateTime(org.joda.time.DateTime) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) Ignore(org.junit.Ignore) BaseRawEventsJpaDaoTest(org.apereo.portal.test.BaseRawEventsJpaDaoTest) Test(org.junit.Test)

Aggregations

CallableWithoutResult (org.apereo.portal.concurrency.CallableWithoutResult)32 Test (org.junit.Test)28 BasePortalJpaDaoTest (org.apereo.portal.test.BasePortalJpaDaoTest)14 BaseAggrEventsJpaDaoTest (org.apereo.portal.test.BaseAggrEventsJpaDaoTest)12 DateTime (org.joda.time.DateTime)8 List (java.util.List)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 CompositeName (javax.naming.CompositeName)7 IEntityGroup (org.apereo.portal.groups.IEntityGroup)7 LinkedList (java.util.LinkedList)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 AggregatedGroupMapping (org.apereo.portal.events.aggr.groups.AggregatedGroupMapping)5 Ignore (org.junit.Ignore)5 Random (java.util.Random)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 MutableInt (org.apache.commons.lang.mutable.MutableInt)3 MutableObject (org.apache.commons.lang.mutable.MutableObject)3 FunctionWithoutResult (org.apereo.portal.concurrency.FunctionWithoutResult)3 File (java.io.File)2 FileWriter (java.io.FileWriter)2