Search in sources :

Example 1 with LoggerResource

use of org.apache.ignite.resources.LoggerResource in project ignite by apache.

the class IgfsSecondaryFileSystemInjectionSelfTest method testInjectPrimaryByField.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings({ "UnusedDeclaration" })
public void testInjectPrimaryByField() throws Exception {
    secondary = new TestBaseSecondaryFsMock() {

        @FileSystemResource
        private IgfsImpl igfs;

        @LoggerResource
        private IgniteLogger log;

        @IgniteInstanceResource
        private Ignite ig;

        @Override
        boolean checkInjection(Ignite ignite, IgniteFileSystem primary) {
            return igfs == primary && log instanceof IgniteLogger && ig == ignite;
        }
    };
    Ignite ig = startGrid(0);
    IgniteFileSystem igfs = ig.fileSystem(IGFS_NAME);
    assert secondary.checkInjection(ig, igfs);
}
Also used : LoggerResource(org.apache.ignite.resources.LoggerResource) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) Ignite(org.apache.ignite.Ignite) IgniteFileSystem(org.apache.ignite.IgniteFileSystem) FileSystemResource(org.apache.ignite.resources.FileSystemResource) IgniteLogger(org.apache.ignite.IgniteLogger)

Example 2 with LoggerResource

use of org.apache.ignite.resources.LoggerResource in project ignite by apache.

the class IgfsSecondaryFileSystemInjectionSelfTest method testInjectPrimaryByMethods.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings({ "UnusedDeclaration" })
public void testInjectPrimaryByMethods() throws Exception {
    secondary = new TestBaseSecondaryFsMock() {

        /**
         * Ignite instance.
         */
        private Ignite ig;

        /**
         * IGFS instance.
         */
        private IgniteFileSystem igfs;

        /**
         * Logger injected flag
         */
        private boolean logSet;

        /**
         * @param igfs Primary IGFS.
         */
        @FileSystemResource
        void setPrimaryIgfs(IgfsImpl igfs) {
            this.igfs = igfs;
        }

        /**
         * @param log Ignite logger.
         */
        @LoggerResource
        void setIgLogger(IgniteLogger log) {
            logSet = log instanceof IgniteLogger;
        }

        /**
         * @param ig Ignite instance.
         */
        @IgniteInstanceResource
        void setIgniteInst(Ignite ig) {
            this.ig = ig;
        }

        @Override
        boolean checkInjection(Ignite ignite, IgniteFileSystem primary) {
            return ignite == ig && primary == igfs && logSet;
        }
    };
    Ignite ig = startGrid(0);
    IgniteFileSystem igfs = ig.fileSystem(IGFS_NAME);
    assert secondary.checkInjection(ig, igfs);
}
Also used : LoggerResource(org.apache.ignite.resources.LoggerResource) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) Ignite(org.apache.ignite.Ignite) IgniteFileSystem(org.apache.ignite.IgniteFileSystem) FileSystemResource(org.apache.ignite.resources.FileSystemResource) IgniteLogger(org.apache.ignite.IgniteLogger)

Example 3 with LoggerResource

use of org.apache.ignite.resources.LoggerResource in project ignite by apache.

the class GridLoggerInjectionSelfTest method testClosureMethod.

/**
 * Test that closure gets right log category injected on all nodes using method injection.
 *
 * @throws Exception If failed.
 */
@Test
public void testClosureMethod() throws Exception {
    Ignite ignite = grid(0);
    ignite.compute().call(new IgniteCallable<Object>() {

        @LoggerResource(categoryClass = GridLoggerInjectionSelfTest.class)
        private void log(IgniteLogger log) {
            if (log instanceof GridLoggerProxy) {
                Object category = U.field(log, "ctgr");
                assertTrue("Logger created for the wrong category.", category.toString().contains(GridLoggerInjectionSelfTest.class.getName()));
            } else
                fail("This test should be run with proxy logger.");
        }

        @Override
        public Object call() throws Exception {
            return null;
        }
    });
}
Also used : LoggerResource(org.apache.ignite.resources.LoggerResource) GridLoggerProxy(org.apache.ignite.internal.GridLoggerProxy) Ignite(org.apache.ignite.Ignite) IgniteLogger(org.apache.ignite.IgniteLogger) IOException(java.io.IOException) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 4 with LoggerResource

use of org.apache.ignite.resources.LoggerResource in project ignite by apache.

