<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gemstone.gemfire.cache30;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.gemstone.gemfire.cache.AttributesFactory;
import com.gemstone.gemfire.cache.CacheException;
import com.gemstone.gemfire.cache.DataPolicy;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.RegionAttributes;
import com.gemstone.gemfire.cache.Scope;
import com.gemstone.gemfire.distributed.Locator;
import com.gemstone.gemfire.distributed.internal.DistributionConfig;
import com.gemstone.gemfire.distributed.internal.InternalLocator;
import com.gemstone.gemfire.distributed.internal.SharedConfiguration;
import com.gemstone.gemfire.internal.AvailablePortHelper;
import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
import com.gemstone.gemfire.internal.cache.OffHeapTestUtil;
import com.gemstone.gemfire.test.dunit.DistributedTestCase;
import com.gemstone.gemfire.test.dunit.Host;
import com.gemstone.gemfire.test.dunit.Invoke;
import com.gemstone.gemfire.test.dunit.SerializableCallable;
import com.gemstone.gemfire.test.dunit.SerializableRunnable;
import com.gemstone.gemfire.test.dunit.VM;

public class DistributedMulticastRegionDUnitTest extends CacheTestCase {

  static int locatorVM = 3;
  static String mcastport = "42786";
  static String mcastttl = "0";
  
  private int locatorPort;

  public DistributedMulticastRegionDUnitTest(String name) {
    super(name);
  }
  
  @Override
  public void setUp() throws Exception {
    clean();
    super.setUp();    
  }
  
  @Override
  protected final void preTearDownCacheTestCase() throws Exception {
    clean();
  }
  
  private void clean(){
    SerializableRunnable cleanVM =
        new CacheSerializableRunnable("clean VM") {
            public void run2() throws CacheException {
              disconnectFromDS();
            }
        };
    Invoke.invokeInEveryVM(cleanVM);    
  }
  
  public void testMulticastEnabled() {
    final String name = "mcastRegion";
    SerializableRunnable create =
      new CacheSerializableRunnable("Create Region") {
          public void run2() throws CacheException {
            createRegion(name, getRegionAttributes());
          }
        };

    locatorPort = startLocator();
    Host host = Host.getHost(0);
    final VM vm0 = host.getVM(0);
    final VM vm1 = host.getVM(1);
    //1. start locator with mcast port
    vm0.invoke(create);
    vm1.invoke(create);
    
    SerializableRunnable validateMulticastBeforeRegionOps =
        new CacheSerializableRunnable("validateMulticast before region ops") {
            public void run2() throws CacheException {
              validateMulticastOpsBeforeRegionOps();
            }
        };
      
    vm0.invoke(validateMulticastBeforeRegionOps);
    vm1.invoke(validateMulticastBeforeRegionOps);
    
    SerializableRunnable doPuts =
      new CacheSerializableRunnable("do put") {
          public void run2() throws CacheException {
            final Region region =
                getRootRegion().getSubregion(name);
            for(int i =0 ; i < 5; i++) {
              region.put(i, i);
            }
          }
      };
      
    vm0.invoke(doPuts);
    
    SerializableRunnable validateMulticastAfterRegionOps =
      new CacheSerializableRunnable("validateMulticast after region ops") {
          public void run2() throws CacheException {
            validateMulticastOpsAfterRegionOps();
          }
      };
    
      vm0.invoke(validateMulticastAfterRegionOps);
      vm1.invoke(validateMulticastAfterRegionOps);
   
      closeLocator();      
  }
  
  protected RegionAttributes getRegionAttributes() {
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_ACK);
    factory.setDataPolicy(DataPolicy.PRELOADED);
    factory.setEarlyAck(false);
    factory.setConcurrencyChecksEnabled(false);
    factory.setMulticastEnabled(true);<commit_msg>GEODE-1003 added dunit to verify de-serialization failure in peer node<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gemstone.gemfire.cache30;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.gemstone.gemfire.cache.AttributesFactory;
import com.gemstone.gemfire.cache.CacheException;
import com.gemstone.gemfire.cache.DataPolicy;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.RegionAttributes;
import com.gemstone.gemfire.cache.Scope;
import com.gemstone.gemfire.distributed.Locator;
import com.gemstone.gemfire.distributed.internal.DistributionConfig;
import com.gemstone.gemfire.distributed.internal.InternalLocator;
import com.gemstone.gemfire.distributed.internal.ReplyException;
import com.gemstone.gemfire.distributed.internal.SharedConfiguration;
import com.gemstone.gemfire.internal.AvailablePortHelper;
import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
import com.gemstone.gemfire.internal.cache.OffHeapTestUtil;
import com.gemstone.gemfire.pdx.PdxReader;
import com.gemstone.gemfire.pdx.PdxSerializable;
import com.gemstone.gemfire.pdx.PdxSerializationException;
import com.gemstone.gemfire.pdx.PdxWriter;
import com.gemstone.gemfire.test.dunit.DistributedTestCase;
import com.gemstone.gemfire.test.dunit.Host;
import com.gemstone.gemfire.test.dunit.Invoke;
import com.gemstone.gemfire.test.dunit.SerializableCallable;
import com.gemstone.gemfire.test.dunit.SerializableRunnable;
import com.gemstone.gemfire.test.dunit.VM;

public class DistributedMulticastRegionDUnitTest extends CacheTestCase {

  static int locatorVM = 3;
  static String mcastport = "42786";
  static String mcastttl = "0";
  
  private int locatorPort;

  public DistributedMulticastRegionDUnitTest(String name) {
    super(name);
  }
  
