Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
122 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,7 @@ | ||
package geode.kafka.source; | ||
|
||
import java.util.concurrent.BlockingQueue; | ||
import java.util.function.Supplier; | ||
|
||
public interface EventBufferSupplier extends Supplier<BlockingQueue<GeodeEvent>> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,37 @@ | ||
package geode.kafka.source; | ||
|
||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import java.util.function.Supplier; | ||
|
||
public class SharedEventBufferSupplier implements EventBufferSupplier { | ||
|
||
private static BlockingQueue<GeodeEvent> eventBuffer; | ||
|
||
public SharedEventBufferSupplier(int size) { | ||
recreateEventBufferIfNeeded(size); | ||
} | ||
|
||
BlockingQueue recreateEventBufferIfNeeded(int size) { | ||
if (eventBuffer == null || (eventBuffer.size() + eventBuffer.remainingCapacity()) != size) { | ||
synchronized (GeodeKafkaSource.class) { | ||
if (eventBuffer == null || (eventBuffer.size() + eventBuffer.remainingCapacity()) != size) { | ||
BlockingQueue<GeodeEvent> oldEventBuffer = eventBuffer; | ||
eventBuffer = new LinkedBlockingQueue<>(size); | ||
if (oldEventBuffer != null) { | ||
eventBuffer.addAll(oldEventBuffer); | ||
} | ||
} | ||
} | ||
} | ||
return eventBuffer; | ||
} | ||
|
||
/** | ||
* Callers should not store a reference to this and instead always call get to make sure we always use the latest buffer | ||
* Buffers themselves shouldn't change often but in cases where we want to modify the size | ||
*/ | ||
public BlockingQueue<GeodeEvent> get() { | ||
return eventBuffer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,51 @@ | ||
package geode.kafka.source; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.concurrent.BlockingQueue; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.mockito.Mockito.mock; | ||
|
||
public class SharedEventBufferSupplierTest { | ||
|
||
@Test | ||
public void creatingNewSharedEventSupplierShouldCreateInstance() { | ||
SharedEventBufferSupplier supplier = new SharedEventBufferSupplier(1); | ||
assertNotNull(supplier.get()); | ||
} | ||
|
||
@Test | ||
public void alreadySharedEventSupplierShouldReturnSameInstanceOfEventBuffer() { | ||
SharedEventBufferSupplier supplier = new SharedEventBufferSupplier(1); | ||
BlockingQueue<GeodeEvent> queue = supplier.get(); | ||
supplier = new SharedEventBufferSupplier(1); | ||
assertEquals(queue, supplier.get()); | ||
} | ||
|
||
@Test | ||
public void newEventBufferShouldBeReflectedInAllSharedSuppliers() { | ||
SharedEventBufferSupplier supplier = new SharedEventBufferSupplier(1); | ||
SharedEventBufferSupplier newSupplier = new SharedEventBufferSupplier(2); | ||
assertEquals(supplier.get(), newSupplier.get()); | ||
} | ||
|
||
@Test | ||
public void newEventBufferSuppliedShouldNotBeTheOldQueue() { | ||
SharedEventBufferSupplier supplier = new SharedEventBufferSupplier(1); | ||
BlockingQueue<GeodeEvent> queue = supplier.get(); | ||
SharedEventBufferSupplier newSupplier = new SharedEventBufferSupplier(2); | ||
assertNotEquals(queue, newSupplier.get()); | ||
} | ||
|
||
@Test | ||
public void newEventBufferShouldContainAllEventsFromTheOldSupplier() { | ||
SharedEventBufferSupplier supplier = new SharedEventBufferSupplier(1); | ||
GeodeEvent geodeEvent = mock(GeodeEvent.class); | ||
supplier.get().add(geodeEvent); | ||
SharedEventBufferSupplier newSupplier = new SharedEventBufferSupplier(2); | ||
assertEquals(geodeEvent, newSupplier.get().poll()); | ||
} | ||
} |