Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public LuceneEventListener(InternalCache cache, RepositoryManager repositoryMana
@Override
public void close() {}

void logDebugMessage(String msg, Exception e) {
logger.debug(msg, e);
}

@Override
public boolean processEvents(List<AsyncEvent> events) {
try {
Expand Down Expand Up @@ -103,13 +107,13 @@ protected boolean process(final List<AsyncEvent> events) {
}
return true;
} catch (BucketNotFoundException | RegionDestroyedException | PrimaryBucketException e) {
logger.debug("Bucket not found while saving to lucene index: " + e.getMessage(), e);
logDebugMessage("Bucket not found while saving to lucene index: " + e.getMessage(), e);
return false;
} catch (CacheClosedException e) {
logger.debug("Unable to save to lucene index, cache has been closed", e);
logDebugMessage("Unable to save to lucene index, cache has been closed", e);
return false;
} catch (AlreadyClosedException e) {
logger.debug("Unable to commit, the lucene index is already closed", e);
logDebugMessage("Unable to commit, the lucene index is already closed", e);
return false;
} catch (IOException e) {
throw new InternalGemFireError("Unable to save to lucene index", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.contains;
import static org.mockito.ArgumentMatchers.startsWith;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -36,20 +37,16 @@
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import org.apache.logging.log4j.Logger;
import org.apache.lucene.store.AlreadyClosedException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import org.apache.geode.InternalGemFireError;
import org.apache.geode.cache.CacheClosedException;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.asyncqueue.AsyncEvent;
import org.apache.geode.cache.lucene.internal.repository.IndexRepository;
Expand All @@ -58,17 +55,13 @@
import org.apache.geode.internal.cache.BucketNotFoundException;
import org.apache.geode.internal.cache.EntrySnapshot;
import org.apache.geode.internal.cache.InternalCache;
import org.apache.geode.internal.logging.LogService;
import org.apache.geode.test.fake.Fakes;
import org.apache.geode.test.junit.categories.LuceneTest;

/**
* Unit test that async event listener dispatched the events to the appropriate repository.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({LuceneEventListener.class, LogService.class})
@Category({LuceneTest.class})
@PowerMockIgnore({"*.UnitTest", "*.LuceneTest"})
public class LuceneEventListenerJUnitTest {

private RepositoryManager manager;
Expand All @@ -80,6 +73,7 @@ public void setup() {
cache = Fakes.cache();
manager = Mockito.mock(RepositoryManager.class);
listener = new LuceneEventListener(cache, manager);
listener = spy(listener);
}

@After
Expand Down Expand Up @@ -154,19 +148,43 @@ public void testProcessBatch() throws Exception {
}

@Test
public void shouldHandleBucketNotFoundExceptionWithoutLoggingError()
public void shouldHandleBucketNotFoundException()
throws BucketNotFoundException {
Mockito.when(manager.getRepository(any(), any(), any()))
.thenThrow(BucketNotFoundException.class);

PowerMockito.mockStatic(LogService.class);
Logger logger = Mockito.mock(Logger.class);
Mockito.when(LogService.getLogger()).thenReturn(logger);
AsyncEvent event = Mockito.mock(AsyncEvent.class);
boolean result = listener.processEvents(Arrays.asList(event));
assertFalse(result);
verify(listener, times(1))
.logDebugMessage(startsWith("Bucket not found"), any(BucketNotFoundException.class));
}

@Test
public void shouldHandleCacheClosedException()
throws BucketNotFoundException {
Mockito.when(manager.getRepository(any(), any(), any()))
.thenThrow(CacheClosedException.class);

AsyncEvent event = Mockito.mock(AsyncEvent.class);
boolean result = listener.processEvents(Arrays.asList(event));
assertFalse(result);
verify(listener, times(1))
.logDebugMessage(contains("cache has been closed"), any(CacheClosedException.class));
}

@Test
public void shouldHandleAlreadyClosedException()
throws BucketNotFoundException {
Mockito.when(manager.getRepository(any(), any(), any()))
.thenThrow(AlreadyClosedException.class);

AsyncEvent event = Mockito.mock(AsyncEvent.class);
boolean result = listener.processEvents(Arrays.asList(event));
assertFalse(result);
verify(logger, never()).error(anyString(), any(Exception.class));
verify(listener, times(1))
.logDebugMessage(contains("the lucene index is already closed"),
any(AlreadyClosedException.class));
}

@Test
Expand Down