@Override public <T extends Spy<T>> Spy<T> verify(Spy<T> spy) throws TcpConnectionsExpectationError { Map<SocketMetaData, SocketStats> socketOperations = spy.getSocketOperations(threads, host, true); Set<Integer> connectionIds = new HashSet<Integer>(); for (SocketMetaData socketMetaData : socketOperations.keySet()) { connectionIds.add(socketMetaData.connectionId); } int numConnections = connectionIds.size(); if ((-1 != max && numConnections > max) || (-1 != min && numConnections < min)) { throw new TcpConnectionsExpectationError(this, socketOperations, numConnections); } return spy; }
@Override public <T extends Spy<T>> Spy<T> verify(Spy<T> spy) throws TcpConnectionsExpectationError { Map<SocketMetaData, SocketStats> socketOperations = spy.getSocketOperations(threads, host, true); Set<Integer> connectionIds = new HashSet<Integer>(); for (SocketMetaData socketMetaData : socketOperations.keySet()) { connectionIds.add(socketMetaData.connectionId); } int numConnections = connectionIds.size(); if ((-1 != max && numConnections > max) || (-1 != min && numConnections < min)) { throw new TcpConnectionsExpectationError(this, socketOperations, numConnections); } return spy; }
@Test public void testInstall() throws Exception { SnifferSocketImplFactory.uninstall(); SnifferSocketImplFactory.install(); try (Spy<?> s = Sniffy.spy()) { performSocketOperation(); Thread thread = new Thread(this::performSocketOperation); thread.start(); thread.join(); // Current thread socket operations assertEquals(1, (long) s.getSocketOperations(CURRENT, null, true).entrySet().size()); s.getSocketOperations(CURRENT, null, true).values().stream().findAny().ifPresent((socketStats) -> { assertEquals(REQUEST.length, socketStats.bytesUp.intValue()); assertEquals(RESPONSE.length, socketStats.bytesDown.intValue()); }); // Other threads socket operations assertEquals(1, s.getSocketOperations(OTHERS, null, true).entrySet().stream().count()); s.getSocketOperations(OTHERS, null, true).values().stream().findAny().ifPresent((socketStats) -> { assertEquals(REQUEST.length, socketStats.bytesUp.intValue()); assertEquals(RESPONSE.length, socketStats.bytesDown.intValue()); }); // Any threads socket operations assertEquals(2, s.getSocketOperations(ANY, null, true).entrySet().stream().count()); s.getSocketOperations(OTHERS, null, true).values().stream().forEach((socketStats) -> { assertEquals(REQUEST.length, socketStats.bytesUp.intValue()); assertEquals(RESPONSE.length, socketStats.bytesDown.intValue()); }); } }
@Test public void testGetXAConnectionWithCredentialsWithSocketOperation() throws Exception { JdbcDataSource h2DataSource = new JdbcDataSource(); h2DataSource.setURL("jdbc:h2:mem:"); JdbcDataSource targetDataSource = Mockito.spy(h2DataSource); when(targetDataSource.getXAConnection(anyString(), anyString())).then(invocation -> { Sniffy.logSocket(1, new InetSocketAddress(InetAddress.getLoopbackAddress(), 9876), 2, 3, 4); return invocation.callRealMethod(); }); SniffyDataSource sniffyDataSource = new SniffyDataSource(targetDataSource); try (Spy spy = Sniffy.expect(SqlQueries.exactQueries(1)).expect(TcpConnections.none()); Connection connection = sniffyDataSource.getXAConnection("sa","sa").getConnection()) { assertNotNull(connection); assertTrue(Proxy.isProxyClass(connection.getClass())); try (Statement statement = connection.createStatement()) { statement.execute("SELECT 1 FROM DUAL"); statement.getResultSet().next(); } assertTrue(spy.getSocketOperations(Threads.CURRENT, null, false).isEmpty()); } }
@Test public void testGetConnectionWithCredentialsWithSocketOperation() throws Exception { JdbcDataSource h2DataSource = new JdbcDataSource(); h2DataSource.setURL("jdbc:h2:mem:"); JdbcDataSource targetDataSource = Mockito.spy(h2DataSource); when(targetDataSource.getConnection(anyString(), anyString())).then(invocation -> { Sniffy.logSocket(1, new InetSocketAddress(InetAddress.getLoopbackAddress(), 9876), 2, 3, 4); return invocation.callRealMethod(); }); SniffyDataSource sniffyDataSource = new SniffyDataSource(targetDataSource); try (Spy spy = Sniffy.expect(SqlQueries.exactQueries(1)).expect(TcpConnections.none()); Connection connection = sniffyDataSource.getConnection("sa","sa")) { assertNotNull(connection); assertTrue(Proxy.isProxyClass(connection.getClass())); try (Statement statement = connection.createStatement()) { statement.execute("SELECT 1 FROM DUAL"); statement.getResultSet().next(); } assertTrue(spy.getSocketOperations(Threads.CURRENT, null, false).isEmpty()); } }
@Test public void testGetPooledConnectionWithCredentialsWithSocketOperation() throws Exception { JdbcDataSource h2DataSource = new JdbcDataSource(); h2DataSource.setURL("jdbc:h2:mem:"); JdbcDataSource targetDataSource = Mockito.spy(h2DataSource); when(targetDataSource.getXAConnection(anyString(), anyString())).then(invocation -> { Sniffy.logSocket(1, new InetSocketAddress(InetAddress.getLoopbackAddress(), 9876), 2, 3, 4); return invocation.callRealMethod(); }); SniffyDataSource sniffyDataSource = new SniffyDataSource(targetDataSource); try (Spy spy = Sniffy.expect(SqlQueries.exactQueries(1)).expect(TcpConnections.none()); Connection connection = sniffyDataSource.getPooledConnection("sa","sa").getConnection()) { assertNotNull(connection); assertTrue(Proxy.isProxyClass(connection.getClass())); try (Statement statement = connection.createStatement()) { statement.execute("SELECT 1 FROM DUAL"); statement.getResultSet().next(); } assertTrue(spy.getSocketOperations(Threads.CURRENT, null, false).isEmpty()); } }
@Test public void testGetConnectionWithSocketOperation() throws Exception { JdbcDataSource h2DataSource = new JdbcDataSource(); h2DataSource.setURL("jdbc:h2:mem:"); JdbcDataSource targetDataSource = Mockito.spy(h2DataSource); when(targetDataSource.getConnection()).then(invocation -> { Sniffy.logSocket(1, new InetSocketAddress(InetAddress.getLoopbackAddress(), 9876), 2, 3, 4); return invocation.callRealMethod(); }); SniffyDataSource sniffyDataSource = new SniffyDataSource(targetDataSource); try (Spy spy = Sniffy.expect(SqlQueries.exactQueries(1)).expect(TcpConnections.none()); Connection connection = sniffyDataSource.getConnection()) { assertNotNull(connection); assertTrue(Proxy.isProxyClass(connection.getClass())); try (Statement statement = connection.createStatement()) { statement.execute("SELECT 1 FROM DUAL"); statement.getResultSet().next(); } assertTrue(spy.getSocketOperations(Threads.CURRENT, null, false).isEmpty()); } }
@Test public void testGetPooledConnectionWithSocketOperation() throws Exception { JdbcDataSource h2DataSource = new JdbcDataSource(); h2DataSource.setURL("jdbc:h2:mem:"); JdbcDataSource targetDataSource = Mockito.spy(h2DataSource); when(targetDataSource.getXAConnection()).then(invocation -> { Sniffy.logSocket(1, new InetSocketAddress(InetAddress.getLoopbackAddress(), 9876), 2, 3, 4); return invocation.callRealMethod(); }); SniffyDataSource sniffyDataSource = new SniffyDataSource(targetDataSource); try (Spy spy = Sniffy.expect(SqlQueries.exactQueries(1)).expect(TcpConnections.none()); Connection connection = sniffyDataSource.getPooledConnection().getConnection()) { assertNotNull(connection); assertTrue(Proxy.isProxyClass(connection.getClass())); try (Statement statement = connection.createStatement()) { statement.execute("SELECT 1 FROM DUAL"); statement.getResultSet().next(); } assertTrue(spy.getSocketOperations(Threads.CURRENT, null, false).isEmpty()); } }
@Test public void testGetXAConnectionWithSocketOperation() throws Exception { JdbcDataSource h2DataSource = new JdbcDataSource(); h2DataSource.setURL("jdbc:h2:mem:"); JdbcDataSource targetDataSource = Mockito.spy(h2DataSource); when(targetDataSource.getXAConnection()).then(invocation -> { Sniffy.logSocket(1, new InetSocketAddress(InetAddress.getLoopbackAddress(), 9876), 2, 3, 4); return invocation.callRealMethod(); }); SniffyDataSource sniffyDataSource = new SniffyDataSource(targetDataSource); try (Spy spy = Sniffy.expect(SqlQueries.exactQueries(1)).expect(TcpConnections.none()); Connection connection = sniffyDataSource.getXAConnection().getConnection()) { assertNotNull(connection); assertTrue(Proxy.isProxyClass(connection.getClass())); try (Statement statement = connection.createStatement()) { statement.execute("SELECT 1 FROM DUAL"); statement.getResultSet().next(); } assertTrue(spy.getSocketOperations(Threads.CURRENT, null, false).isEmpty()); } }
@Test public void testUninstall() throws Exception { Sniffy.initialize(); SnifferSocketImplFactory.uninstall(); try (Spy<?> s = Sniffy.spy()) { performSocketOperation(); assertTrue(s.getSocketOperations(CURRENT, null, true).isEmpty()); } SnifferSocketImplFactory.install(); }