Search in sources :

Example 1 with ExecutionContext

use of org.spf4j.base.ExecutionContext in project spf4j by zolyfarkas.

the class Spf4jTestLogRunListenerSingleton method testFinished.

@Override
public synchronized void testFinished(final Description description) {
    LogCollection<ArrayDeque<LogRecord>> handler = collections.remove(description);
    try (LogCollection<ArrayDeque<LogRecord>> h = handler) {
        handleUncaughtExceptions(description, h.get());
    }
    ExecutionContext ctx = ctxts.remove(description);
    ExecutionContext currentThreadContext = ExecutionContexts.current();
    if (ctx == currentThreadContext) {
        ctx.close();
        List<AutoCloseable> closeables = (List<AutoCloseable>) ctx.get(AutoCloseable.class);
        if (closeables != null) {
            Exception ex = Closeables.closeAll(closeables);
            if (ex != null) {
                throw new IllegalStateException("cannot close " + closeables, ex);
            }
        }
    } else {
        throw new IllegalStateException("JUnit Threading model not as expected " + ctx + " != " + currentThreadContext);
    }
}
Also used : ExecutionContext(org.spf4j.base.ExecutionContext) List(java.util.List) ArrayDeque(java.util.ArrayDeque)

Example 2 with ExecutionContext

use of org.spf4j.base.ExecutionContext in project spf4j by zolyfarkas.

the class TestLoggers method addConfig.

private void addConfig(final String category, final LogHandler handler, final HandlerRegistration reg) {
    synchronized (sync) {
        config = config.add(category, handler);
        resetJulConfig();
        ExecutionContext ctx = ExecutionContexts.current();
        if (ctx != null) {
            ctx.compute(AutoCloseable.class, (Class<AutoCloseable> k, ArrayList<AutoCloseable> v) -> {
                if (v == null) {
                    ArrayList<AutoCloseable> res = new ArrayList();
                    res.add(reg);
                    return res;
                } else {
                    v.add(reg);
                    return v;
                }
            });
        }
    }
}
Also used : ExecutionContext(org.spf4j.base.ExecutionContext) ArrayList(java.util.ArrayList)

Example 3 with ExecutionContext

use of org.spf4j.base.ExecutionContext in project spf4j by zolyfarkas.

the class JdbcTemplate method transactOnConnection.

@SuppressFBWarnings("BED_BOGUS_EXCEPTION_DECLARATION")
public <R> R transactOnConnection(final HandlerNano<Connection, R, SQLException> handler, final long timeout, final TimeUnit tu) throws SQLException, InterruptedException {
    try (ExecutionContext ctx = ExecutionContexts.start(handler.toString(), timeout, tu)) {
        return (R) retryPolicy.call(new Callable() {

            @Override
            public R call() throws SQLException {
                try (Connection conn = dataSource.getConnection()) {
                    boolean autocomit = conn.getAutoCommit();
                    if (autocomit) {
                        conn.setAutoCommit(false);
                    }
                    try {
                        R result = handler.handle(conn, ctx.getDeadlineNanos());
                        conn.commit();
                        return result;
                    } catch (SQLException | RuntimeException ex) {
                        conn.rollback();
                        throw ex;
                    } finally {
                        if (autocomit) {
                            conn.setAutoCommit(true);
                        }
                    }
                }
            }
        }, SQLException.class, ctx.getDeadlineNanos());
    } catch (TimeoutException ex) {
        throw new SQLTimeoutException(ex);
    }
}
Also used : ExecutionContext(org.spf4j.base.ExecutionContext) SQLException(java.sql.SQLException) Connection(java.sql.Connection) SQLTimeoutException(java.sql.SQLTimeoutException) Callable(java.util.concurrent.Callable) SQLTimeoutException(java.sql.SQLTimeoutException) TimeoutException(java.util.concurrent.TimeoutException) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 4 with ExecutionContext

use of org.spf4j.base.ExecutionContext in project spf4j by zolyfarkas.

the class MonitorTest method main.

