@Test public void socketInvalidatedWhenError() throws Exception { GenericKeyedObjectPool<InetSocketAddress, Socket> pool = mock(GenericKeyedObjectPool.class); Socket socket = mock(Socket.class); when(pool.borrowObject(Matchers.any(InetSocketAddress.class))).thenReturn(socket); UnflushableByteArrayOutputStream out = new UnflushableByteArrayOutputStream(); when(socket.getOutputStream()).thenReturn(out); GraphiteWriter writer = GraphiteWriter.builder().setHost("localhost").setPort(2003).build(); writer.setPool(pool); writer.doWrite(dummyServer(), dummyQuery(), dummyResults()); Mockito.verify(pool) .invalidateObject(Matchers.any(InetSocketAddress.class), Matchers.eq(socket)); Mockito.verify(pool, Mockito.never()) .returnObject(Matchers.any(InetSocketAddress.class), Matchers.eq(socket)); }
@Test public void booleanAsNumberWorks() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); GenericKeyedObjectPool<InetSocketAddress, Socket> pool = mock(GenericKeyedObjectPool.class); Socket socket = mock(Socket.class); when(pool.borrowObject(Matchers.any(InetSocketAddress.class))).thenReturn(socket); when(socket.getOutputStream()).thenReturn(out); GraphiteWriter writer = GraphiteWriter.builder().setHost("localhost").setPort(123).setBooleanAsNumber(true).build(); writer.setPool(pool); writer.doWrite(dummyServer(), dummyQuery(), singleTrueResult()); // check that the booleanAsNumber property was picked up from the JSON assertThat(out.toString()) .startsWith("servers.host_example_net_4321.VerboseMemory.Verbose 1 0"); }
private static String getOutput(Server server, Query query, Result result) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); GraphiteWriter writer = getGraphiteWriter(out); writer.doWrite(server, query, of(result)); return out.toString(); }
@Test public void checkEmptyTypeNamesAreIgnored() throws Exception { Server server = serverWithNoQuery(); // Set useObjDomain to true Query query = Query.builder() .setUseObjDomainAsKey(true) .setAllowDottedKeys(true) .setObj("\"yammer.metrics\":name=\"uniqueName\",type=\"\"") .build(); Result result = new Result( System.currentTimeMillis(), "Attribute", "com.yammer.metrics.reporting.JmxReporter$Counter", "yammer.metrics", null, "name=\"uniqueName\",type=\"\"", ImmutableMap.of("Attribute", (Object) 0)); ByteArrayOutputStream out = new ByteArrayOutputStream(); ArrayList<String> typeNames = new ArrayList<String>(); typeNames.add("name"); typeNames.add("type"); GraphiteWriter writer = getGraphiteWriter(out, typeNames); writer.doWrite(server, query, of(result)); // check that the empty type "type" is ignored when allowDottedKeys is true assertThat(out.toString()) .startsWith("servers.host_example_net_4321.yammer.metrics.uniqueName.Attribute 0 "); // check that this also works when literal " characters aren't included in the JMX ObjectName query = Query.builder() .setUseObjDomainAsKey(true) .setAllowDottedKeys(true) .setObj("yammer.metrics:name=uniqueName,type=") .build(); out = new ByteArrayOutputStream(); writer = getGraphiteWriter(out, typeNames); writer.doWrite(server, query, of(result)); assertThat(out.toString()) .startsWith("servers.host_example_net_4321.yammer.metrics.uniqueName.Attribute 0 "); // check that the empty type "type" is ignored when allowDottedKeys is false query = Query.builder() .setUseObjDomainAsKey(true) .setAllowDottedKeys(false) .setObj("\"yammer.metrics\":name=\"uniqueName\",type=\"\"") .build(); out = new ByteArrayOutputStream(); writer = getGraphiteWriter(out, typeNames); writer.doWrite(server, query, of(result)); assertThat(out.toString()) .startsWith("servers.host_example_net_4321.yammer_metrics.uniqueName.Attribute 0 "); }