Search in sources :

Example 46 with Cache

use of org.apache.geode.cache.Cache in project geode by apache.

the class QueueCommandsDUnitTest method testCreateUpdatesSharedConfig.

/**
   * Asserts that creating async event queues correctly updates the shared configuration.
   */
// GEODE-1976
@Category(FlakyTest.class)
@Test
public void testCreateUpdatesSharedConfig() throws IOException {
    disconnectAllFromDS();
    final int[] ports = AvailablePortHelper.getRandomAvailableTCPPorts(2);
    jmxPort = ports[0];
    httpPort = ports[1];
    try {
        jmxHost = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException ignore) {
        jmxHost = "localhost";
    }
    final String queueName = "testAsyncEventQueueQueue";
    final String groupName = "testAsyncEventQueueSharedConfigGroup";
    final Properties locatorProps = new Properties();
    locatorProps.setProperty(NAME, "Locator");
    locatorProps.setProperty(MCAST_PORT, "0");
    locatorProps.setProperty(LOG_LEVEL, "fine");
    locatorProps.setProperty(ENABLE_CLUSTER_CONFIGURATION, "true");
    locatorProps.setProperty(JMX_MANAGER, "true");
    locatorProps.setProperty(JMX_MANAGER_START, "true");
    locatorProps.setProperty(JMX_MANAGER_BIND_ADDRESS, String.valueOf(jmxHost));
    locatorProps.setProperty(JMX_MANAGER_PORT, String.valueOf(jmxPort));
    locatorProps.setProperty(HTTP_SERVICE_PORT, String.valueOf(httpPort));
    // Start the Locator and wait for shared configuration to be available
    final int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
    Host.getHost(0).getVM(0).invoke(new SerializableRunnable() {

        @Override
        public void run() {
            final File locatorLogFile = new File("locator-" + locatorPort + ".log");
            try {
                final InternalLocator locator = (InternalLocator) Locator.startLocatorAndDS(locatorPort, locatorLogFile, null, locatorProps);
                WaitCriterion wc = new WaitCriterion() {

                    @Override
                    public boolean done() {
                        return locator.isSharedConfigurationRunning();
                    }

                    @Override
                    public String description() {
                        return "Waiting for shared configuration to be started";
                    }
                };
                waitForCriterion(wc, 5000, 500, true);
            } catch (IOException ioex) {
                fail("Unable to create a locator with a shared configuration");
            }
        }
    });
    connect(jmxHost, jmxPort, httpPort, getDefaultShell());
    // Create a cache in VM 1
    VM vm = Host.getHost(0).getVM(1);
    vm.invoke(new SerializableRunnable() {

        @Override
        public void run() {
            Properties localProps = new Properties();
            localProps.setProperty(MCAST_PORT, "0");
            localProps.setProperty(LOCATORS, "localhost[" + locatorPort + "]");
            localProps.setProperty(GROUPS, groupName);
            getSystem(localProps);
            assertNotNull(getCache());
        }
    });
    // Deploy a JAR file with an AsyncEventListener that can be instantiated on each server
    final File jarFile = new File(new File(".").getAbsolutePath(), "QueueCommandsDUnit.jar");
    QueueCommandsDUnitTest.this.filesToBeDeleted.add(jarFile.getAbsolutePath());
    ClassBuilder classBuilder = new ClassBuilder();
    byte[] jarBytes = classBuilder.createJarFromClassContent("com/qcdunit/QueueCommandsDUnitTestListener", "package com.qcdunit;" + "import java.util.List; import java.util.Properties;" + "import org.apache.geode.internal.cache.xmlcache.Declarable2; import org.apache.geode.cache.asyncqueue.AsyncEvent;" + "import org.apache.geode.cache.asyncqueue.AsyncEventListener;" + "public class QueueCommandsDUnitTestListener implements Declarable2, AsyncEventListener {" + "Properties props;" + "public boolean processEvents(List<AsyncEvent> events) { return true; }" + "public void close() {}" + "public void init(final Properties props) {this.props = props;}" + "public Properties getConfig() {return this.props;}}");
    writeJarBytesToFile(jarFile, jarBytes);
    CommandResult cmdResult = executeCommand("deploy --jar=QueueCommandsDUnit.jar");
    assertEquals(Result.Status.OK, cmdResult.getStatus());
    // Test creating the queue
    CommandStringBuilder commandStringBuilder = new CommandStringBuilder(CliStrings.CREATE_ASYNC_EVENT_QUEUE);
    commandStringBuilder.addOption(CliStrings.CREATE_ASYNC_EVENT_QUEUE__ID, queueName);
    commandStringBuilder.addOption(CliStrings.CREATE_ASYNC_EVENT_QUEUE__GROUP, groupName);
    commandStringBuilder.addOption(CliStrings.CREATE_ASYNC_EVENT_QUEUE__LISTENER, "com.qcdunit.QueueCommandsDUnitTestListener");
    cmdResult = executeCommand(commandStringBuilder.toString());
    assertEquals(Result.Status.OK, cmdResult.getStatus());
    // Make sure the queue exists in the shared config
    Host.getHost(0).getVM(0).invoke(new SerializableRunnable() {

        @Override
        public void run() {
            ClusterConfigurationService sharedConfig = ((InternalLocator) Locator.getLocator()).getSharedConfiguration();
            String xmlFromConfig;
            try {
                xmlFromConfig = sharedConfig.getConfiguration(groupName).getCacheXmlContent();
                assertTrue(xmlFromConfig.contains(queueName));
            } catch (Exception e) {
                fail("Error occurred in cluster configuration service", e);
            }
        }
    });
    // Close cache in the vm1 and restart it to get the shared configuration
    vm = Host.getHost(0).getVM(1);
    vm.invoke(new SerializableRunnable() {

        @Override
        public void run() {
            Cache cache = getCache();
            assertNotNull(cache);
            cache.close();
            assertTrue(cache.isClosed());
            Properties localProps = new Properties();
            localProps.setProperty(MCAST_PORT, "0");
            localProps.setProperty(LOCATORS, "localhost[" + locatorPort + "]");
            localProps.setProperty(GROUPS, groupName);
            localProps.setProperty(USE_CLUSTER_CONFIGURATION, "true");
            getSystem(localProps);
            cache = getCache();
            assertNotNull(cache);
            AsyncEventQueue aeq = cache.getAsyncEventQueue(queueName);
            assertNotNull(aeq);
        }
    });
}
Also used : UnknownHostException(java.net.UnknownHostException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) IOException(java.io.IOException) Properties(java.util.Properties) ClassBuilder(org.apache.geode.internal.ClassBuilder) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) InternalLocator(org.apache.geode.distributed.internal.InternalLocator) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) ClusterConfigurationService(org.apache.geode.distributed.internal.ClusterConfigurationService) CommandStringBuilder(org.apache.geode.management.internal.cli.util.CommandStringBuilder) VM(org.apache.geode.test.dunit.VM) AsyncEventQueue(org.apache.geode.cache.asyncqueue.AsyncEventQueue) File(java.io.File) Cache(org.apache.geode.cache.Cache) Category(org.junit.experimental.categories.Category) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 47 with Cache

