|
| 1 | +package org.dcache.pool.migration; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.Comparator; |
| 5 | +import java.util.concurrent.ScheduledExecutorService; |
| 6 | +import java.util.concurrent.TimeUnit; |
| 7 | +import java.util.function.Predicate; |
| 8 | + |
| 9 | +import org.dcache.cells.CellStub; |
| 10 | +import org.dcache.namespace.FileAttribute; |
| 11 | +import org.dcache.pool.repository.CacheEntry; |
| 12 | +import org.dcache.pool.repository.Repository; |
| 13 | +import org.dcache.vehicles.FileAttributes; |
| 14 | +import org.junit.Before; |
| 15 | +import org.junit.Test; |
| 16 | +import org.mockito.ArgumentCaptor; |
| 17 | + |
| 18 | +import static org.mockito.ArgumentMatchers.any; |
| 19 | +import static org.mockito.ArgumentMatchers.anyLong; |
| 20 | +import static org.mockito.Mockito.atLeast; |
| 21 | +import static org.mockito.Mockito.mock; |
| 22 | +import static org.mockito.Mockito.times; |
| 23 | +import static org.mockito.Mockito.verify; |
| 24 | +import static org.mockito.Mockito.when; |
| 25 | + |
| 26 | +import com.google.common.collect.ImmutableList; |
| 27 | + |
| 28 | +import diskCacheV111.util.PnfsId; |
| 29 | +import dmg.cells.nucleus.CellAddressCore; |
| 30 | +import dmg.cells.nucleus.CellPath; |
| 31 | + |
| 32 | +public class MigrationModuleSpinTest { |
| 33 | + |
| 34 | + private MigrationContext context; |
| 35 | + private ScheduledExecutorService executor; |
| 36 | + private RefreshablePoolList poolList; |
| 37 | + private RefreshablePoolList sourceList; |
| 38 | + private JobDefinition definition; |
| 39 | + private Repository repository; |
| 40 | + private Predicate<CacheEntry> filter; |
| 41 | + |
| 42 | + private JobDefinition createJobDefinition(boolean waitForTargets) { |
| 43 | + CacheEntryMode mode = new CacheEntryMode(CacheEntryMode.State.SAME, |
| 44 | + Collections.emptyList()); |
| 45 | + return new JobDefinition( |
| 46 | + filter, // filter |
| 47 | + mode, // sourceMode |
| 48 | + mode, // targetMode |
| 49 | + mock(PoolSelectionStrategy.class), // selectionStrategy |
| 50 | + mock(Comparator.class), // comparator |
| 51 | + sourceList, // sourceList |
| 52 | + poolList, // poolList |
| 53 | + 0, // refreshPeriod |
| 54 | + false, // isPermanent |
| 55 | + false, // isEager |
| 56 | + false, // isMetaOnly |
| 57 | + 1, // replicas |
| 58 | + false, // mustMovePins |
| 59 | + false, // computeChecksumOnUpdate |
| 60 | + false, // maintainAtime |
| 61 | + null, // pauseWhen |
| 62 | + null, // stopWhen |
| 63 | + false, // forceSourceMode |
| 64 | + waitForTargets // waitForTargets |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + @Before |
| 69 | + public void setUp() { |
| 70 | + context = mock(MigrationContext.class); |
| 71 | + executor = mock(ScheduledExecutorService.class); |
| 72 | + poolList = mock(RefreshablePoolList.class); |
| 73 | + sourceList = mock(RefreshablePoolList.class); |
| 74 | + repository = mock(Repository.class); |
| 75 | + filter = mock(Predicate.class); |
| 76 | + |
| 77 | + when(context.getExecutor()).thenReturn(executor); |
| 78 | + when(context.getPoolStub()).thenReturn(mock(CellStub.class)); |
| 79 | + when(context.getPnfsStub()).thenReturn(mock(CellStub.class)); |
| 80 | + |
| 81 | + CellStub pinManagerStub = mock(CellStub.class); |
| 82 | + CellAddressCore cellAddress = new CellAddressCore("PinManager"); |
| 83 | + CellPath cellPath = new CellPath(cellAddress); |
| 84 | + when(pinManagerStub.getDestinationPath()).thenReturn(cellPath); |
| 85 | + when(context.getPinManagerStub()).thenReturn(pinManagerStub); |
| 86 | + when(context.lock(any(PnfsId.class))).thenReturn(true); |
| 87 | + |
| 88 | + when(context.getRepository()).thenReturn(repository); |
| 89 | + when(repository.iterator()).thenReturn(Collections.emptyIterator()); |
| 90 | + |
| 91 | + definition = createJobDefinition(false); |
| 92 | + |
| 93 | + when(poolList.isValid()).thenReturn(true); |
| 94 | + when(sourceList.isValid()).thenReturn(true); |
| 95 | + |
| 96 | + // Mock pool list to return empty list (no pools available) |
| 97 | + when(poolList.getPools()).thenReturn(ImmutableList.of()); |
| 98 | + when(poolList.getOfflinePools()).thenReturn(ImmutableList.of()); |
| 99 | + |
| 100 | + FileAttributes attributes = mock(FileAttributes.class); |
| 101 | + when(attributes.isDefined(FileAttribute.LOCATIONS)).thenReturn(true); |
| 102 | + when(attributes.getLocations()).thenReturn(Collections.emptyList()); |
| 103 | + |
| 104 | + // We need to make sure any CacheEntry returns these attributes |
| 105 | + // But CacheEntry is mocked in test methods. |
| 106 | + // I'll add a helper or just set it up in test methods. |
| 107 | + } |
| 108 | + |
| 109 | + @Test(timeout = 5000) |
| 110 | + public void testSpin() throws Exception { |
| 111 | + // Setup a job with some queued tasks |
| 112 | + PnfsId pnfsId = new PnfsId("000000000000000000000001"); |
| 113 | + CacheEntry entry = mock(CacheEntry.class); |
| 114 | + when(entry.getPnfsId()).thenReturn(pnfsId); |
| 115 | + |
| 116 | + FileAttributes attributes = new FileAttributes(); |
| 117 | + attributes.setLocations(Collections.emptyList()); |
| 118 | + when(entry.getFileAttributes()).thenReturn(attributes); |
| 119 | + |
| 120 | + // Mock repository to return the entry |
| 121 | + when(repository.iterator()).thenReturn(Collections.singletonList(pnfsId).iterator()); |
| 122 | + when(repository.getEntry(pnfsId)).thenReturn(entry); |
| 123 | + |
| 124 | + // Mock filter to accept the entry |
| 125 | + when(filter.test(entry)).thenReturn(true); |
| 126 | + |
| 127 | + Job job = new Job(context, definition); |
| 128 | + |
| 129 | + // Start the job |
| 130 | + job.start(); |
| 131 | + |
| 132 | + // Capture and run the initialization task |
| 133 | + ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class); |
| 134 | + verify(executor).submit(captor.capture()); |
| 135 | + captor.getValue().run(); |
| 136 | + |
| 137 | + // If the bug is fixed, the job should finish without sleeping/spinning |
| 138 | + verify(executor, times(0)).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class)); |
| 139 | + } |
| 140 | + |
| 141 | + @Test(timeout = 5000) |
| 142 | + public void testInsufficientPools_WaitForTargetsFalse() throws Exception { |
| 143 | + // Setup a job with some queued tasks |
| 144 | + PnfsId pnfsId = new PnfsId("000000000000000000000001"); |
| 145 | + CacheEntry entry = mock(CacheEntry.class); |
| 146 | + when(entry.getPnfsId()).thenReturn(pnfsId); |
| 147 | + |
| 148 | + // Mock repository to return the entry |
| 149 | + when(repository.iterator()).thenReturn(Collections.singletonList(pnfsId).iterator()); |
| 150 | + when(repository.getEntry(pnfsId)).thenReturn(entry); |
| 151 | + |
| 152 | + FileAttributes attributes = new FileAttributes(); |
| 153 | + attributes.setLocations(Collections.emptyList()); |
| 154 | + when(entry.getFileAttributes()).thenReturn(attributes); |
| 155 | + |
| 156 | + // Mock pool list to be valid but empty |
| 157 | + when(poolList.isValid()).thenReturn(true); |
| 158 | + when(poolList.getPools()).thenReturn(ImmutableList.of()); |
| 159 | + |
| 160 | + // Mock filter to accept the entry |
| 161 | + when(filter.test(entry)).thenReturn(true); |
| 162 | + |
| 163 | + Job job = new Job(context, definition); |
| 164 | + job.start(); |
| 165 | + |
| 166 | + // Capture and run the initialization task |
| 167 | + ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class); |
| 168 | + verify(executor).submit(captor.capture()); |
| 169 | + captor.getValue().run(); |
| 170 | + |
| 171 | + // If the fix works, the task should complete (with insufficient replicas) and NOT retry. |
| 172 | + // So getPools() should be called at least once. |
| 173 | + verify(poolList, atLeast(1)).getPools(); |
| 174 | + |
| 175 | + // Verify that Job did NOT sleep (schedule was not called for sleeping) |
| 176 | + verify(executor, times(0)).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class)); |
| 177 | + } |
| 178 | + |
| 179 | + @Test(timeout = 5000) |
| 180 | + public void testInsufficientPools_WaitForTargetsTrue() throws Exception { |
| 181 | + // Setup a job with some queued tasks |
| 182 | + PnfsId pnfsId = new PnfsId("000000000000000000000001"); |
| 183 | + CacheEntry entry = mock(CacheEntry.class); |
| 184 | + when(entry.getPnfsId()).thenReturn(pnfsId); |
| 185 | + |
| 186 | + // Mock repository to return the entry |
| 187 | + when(repository.iterator()).thenReturn(Collections.singletonList(pnfsId).iterator()); |
| 188 | + when(repository.getEntry(pnfsId)).thenReturn(entry); |
| 189 | + |
| 190 | + FileAttributes attributes = new FileAttributes(); |
| 191 | + attributes.setLocations(Collections.emptyList()); |
| 192 | + when(entry.getFileAttributes()).thenReturn(attributes); |
| 193 | + |
| 194 | + // Mock pool list to be valid but empty |
| 195 | + when(poolList.isValid()).thenReturn(true); |
| 196 | + when(poolList.getPools()).thenReturn(ImmutableList.of()); |
| 197 | + |
| 198 | + // Mock filter to accept the entry |
| 199 | + when(filter.test(entry)).thenReturn(true); |
| 200 | + |
| 201 | + // Create definition with waitForTargets = true |
| 202 | + definition = createJobDefinition(true); |
| 203 | + |
| 204 | + Job job = new Job(context, definition); |
| 205 | + job.start(); |
| 206 | + |
| 207 | + // Capture and run the initialization task |
| 208 | + ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class); |
| 209 | + verify(executor).submit(captor.capture()); |
| 210 | + captor.getValue().run(); |
| 211 | + |
| 212 | + // Task should NOT start. Job should sleep immediately. |
| 213 | + |
| 214 | + // Verify that Job scheduled a sleep |
| 215 | + verify(executor, times(1)).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class)); |
| 216 | + |
| 217 | + // Verify that execute was NOT called (Task was not started) |
| 218 | + verify(executor, times(0)).execute(any(Runnable.class)); |
| 219 | + } |
| 220 | +} |
0 commit comments