Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. import eu.rekawek.toxiproxy.model.Toxic;
  2. import io.lettuce.core.ClientOptions;
  3. import io.lettuce.core.RedisClient;
  4. import io.lettuce.core.RedisURI;
  5. import io.lettuce.core.TimeoutOptions;
  6. import io.lettuce.core.api.StatefulRedisConnection;
  7. import io.lettuce.core.api.async.RedisAsyncCommands;
  8. import org.junit.After;
  9. import org.junit.Before;
  10. import org.junit.BeforeClass;
  11. import org.junit.ClassRule;
  12. import org.junit.Test;
  13. import org.testcontainers.containers.GenericContainer;
  14. import org.testcontainers.containers.Network;
  15. import org.testcontainers.containers.ToxiproxyContainer;
  16.  
  17. import java.time.Duration;
  18.  
  19. import static org.hamcrest.Matchers.closeTo;
  20. import static org.hamcrest.Matchers.lessThanOrEqualTo;
  21. import static org.junit.Assert.assertThat;
  22.  
  23. public class DisconnectTest {
  24. @ClassRule
  25. public static Network network = Network.newNetwork();
  26. @ClassRule
  27. public static GenericContainer redisContainer = new GenericContainer<>("redis:5.0.5-alpine")
  28. .withExposedPorts(6379).withNetwork(network);
  29. @ClassRule
  30. public static ToxiproxyContainer toxiproxy = new ToxiproxyContainer().withNetwork(network);
  31. public static ToxiproxyContainer.ContainerProxy redisProxy;
  32.  
  33. RedisClient redisClient;
  34. StatefulRedisConnection<String, String> connection;
  35. RedisAsyncCommands<String, String> redis;
  36.  
  37. @BeforeClass
  38. public static void setUpClass() {
  39. redisProxy = toxiproxy.getProxy(redisContainer, 6379);
  40. }
  41.  
  42. @Before
  43. public void connectToRedis() {
  44. redisClient = RedisClient.create(
  45. new RedisURI(redisProxy.getContainerIpAddress(), redisProxy.getProxyPort(), Duration.ofMillis(100)));
  46. redisClient.setOptions(ClientOptions.builder()
  47. .disconnectedBehavior(ClientOptions.DisconnectedBehavior.REJECT_COMMANDS)
  48. // enhance with setting socket read or write timeout?
  49. .timeoutOptions(TimeoutOptions.builder().connectionTimeout().build())
  50. .build());
  51. connection = redisClient.connect();
  52. redis = connection.async();
  53. }
  54.  
  55. @After
  56. public void closeRedisClient() {
  57. connection.close();
  58. redisClient.shutdown();
  59. }
  60.  
  61. @After
  62. public void resetProxy() throws Exception {
  63. for (Toxic t : redisProxy.toxics().getAll()) {
  64. t.remove();
  65. }
  66. }
  67.  
  68. // Note: This test is using .get() on all the CompletableFutures to make the test
  69. // easier, but the principle should apply even if programming in a truly async style;
  70. // we would just expect to see the CompletionStages complete exceptionally immediately.
  71. @Test(timeout = 1000)
  72. public void commandsFailImmediatelyAfterTimeout() throws Exception {
  73. redis.set("test", "123").get();
  74.  
  75. // Disconnect and see that command timeout works
  76. redisProxy.setConnectionCut(true);
  77. long start = System.nanoTime();
  78. redis.get("test").exceptionally(ignore -> null).toCompletableFuture().get();
  79. long stop = System.nanoTime();
  80. assertThat((stop - start) / 1000000., closeTo(100., 25.));
  81.  
  82. // Want to detect that connection is not working anymore and further commands should
  83. // be rejected immediately
  84. start = System.nanoTime();
  85. redis.get("test").exceptionally(ignore -> null).toCompletableFuture().get();
  86. stop = System.nanoTime();
  87. assertThat((stop - start) / 1000000., lessThanOrEqualTo(1.));
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement