|
public Builder fastForward(FastForward client) { |
|
this.client = client; |
|
return this; |
|
} |
The current setup for FastForwardReporter requires a user to pass in a real FastForward client, which is not easily unit testable -- in a production environment the FastForward client would be already running within the container, but in a local unit testing environment this can be pretty cumbersome to set up.
It would be simpler if we had some kind of interface or abstract class to allow the user to sub in different implementations of the FastForward client -- the only method it would have to implement is public void send(Metric metric) { }.
Quick code sketch of what I'm thinking:
public abstract class FastForwardClient {
public abstract void send(Metric metric) throws IOException;
// Default implementation
public static FastForwardClient create(String host, int port) throws UnknownHostException, SocketException {
return new FastForwardClient() {
private final FastForward underlying = FastForward.setup(host, port);
@Override
public void send(Metric metric) throws IOException {
underlying.send(metric);
}
};
}
}
Then the FastForwardReporter's builder would look like:
public Builder fastForward(FastForwardClient client) {
this.client = client;
return this;
}
public FastForwardReporter build() throws IOException {
final FastForwardClient client = this.client != null ? this.client : FastForwardClient.create(host, port);
...
}
And in a unit-testing context we could easily sub in something like this that's easy to validate:
public class StubbedFastForwardClient extends FastForwardClient {
private final List<Metric> collectedMetrics;
public StubbedFastForwardClient(List<Metric> collectedMetrics) {
this.collectedMetrics = new ArrayList<>();
}
@Override
public void send(Metric metric) throws IOException {
collectedMetrics.add(metric);
}
public List<Metric> getCollectedMetrics() {
return collectedMetrics;
}
}
lmk what you think! I'm happy to make the PR myself if this seems reasonable.
(Not sure if this issue belongs more in ffwd-client-java... it would probably be better to fix it there, but it's also a bigger/more impactful change to make.)
semantic-metrics/ffwd-reporter/src/main/java/com/spotify/metrics/ffwd/FastForwardReporter.java
Lines 156 to 159 in 3890ddd
The current setup for
FastForwardReporterrequires a user to pass in a realFastForwardclient, which is not easily unit testable -- in a production environment theFastForwardclient would be already running within the container, but in a local unit testing environment this can be pretty cumbersome to set up.It would be simpler if we had some kind of interface or abstract class to allow the user to sub in different implementations of the FastForward client -- the only method it would have to implement is
public void send(Metric metric) { }.Quick code sketch of what I'm thinking:
Then the
FastForwardReporter's builder would look like:And in a unit-testing context we could easily sub in something like this that's easy to validate:
lmk what you think! I'm happy to make the PR myself if this seems reasonable.
(Not sure if this issue belongs more in ffwd-client-java... it would probably be better to fix it there, but it's also a bigger/more impactful change to make.)