Guest User

Untitled

a guest
Apr 21st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 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 RemoteServiceTestCommand {
  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. var threadPoolProperties = HystrixThreadPoolProperties.Setter()
  59. .withMaxQueueSize(1)
  60. .withCoreSize(1)
  61. .withQueueSizeRejectionThreshold(1);
  62.  
  63. var config = baseConfig
  64. .andCommandPropertiesDefaults(circuitBreakerProperties)
  65. .andThreadPoolPropertiesDefaults(threadPoolProperties);
  66.  
  67. assertNull(callRemoteService(config, 1_000L));
  68. assertNull(callRemoteService(config, 1_000L));
  69. assertNull(callRemoteService(config, 500L));
  70.  
  71. TimeUnit.SECONDS.sleep(2L);
  72.  
  73. assertEquals(SUCCESS, callRemoteService(config, 500L));
  74. }
  75.  
  76. private String callRemoteService(HystrixCommand.Setter config, long timeout) {
  77. try {
  78. return new RemoteServiceCommand(config, new RemoteService(timeout)).execute();
  79. } catch (HystrixRuntimeException e) {
  80. var cause = e.getCause();
  81. if (cause.getMessage() != null) {
  82. LOGGER.info("{} {}", e.getMessage(), cause.getMessage());
  83. } else {
  84. LOGGER.info(e.getMessage());
  85. }
  86. return null;
  87. }
  88. }
  89. }
Add Comment
Please, Sign In to add comment