the class GridLoggerInjectionSelfTest method testStringCategory.

/**
 * Test that closure gets right log category injected through {@link org.apache.ignite.resources.LoggerResource#categoryName()}.
 *
 * @throws Exception If failed.
 */
@Test
public void testStringCategory() throws Exception {
    Ignite ignite = grid(0);
    ignite.compute().call(new IgniteCallable<Object>() {

        @LoggerResource(categoryName = "GridLoggerInjectionSelfTest")
        private void log(IgniteLogger log) {
            if (log instanceof GridLoggerProxy) {
                Object category = U.field(log, "ctgr");
                assertTrue("Logger created for the wrong category.", "GridLoggerInjectionSelfTest".equals(category.toString()));
            } else
                fail("This test should be run with proxy logger.");
        }

        @Override
        public Object call() throws Exception {
            return null;
        }
    });
}
Also used : LoggerResource(org.apache.ignite.resources.LoggerResource) GridLoggerProxy(org.apache.ignite.internal.GridLoggerProxy) Ignite(org.apache.ignite.Ignite) IgniteLogger(org.apache.ignite.IgniteLogger) IOException(java.io.IOException) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 5 with LoggerResource

use of org.apache.ignite.resources.LoggerResource in project ignite by apache.

the class IgniteSemaphoreAbstractSelfTest method checkSemaphore.

/**
 * @throws Exception If failed.
 */
private void checkSemaphore() throws Exception {
    // Test API.
    checkAcquire();
    checkRelease();
    checkFailoverSafe();
    // Test main functionality.
    final IgniteSemaphore semaphore1 = grid(0).semaphore("semaphore", -2, true, true);
    assertEquals(-2, semaphore1.availablePermits());
    IgniteFuture<Object> fut = grid(0).compute().callAsync(new IgniteCallable<Object>() {

        @IgniteInstanceResource
        private Ignite ignite;

        @LoggerResource
        private IgniteLogger log;

        @Nullable
        @Override
        public Object call() throws Exception {
            // Test semaphore in multiple threads on each node.
            IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

                @Nullable
                @Override
                public Object call() throws Exception {
                    IgniteSemaphore semaphore = ignite.semaphore("semaphore", -2, true, true);
                    assert semaphore != null && semaphore.availablePermits() == -2;
                    log.info("Thread is going to wait on semaphore: " + Thread.currentThread().getName() + ", node = " + ignite.cluster().localNode() + ", sem = " + semaphore);
                    assert semaphore.tryAcquire(1, 1, MINUTES);
                    log.info("Thread is again runnable: " + Thread.currentThread().getName() + ", node = " + ignite.cluster().localNode() + ", sem = " + semaphore);
                    semaphore.release();
                    return null;
                }
            }, 5, "test-thread");
            fut.get();
            return null;
        }
    });
    Thread.sleep(3000);
    semaphore1.release(2);
    assert semaphore1.availablePermits() == 0;
    semaphore1.release(1);
    // Ensure there are no hangs.
    fut.get();
    // Test operations on removed semaphore.
    semaphore1.close();
    checkRemovedSemaphore(semaphore1);
}
Also used : LoggerResource(org.apache.ignite.resources.LoggerResource) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) ExpectedException(org.junit.rules.ExpectedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteSemaphore(org.apache.ignite.IgniteSemaphore) Ignite(org.apache.ignite.Ignite) IgniteLogger(org.apache.ignite.IgniteLogger) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Ignite (org.apache.ignite.Ignite)10 IgniteLogger (org.apache.ignite.IgniteLogger)10 LoggerResource (org.apache.ignite.resources.LoggerResource)10 IOException (java.io.IOException)6 IgniteInstanceResource (org.apache.ignite.resources.IgniteInstanceResource)6 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)4 Test (org.junit.Test)4 Callable (java.util.concurrent.Callable)3 GridLoggerProxy (org.apache.ignite.internal.GridLoggerProxy)3 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)3 IgniteCallable (org.apache.ignite.lang.IgniteCallable)3 Nullable (org.jetbrains.annotations.Nullable)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 IgniteException (org.apache.ignite.IgniteException)2 IgniteFileSystem (org.apache.ignite.IgniteFileSystem)2 FileSystemResource (org.apache.ignite.resources.FileSystemResource)2 ExpectedException (org.junit.rules.ExpectedException)2 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 CountDownLatch (java.util.concurrent.CountDownLatch)1