Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.hazelcast.util;
- import com.google.common.base.Function;
- import com.google.common.base.Splitter;
- import com.google.common.collect.ImmutableList;
- import com.google.common.util.concurrent.MoreExecutors;
- import com.hazelcast.core.Hazelcast;
- import java.util.*;
- import org.junit.*;
- import static org.junit.Assert.assertEquals;
- public class ConcurrentCacheLoaderTest {
- @AfterClass public static void shutdown() {
- Hazelcast.shutdownAll();
- }
- final Iterable<String> inputs = ImmutableList.of(
- "abc def",
- "ghij klm n op",
- "qr st uv",
- "wxyz"
- );
- @Test public void testConcurrentCacheLoader() throws InterruptedException {
- ConcurrentCacheLoader<String, List<String>> ccl =
- ConcurrentCacheLoader.<String, List<String>>newBuilder()
- .build(new NotSerializableSplitFunction());
- commonAssertions(ccl.loadAll(inputs));
- }
- @Test public void testDistributedCacheLoader() throws InterruptedException {
- ConcurrentCacheLoader<String, List<String>> ccl =
- ConcurrentCacheLoader.<String, List<String>>newBuilder()
- .usingDefaultDistributedService()
- .build(new SerializableSplitFunction());
- commonAssertions(ccl.loadAll(inputs));
- }
- @Test(expected=IllegalArgumentException.class)
- public void testKeyFunctionWithStandardExec() throws InterruptedException {
- ConcurrentCacheLoader<String, List<String>> ccl =
- ConcurrentCacheLoader.<String, List<String>>newBuilder()
- .using(MoreExecutors.sameThreadExecutor())
- .byKey()
- .build(new SerializableSplitFunction());
- }
- @Test(expected=IllegalArgumentException.class)
- public void testFunctionNotSerializableSplitFunction() throws InterruptedException {
- ConcurrentCacheLoader<String, List<String>> ccl =
- ConcurrentCacheLoader.<String, List<String>>newBuilder()
- .usingDefaultDistributedService()
- .byKey()
- .build(new NotSerializableSplitFunction());
- }
- @Test(expected=IllegalArgumentException.class)
- public void testNonHazelcastExecutorWithKeyFunction() throws InterruptedException {
- ConcurrentCacheLoader<String, List<String>> ccl =
- ConcurrentCacheLoader.<String, List<String>>newBuilder()
- .usingDefaultDistributedService()
- .byKey()
- .using(MoreExecutors.sameThreadExecutor())
- .build(new NotSerializableSplitFunction());
- }
- private void commonAssertions(Map<String, List<String>> result) {
- assertEquals(4, result.size());
- assertEquals(ImmutableList.of("abc", "def"), result.get("abc def"));
- assertEquals(ImmutableList.of("wxyz"), result.get("wxyz"));
- }
- static class NotSerializableSplitFunction implements Function<String, List<String>> {
- public List<String> apply(String text) {
- return ImmutableList.copyOf(SPLITTER.split(text));
- }
- }
- static class SerializableSplitFunction extends NotSerializableSplitFunction
- implements java.io.Serializable {
- }
- private static final Splitter SPLITTER = Splitter.on(" ").trimResults();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement