Guest User

Untitled

a guest
May 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. public class RemoteServiceCommand extends HystrixCommand<String> {
  2. private RemoteService remoteService;
  3.  
  4. RemoteServiceCommand(Setter config, RemoteService remoteService) {
  5. super(config);
  6. this.remoteService = remoteService;
  7. }
  8.  
  9. @Override
  10. protected String run() throws Exception {
  11. return remoteService.execute();
  12. }
  13. }
  14.  
  15.  
  16. public class RemoteService {
  17. static final String SUCCESS = "Success";
  18.  
  19. private long wait;
  20.  
  21. RemoteService(long wait) {
  22. this.wait = wait;
  23. }
  24.  
  25. String execute() throws InterruptedException {
  26. TimeUnit.MILLISECONDS.sleep(wait);
  27. return SUCCESS;
  28. }
  29. }
  30.  
  31. public class RemoteServiceTest {
  32. private static final Logger LOGGER = LoggerFactory.getLogger(RemoteServiceTestCommand.class);
  33.  
  34. private static final HystrixCommandGroupKey GROUP_KEY = Factory.asKey("RemoteServiceGroup");
  35.  
  36. private HystrixCommand.Setter baseConfig;
  37.  
  38. @BeforeEach
  39. void setUp() {
  40. baseConfig = HystrixCommand.Setter.withGroupKey(GROUP_KEY);
  41. }
  42.  
  43. @Test
  44. void testWithTimeoutOf100AndDefaultSettings() {
  45. var remoteService = new RemoteService(100L);
  46.  
  47. assertEquals(SUCCESS, new RemoteServiceCommand(baseConfig, remoteService).execute());
  48. }
  49.  
  50. @Test
  51. void testWithCircuitBreakerSetup() throws InterruptedException {
  52. var circuitBreakerProperties = HystrixCommandProperties.Setter()
  53. .withExecutionTimeoutInMilliseconds(1_000)
  54. .withCircuitBreakerSleepWindowInMilliseconds(2_000)
  55. .withExecutionIsolationStrategy(ExecutionIsolationStrategy.THREAD)
  56. .withCircuitBreakerEnabled(true)
  57. .withCircuitBreakerRequestVolumeThreshold(1);
  58.  
  59. var threadPoolProperties = HystrixThreadPoolProperties.Setter()
  60. .withMaxQueueSize(1)
  61. .withCoreSize(1)
  62. .withQueueSizeRejectionThreshold(1);
  63.  
  64. var config = baseConfig
  65. .andCommandPropertiesDefaults(circuitBreakerProperties)
  66. .andThreadPoolPropertiesDefaults(threadPoolProperties);
  67.  
  68. assertNull(callRemoteService(config, 1_000L));
  69. assertNull(callRemoteService(config, 1_000L));
  70. assertNull(callRemoteService(config, 500L));
  71.  
  72. TimeUnit.SECONDS.sleep(2L);
  73.  
  74. assertEquals(SUCCESS, callRemoteService(config, 500L));
  75. }
  76.  
  77. private String callRemoteService(HystrixCommand.Setter config, long timeout) {
  78. try {
  79. return new RemoteServiceCommand(config, new RemoteService(timeout)).execute();
  80. } catch (HystrixRuntimeException e) {
  81. var cause = e.getCause();
  82. if (cause.getMessage() != null) {
  83. LOGGER.info("{} {}", e.getMessage(), cause.getMessage());
  84. } else {
  85. LOGGER.info(e.getMessage());
  86. }
  87.  
  88. return null;
  89. }
  90. }
  91. }
Add Comment
Please, Sign In to add comment