  @Override
  public void setUp() throws Exception {
    clean();
    super.setUp();    
  }
  
  @Override
  protected final void preTearDownCacheTestCase() throws Exception {
    clean();
  }
  
  private void clean(){
    SerializableRunnable cleanVM =
        new CacheSerializableRunnable("clean VM") {
            public void run2() throws CacheException {
              disconnectFromDS();
            }
        };
    Invoke.invokeInEveryVM(cleanVM);    
  }
  
  public void testMulticastEnabled() {
    final String name = "mcastRegion";
    SerializableRunnable create =
      new CacheSerializableRunnable("Create Region") {
          public void run2() throws CacheException {
            createRegion(name, getRegionAttributes());
          }
        };

    locatorPort = startLocator();
    Host host = Host.getHost(0);
    final VM vm0 = host.getVM(0);
    final VM vm1 = host.getVM(1);
    //1. start locator with mcast port
    vm0.invoke(create);
    vm1.invoke(create);
    
    SerializableRunnable validateMulticastBeforeRegionOps =
        new CacheSerializableRunnable("validateMulticast before region ops") {
            public void run2() throws CacheException {
              validateMulticastOpsBeforeRegionOps();
            }
        };
      
    vm0.invoke(validateMulticastBeforeRegionOps);
    vm1.invoke(validateMulticastBeforeRegionOps);
    
    SerializableRunnable doPuts =
      new CacheSerializableRunnable("do put") {
          public void run2() throws CacheException {
            final Region region =
                getRootRegion().getSubregion(name);
            for(int i =0 ; i < 5; i++) {
              region.put(i, i);
            }
          }
      };
      
    vm0.invoke(doPuts);
    
    SerializableRunnable validateMulticastAfterRegionOps =
      new CacheSerializableRunnable("validateMulticast after region ops") {
          public void run2() throws CacheException {
            validateMulticastOpsAfterRegionOps();
          }
      };
    
      vm0.invoke(validateMulticastAfterRegionOps);
      vm1.invoke(validateMulticastAfterRegionOps);
   
      closeLocator();      
  }
  
  public class TestObjectThrowsException implements PdxSerializable {
    String name = "TestObjectThrowsException";

    @Override
    public void toData(PdxWriter writer) {
      writer.writeString("name", name);      
    }

    @Override
    public void fromData(PdxReader reader) {
      throw new RuntimeException("Unable to desrialize message ");
      //name = reader.readString("name");
    }
    
  }
  
  public void testMulticastWithRegionOpsException() {
    Host host = Host.getHost(0);
    final VM vm0 = host.getVM(0);
    final VM vm1 = host.getVM(1);

    try {
      final String name = "mcastRegion";
      
      SerializableRunnable setSysProp = new CacheSerializableRunnable("Create Region") {
        public void run2() throws CacheException {
          System.setProperty("gemfire.STORE_ALL_VALUE_FORMS", "true");
        }
      };
      
      
      SerializableRunnable create =
        new CacheSerializableRunnable("Create Region") {
            public void run2() throws CacheException {
              createRegion(name, getRegionAttributes());
            }
          };
  
      locatorPort = startLocator();
      
      vm0.invoke(setSysProp);
      vm1.invoke(setSysProp);
      
      //1. start locator with mcast port
      vm0.invoke(create);
      vm1.invoke(create);
      
      SerializableRunnable validateMulticastBeforeRegionOps =
          new CacheSerializableRunnable("validateMulticast before region ops") {
              public void run2() throws CacheException {
                validateMulticastOpsBeforeRegionOps();
              }
          };
        
      vm0.invoke(validateMulticastBeforeRegionOps);
      vm1.invoke(validateMulticastBeforeRegionOps);
      
      SerializableRunnable doPuts =
        new CacheSerializableRunnable("do put") {
            public void run2() throws CacheException {
              final Region region =
                  getRootRegion().getSubregion(name);
              boolean gotReplyException = false;
              for(int i =0 ; i < 1; i++) {
                try {
                  region.put(i, new TestObjectThrowsException());
                }catch(PdxSerializationException e) {
                  gotReplyException = true;
                }catch(Exception e ) {
                  region.getCache().getLogger().info("Got exception of type " + e.getClass().toString());
                } 
              }
              assertTrue("We should have got ReplyEception ", gotReplyException);
            }
        };
        
      vm0.invoke(doPuts);
      
      SerializableRunnable validateMulticastAfterRegionOps =
        new CacheSerializableRunnable("validateMulticast after region ops") {
            public void run2() throws CacheException {
              validateMulticastOpsAfterRegionOps();
            }
        };
      
        vm0.invoke(validateMulticastAfterRegionOps);
        vm1.invoke(validateMulticastAfterRegionOps);
     
        closeLocator();
    }finally {
      SerializableRunnable unsetSysProp = new CacheSerializableRunnable("Create Region") {
        public void run2() throws CacheException {
          System.setProperty("gemfire.STORE_ALL_VALUE_FORMS", "false");          
        }
      };
      vm0.invoke(unsetSysProp);
      vm1.invoke(unsetSysProp);
    }
  }
  
  protected RegionAttributes getRegionAttributes() {
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_ACK);
    factory.setDataPolicy(DataPolicy.PRELOADED);
    factory.setEarlyAck(false);
    factory.setConcurrencyChecksEnabled(false);
    factory.setMulticastEnabled(true);
{'fraction_non_alphanumeric': 0.08551734585668838, 'fraction_numerical': 0.007195879412210271, 'mean_word_length': 3.010631834750911, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1378722', 'n_tokens_mistral': 3700, 'n_tokens_neox': 3611, 'n_words': 840}