Skip to content

Instantly share code, notes, and snippets.

@mp911de
Created July 7, 2016 09:06
Show Gist options
  • Save mp911de/327cb876117bc1d74049aff25e5509cb to your computer and use it in GitHub Desktop.
Save mp911de/327cb876117bc1d74049aff25e5509cb to your computer and use it in GitHub Desktop.
Testcase for DATAREDIS-531
package com.example;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import java.util.Map.Entry;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Test case for:
*
* @author
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class SpringDataRedisTests {
@Autowired
RedisOperations<String, String> redisOperations;
ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 1, TimeUnit.MINUTES, new LinkedBlockingDeque<>());
@Test
public void contextLoads() throws InterruptedException {
BoundHashOperations<String, String, String> hash = redisOperations.boundHashOps("hash");
AtomicReference<Exception> exception = new AtomicReference<>();
// Create some keys so that SCAN requires a while to return all data.
for (int i = 0; i < 10000; i++) {
hash.put("key-" + i, "value");
}
// Concurrent access
for (int i = 0; i < 10; i++) {
executor.submit(new Runnable() {
@Override
public void run() {
try {
Cursor<Entry<Object, Object>> cursorMap = redisOperations.boundHashOps("hash")
.scan(ScanOptions.scanOptions().match("*").count(100).build());
// This line invokes the lazy SCAN invocation
while (cursorMap.hasNext()) {
cursorMap.next();
}
} catch (Exception e) {
exception.set(e);
}
}
});
}
// Wait until work is finished
while (executor.getActiveCount() > 0) {
Thread.sleep(100);
}
executor.shutdown();
assertThat(exception.get(), is(nullValue()));
}
@SpringBootApplication
@Configuration
static class Config {
@Bean
RedisConnectionFactory redisConnectionFactory() {
return new JedisConnectionFactory();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment