It is often useful when unit testing instrumented code
to verify that the expected instrumenation is being recorded
in addition to verifying the correctness and performance of
that code. This commit adds a mock client that enables that
form of verification without having to spin up a UDP listener.
The basic approach here is to provide a level of indirection
between the statsd.Statsd interface and the methods use to
implement that interface. That allows arbitrary behavior to
be invoked in response to different methods invoked on the
MockStatsdClient. If no such method or methods are provided,
calling a method on the MockStatsdClient will just be no-op.
That alone would allow for test cases to provide their own
mock functions. But the common use case for testing is to record
the events sent to the mock client and verify that the
instrumentation methods one expected to be invoked were invoked
with the proper arguments. To support this common use case,
a series of flient builders are provided for MockStatsdClient
that will allow a client to provide a slice to be used to record
events. The test file provides examples of using this fluent builder.
It is often useful when unit testing instrumented code to verify that the expected instrumenation is being recorded in addition to verifying the correctness and performance of that code. This commit adds a mock client that enables that form of verification without having to spin up a UDP listener.
The basic approach here is to provide a level of indirection between the statsd.Statsd interface and the methods use to implement that interface. That allows arbitrary behavior to be invoked in response to different methods invoked on the MockStatsdClient. If no such method or methods are provided, calling a method on the MockStatsdClient will just be no-op.
That alone would allow for test cases to provide their own mock functions. But the common use case for testing is to record the events sent to the mock client and verify that the instrumentation methods one expected to be invoked were invoked with the proper arguments. To support this common use case, a series of flient builders are provided for MockStatsdClient that will allow a client to provide a slice to be used to record events. The test file provides examples of using this fluent builder.