-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
We can align `GraphiteUDP` with the TCP based `Graphite` and close the socket in the `close` method instead of the connect method. The close method will be invoked every time after a portion of metrics has been sent, so we can close the socket there. We also do not need to call the method `isConnected` during sending metrics because the socket is opened right before sending data, so it can't be closed by timeout. (cherry picked from commit d18b6ab)
- Loading branch information
Showing
2 changed files
with
65 additions
and
11 deletions.
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
43 changes: 43 additions & 0 deletions
43
metrics-graphite/src/test/java/com/codahale/metrics/graphite/GraphiteUDPTest.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,43 @@ | ||
package com.codahale.metrics.graphite; | ||
|
||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.DatagramChannel; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.verify; | ||
|
||
public class GraphiteUDPTest { | ||
|
||
private final String host = "example.com"; | ||
private final int port = 1234; | ||
|
||
private GraphiteUDP graphiteUDP; | ||
|
||
@Test | ||
public void connects() throws Exception { | ||
graphiteUDP = new GraphiteUDP(host, port); | ||
graphiteUDP.connect(); | ||
|
||
assertThat(graphiteUDP.getDatagramChannel()).isNotNull(); | ||
assertThat(graphiteUDP.getAddress()).isEqualTo(new InetSocketAddress(host, port)); | ||
|
||
graphiteUDP.close(); | ||
} | ||
|
||
@Test | ||
public void writesValue() throws Exception { | ||
graphiteUDP = new GraphiteUDP(host, port); | ||
DatagramChannel mockDatagramChannel = Mockito.mock(DatagramChannel.class); | ||
graphiteUDP.setDatagramChannel(mockDatagramChannel); | ||
graphiteUDP.setAddress(new InetSocketAddress(host, port)); | ||
|
||
graphiteUDP.send("name woo", "value", 100); | ||
verify(mockDatagramChannel).send(ByteBuffer.wrap("name-woo value 100\n".getBytes("UTF-8")), | ||
new InetSocketAddress(host, port)); | ||
} | ||
|
||
} |