use of org.apache.geode.cache.Cache in project geode by apache.

the class DescribeDiskStoreFunctionJUnitTest method testExecuteOnMemberHavingANonGemFireCache.

@Test
public void testExecuteOnMemberHavingANonGemFireCache() throws Throwable {
    final Cache mockCache = mockContext.mock(Cache.class, "Cache");
    final FunctionContext mockFunctionContext = mockContext.mock(FunctionContext.class, "FunctionContext");
    final TestResultSender testResultSender = new TestResultSender();
    mockContext.checking(new Expectations() {

        {
            exactly(0).of(mockFunctionContext).getResultSender();
            will(returnValue(testResultSender));
        }
    });
    final DescribeDiskStoreFunction function = createDescribeDiskStoreFunction(mockCache);
    function.execute(mockFunctionContext);
    final List<?> results = testResultSender.getResults();
    assertNotNull(results);
    assertTrue(results.isEmpty());
}
Also used : Expectations(org.jmock.Expectations) FunctionContext(org.apache.geode.cache.execute.FunctionContext) InternalCache(org.apache.geode.internal.cache.InternalCache) Cache(org.apache.geode.cache.Cache) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 48 with Cache

use of org.apache.geode.cache.Cache in project geode by apache.

the class ShowMetricsDUnitTest method testShowMetricsRegionFromMember.

