|
| 1 | +package io.valkey.util; |
| 2 | + |
| 3 | +import java.time.Duration; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.UUID; |
| 6 | + |
| 7 | +import io.valkey.DefaultJedisClientConfig; |
| 8 | +import io.valkey.ExceptionHandler; |
| 9 | +import io.valkey.HostAndPort; |
| 10 | +import io.valkey.HostAndPorts; |
| 11 | +import io.valkey.Jedis; |
| 12 | +import io.valkey.UnifiedJedis; |
| 13 | +import io.valkey.providers.PooledConnectionProvider; |
| 14 | +import org.junit.Assert; |
| 15 | +import org.junit.Before; |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +public class ExceptionHandlerTest { |
| 19 | + private final HostAndPort hostAndPort = HostAndPorts.getRedisServers().get(0); |
| 20 | + private Jedis jedis; |
| 21 | + |
| 22 | + @Before |
| 23 | + public void setUp() { |
| 24 | + jedis = new Jedis(hostAndPort); |
| 25 | + jedis.auth("foobared"); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void retryWithExceptionHandler() { |
| 30 | + int maxAttempts = 10; |
| 31 | + Duration maxTotalRetriesDuration = Duration.ofSeconds(10); |
| 32 | + PooledConnectionProvider provider = new PooledConnectionProvider(hostAndPort, |
| 33 | + DefaultJedisClientConfig.builder().password("foobared").build()); |
| 34 | + ExceptionHandler handler = new ExceptionHandler(); |
| 35 | + handler.register( |
| 36 | + message -> message.contains("WRONGTYPE Operation against a key holding the wrong kind of value"), |
| 37 | + errorMessage -> { |
| 38 | + try { |
| 39 | + System.out.println("Retrying..."); |
| 40 | + Thread.sleep(1000); |
| 41 | + } catch (InterruptedException e) { |
| 42 | + throw new RuntimeException(e); |
| 43 | + } |
| 44 | + } |
| 45 | + ); |
| 46 | + |
| 47 | + String key = UUID.randomUUID().toString(); |
| 48 | + Assert.assertEquals("OK", jedis.set(key, key)); |
| 49 | + new Thread(new Runnable() { |
| 50 | + @Override |
| 51 | + public void run() { |
| 52 | + try { |
| 53 | + // after 5s, del this key. |
| 54 | + Thread.sleep(5000); |
| 55 | + Assert.assertEquals(1, jedis.del(key)); |
| 56 | + } catch (Exception e) { |
| 57 | + e.printStackTrace(); |
| 58 | + } |
| 59 | + } |
| 60 | + }).start(); |
| 61 | + |
| 62 | + UnifiedJedis unifiedJedis = new UnifiedJedis(provider, maxAttempts, maxTotalRetriesDuration, handler); |
| 63 | + Map<String, String> map = unifiedJedis.hgetAll(key); |
| 64 | + Assert.assertTrue(map.isEmpty()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void retryWithExponentialBackoffCallback() { |
| 69 | + int maxAttempts = 4; |
| 70 | + Duration maxTotalRetriesDuration = Duration.ofSeconds(20); |
| 71 | + PooledConnectionProvider provider = new PooledConnectionProvider(hostAndPort, |
| 72 | + DefaultJedisClientConfig.builder().password("foobared").build()); |
| 73 | + String key = UUID.randomUUID().toString(); |
| 74 | + Assert.assertEquals("OK", jedis.set(key, key)); |
| 75 | + ExceptionHandler handler = new ExceptionHandler(); |
| 76 | + class ExponentialBackoffCallback implements ExceptionHandler.ErrorCallback { |
| 77 | + private int attempt = 0; // 计数器 |
| 78 | + |
| 79 | + @Override |
| 80 | + public void onError(String errorMessage) { |
| 81 | + // 计算 sleep 时间(2^attempt 秒) |
| 82 | + int sleepTime = (int) Math.pow(2, attempt); |
| 83 | + try { |
| 84 | + System.out.println("Sleeping for " + sleepTime + " seconds before handling: " + errorMessage); |
| 85 | + Thread.sleep(sleepTime * 1000); // 转换为毫秒 |
| 86 | + if (attempt == maxAttempts - 2) { |
| 87 | + Assert.assertEquals(1, jedis.del(key)); |
| 88 | + } |
| 89 | + } catch (InterruptedException ie) { |
| 90 | + Thread.currentThread().interrupt(); // 恢复中断状态 |
| 91 | + } |
| 92 | + attempt++; // 增加计数 |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + handler.register( |
| 97 | + message -> message.contains("WRONGTYPE Operation against a key holding the wrong kind of value"), |
| 98 | + new ExponentialBackoffCallback() |
| 99 | + ); |
| 100 | + |
| 101 | + UnifiedJedis unifiedJedis = new UnifiedJedis(provider, maxAttempts, maxTotalRetriesDuration, handler); |
| 102 | + long start = System.currentTimeMillis(); |
| 103 | + Map<String, String> map = unifiedJedis.hgetAll(key); |
| 104 | + long end = System.currentTimeMillis(); |
| 105 | + Assert.assertTrue(map.isEmpty()); |
| 106 | + Assert.assertTrue(end - start >= 7000); // 2^0 + 2^1 + 2^2 = 7 seconds |
| 107 | + } |
| 108 | +} |
0 commit comments