Last active
August 29, 2015 14:16
-
-
Save gfrison/a8d31c761040203b4014 to your computer and use it in GitHub Desktop.
embedded (without spring boot) spring cloud netflix-hystrix example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = {HistrixIT.Config.class}) | |
public class HistrixIT | |
{ | |
@Autowired | |
private RemoteService service; | |
private static Function<String, String> function = mock(Function.class); | |
@Test | |
public void synchDefault() | |
{ | |
assertThat(service.synch(), equalTo("default")); | |
} | |
@Test | |
public void asynchDefault() throws ExecutionException, InterruptedException | |
{ | |
assertThat(service.asynch().get(), equalTo("default")); | |
} | |
@Test | |
public void observableDefault() throws ExecutionException, InterruptedException | |
{ | |
service.observable().subscribe(s -> assertThat(s, equalTo("default"))); | |
} | |
@Test(expected = IllegalArgumentException.class) | |
public void ignoredException() | |
{ | |
assertThat(service.ignoreException(), equalTo("default")); | |
} | |
@Test | |
public void cached() throws Exception | |
{ | |
when(function.apply(anyString())).thenReturn("return"); | |
assertThat(service.cached("id"), equalTo("return")); | |
verify(function, times(1)).apply(anyString()); | |
reset(function); | |
assertThat(service.cached("id"), equalTo("return")); | |
verify(function, times(0)).apply(anyString()); | |
} | |
public static class TestService implements RemoteService | |
{ | |
@HystrixCommand(fallbackMethod = "defaultValue") | |
public String synch() | |
{ | |
throw new RuntimeException(); | |
} | |
@HystrixCommand(fallbackMethod = "defaultValue") | |
public Future<String> asynch() | |
{ | |
return new AsyncResult() | |
{ | |
@Override | |
public String invoke() | |
{ | |
throw new RuntimeException(); | |
} | |
}; | |
} | |
@HystrixCommand(fallbackMethod = "defaultValue") | |
public Observable<String> observable() | |
{ | |
return new ObservableResult() | |
{ | |
@Override | |
public String invoke() | |
{ | |
throw new RuntimeException(); | |
} | |
}; | |
} | |
@HystrixCommand(fallbackMethod = "defaultValue", ignoreExceptions = IllegalArgumentException.class) | |
public String ignoreException() | |
{ | |
throw new IllegalArgumentException(); | |
} | |
@CacheResult | |
@HystrixCommand | |
public String cached(@CacheKey String id) | |
{ | |
return function.apply(id); | |
} | |
public String defaultValue() | |
{ | |
return "default"; | |
} | |
} | |
public static interface RemoteService | |
{ | |
public String synch(); | |
public Future<String> asynch(); | |
public Observable<String> observable(); | |
public String ignoreException(); | |
public String cached(String id); | |
} | |
@Configuration | |
@EnableAspectJAutoProxy | |
public static class Config | |
{ | |
@PostConstruct | |
public void init() | |
{ | |
HystrixRequestContext.initializeContext(); | |
} | |
@Bean | |
public HystrixCommandAspect hystrixCommandAspect() | |
{ | |
return new HystrixCommandAspect(); | |
} | |
@Bean | |
public TestService testService() | |
{ | |
return new TestService(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment