-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for DD_DOGSTATSD_URL #217
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
src/test/java/com/timgroup/statsd/BuilderAddressTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package com.timgroup.statsd; | ||
|
||
import java.net.SocketAddress; | ||
import java.net.InetSocketAddress; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
import jnr.unixsocket.UnixSocketAddress; | ||
|
||
import org.junit.Assume; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.Rule; | ||
import org.junit.contrib.java.lang.system.EnvironmentVariables; | ||
import org.junit.runners.MethodSorters; | ||
import org.junit.function.ThrowingRunnable; | ||
|
||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.junit.runners.Parameterized.Parameters; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
@RunWith(Parameterized.class) | ||
public class BuilderAddressTest { | ||
@Rule | ||
public final EnvironmentVariables environmentVariables = new EnvironmentVariables(); | ||
|
||
final String url; | ||
final String host; | ||
final String port; | ||
final String pipe; | ||
final SocketAddress expected; | ||
|
||
public BuilderAddressTest(String url, String host, String port, String pipe, SocketAddress expected) { | ||
this.url = url; | ||
this.host = host; | ||
this.port = port; | ||
this.pipe = pipe; | ||
this.expected = expected; | ||
} | ||
|
||
static boolean isJnrAvailable() { | ||
try { | ||
Class.forName("jnr.unixsocket.UnixDatagramChannel"); | ||
return true; | ||
} catch (ClassNotFoundException e) { | ||
return false; | ||
} | ||
} | ||
|
||
static final private int defaultPort = NonBlockingStatsDClient.DEFAULT_DOGSTATSD_PORT; | ||
|
||
@Parameters | ||
public static Collection<Object[]> parameters() { | ||
ArrayList params = new ArrayList(); | ||
|
||
params.addAll(Arrays.asList(new Object[][]{ | ||
// DD_DOGSTATSD_URL | ||
{ "udp://1.1.1.1", null, null, null, new InetSocketAddress("1.1.1.1", defaultPort) }, | ||
{ "udp://1.1.1.1:9999", null, null, null, new InetSocketAddress("1.1.1.1", 9999) }, | ||
{ "\\\\.\\pipe\\foo", null, null, null, new NamedPipeSocketAddress("\\\\.\\pipe\\foo") }, | ||
|
||
// DD_AGENT_HOST | ||
{ null, "1.1.1.1", null, null, new InetSocketAddress("1.1.1.1", defaultPort) }, | ||
|
||
// DD_AGENT_HOST, DD_DOGSTATSD_PORT | ||
{ null, "1.1.1.1", "9999", null, new InetSocketAddress("1.1.1.1", 9999) }, | ||
|
||
{ null, null, null, "foo", new NamedPipeSocketAddress("\\\\.\\pipe\\foo") }, | ||
|
||
// DD_DOGSTATSD_URL overrides other env vars. | ||
{ "udp://1.1.1.1", null, null, "foo", new InetSocketAddress("1.1.1.1", defaultPort) }, | ||
{ "udp://1.1.1.1:9999", null, null, "foo", new InetSocketAddress("1.1.1.1", 9999) }, | ||
{ "\\\\.\\pipe\\foo", null, null, "bar", new NamedPipeSocketAddress("\\\\.\\pipe\\foo") }, | ||
{ "\\\\.\\pipe\\foo", "1.1.1.1", null, null, new NamedPipeSocketAddress("\\\\.\\pipe\\foo") }, | ||
{ "\\\\.\\pipe\\foo", "1.1.1.1", "9999", null, new NamedPipeSocketAddress("\\\\.\\pipe\\foo") }, | ||
|
||
// DD_DOGSTATSD_NAMED_PIPE overrides DD_AGENT_HOST. | ||
{ null, "1.1.1.1", null, "foo", new NamedPipeSocketAddress("\\\\.\\pipe\\foo") }, | ||
{ null, "1.1.1.1", "9999", "foo", new NamedPipeSocketAddress("\\\\.\\pipe\\foo") }, | ||
})); | ||
|
||
if (isJnrAvailable()) { | ||
params.addAll(Arrays.asList(new Object[][]{ | ||
{ "unix:///dsd.sock", null, null, null, new UnixSocketAddress("/dsd.sock") }, | ||
{ "unix://unused/dsd.sock", null, null, null, new UnixSocketAddress("/dsd.sock") }, | ||
{ "unix://unused:9999/dsd.sock", null, null, null, new UnixSocketAddress("/dsd.sock") }, | ||
{ null, "/dsd.sock", "0", null, new UnixSocketAddress("/dsd.sock") }, | ||
{ "unix:///dsd.sock", "1.1.1.1", "9999", null, new UnixSocketAddress("/dsd.sock") }, | ||
})); | ||
} | ||
|
||
return params; | ||
} | ||
|
||
@Before | ||
public void set() { | ||
set(NonBlockingStatsDClient.DD_DOGSTATSD_URL_ENV_VAR, url); | ||
set(NonBlockingStatsDClient.DD_AGENT_HOST_ENV_VAR, host); | ||
set(NonBlockingStatsDClient.DD_DOGSTATSD_PORT_ENV_VAR, port); | ||
set(NonBlockingStatsDClient.DD_NAMED_PIPE_ENV_VAR, pipe); | ||
} | ||
|
||
void set(String name, String val) { | ||
if (val != null) { | ||
environmentVariables.set(name, val); | ||
} else { | ||
environmentVariables.clear(name); | ||
} | ||
} | ||
|
||
@Test(timeout = 5000L) | ||
public void address_resolution() throws Exception { | ||
NonBlockingStatsDClientBuilder b; | ||
|
||
// Default configuration matches env vars | ||
b = new NonBlockingStatsDClientBuilder().resolve(); | ||
assertEquals(expected, b.addressLookup.call()); | ||
|
||
// Explicit configuration is used regardless of environment variables. | ||
b = new NonBlockingStatsDClientBuilder().hostname("2.2.2.2").resolve(); | ||
assertEquals(new InetSocketAddress("2.2.2.2", defaultPort), b.addressLookup.call()); | ||
|
||
b = new NonBlockingStatsDClientBuilder().hostname("2.2.2.2").port(2222).resolve(); | ||
assertEquals(new InetSocketAddress("2.2.2.2", 2222), b.addressLookup.call()); | ||
|
||
b = new NonBlockingStatsDClientBuilder().namedPipe("ook").resolve(); | ||
assertEquals(new NamedPipeSocketAddress("ook"), b.addressLookup.call()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this used anywhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is used implicitly in tests when we
assertEquals
twoNamedPipeSocketAddress
es