Skip to content

Add Sampler<T>, and CompletableSampler<T> and resultOfSampling. #27

@tobyclemson

Description

@tobyclemson

See matchbox for eventually matcher.

Account account = resultOfSampling(new Sampler<Account>(){
  @Override
  public Account takeSample() {
      return repository.findBy(id);
  }

  @Override
  public boolean isComplete(Account account) {
      return account != null;
  }
});

assertThat(account.getBalance(), is(300));

public static <T> T resultOfSampling(Sampler<T> sampler) {
   return resultOfSampling(sampler, new Duration(200), standardSeconds(30));
}

public static <T> T resultOfSampling(Sampler<T> sampler, Duration sampleInterval, Duration sampleDuration) {
   long startTime = System.currentTimeMillis();
   long endTime = startTime + sampleDuration.getMillis();
   T result;
   while(System.currentTimeMillis() < endTime) {
       result = sampler.takeSample();
       if (sampler.isComplete(result)) {
           break;
       }
       try {
           Thread.sleep(sampleInterval.getMillis())
       } catch (InterruptedException exception) {
           throw new RuntimeException(exception);
       }
   }
   return result;
}

public interface Sampler<T> {
   T takeSample();
   boolean isComplete(T sample);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions