Search in sources :

Example 1 with VersionFilteredServiceDiscovered

use of co.cask.cdap.gateway.discovery.VersionFilteredServiceDiscovered in project cdap by caskdata.

the class VersionFilteredServiceDiscoveredTest method testVersion.

@Test
public void testVersion() throws Exception {
    ProgramId serviceId = new ApplicationId("n1", "a1").service("s1");
    String discoverableName = ServiceDiscoverable.getName(serviceId);
    List<Discoverable> candidates = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        candidates.add(new Discoverable(discoverableName, null, Bytes.toBytes(Integer.toString(i))));
        candidates.add(new Discoverable(discoverableName, null, Bytes.toBytes(Integer.toString(i))));
    }
    SimpleServiceDiscovered serviceDiscovered = new SimpleServiceDiscovered(candidates);
    // With '1' as the filter, only that version should be returned
    VersionFilteredServiceDiscovered filteredServiceDiscovered = new VersionFilteredServiceDiscovered(serviceDiscovered, "1");
    Iterator<Discoverable> filteredIterator = filteredServiceDiscovered.iterator();
    while (filteredIterator.hasNext()) {
        Assert.assertArrayEquals("1".getBytes(), filteredIterator.next().getPayload());
    }
    // With null, all the versions should be returned
    filteredServiceDiscovered = new VersionFilteredServiceDiscovered(serviceDiscovered, null);
    filteredIterator = filteredServiceDiscovered.iterator();
    Set<String> versions = new HashSet<>();
    while (filteredIterator.hasNext()) {
        versions.add(Bytes.toString(filteredIterator.next().getPayload()));
    }
    Assert.assertTrue(versions.size() == 5);
}
Also used : ServiceDiscoverable(co.cask.cdap.common.service.ServiceDiscoverable) Discoverable(org.apache.twill.discovery.Discoverable) VersionFilteredServiceDiscovered(co.cask.cdap.gateway.discovery.VersionFilteredServiceDiscovered) ArrayList(java.util.ArrayList) ProgramId(co.cask.cdap.proto.id.ProgramId) ApplicationId(co.cask.cdap.proto.id.ApplicationId) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ServiceDiscoverable (co.cask.cdap.common.service.ServiceDiscoverable)1 VersionFilteredServiceDiscovered (co.cask.cdap.gateway.discovery.VersionFilteredServiceDiscovered)1 ApplicationId (co.cask.cdap.proto.id.ApplicationId)1 ProgramId (co.cask.cdap.proto.id.ProgramId)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Discoverable (org.apache.twill.discovery.Discoverable)1 Test (org.junit.Test)1