PageRenderTime 45ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/gemfire-core/src/test/java/com/gemstone/gemfire/internal/cache/tier/sockets/VerifyUpdatesFromNonInterestEndPointDUnitTest.java

https://gitlab.com/kidaa/incubator-geode
Java | 248 lines | 188 code | 36 blank | 24 comment | 12 complexity | 840696222cb45e2995024880e645120f MD5 | raw file
  1. /*=========================================================================
  2. * Copyright (c) 2010-2014 Pivotal Software, Inc. All Rights Reserved.
  3. * This product is protected by U.S. and international copyright
  4. * and intellectual property laws. Pivotal products are covered by
  5. * one or more patents listed at http://www.pivotal.io/patents.
  6. *=========================================================================
  7. */
  8. package com.gemstone.gemfire.internal.cache.tier.sockets;
  9. import java.util.Properties;
  10. import com.gemstone.gemfire.cache.AttributesFactory;
  11. import com.gemstone.gemfire.cache.Cache;
  12. import com.gemstone.gemfire.cache.CacheFactory;
  13. import com.gemstone.gemfire.cache.DataPolicy;
  14. import com.gemstone.gemfire.cache.Region;
  15. import com.gemstone.gemfire.cache.RegionAttributes;
  16. import com.gemstone.gemfire.cache.Scope;
  17. import com.gemstone.gemfire.cache.util.BridgeServer;
  18. import com.gemstone.gemfire.distributed.DistributedSystem;
  19. import com.gemstone.gemfire.distributed.internal.DistributionConfig;
  20. import com.gemstone.gemfire.internal.AvailablePort;
  21. import com.gemstone.gemfire.internal.cache.EntryEventImpl;
  22. import com.gemstone.gemfire.internal.cache.EventID;
  23. import com.gemstone.gemfire.internal.cache.LocalRegion;
  24. import com.gemstone.gemfire.cache.client.*;
  25. import com.gemstone.gemfire.cache.client.internal.PoolImpl;
  26. import com.gemstone.gemfire.cache.client.internal.Connection;
  27. import com.gemstone.gemfire.cache.client.internal.ServerRegionProxy;
  28. import dunit.DistributedTestCase;
  29. import dunit.Host;
  30. import dunit.VM;
  31. /**
  32. * One Client , two servers.
  33. * Ensure that client 1 has registered interest list on server 2.
  34. * Now Client does a put on server1 .
  35. * The Client should not receive callback of his own put.
  36. * @author Yogesh Mahajan
  37. * @author Suyog Bhokare
  38. *
  39. */
  40. public class VerifyUpdatesFromNonInterestEndPointDUnitTest extends DistributedTestCase
  41. {
  42. VM vm0 = null;
  43. VM vm1 = null;
  44. VM vm2 = null;
  45. private static int PORT1;
  46. private static int PORT2;
  47. private static final String REGION_NAME = "VerifyUpdatesFromNonInterestEndPointDUnitTest_region";
  48. private static Cache cache = null;
  49. /** constructor */
  50. public VerifyUpdatesFromNonInterestEndPointDUnitTest(String name) {
  51. super(name);
  52. }
  53. public void setUp() throws Exception {
  54. super.setUp();
  55. disconnectAllFromDS();
  56. pause(5000);
  57. final Host host = Host.getHost(0);
  58. vm0 = host.getVM(0);
  59. vm1 = host.getVM(1);
  60. vm2 = host.getVM(2);
  61. PORT1 = ((Integer)vm0.invoke(VerifyUpdatesFromNonInterestEndPointDUnitTest.class, "createServerCache" )).intValue();
  62. PORT2 = ((Integer)vm1.invoke(VerifyUpdatesFromNonInterestEndPointDUnitTest.class, "createServerCache" )).intValue();
  63. vm2.invoke(VerifyUpdatesFromNonInterestEndPointDUnitTest.class, "createClientCache",
  64. new Object[] { getServerHostName(vm0.getHost()), new Integer(PORT1),new Integer(PORT2)});
  65. }
  66. private Cache createCache(Properties props) throws Exception
  67. {
  68. DistributedSystem ds = getSystem(props);
  69. Cache cache = null;
  70. cache = CacheFactory.create(ds);
  71. if (cache == null) {
  72. throw new Exception("CacheFactory.create() returned null ");
  73. }
  74. return cache;
  75. }
  76. public void testVerifyUpdatesFromNonInterestEndPoint()
  77. {
  78. vm2.invoke(VerifyUpdatesFromNonInterestEndPointDUnitTest.class, "createEntries");
  79. vm1.invoke(VerifyUpdatesFromNonInterestEndPointDUnitTest.class, "createEntries");
  80. vm0.invoke(VerifyUpdatesFromNonInterestEndPointDUnitTest.class, "createEntries");
  81. vm2.invoke(VerifyUpdatesFromNonInterestEndPointDUnitTest.class, "registerKey");
  82. vm2.invoke(VerifyUpdatesFromNonInterestEndPointDUnitTest.class, "acquireConnectionsAndPut", new Object[] { new Integer(PORT2)});
  83. pause(30000);
  84. vm2.invoke(VerifyUpdatesFromNonInterestEndPointDUnitTest.class, "verifyPut");
  85. }
  86. public static void acquireConnectionsAndPut(Integer port)
  87. {
  88. try {
  89. Region r1 = cache.getRegion(Region.SEPARATOR + REGION_NAME);
  90. String poolName = r1.getAttributes().getPoolName();
  91. assertNotNull(poolName);
  92. PoolImpl pool = (PoolImpl)PoolManager.find(poolName);
  93. assertNotNull(pool);
  94. Connection conn1 = pool.acquireConnection();
  95. Connection conn2 = pool.acquireConnection();
  96. ServerRegionProxy srp = new ServerRegionProxy(Region.SEPARATOR + REGION_NAME, pool);
  97. // put on a connection which is is not interest list ep
  98. if(conn1.getServer().getPort() == port.intValue() ){
  99. srp.putOnForTestsOnly(conn1, "key-1", "server-value1", new EventID(new byte[] { 1 },1,1),null);
  100. srp.putOnForTestsOnly(conn1, "key-2", "server-value2", new EventID(new byte[] { 1 },1,2),null);
  101. }
  102. else if(conn2.getServer().getPort() == port.intValue()){
  103. srp.putOnForTestsOnly(conn2, "key-1", "server-value1", new EventID(new byte[] { 1 },1,1),null);
  104. srp.putOnForTestsOnly(conn2, "key-2", "server-value2", new EventID(new byte[] { 1 },1,2),null);
  105. }
  106. }
  107. catch (Exception ex) {
  108. fail("while setting acquireConnections "+ ex);
  109. }
  110. }
  111. public static void createEntries()
  112. {
  113. try {
  114. LocalRegion r1 = (LocalRegion)cache.getRegion(Region.SEPARATOR + REGION_NAME);
  115. if (!r1.containsKey("key-1")) {
  116. r1.create("key-1", "key-1");
  117. }
  118. if (!r1.containsKey("key-2")) {
  119. r1.create("key-2", "key-2");
  120. }
  121. assertEquals(r1.getEntry("key-1").getValue(), "key-1");
  122. assertEquals(r1.getEntry("key-2").getValue(), "key-2");
  123. }
  124. catch (Exception ex) {
  125. fail("failed while createEntries()", ex);
  126. }
  127. }
  128. public static void createClientCache(String host, Integer port1, Integer port2) throws Exception
  129. {
  130. VerifyUpdatesFromNonInterestEndPointDUnitTest test = new VerifyUpdatesFromNonInterestEndPointDUnitTest("temp");
  131. Properties props = new Properties();
  132. props.setProperty(DistributionConfig.MCAST_PORT_NAME, "0");
  133. props.setProperty(DistributionConfig.LOCATORS_NAME, "");
  134. cache = test.createCache(props);
  135. Pool p;
  136. try {
  137. p = PoolManager.createFactory()
  138. .addServer(host, port1.intValue())
  139. .addServer(host, port2.intValue())
  140. .setSubscriptionEnabled(true)
  141. .setSubscriptionRedundancy(-1)
  142. .setMinConnections(6)
  143. .setSocketBufferSize(32768)
  144. .setReadTimeout(2000)
  145. // .setRetryInterval(250)
  146. // .setRetryAttempts(5)
  147. .create("UpdatePropagationDUnitTestPool");
  148. } finally {
  149. CacheServerTestUtil.enableShufflingOfEndpoints();
  150. }
  151. AttributesFactory factory = new AttributesFactory();
  152. factory.setScope(Scope.DISTRIBUTED_ACK);
  153. factory.setPoolName(p.getName());
  154. RegionAttributes attrs = factory.create();
  155. cache.createRegion(REGION_NAME, attrs);
  156. }
  157. public static Integer createServerCache() throws Exception
  158. {
  159. cache = new VerifyUpdatesFromNonInterestEndPointDUnitTest("temp").createCache(new Properties());
  160. AttributesFactory factory = new AttributesFactory();
  161. factory.setScope(Scope.DISTRIBUTED_ACK);
  162. factory.setDataPolicy(DataPolicy.REPLICATE);
  163. RegionAttributes attrs = factory.create();
  164. cache.createRegion(REGION_NAME, attrs);
  165. BridgeServer server1 = cache.addBridgeServer();
  166. int port = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET) ;
  167. server1.setPort(port);
  168. server1.setNotifyBySubscription(true);
  169. server1.start();
  170. return new Integer(server1.getPort());
  171. }
  172. public static void registerKey()
  173. {
  174. try {
  175. Region r = cache.getRegion(Region.SEPARATOR + REGION_NAME);
  176. assertNotNull(r);
  177. r.registerInterest("key-1");
  178. }
  179. catch (Exception ex) {
  180. fail("failed while registerKey()", ex);
  181. }
  182. }
  183. public static void verifyPut()
  184. {
  185. try {
  186. Region r = cache.getRegion(Region.SEPARATOR + REGION_NAME);
  187. assertNotNull(r);
  188. // verify no update
  189. assertEquals("key-1", r.getEntry("key-1").getValue());
  190. assertEquals("key-2", r.getEntry("key-2").getValue());
  191. }
  192. catch (Exception ex) {
  193. fail("failed while verifyPut()", ex);
  194. }
  195. }
  196. public static void closeCache()
  197. {
  198. if (cache != null && !cache.isClosed()) {
  199. cache.close();
  200. cache.getDistributedSystem().disconnect();
  201. }
  202. }
  203. public void tearDown2() throws Exception
  204. {
  205. //close client
  206. vm2.invoke(VerifyUpdatesFromNonInterestEndPointDUnitTest.class, "closeCache");
  207. //close server
  208. vm0.invoke(VerifyUpdatesFromNonInterestEndPointDUnitTest.class, "closeCache");
  209. vm1.invoke(VerifyUpdatesFromNonInterestEndPointDUnitTest.class, "closeCache");
  210. }
  211. }