/** * Get a random key from the given region that is within this instance's range. If no keys qualify * in the region, return null. * * @param aRegion - The region to get the key from. * @param excludeKey - The region to get the key from. * @returns A key from aRegion, or null. */ public Object getRandomKey(Region aRegion, Object excludeKey) { long start = System.currentTimeMillis(); int lower = ((Integer) (lowerKeyRange.get())).intValue(); int upper = ((Integer) (upperKeyRange.get())).intValue(); long randomKeyIndex = TestConfig.tab().getRandGen().nextLong(lower, upper); long startKeyIndex = randomKeyIndex; Object key = NameFactory.getObjectNameForCounter(randomKeyIndex); do { boolean done = false; if ((!(key.equals(excludeKey))) && (aRegion.containsKey(key))) done = true; if (done) break; randomKeyIndex++; // go to the next key if (randomKeyIndex > upper) randomKeyIndex = lower; if (randomKeyIndex == startKeyIndex) { // considered all keys key = null; break; } key = NameFactory.getObjectNameForCounter(randomKeyIndex); } while (true); long end = System.currentTimeMillis(); Log.getLogWriter() .info( "Done in TxUtilKeyRange:getRandomKey, key is " + key + " " + aRegion.getFullPath() + " getRandomKey took " + (end - start) + " millis"); return key; }
/** * Creates a new key/value in the given region by creating a new key within the range and a random * value. * * @param aRegion The region to create the new key in. * @param exists Not used in this overridden method; this test wants to use unique keys even on * creates, so we don't do anything different here based on the value of exists. * @return An instance of Operation describing the create operation. */ @Override public Operation createEntry(Region aRegion, boolean exists) { int lower = ((Integer) (lowerKeyRange.get())).intValue(); int upper = ((Integer) (upperKeyRange.get())).intValue(); long keyIndex = TestConfig.tab().getRandGen().nextInt(lower, upper); long startKeyIndex = keyIndex; Object key = NameFactory.getObjectNameForCounter(keyIndex); boolean containsKey = aRegion.containsKey(key); while (containsKey) { // looking for a key that does not exist keyIndex++; // go to the next key if (keyIndex > upper) keyIndex = lower; if (keyIndex == startKeyIndex) { // considered all keys return null; } key = NameFactory.getObjectNameForCounter(keyIndex); containsKey = aRegion.containsKey(key); } BaseValueHolder vh = new ValueHolder(key, randomValues, new Integer(modValInitializer++)); try { Log.getLogWriter() .info( "createEntryKeyRange: putting key " + key + ", object " + vh.toString() + " in region " + aRegion.getFullPath()); aRegion.put(key, vh); Log.getLogWriter() .info( "createEntryKeyRange: done putting key " + key + ", object " + vh.toString() + " in region " + aRegion.getFullPath()); } catch (Exception e) { throw new TestException(TestHelper.getStackTrace(e)); } return new Operation(aRegion.getFullPath(), key, Operation.ENTRY_CREATE, null, vh.modVal); }
/** * Check that containsKey() called on the tests' region has the expected result. Throw an error if * any problems. * * @param key The key to check. * @param expected The expected result of containsKey * @param logStr Used if throwing an error due to an unexpected value. */ protected void checkContainsKey(Object key, boolean expected, String logStr) { boolean containsKey = aRegion.containsKey(key); if (containsKey != expected) throw new TestException( "Expected containsKey(" + key + ") to be " + expected + ", but it was " + containsKey + ": " + logStr); }
/** Called for debugging */ public void printKeyRange(Region aRegion) { int lower = ((Integer) (lowerKeyRange.get())).intValue(); int upper = ((Integer) (upperKeyRange.get())).intValue(); StringBuffer aStr = new StringBuffer(); for (int i = lower; i <= upper; i++) { Object key = NameFactory.getObjectNameForCounter(i); aStr.append( "key " + key + " containsKey: " + aRegion.containsKey(key) + ", containsValueForKey: " + aRegion.containsValueForKey(key) + ", getValueInVM: " + diskReg.DiskRegUtil.getValueInVM(aRegion, key) + "\n"); } Log.getLogWriter().info(aStr.toString()); }
/** * check distributed ops that originate in a PROXY are correctly distributed to non-proxy regions. */ private void distributedOps(DataPolicy dp, InterestPolicy ip) throws CacheException { initOtherId(); AttributesFactory af = new AttributesFactory(); af.setDataPolicy(dp); af.setSubscriptionAttributes(new SubscriptionAttributes(ip)); af.setScope(Scope.DISTRIBUTED_ACK); Region r = createRootRegion("ProxyDUnitTest", af.create()); doCreateOtherVm(); r.put("putkey", "putvalue1"); getOtherVm() .invoke( new CacheSerializableRunnable("check put") { public void run2() throws CacheException { Region r = getRootRegion("ProxyDUnitTest"); assertEquals(true, r.containsKey("putkey")); assertEquals("putvalue1", r.getEntry("putkey").getValue()); r.put("putkey", "putvalue2"); } }); assertEquals(false, r.containsKey("putkey")); assertEquals("putvalue2", r.get("putkey")); // netsearch r.invalidate("putkey"); getOtherVm() .invoke( new CacheSerializableRunnable("check invalidate") { public void run2() throws CacheException { Region r = getRootRegion("ProxyDUnitTest"); assertEquals(true, r.containsKey("putkey")); assertEquals(null, r.getEntry("putkey").getValue()); } }); assertEquals(null, r.get("putkey")); // invalid so total miss r.destroy("putkey"); getOtherVm() .invoke( new CacheSerializableRunnable("check destroy") { public void run2() throws CacheException { Region r = getRootRegion("ProxyDUnitTest"); assertEquals(false, r.containsKey("putkey")); } }); assertEquals(null, r.get("putkey")); // total miss r.create("createKey", "createValue1"); getOtherVm() .invoke( new CacheSerializableRunnable("check create") { public void run2() throws CacheException { Region r = getRootRegion("ProxyDUnitTest"); assertEquals(true, r.containsKey("createKey")); assertEquals("createValue1", r.getEntry("createKey").getValue()); } }); { Map m = new HashMap(); m.put("putAllKey1", "putAllValue1"); m.put("putAllKey2", "putAllValue2"); r.putAll(m, "putAllCallback"); } getOtherVm() .invoke( new CacheSerializableRunnable("check putAll") { public void run2() throws CacheException { Region r = getRootRegion("ProxyDUnitTest"); assertEquals(true, r.containsKey("putAllKey1")); assertEquals("putAllValue1", r.getEntry("putAllKey1").getValue()); assertEquals(true, r.containsKey("putAllKey2")); assertEquals("putAllValue2", r.getEntry("putAllKey2").getValue()); } }); r.clear(); getOtherVm() .invoke( new CacheSerializableRunnable("check clear") { public void run2() throws CacheException { Region r = getRootRegion("ProxyDUnitTest"); assertEquals(0, r.size()); } }); getOtherVm() .invoke( new CacheSerializableRunnable("install CacheWriter") { public void run2() throws CacheException { Region r = getRootRegion("ProxyDUnitTest"); AttributesMutator am = r.getAttributesMutator(); CacheWriter cw = new CacheWriterAdapter() { public void beforeCreate(EntryEvent event) throws CacheWriterException { throw new CacheWriterException("expected"); } }; am.setCacheWriter(cw); } }); try { r.put("putkey", "putvalue"); fail("expected CacheWriterException"); } catch (CacheWriterException expected) { } getOtherVm() .invoke( new CacheSerializableRunnable("check clear") { public void run2() throws CacheException { Region r = getRootRegion("ProxyDUnitTest"); assertEquals(0, r.size()); } }); assertEquals(null, r.get("loadkey")); // total miss getOtherVm() .invoke( new CacheSerializableRunnable("install CacheLoader") { public void run2() throws CacheException { Region r = getRootRegion("ProxyDUnitTest"); AttributesMutator am = r.getAttributesMutator(); am.setCacheWriter(null); // clear csche writer CacheLoader cl = new CacheLoader() { public Object load(LoaderHelper helper) throws CacheLoaderException { if (helper.getKey().equals("loadkey")) { return "loadvalue"; } else if (helper.getKey().equals("loadexception")) { throw new CacheLoaderException("expected"); } else { return null; } } public void close() {} }; am.setCacheLoader(cl); } }); assertEquals("loadvalue", r.get("loadkey")); // net load assertEquals(null, r.get("foobar")); // total miss try { r.get("loadexception"); fail("expected CacheLoaderException"); } catch (CacheLoaderException expected) { } r.destroyRegion(); getOtherVm() .invoke( new CacheSerializableRunnable("check clear") { public void run2() throws CacheException { Region r = getRootRegion("ProxyDUnitTest"); assertEquals(null, r); } }); }