|
| 1 | +/* |
| 2 | + * ----------------------------------------------------------------------------- |
| 3 | + * manually |
| 4 | + * Removed text manually |
| 5 | + * Removed text manually |
| 6 | + * ----------------------------------------------------------------------------- |
| 7 | + */ |
| 8 | +package org.eclipse.persistence.testing.tests.junit.helper; |
| 9 | + |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import org.eclipse.persistence.internal.helper.ConcurrencyManager; |
| 13 | +import org.eclipse.persistence.internal.helper.ReadLockManager; |
| 14 | +import org.eclipse.persistence.internal.helper.type.ReadLockAcquisitionMetadata; |
| 15 | +import org.eclipse.persistence.internal.identitymaps.CacheKey; |
| 16 | +import org.junit.Assert; |
| 17 | +import org.junit.Before; |
| 18 | +import org.junit.Test; |
| 19 | + |
| 20 | +/** |
| 21 | + * Unit test for the class {@link org.eclipse.persistence.internal.helper.ReadLockManager} |
| 22 | + */ |
| 23 | +public class ReadLockAcquisitionMetadataTest { |
| 24 | + |
| 25 | + // Inner class |
| 26 | + /** |
| 27 | + * Unit test extension class to allows to create some hacky methods to access |
| 28 | + * and manipulate the state of the read lock manager. |
| 29 | + * Such as adding NULL records into the state. |
| 30 | + */ |
| 31 | + public static class ReadLockManagerExtension extends ReadLockManager{ |
| 32 | + |
| 33 | + /** |
| 34 | + * This is a dummy method hack our state maps with null entries. |
| 35 | + * Our expectation is that this should never happen in real life |
| 36 | + * but we have seen null pointer exceptions that seem to indicate that this is factuallz posisbly to happen |
| 37 | + * although we have understoor the underlying reason for the null entries. |
| 38 | + * |
| 39 | + * With this method we simulate corrupting our read data structures with null entries. |
| 40 | + */ |
| 41 | + public void hackReadLockManagerToBeCorruptedWithNullRecordsItsState() { |
| 42 | + ConcurrencyManager cacheKeyNull = null; |
| 43 | + readLocks.add(cacheKeyNull); |
| 44 | + final Thread currentThread = Thread.currentThread(); |
| 45 | + final long currentThreadId = currentThread.getId(); |
| 46 | + ReadLockAcquisitionMetadata readLockAcquisitionMetadataNull = null; |
| 47 | + List<ReadLockAcquisitionMetadata> readLocksAcquiredDuringCurrentThreadList = mapThreadToReadLockAcquisitionMetadata.get(currentThreadId); |
| 48 | + readLocksAcquiredDuringCurrentThreadList.add(readLockAcquisitionMetadataNull); |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + } |
| 54 | + // helpver variables |
| 55 | + |
| 56 | + final ConcurrencyManager cacheKeyA = new CacheKey("cacheKeyA"); |
| 57 | + final ConcurrencyManager cacheKeyB = new CacheKey("cacheKeyB"); |
| 58 | + |
| 59 | + |
| 60 | + @Before |
| 61 | + public void before() { |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + @Test |
| 67 | + public void normalHappyPathLogicAddingAndRemovingMetadataIntoTheReadLockManager() { |
| 68 | + // SETUP: |
| 69 | + // basic variable initialization |
| 70 | + ReadLockManagerExtension testee = new ReadLockManagerExtension(); |
| 71 | + |
| 72 | + |
| 73 | + // EXECUTE 1 - Add cache key A |
| 74 | + testee.addReadLock(cacheKeyA); |
| 75 | + |
| 76 | + // VERIFY 1 |
| 77 | + |
| 78 | + Assert.assertEquals(1, testee.getReadLocks().size()); |
| 79 | + Assert.assertTrue(testee.getReadLocks().contains(cacheKeyA)); |
| 80 | + Assert.assertFalse(testee.getReadLocks().contains(cacheKeyB)); |
| 81 | + |
| 82 | + Assert.assertEquals(1, testee.getMapThreadToReadLockAcquisitionMetadata().size()); |
| 83 | + List<ReadLockAcquisitionMetadata> cacheKeyMetadataForCurrentThread =testee.getMapThreadToReadLockAcquisitionMetadata().get(Thread.currentThread().getId()); |
| 84 | + Assert.assertEquals(1, cacheKeyMetadataForCurrentThread.size()); |
| 85 | + |
| 86 | + Assert.assertTrue(cacheKeyA == cacheKeyMetadataForCurrentThread.get(0).getCacheKeyWhoseNumberOfReadersThreadIsIncrementing()); |
| 87 | + Assert.assertFalse(cacheKeyB == cacheKeyMetadataForCurrentThread.get(0).getCacheKeyWhoseNumberOfReadersThreadIsIncrementing()); |
| 88 | + |
| 89 | + |
| 90 | + // EXECUTE 2 - Add cache key B |
| 91 | + testee.addReadLock(cacheKeyB); |
| 92 | + |
| 93 | + // VERIFY 2 |
| 94 | + |
| 95 | + Assert.assertEquals(2, testee.getReadLocks().size()); |
| 96 | + Assert.assertTrue(testee.getReadLocks().contains(cacheKeyA)); |
| 97 | + Assert.assertTrue(testee.getReadLocks().contains(cacheKeyB)); |
| 98 | + |
| 99 | + Assert.assertEquals(1, testee.getMapThreadToReadLockAcquisitionMetadata().size()); |
| 100 | + cacheKeyMetadataForCurrentThread =testee.getMapThreadToReadLockAcquisitionMetadata().get(Thread.currentThread().getId()); |
| 101 | + Assert.assertEquals(2, cacheKeyMetadataForCurrentThread.size()); |
| 102 | + |
| 103 | + // note: when we are adding, we are adding the entries to the HEAD of the list |
| 104 | + Assert.assertTrue(cacheKeyB == cacheKeyMetadataForCurrentThread.get(0).getCacheKeyWhoseNumberOfReadersThreadIsIncrementing()); |
| 105 | + Assert.assertTrue(cacheKeyA == cacheKeyMetadataForCurrentThread.get(1).getCacheKeyWhoseNumberOfReadersThreadIsIncrementing()); |
| 106 | + |
| 107 | + |
| 108 | + // EXECUTE 3 - Remove cache keys |
| 109 | + testee.removeReadLock(cacheKeyA); |
| 110 | + |
| 111 | + // VERIFY 3 |
| 112 | + Assert.assertEquals(1, testee.getReadLocks().size()); |
| 113 | + Assert.assertFalse(testee.getReadLocks().contains(cacheKeyA)); |
| 114 | + Assert.assertTrue(testee.getReadLocks().contains(cacheKeyB)); |
| 115 | + |
| 116 | + Assert.assertEquals(1, testee.getMapThreadToReadLockAcquisitionMetadata().size()); |
| 117 | + cacheKeyMetadataForCurrentThread =testee.getMapThreadToReadLockAcquisitionMetadata().get(Thread.currentThread().getId()); |
| 118 | + Assert.assertEquals(1, cacheKeyMetadataForCurrentThread.size()); |
| 119 | + |
| 120 | + Assert.assertTrue(cacheKeyB == cacheKeyMetadataForCurrentThread.get(0).getCacheKeyWhoseNumberOfReadersThreadIsIncrementing()); |
| 121 | + Assert.assertFalse(cacheKeyA == cacheKeyMetadataForCurrentThread.get(0).getCacheKeyWhoseNumberOfReadersThreadIsIncrementing()); |
| 122 | + |
| 123 | + // EXECUTE 4 - Remove cache keys |
| 124 | + testee.removeReadLock(cacheKeyB); |
| 125 | + |
| 126 | + // VERIFY 4 |
| 127 | + Assert.assertEquals(0, testee.getReadLocks().size()); |
| 128 | + Assert.assertFalse(testee.getReadLocks().contains(cacheKeyA)); |
| 129 | + Assert.assertFalse(testee.getReadLocks().contains(cacheKeyB)); |
| 130 | + Assert.assertEquals(0, testee.getMapThreadToReadLockAcquisitionMetadata().size()); |
| 131 | + |
| 132 | + |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testAddNullReadCacheKeyDoesNothing() { |
| 137 | + // SETUP: |
| 138 | + // basic variable initialization |
| 139 | + ReadLockManagerExtension testee = new ReadLockManagerExtension(); |
| 140 | + ConcurrencyManager cacheKeyNull = null; |
| 141 | + |
| 142 | + // EXECUTE |
| 143 | + // try to add a null cache key to the map |
| 144 | + testee.addReadLock(cacheKeyNull); |
| 145 | + |
| 146 | + // VERIFY |
| 147 | + Assert.assertEquals(0, testee.getReadLocks().size()); |
| 148 | + Assert.assertEquals(0, testee.getMapThreadToReadLockAcquisitionMetadata().size()); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * The purpose of this unit test is to make sure that if for some unknown reason |
| 153 | + * we ever end up adding NULL as metadata to either the READ Lock manager |
| 154 | + * or to the VECTOR of read locks |
| 155 | + * that we can self heald and remove them from the map automatically. |
| 156 | + * |
| 157 | + */ |
| 158 | + @Test |
| 159 | + public void testRemoveWhen_mapThreadToReadLockAcquisitionMetadata_containsNull() { |
| 160 | + // SETUP: |
| 161 | + // let us start by adding some entrires to the read lock manager |
| 162 | + ReadLockManagerExtension testee = new ReadLockManagerExtension(); |
| 163 | + testee.addReadLock(cacheKeyA); |
| 164 | + testee.addReadLock(cacheKeyB); |
| 165 | + Assert.assertEquals(2, testee.getReadLocks().size()); |
| 166 | + Assert.assertEquals(1, testee.getMapThreadToReadLockAcquisitionMetadata().size()); |
| 167 | + List<ReadLockAcquisitionMetadata> cacheKeyMetadataForCurrentThread =testee.getMapThreadToReadLockAcquisitionMetadata().get(Thread.currentThread().getId()); |
| 168 | + Assert.assertEquals(2, cacheKeyMetadataForCurrentThread.size()); |
| 169 | + |
| 170 | + // SETUP: |
| 171 | + // now we are going to hack our state to put in here null entires both in the read locks map and in the list of metadata |
| 172 | + // Validate that that the maps are now properly hacked |
| 173 | + testee.hackReadLockManagerToBeCorruptedWithNullRecordsItsState(); |
| 174 | + Assert.assertEquals(3, testee.getReadLocks().size()); |
| 175 | + Assert.assertTrue( testee.getReadLocks().contains(null)); |
| 176 | + cacheKeyMetadataForCurrentThread =testee.getMapThreadToReadLockAcquisitionMetadata().get(Thread.currentThread().getId()); |
| 177 | + Assert.assertEquals(3, cacheKeyMetadataForCurrentThread.size()); |
| 178 | + Assert.assertTrue( cacheKeyMetadataForCurrentThread.contains(null)); |
| 179 | + |
| 180 | + // EXECUTE |
| 181 | + // Now we will try to REMOVE from the read lock manager a cache key that does not actually exist in the map |
| 182 | + // this MUST have the side effect of causing the code to loop over ALL OF THE read lock manager metadata and spot that we |
| 183 | + // have NULL records in the metadata array |
| 184 | + // in so doing the code should self-heal and get rid of all that garbage |
| 185 | + ConcurrencyManager cacheKeyNotExistingInTheReadLockManagerToCallFullLoopOverData = new CacheKey("cacheKeyNotExistingInTheReadLockManagerToCallFullLoopOverData"); |
| 186 | + testee.removeReadLock(cacheKeyNotExistingInTheReadLockManagerToCallFullLoopOverData); |
| 187 | + |
| 188 | + // VERIFY that our code self healeded |
| 189 | + Assert.assertEquals(2, testee.getReadLocks().size()); |
| 190 | + Assert.assertFalse( testee.getReadLocks().contains(null)); |
| 191 | + cacheKeyMetadataForCurrentThread =testee.getMapThreadToReadLockAcquisitionMetadata().get(Thread.currentThread().getId()); |
| 192 | + Assert.assertEquals(2, cacheKeyMetadataForCurrentThread.size()); |
| 193 | + Assert.assertFalse( cacheKeyMetadataForCurrentThread.contains(null)); |
| 194 | + |
| 195 | + } |
| 196 | +} |
0 commit comments