forked from Netflix/Prana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
110 additions
and
5 deletions.
There are no files selected for viewing
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
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
19 changes: 19 additions & 0 deletions
19
src/main/java/com/netflix/prana/service/EurekaHostService.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.netflix.prana.service; | ||
|
||
import com.netflix.appinfo.InstanceInfo; | ||
import com.netflix.discovery.DiscoveryClient; | ||
|
||
import java.util.List; | ||
|
||
public class EurekaHostService implements HostService { | ||
|
||
private DiscoveryClient discoveryClient; | ||
|
||
public EurekaHostService(DiscoveryClient discoveryClient) { | ||
this.discoveryClient = discoveryClient; | ||
} | ||
@Override | ||
public List<InstanceInfo> getHosts(String appName) { | ||
return discoveryClient.getApplication(appName).getInstances(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.netflix.prana.service; | ||
|
||
import com.google.inject.Inject; | ||
import com.netflix.appinfo.InstanceInfo; | ||
|
||
import java.util.List; | ||
|
||
public interface HostService { | ||
|
||
public List<InstanceInfo> getHosts(String appName); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/netflix/prana/service/ServiceModule.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.netflix.prana.service; | ||
|
||
import com.google.inject.AbstractModule; | ||
import com.google.inject.Scopes; | ||
|
||
public class ServiceModule extends AbstractModule { | ||
@Override | ||
protected void configure() { | ||
bind(HostService.class).to(EurekaHostService.class).in(Scopes.SINGLETON); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/test/java/com/netflix/prana/http/api/HostsHandlerTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.netflix.prana.http.api; | ||
|
||
import com.netflix.appinfo.InstanceInfo; | ||
import com.netflix.discovery.DiscoveryClient; | ||
import com.netflix.discovery.shared.Application; | ||
import com.netflix.prana.service.HostService; | ||
import io.netty.buffer.ByteBuf; | ||
import io.netty.channel.ChannelOption; | ||
import io.reactivex.netty.RxNetty; | ||
import io.reactivex.netty.pipeline.PipelineConfigurators; | ||
import io.reactivex.netty.protocol.http.client.HttpClient; | ||
import io.reactivex.netty.protocol.http.client.HttpClientRequest; | ||
import io.reactivex.netty.protocol.http.server.HttpServer; | ||
import org.junit.*; | ||
|
||
import java.util.ArrayList; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* Created by dchoudhury on 10/28/14. | ||
*/ | ||
public class HostsHandlerTest { | ||
|
||
private HttpServer<ByteBuf, ByteBuf> server; | ||
|
||
private HttpClient<ByteBuf, ByteBuf> client; | ||
|
||
private final int port = 23455; | ||
|
||
private HostService hostService; | ||
|
||
@Before | ||
public void setUp() { | ||
hostService = mock(HostService.class); | ||
ArrayList<InstanceInfo> instanceInfos = new ArrayList<>(); | ||
instanceInfos.add(InstanceInfo.Builder.newBuilder().setAppName("foo").setVIPAddress("bar").setHostName("host1").build()); | ||
instanceInfos.add(InstanceInfo.Builder.newBuilder().setAppName("foo").setVIPAddress("bar").setHostName("host2").build()); | ||
when(hostService.getHosts("foo")).thenReturn(instanceInfos); | ||
server = RxNetty.newHttpServerBuilder(port, new HostsHandler(hostService)) | ||
.pipelineConfigurator(PipelineConfigurators.<ByteBuf, ByteBuf>httpServerConfigurator()).build(); | ||
server.start(); | ||
client = RxNetty.<ByteBuf, ByteBuf>newHttpClientBuilder("localhost", port) | ||
.pipelineConfigurator(PipelineConfigurators.<ByteBuf, ByteBuf>httpClientConfigurator()) | ||
.channelOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2000) | ||
.build(); | ||
} | ||
|
||
@After | ||
public void tearDown() throws InterruptedException { | ||
server.shutdown(); | ||
} | ||
|
||
@Test | ||
public void shouldReturnAListOfHostsWhenBothVipAndAppIsSpecified() { | ||
HttpClientRequest<ByteBuf> request = HttpClientRequest.<ByteBuf>createGet("/hosts?appName=foo&vip=bar"); | ||
String response = Utils.getResponse(request, client); | ||
Assert.assertEquals("[\"host1\",\"host2\"]", response); | ||
} | ||
|
||
} |