Search in sources :

Example 6 with Flushable

use of java.io.Flushable in project j2objc by google.

the class FormatterTest method test_flush.

/**
     * java.util.Formatter#flush()
     */
public void test_flush() throws IOException {
    Formatter f = null;
    f = new Formatter(notExist);
    assertTrue(f instanceof Flushable);
    f.close();
    try {
        f.flush();
        fail("should throw FormatterClosedException");
    } catch (FormatterClosedException e) {
    // expected
    }
    f = new Formatter();
    // For destination that does not implement Flushable
    // No exception should be thrown
    f.flush();
}
Also used : Formatter(java.util.Formatter) FormatterClosedException(java.util.FormatterClosedException) Flushable(java.io.Flushable)

Example 7 with Flushable

use of java.io.Flushable in project neo4j by neo4j.

the class BatchingTransactionAppenderConcurrencyTest method setUp.

@Before
public void setUp() {
    class Channel extends InMemoryClosableChannel implements Flushable {

        @Override
        public Flushable prepareForFlush() {
            try {
                channelCommandQueue.put(ChannelCommand.emptyBufferIntoChannelAndClearIt);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return this;
        }

        @Override
        public void flush() throws IOException {
            try {
                forceSemaphore.release();
                channelCommandQueue.put(ChannelCommand.force);
            } catch (InterruptedException e) {
                throw new IOException(e);
            }
        }
    }
    when(logFile.getWriter()).thenReturn(new Channel());
}
Also used : IOException(java.io.IOException) Flushable(java.io.Flushable) Before(org.junit.Before)

Example 8 with Flushable

use of java.io.Flushable in project tomee by apache.

the class FlushableDataSourceHandler method invoke.

@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
    final CommonDataSource actualDelegate = delegate.get();
    if (Object.class == method.getDeclaringClass()) {
        if ("hashCode".equals(method.getName())) {
            return hashCode();
        }
        if ("toString".equals(method.getName())) {
            return "Flushable[" + actualDelegate.toString() + "]";
        }
    }
    if (Flushable.class == method.getDeclaringClass()) {
        final Lock l = lock.writeLock();
        l.lock();
        try {
            createANewDelegate();
            if (Flushable.class.isInstance(actualDelegate)) {
                // these sanity could be enhanced
                if (!Proxy.isProxyClass(actualDelegate.getClass()) || // reset implies flush so we need to check both
                (!FlushableDataSourceHandler.class.isInstance(Proxy.getInvocationHandler(actualDelegate)) && !ResettableDataSourceHandler.class.isInstance(Proxy.getInvocationHandler(actualDelegate)))) {
                    Flushable.class.cast(actualDelegate).flush();
                }
            }
        } finally {
            l.unlock();
        }
        return null;
    }
    final Lock l = lock.readLock();
    l.lock();
    try {
        return method.invoke(getDelegate(), args);
    } catch (final InvocationTargetException ite) {
        throw ite.getCause();
    } finally {
        l.unlock();
    }
}
Also used : Flushable(java.io.Flushable) CommonDataSource(javax.sql.CommonDataSource) InvocationTargetException(java.lang.reflect.InvocationTargetException) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Example 9 with Flushable

use of java.io.Flushable in project closure-templates by google.

the class RenderVisitorTest method testRenderFuture.