@SuppressFBWarnings("MDM_THREAD_YIELD")
public static void main(final String[] args) throws InterruptedException {
    stopped = false;
    try (ExecutionContext ctx = ExecutionContexts.start("main", 10, TimeUnit.MINUTES)) {
        List<Thread> threads = new ArrayList<Thread>(20);
        for (int i = 0; i < 20; i++) {
            Thread t;
            t = new Thread(new AbstractRunnable() {

                @Override
                public void doRun() throws InterruptedException {
                    try (ExecutionContext tctx = ExecutionContexts.start("testThread", ctx, 10, TimeUnit.MINUTES)) {
                        while (!stopped) {
                            double rnd = Math.random();
                            if (rnd < 0.33) {
                                doStuff1(rnd, 50);
                            } else if (rnd < 0.66) {
                                doStuff2(rnd);
                            } else {
                                doStuff3(rnd);
                            }
                        }
                    }
                }

                @SuppressFBWarnings("MDM_THREAD_YIELD")
                private double doStuff3(final double rnd) throws InterruptedException {
                    Thread.sleep(1);
                    if (rnd > 0.8) {
                        doStuff2(rnd);
                    }
                    return rnd * Math.pow(2, 10000);
                }

                @SuppressFBWarnings("MDM_THREAD_YIELD")
                private double doStuff2(final double prnd) throws InterruptedException {
                    double rnd = prnd;
                    Thread.sleep(1);
                    for (int j = 0; j < 10000; j++) {
                        rnd = rnd + rnd;
                    }
                    return rnd;
                }

                @SuppressFBWarnings("MDM_THREAD_YIELD")
                private void doStuff1(final double rnd, final int depth) throws InterruptedException {
                    if (depth <= 0) {
                        Thread.sleep(10);
                    } else {
                        doStuff1(rnd, depth - 1);
                    }
                }
            }, "Thread" + i);
            t.start();
            threads.add(t);
        }
        Thread.sleep(5000);
        stopped = true;
        for (Thread t : threads) {
            t.join(3000);
        }
    }
}
Also used : AbstractRunnable(org.spf4j.base.AbstractRunnable) ExecutionContext(org.spf4j.base.ExecutionContext) ArrayList(java.util.ArrayList) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 5 with ExecutionContext

use of org.spf4j.base.ExecutionContext in project spf4j by zolyfarkas.

the class Spf4jTestLogRunListenerSingleton method testStarted.

@Override
public void testStarted(final Description description) throws Exception {
    Test ta = description.getAnnotation(Test.class);
    ExecutionContext ctx;
    if (ta != null && ta.timeout() > 0) {
        ctx = ExecutionContexts.start(description.getDisplayName(), ta.timeout(), TimeUnit.MILLISECONDS);
    } else {
        ctx = ExecutionContexts.start(description.getDisplayName());
    }
    CollectTrobleshootingLogs ca = description.getAnnotation(CollectTrobleshootingLogs.class);
    Level mll = ca == null ? minLogLevel : ca.minLevel();
    boolean clp = ca == null ? collectPrinted : ca.collectPrinted();
    collections.put(description, TestLoggers.sys().collect(mll, maxDebugLogsCollected, clp));
    ctxts.put(description, ctx);
    super.testStarted(description);
}
Also used : CollectTrobleshootingLogs(org.spf4j.test.log.CollectTrobleshootingLogs) ExecutionContext(org.spf4j.base.ExecutionContext) Test(org.junit.Test) Level(org.spf4j.test.log.Level)

Aggregations

ExecutionContext (org.spf4j.base.ExecutionContext)9 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 ArrayList (java.util.ArrayList)2 Callable (java.util.concurrent.Callable)2 Test (org.junit.Test)2 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)1 UncheckedTimeoutException (com.google.common.util.concurrent.UncheckedTimeoutException)1 TMap (gnu.trove.map.TMap)1 THashMap (gnu.trove.map.hash.THashMap)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 SQLTimeoutException (java.sql.SQLTimeoutException)1 ArrayDeque (java.util.ArrayDeque)1 List (java.util.List)1 Map (java.util.Map)1 TimeoutException (java.util.concurrent.TimeoutException)1 Around (org.aspectj.lang.annotation.Around)1 AbstractRunnable (org.spf4j.base.AbstractRunnable)1 CollectTrobleshootingLogs (org.spf4j.test.log.CollectTrobleshootingLogs)1 Level (org.spf4j.test.log.Level)1