use of org.apache.jute.OutputArchive in project zookeeper by apache.
the class Zab1_0Test method testLeaderConversation.
public void testLeaderConversation(LeaderConversation conversation) throws Exception {
Socket[] pair = getSocketPair();
Socket leaderSocket = pair[0];
Socket followerSocket = pair[1];
File tmpDir = File.createTempFile("test", "dir", testData);
tmpDir.delete();
tmpDir.mkdir();
LeadThread leadThread = null;
Leader leader = null;
try {
QuorumPeer peer = createQuorumPeer(tmpDir);
leader = createLeader(tmpDir, peer);
peer.leader = leader;
leadThread = new LeadThread(leader);
leadThread.start();
while (leader.cnxAcceptor == null || !leader.cnxAcceptor.isAlive()) {
Thread.sleep(20);
}
LearnerHandler lh = new LearnerHandler(leaderSocket, new BufferedInputStream(leaderSocket.getInputStream()), leader);
lh.start();
leaderSocket.setSoTimeout(4000);
InputArchive ia = BinaryInputArchive.getArchive(followerSocket.getInputStream());
OutputArchive oa = BinaryOutputArchive.getArchive(followerSocket.getOutputStream());
conversation.converseWithLeader(ia, oa, leader);
} finally {
if (leader != null) {
leader.shutdown("end of test");
}
if (leadThread != null) {
leadThread.interrupt();
leadThread.join();
}
TestUtils.deleteFileRecursively(tmpDir);
}
}
use of org.apache.jute.OutputArchive in project zookeeper by apache.
the class Zab1_0Test method testFollowerConversation.
public void testFollowerConversation(FollowerConversation conversation) throws Exception {
File tmpDir = File.createTempFile("test", "dir", testData);
tmpDir.delete();
tmpDir.mkdir();
Thread followerThread = null;
ConversableFollower follower = null;
QuorumPeer peer = null;
try {
peer = createQuorumPeer(tmpDir);
follower = createFollower(tmpDir, peer);
peer.follower = follower;
ServerSocket ss = new ServerSocket(0, 50, InetAddress.getByName("127.0.0.1"));
QuorumServer leaderQS = new QuorumServer(1, (InetSocketAddress) ss.getLocalSocketAddress());
follower.setLeaderQuorumServer(leaderQS);
final Follower followerForThread = follower;
followerThread = new Thread() {
public void run() {
try {
followerForThread.followLeader();
} catch (InterruptedException e) {
LOG.info("Follower thread interrupted", e);
} catch (Exception e) {
LOG.warn("Unexpected exception in follower thread", e);
}
}
};
followerThread.start();
Socket leaderSocket = ss.accept();
InputArchive ia = BinaryInputArchive.getArchive(leaderSocket.getInputStream());
OutputArchive oa = BinaryOutputArchive.getArchive(leaderSocket.getOutputStream());
conversation.converseWithFollower(ia, oa, follower);
} finally {
if (follower != null) {
follower.shutdown();
}
if (followerThread != null) {
followerThread.interrupt();
followerThread.join();
}
if (peer != null) {
peer.shutdown();
}
TestUtils.deleteFileRecursively(tmpDir);
}
}
use of org.apache.jute.OutputArchive in project zookeeper by apache.
the class SerializeUtilsTest method testSerializeRequestWithoutTxn.
@Test
public void testSerializeRequestWithoutTxn() throws IOException {
// Arrange
TxnHeader header = mock(TxnHeader.class);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
OutputArchive oa = (OutputArchive) args[0];
oa.writeString("header", "test");
return null;
}
}).when(header).serialize(any(OutputArchive.class), anyString());
Request request = new Request(1, 2, 3, header, null, 4);
// Act
byte[] data = SerializeUtils.serializeRequest(request);
// Assert
assertNotNull(data);
verify(header).serialize(any(OutputArchive.class), eq("hdr"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos);
boa.writeString("header", "test");
baos.close();
assertArrayEquals(baos.toByteArray(), data);
}
use of org.apache.jute.OutputArchive in project zookeeper by apache.
the class Util method marshallTxnEntry.
/**
* Serializes transaction header and transaction data into a byte buffer.
*
* @param hdr transaction header
* @param txn transaction data
* @return serialized transaction record
* @throws IOException
*/
public static byte[] marshallTxnEntry(TxnHeader hdr, Record txn) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputArchive boa = BinaryOutputArchive.getArchive(baos);
hdr.serialize(boa, "hdr");
if (txn != null) {
txn.serialize(boa, "txn");
}
return baos.toByteArray();
}
use of org.apache.jute.OutputArchive in project zookeeper by apache.
the class Zab1_0Test method testObserverConversation.
public void testObserverConversation(ObserverConversation conversation) throws Exception {
File tmpDir = File.createTempFile("test", "dir", testData);
tmpDir.delete();
tmpDir.mkdir();
Thread observerThread = null;
ConversableObserver observer = null;
QuorumPeer peer = null;
try {
peer = createQuorumPeer(tmpDir);
peer.setSyncEnabled(true);
observer = createObserver(tmpDir, peer);
peer.observer = observer;
ServerSocket ss = new ServerSocket(0, 50, InetAddress.getByName("127.0.0.1"));
QuorumServer leaderQS = new QuorumServer(1, (InetSocketAddress) ss.getLocalSocketAddress());
observer.setLeaderQuorumServer(leaderQS);
final Observer observerForThread = observer;
observerThread = new Thread() {
public void run() {
try {
observerForThread.observeLeader();
} catch (Exception e) {
e.printStackTrace();
}
}
};
observerThread.start();
Socket leaderSocket = ss.accept();
InputArchive ia = BinaryInputArchive.getArchive(leaderSocket.getInputStream());
OutputArchive oa = BinaryOutputArchive.getArchive(leaderSocket.getOutputStream());
conversation.converseWithObserver(ia, oa, observer);
} finally {
if (observer != null) {
observer.shutdown();
}
if (observerThread != null) {
observerThread.interrupt();
observerThread.join();
}
if (peer != null) {
peer.shutdown();
}
TestUtils.deleteFileRecursively(tmpDir);
}
}
Aggregations