@Test
public void testShowMetricsRegionFromMember() throws ClassNotFoundException, IOException, InterruptedException {
    systemSetUp();
    Cache cache = getCache();
    final DistributedMember distributedMember = cache.getDistributedSystem().getDistributedMember();
    final String exportFileName = "regionOnAMemberReport.csv";
    final String regionName = "REGION1";
    SerializableCallable showMetricCmd = new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            WaitCriterion wc = createMBeanWaitCriterion(4, regionName, distributedMember, 0);
            waitForCriterion(wc, 5000, 500, true);
            CommandProcessor commandProcessor = new CommandProcessor();
            Result result = commandProcessor.createCommandStatement("show metrics --region=" + regionName + " --member=" + distributedMember.getName() + " --file=" + exportFileName, Collections.EMPTY_MAP).process();
            String resultAsString = commandResultToString((CommandResult) result);
            assertEquals(resultAsString, true, result.getStatus().equals(Status.OK));
            assertTrue(result.hasIncomingFiles());
            result.saveIncomingFiles(null);
            File file = new File(exportFileName);
            file.deleteOnExit();
            assertTrue(file.exists());
            file.delete();
            return resultAsString;
        }
    };
    // Invoke the command in the Manager VM
    final VM managerVm = Host.getHost(0).getVM(0);
    Object managerResultObj = managerVm.invoke(showMetricCmd);
    String managerResult = (String) managerResultObj;
    getLogWriter().info("#SB Manager");
    getLogWriter().info(managerResult);
}
Also used : DistributedMember(org.apache.geode.distributed.DistributedMember) CommandProcessor(org.apache.geode.management.internal.cli.remote.CommandProcessor) File(java.io.File) Cache(org.apache.geode.cache.Cache) Result(org.apache.geode.management.cli.Result) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 49 with Cache

use of org.apache.geode.cache.Cache in project geode by apache.

the class ShowMetricsDUnitTest method createLocalSetUp.

private void createLocalSetUp() {
    Properties localProps = new Properties();
    localProps.setProperty(NAME, "Controller");
    getSystem(localProps);
    Cache cache = getCache();
    RegionFactory<Integer, Integer> dataRegionFactory = cache.createRegionFactory(RegionShortcut.REPLICATE);
    Region region1 = dataRegionFactory.create("REGION1");
    Region region2 = dataRegionFactory.create("REGION2");
}
Also used : Region(org.apache.geode.cache.Region) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) Cache(org.apache.geode.cache.Cache)

Example 50 with Cache

use of org.apache.geode.cache.Cache in project geode by apache.

the class ListDiskStoresFunctionJUnitTest method testExecuteOnMemberWithANonGemFireCache.

@Test
@SuppressWarnings("unchecked")
public void testExecuteOnMemberWithANonGemFireCache() throws Throwable {
    final Cache mockCache = mockContext.mock(Cache.class, "Cache");
    final FunctionContext mockFunctionContext = mockContext.mock(FunctionContext.class, "FunctionContext");
    final TestResultSender testResultSender = new TestResultSender();
    mockContext.checking(new Expectations() {

        {
            oneOf(mockFunctionContext).getResultSender();
            will(returnValue(testResultSender));
        }
    });
    final ListDiskStoresFunction function = createListDiskStoresFunction(mockCache);
    function.execute(mockFunctionContext);
    final List<?> results = testResultSender.getResults();
    assertNotNull(results);
    assertEquals(1, results.size());
    final Set<DiskStoreDetails> diskStoreDetails = (Set<DiskStoreDetails>) results.get(0);
    assertNotNull(diskStoreDetails);
    assertTrue(diskStoreDetails.isEmpty());
}
Also used : Expectations(org.jmock.Expectations) Set(java.util.Set) DiskStoreDetails(org.apache.geode.management.internal.cli.domain.DiskStoreDetails) FunctionContext(org.apache.geode.cache.execute.FunctionContext) InternalCache(org.apache.geode.internal.cache.InternalCache) Cache(org.apache.geode.cache.Cache) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Aggregations

Cache (org.apache.geode.cache.Cache)1044 Region (org.apache.geode.cache.Region)478 Test (org.junit.Test)476 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)292 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)277 VM (org.apache.geode.test.dunit.VM)264 Host (org.apache.geode.test.dunit.Host)230 AttributesFactory (org.apache.geode.cache.AttributesFactory)229 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)177 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)176 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)164 LocalRegion (org.apache.geode.internal.cache.LocalRegion)153 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)123 ClientCache (org.apache.geode.cache.client.ClientCache)117 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)112 Properties (java.util.Properties)101 CacheException (org.apache.geode.cache.CacheException)101 RegionAttributes (org.apache.geode.cache.RegionAttributes)99 QueryService (org.apache.geode.cache.query.QueryService)95 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)93