@Test
public void testRenderFuture() throws Exception {
    final StringBuilder progress = new StringBuilder();
    Flushable flushable = new Flushable() {

        @Override
        public void flush() {
            progress.append("flush;");
        }
    };
    String soyFileContent = "{namespace ns}\n" + "\n" + "/** @param boo @param foo @param goo */\n" + "{template .callerTemplate autoescape=\"deprecated-noncontextual\"}\n" + "  {call .calleeTemplate data=\"all\" /}\n" + "  {call .calleeTemplate data=\"$foo\" /}\n" + "  {call .calleeTemplate data=\"all\"}\n" + "    {param boo: $foo.boo /}\n" + "  {/call}\n" + "  {call .calleeTemplate data=\"all\"}\n" + "    {param boo: 'moo' /}\n" + "  {/call}\n" + "  {call .calleeTemplate data=\"$foo\"}\n" + "    {param boo}moo{/param}\n" + "  {/call}\n" + "  {call .calleeTemplate}\n" + "    {param boo}zoo{/param}\n" + "    {param goo: $foo.goo /}\n" + "  {/call}\n" + "{/template}\n" + "\n" + "/**\n" + " * @param boo\n" + " * @param goo\n" + " */\n" + "{template .calleeTemplate autoescape=\"deprecated-noncontextual\"}\n" + "  {$boo}{$ij.future}\n" + "  {for $n in $goo} {$n}{/for}{\\n}\n" + "{/template}\n";
    TemplateRegistry templateRegistry = SoyFileSetParserBuilder.forFileContents(soyFileContent).errorReporter(FAIL).parse().registry();
    SoyDict foo = SoyValueConverterUtility.newDict("boo", new TestFuture("foo", progress), "goo", SoyValueConverterUtility.newList(3, 2, 1));
    SoyDict data = SoyValueConverterUtility.newDict("boo", new TestFuture("boo", progress), "foo", foo, "goo", SoyValueConverterUtility.newList(1, 2, 3));
    SoyRecord testIj = SoyValueConverterUtility.newDict("future", new TestFuture("ij", progress));
    StringBuilder outputSb = new StringBuilder();
    CountingFlushableAppendable output = new CountingFlushableAppendable(outputSb, flushable);
    RenderVisitor rv = new RenderVisitor(new EvalVisitorFactoryImpl(), output, templateRegistry, data, testIj, Predicates.<String>alwaysFalse(), null, xidRenamingMap, cssRenamingMap, false);
    rv.exec(templateRegistry.getBasicTemplate("ns.callerTemplate"));
    String expectedOutput = "booij 1 2 3\n" + "fooij 3 2 1\n" + "fooij 1 2 3\n" + "mooij 1 2 3\n" + "mooij 3 2 1\n" + "zooij 3 2 1\n";
    assertThat(outputSb.toString()).isEqualTo(expectedOutput);
    assertThat(progress.toString()).isEqualTo("booflush;ijflush;foo");
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyRecord(com.google.template.soy.data.SoyRecord) SoyDict(com.google.template.soy.data.SoyDict) Flushable(java.io.Flushable) Test(org.junit.Test)

Example 10 with Flushable

use of java.io.Flushable in project neo4j by neo4j.

the class PageCacheTest method flushAndForceMustTolerateAsynchronousFileUnmapping.

@Test
void flushAndForceMustTolerateAsynchronousFileUnmapping() throws Exception {
    configureStandardPageCache();
    Path a = existingFile("a");
    Path b = existingFile("b");
    Path c = existingFile("c");
    BinaryLatch limiterStartLatch = new BinaryLatch();
    BinaryLatch limiterBlockLatch = new BinaryLatch();
    var ioController = new EmptyIOController() {

        @Override
        public void maybeLimitIO(int recentlyCompletedIOs, Flushable flushable, MajorFlushEvent flushEvent) {
            limiterStartLatch.release();
            limiterBlockLatch.await();
            super.maybeLimitIO(recentlyCompletedIOs, flushable, flushEvent);
        }
    };
    Future<?> flusher;
    try (PagedFile pfA = pageCache.map(a, filePageSize, DEFAULT_DATABASE_NAME, immutable.empty(), ioController);
        PagedFile pfB = pageCache.map(b, filePageSize, DEFAULT_DATABASE_NAME, immutable.empty(), ioController);
        PagedFile pfC = pageCache.map(c, filePageSize, DEFAULT_DATABASE_NAME, immutable.empty(), ioController)) {
        // Dirty a bunch of pages.
        try (PageCursor cursor = pfA.io(0, PF_SHARED_WRITE_LOCK, NULL)) {
            assertTrue(cursor.next());
        }
        try (PageCursor cursor = pfB.io(0, PF_SHARED_WRITE_LOCK, NULL)) {
            assertTrue(cursor.next());
        }
        try (PageCursor cursor = pfC.io(0, PF_SHARED_WRITE_LOCK, NULL)) {
            assertTrue(cursor.next());
        }
        flusher = executor.submit(() -> {
            pfA.flushAndForce();
            pfB.flushAndForce();
            pfC.flushAndForce();
            return null;
        });
        // Flusher is now stuck inside flushAndForce.
        limiterStartLatch.await();
    }
    // We should be able to unmap all the files.
    // And then when the flusher resumes again, it should not throw any exceptions from the asynchronously
    // closed files.
    limiterBlockLatch.release();
    // This must not throw.
    flusher.get();
}
Also used : Path(java.nio.file.Path) MajorFlushEvent(org.neo4j.io.pagecache.tracing.MajorFlushEvent) Flushable(java.io.Flushable) BinaryLatch(org.neo4j.util.concurrent.BinaryLatch) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Aggregations

Flushable (java.io.Flushable)12 Test (org.junit.Test)4 IOException (java.io.IOException)3 Test (org.junit.jupiter.api.Test)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Path (java.nio.file.Path)2 RepeatedTest (org.junit.jupiter.api.RepeatedTest)2 MajorFlushEvent (org.neo4j.io.pagecache.tracing.MajorFlushEvent)2 BinaryLatch (org.neo4j.util.concurrent.BinaryLatch)2 SoyDict (com.google.template.soy.data.SoyDict)1 SoyRecord (com.google.template.soy.data.SoyRecord)1 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)1 Closeable (java.io.Closeable)1 File (java.io.File)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 RemoteException (java.rmi.RemoteException)1 Formatter (java.util.Formatter)1 FormatterClosedException (java.util.FormatterClosedException)1 ExecutorService (java.util.concurrent.ExecutorService)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1