@Override
  public List<ServiceInstance> getInstances(String serviceName) {
    if (Objects.equals(serviceName, this.selfInstance.getServiceName())) {
      List<ServiceInstance> serviceInstances = this.delegate.getInstances(serviceName);
      if (containSelf(serviceInstances, this.selfInstance)) {
        // contains self instance already
        return serviceInstances;
      }

      // add self instance to result
      List<ServiceInstance> result = new ArrayList<>(serviceInstances.size() + 1);
      result.add(this.selfInstance);
      result.addAll(serviceInstances);
      return result;
    } else {
      return this.delegate.getInstances(serviceName);
    }
  }
@Test
  void getInstances_same_service_name_without_self() {
    final String otherServiceName = "other-service";
    DatabaseDiscoveryClient client = Mockito.mock(DatabaseDiscoveryClient.class);
    Mockito.when(client.getInstances(otherServiceName))
        .thenReturn(
            Collections.singletonList(
                newServiceInstance(otherServiceName, "http://10.240.34.56:8081/", "beijing")
            )
        );

    final String selfServiceName = "self-service";
    ServiceInstance selfInstance = newServiceInstance(
        selfServiceName, "http://10.240.34.56:8081/", "beijing"
    );
    Mockito.when(client.getInstances(selfServiceName))
        .thenReturn(
            Arrays.asList(
                // same service name but different service instance
                newServiceInstance(selfServiceName, "http://10.240.34.56:8082/", "beijing"),
                newServiceInstance(selfServiceName, "http://10.240.34.56:8083/", "beijing")
            )
        );

    DatabaseDiscoveryClient decorator = new DatabaseDiscoveryClientAlwaysAddSelfInstanceDecoratorImpl(
        client, selfInstance
    );

    List<ServiceInstance> serviceInstances = decorator.getInstances(selfServiceName);
    // because mocked data don't contain self instance
    // after add self instance, there are 3 instances now
    assertEquals(3, serviceInstances.size());

    Mockito.verify(client, Mockito.times(1))
        .getInstances(Mockito.eq(selfServiceName));

    Mockito.verify(client, Mockito.never())
        .getInstances(Mockito.eq(otherServiceName));
  }