"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "common/src/test/java/com/tc/net/core/TCConnectionImplTest.java" between
terracotta-core-5.7.0.tar.gz and terracotta-core-5.7.1.tar.gz

About: Terracotta - Clustering technology for Java ("Network Attached Memory", "Distributed Cache"). Source code of the server (5.x) core components.

TCConnectionImplTest.java  (terracotta-core-5.7.0):TCConnectionImplTest.java  (terracotta-core-5.7.1)
skipping to change at line 27 skipping to change at line 27
* *
*/ */
package com.tc.net.core; package com.tc.net.core;
import com.tc.bytes.TCByteBuffer; import com.tc.bytes.TCByteBuffer;
import com.tc.bytes.TCByteBufferFactory; import com.tc.bytes.TCByteBufferFactory;
import com.tc.net.TCSocketAddress; import com.tc.net.TCSocketAddress;
import com.tc.net.core.event.TCConnectionEvent; import com.tc.net.core.event.TCConnectionEvent;
import com.tc.net.core.event.TCConnectionEventListener; import com.tc.net.core.event.TCConnectionEventListener;
import com.tc.net.protocol.TCNetworkMessage; import com.tc.net.protocol.TCNetworkMessage;
import com.tc.util.PortChooser;
import java.io.IOException; import java.io.IOException;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.ScatteringByteChannel; import java.nio.channels.ScatteringByteChannel;
import java.nio.channels.SocketChannel; import java.nio.channels.SocketChannel;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import static junit.framework.TestCase.fail; import static junit.framework.TestCase.fail;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.terracotta.utilities.test.net.PortManager;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import com.tc.net.protocol.TCProtocolAdaptor; import com.tc.net.protocol.TCProtocolAdaptor;
/** /**
* *
*/ */
public class TCConnectionImplTest { public class TCConnectionImplTest {
@Test @Test
public void testFinishConnection() throws Exception { public void testFinishConnection() throws Exception {
int port = new PortChooser().chooseRandomPort(); try (PortManager.PortRef portRef = PortManager.getInstance().reservePort())
ServerSocket socket = new ServerSocket(port); {
final AtomicBoolean createManager = new AtomicBoolean(true); int port = portRef.port();
TCConnectionEventListener listener = mock(TCConnectionEventListener.class); try (ServerSocket socket = new ServerSocket(port)) {
TCProtocolAdaptor adaptor = mock(TCProtocolAdaptor.class); final AtomicBoolean createManager = new AtomicBoolean(true);
TCConnectionManagerImpl mgr = new TCConnectionManagerImpl(); TCConnectionEventListener listener = mock(TCConnectionEventListener.clas
final CoreNIOServices nioServiceThread = mock(CoreNIOServices.class); s);
SocketParams socketParams = new SocketParams(); TCProtocolAdaptor adaptor = mock(TCProtocolAdaptor.class);
BufferManagerFactory bufferManagerFactory = mock(BufferManagerFactory.class) TCConnectionManagerImpl mgr = new TCConnectionManagerImpl();
; final CoreNIOServices nioServiceThread = mock(CoreNIOServices.class);
when(bufferManagerFactory.createBufferManager(any(SocketChannel.class), anyB SocketParams socketParams = new SocketParams();
oolean())).thenAnswer(invocationOnMock -> { BufferManagerFactory bufferManagerFactory = mock(BufferManagerFactory.cl
verify(nioServiceThread, never()).requestReadInterest(any(TCChannelReader. ass);
class), any(ScatteringByteChannel.class)); when(bufferManagerFactory.createBufferManager(any(SocketChannel.class),
verify(nioServiceThread, never()).requestReadInterest(any(TCChannelReader. anyBoolean())).thenAnswer(invocationOnMock -> {
class), any(ScatteringByteChannel.class)); verify(nioServiceThread, never()).requestReadInterest(any(TCChannelRea
return new ClearTextBufferManager((SocketChannel) invocationOnMock.getArgu der.class), any(ScatteringByteChannel.class));
ments()[0]); verify(nioServiceThread, never()).requestReadInterest(any(TCChannelRea
}); der.class), any(ScatteringByteChannel.class));
TCConnection conn = new TCConnectionImpl(listener, adaptor, mgr, nioServiceT return new ClearTextBufferManager((SocketChannel) invocationOnMock.get
hread, socketParams, bufferManagerFactory); Arguments()[0]);
TCSocketAddress addr = new TCSocketAddress("localhost", port); });
conn.connect(addr, 0); TCConnection conn = new TCConnectionImpl(listener, adaptor, mgr, nioServ
verify(listener).connectEvent(any(TCConnectionEvent.class)); iceThread, socketParams, bufferManagerFactory);
TCSocketAddress addr = new TCSocketAddress("localhost", port);
verify(nioServiceThread).requestReadInterest(any(TCChannelReader.class), any conn.connect(addr, 0);
(ScatteringByteChannel.class)); verify(listener).connectEvent(any(TCConnectionEvent.class));
conn = new TCConnectionImpl(listener, adaptor, mgr, nioServiceThread, socket verify(nioServiceThread).requestReadInterest(any(TCChannelReader.class),
Params, mock(BufferManagerFactory.class)); any(ScatteringByteChannel.class));
createManager.set(false);
conn = new TCConnectionImpl(listener, adaptor, mgr, nioServiceThread, so
try { cketParams, mock(BufferManagerFactory.class));
conn.connect(addr, 0); createManager.set(false);
fail();
} catch (IOException ioe) { try {
// expected; conn.connect(addr, 0);
fail();
} catch (IOException ioe) {
// expected;
}
}
} }
socket.close();
} }
@Test @Test
public void testWriteEndsWhenClose() throws Exception { public void testWriteEndsWhenClose() throws Exception {
int port = new PortChooser().chooseRandomPort(); try (PortManager.PortRef portRef = PortManager.getInstance().reservePort())
ServerSocket socket = new ServerSocket(port); {
final AtomicBoolean createManager = new AtomicBoolean(true); int port = portRef.port();
TCConnectionEventListener listener = mock(TCConnectionEventListener.class); try (ServerSocket socket = new ServerSocket(port)) {
TCProtocolAdaptor adaptor = mock(TCProtocolAdaptor.class); final AtomicBoolean createManager = new AtomicBoolean(true);
TCConnectionManagerImpl mgr = new TCConnectionManagerImpl(); TCConnectionEventListener listener = mock(TCConnectionEventListener.clas
final CoreNIOServices nioServiceThread = mock(CoreNIOServices.class); s);
SocketParams socketParams = new SocketParams(); TCProtocolAdaptor adaptor = mock(TCProtocolAdaptor.class);
BufferManagerFactory bufferManagerFactory = mock(BufferManagerFactory.class) TCConnectionManagerImpl mgr = new TCConnectionManagerImpl();
; final CoreNIOServices nioServiceThread = mock(CoreNIOServices.class);
SocketParams socketParams = new SocketParams();
BufferManager bufferManager = mock(BufferManager.class); BufferManagerFactory bufferManagerFactory = mock(BufferManagerFactory.cl
when(bufferManager.sendFromBuffer()).thenReturn(0); ass);
when(bufferManager.forwardToWriteBuffer(any(ByteBuffer.class))).thenAnswer((
i)->{ BufferManager bufferManager = mock(BufferManager.class);
return ((ByteBuffer)i.getArguments()[0]).remaining(); when(bufferManager.sendFromBuffer()).thenReturn(0);
}); when(bufferManager.forwardToWriteBuffer(any(ByteBuffer.class))).thenAnsw
er((i) -> {
when(bufferManagerFactory.createBufferManager(any(SocketChannel.class), anyB return ((ByteBuffer)i.getArguments()[0]).remaining();
oolean())).thenAnswer(invocationOnMock -> { });
verify(nioServiceThread, never()).requestReadInterest(any(TCChannelReader.
class), any(ScatteringByteChannel.class)); when(bufferManagerFactory.createBufferManager(any(SocketChannel.class),
return bufferManager; anyBoolean())).thenAnswer(
}); invocationOnMock -> {
TCConnectionImpl conn = new TCConnectionImpl(listener, adaptor, mgr, nioServ verify(nioServiceThread, never()).requestReadInterest(any(TCChanne
iceThread, socketParams, bufferManagerFactory); lReader.class),
TCSocketAddress addr = new TCSocketAddress("localhost", port); any(ScatteringByteChannel.class));
conn.connect(addr, 0); return bufferManager;
});
TCNetworkMessage msg = mock(TCNetworkMessage.class); TCConnectionImpl conn = new TCConnectionImpl(listener, adaptor, mgr, nio
when(msg.getEntireMessageData()).thenReturn(new TCByteBuffer[] {TCByteBuffer ServiceThread, socketParams,
Factory.wrap(new byte[512])}); bufferManagerFactory);
when(msg.getDataLength()).thenReturn(512); TCSocketAddress addr = new TCSocketAddress("localhost", port);
conn.connect(addr, 0);
TCNetworkMessage msg = mock(TCNetworkMessage.class);
when(msg.getEntireMessageData()).thenReturn(new TCByteBuffer[] { TCByteB
ufferFactory.wrap(new byte[512]) });
when(msg.getDataLength()).thenReturn(512);
conn.putMessage(msg); conn.putMessage(msg);
new Thread(()->sleepThenClose(conn)).start(); new Thread(() -> sleepThenClose(conn)).start();
int w = conn.doWrite(); int w = conn.doWrite();
Assert.assertEquals(0, w); Assert.assertEquals(0, w);
}
}
} }
@Test @Test
public void testCloseBufferManager() throws Exception { public void testCloseBufferManager() throws Exception {
int port = new PortChooser().chooseRandomPort(); try (PortManager.PortRef portRef = PortManager.getInstance().reservePort())
ServerSocket socket = new ServerSocket(port); {
final AtomicBoolean createManager = new AtomicBoolean(true); int port = portRef.port();
TCConnectionEventListener listener = mock(TCConnectionEventListener.class); try (ServerSocket socket = new ServerSocket(port)) {
TCProtocolAdaptor adaptor = mock(TCProtocolAdaptor.class); final AtomicBoolean createManager = new AtomicBoolean(true);
TCConnectionManagerImpl mgr = new TCConnectionManagerImpl(); TCConnectionEventListener listener = mock(TCConnectionEventListener.clas
final CoreNIOServices nioServiceThread = mock(CoreNIOServices.class); s);
SocketParams socketParams = new SocketParams(); TCProtocolAdaptor adaptor = mock(TCProtocolAdaptor.class);
BufferManagerFactory bufferManagerFactory = mock(BufferManagerFactory.class) TCConnectionManagerImpl mgr = new TCConnectionManagerImpl();
; final CoreNIOServices nioServiceThread = mock(CoreNIOServices.class);
SocketParams socketParams = new SocketParams();
BufferManager bufferManager = mock(BufferManager.class); BufferManagerFactory bufferManagerFactory = mock(BufferManagerFactory.cl
when(bufferManager.sendFromBuffer()).thenReturn(0); ass);
when(bufferManager.forwardToWriteBuffer(any(ByteBuffer.class))).thenAnswer((
i)->{ BufferManager bufferManager = mock(BufferManager.class);
return ((ByteBuffer)i.getArguments()[0]).remaining(); when(bufferManager.sendFromBuffer()).thenReturn(0);
}); when(bufferManager.forwardToWriteBuffer(any(ByteBuffer.class))).thenAnsw
er((i) -> {
when(bufferManagerFactory.createBufferManager(any(SocketChannel.class), anyB return ((ByteBuffer)i.getArguments()[0]).remaining();
oolean())).thenAnswer(invocationOnMock -> { });
verify(nioServiceThread, never()).requestReadInterest(any(TCChannelReader.
class), any(ScatteringByteChannel.class)); when(bufferManagerFactory.createBufferManager(any(SocketChannel.class),
return bufferManager; anyBoolean())).thenAnswer(
}); invocationOnMock -> {
TCConnectionImpl conn = new TCConnectionImpl(listener, adaptor, mgr, nioServ verify(nioServiceThread, never()).requestReadInterest(any(TCChanne
iceThread, socketParams, bufferManagerFactory); lReader.class),
TCSocketAddress addr = new TCSocketAddress("localhost", port); any(ScatteringByteChannel.class));
conn.connect(addr, 0); return bufferManager;
});
TCConnectionImpl conn = new TCConnectionImpl(listener, adaptor, mgr, nio
ServiceThread, socketParams,
bufferManagerFactory);
TCSocketAddress addr = new TCSocketAddress("localhost", port);
conn.connect(addr, 0);
conn.close(100); conn.close(100);
verify(bufferManager).close(); verify(bufferManager).close();
}
}
} }
private void sleepThenClose(TCConnectionImpl conn) { private void sleepThenClose(TCConnectionImpl conn) {
try { try {
TimeUnit.SECONDS.sleep(3); TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
ie.printStackTrace(); ie.printStackTrace();
} }
conn.close(100); conn.close(100);
} }
 End of changes. 11 change blocks. 
107 lines changed or deleted 126 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)