Let the plugin determine whether to flush the output stream after each

packet.
This commit is contained in:
akwizgran
2011-12-08 22:13:35 +00:00
parent 844ae8f0a7
commit 2494ff1a1e
28 changed files with 223 additions and 159 deletions

View File

@@ -139,10 +139,11 @@ public class ProtocolIntegrationTest extends TestCase {
ConnectionWriter conn = connectionWriterFactory.createConnectionWriter(
out, Long.MAX_VALUE, secret.clone());
OutputStream out1 = conn.getOutputStream();
ProtocolWriter proto = protocolWriterFactory.createProtocolWriter(out1);
ProtocolWriter writer = protocolWriterFactory.createProtocolWriter(out1,
false);
Ack a = packetFactory.createAck(Collections.singletonList(ack));
proto.writeAck(a);
writer.writeAck(a);
Collection<byte[]> batch = Arrays.asList(new byte[][] {
message.getSerialised(),
@@ -151,7 +152,7 @@ public class ProtocolIntegrationTest extends TestCase {
message3.getSerialised()
});
RawBatch b = packetFactory.createBatch(batch);
proto.writeBatch(b);
writer.writeBatch(b);
Collection<MessageId> offer = Arrays.asList(new MessageId[] {
message.getId(),
@@ -160,13 +161,13 @@ public class ProtocolIntegrationTest extends TestCase {
message3.getId()
});
Offer o = packetFactory.createOffer(offer);
proto.writeOffer(o);
writer.writeOffer(o);
BitSet requested = new BitSet(4);
requested.set(1);
requested.set(3);
Request r = packetFactory.createRequest(requested, 4);
proto.writeRequest(r);
writer.writeRequest(r);
// Use a LinkedHashMap for predictable iteration order
Map<Group, Long> subs = new LinkedHashMap<Group, Long>();
@@ -174,13 +175,13 @@ public class ProtocolIntegrationTest extends TestCase {
subs.put(group1, 0L);
SubscriptionUpdate s = packetFactory.createSubscriptionUpdate(subs,
timestamp);
proto.writeSubscriptionUpdate(s);
writer.writeSubscriptionUpdate(s);
TransportUpdate t = packetFactory.createTransportUpdate(transports,
timestamp);
proto.writeTransportUpdate(t);
writer.writeTransportUpdate(t);
out1.flush();
writer.flush();
return out.toByteArray();
}
@@ -188,20 +189,19 @@ public class ProtocolIntegrationTest extends TestCase {
InputStream in = new ByteArrayInputStream(connectionData);
byte[] tag = new byte[TAG_LENGTH];
assertEquals(TAG_LENGTH, in.read(tag, 0, TAG_LENGTH));
ConnectionReader r = connectionReaderFactory.createConnectionReader(in,
secret.clone(), tag);
in = r.getInputStream();
ProtocolReader protocolReader =
protocolReaderFactory.createProtocolReader(in);
ConnectionReader conn = connectionReaderFactory.createConnectionReader(
in, secret.clone(), tag);
InputStream in1 = conn.getInputStream();
ProtocolReader reader = protocolReaderFactory.createProtocolReader(in1);
// Read the ack
assertTrue(protocolReader.hasAck());
Ack a = protocolReader.readAck();
assertTrue(reader.hasAck());
Ack a = reader.readAck();
assertEquals(Collections.singletonList(ack), a.getBatchIds());
// Read and verify the batch
assertTrue(protocolReader.hasBatch());
Batch b = protocolReader.readBatch().verify();
assertTrue(reader.hasBatch());
Batch b = reader.readBatch().verify();
Collection<Message> messages = b.getMessages();
assertEquals(4, messages.size());
Iterator<Message> it = messages.iterator();
@@ -211,8 +211,8 @@ public class ProtocolIntegrationTest extends TestCase {
checkMessageEquality(message3, it.next());
// Read the offer
assertTrue(protocolReader.hasOffer());
Offer o = protocolReader.readOffer();
assertTrue(reader.hasOffer());
Offer o = reader.readOffer();
Collection<MessageId> offered = o.getMessageIds();
assertEquals(4, offered.size());
Iterator<MessageId> it1 = offered.iterator();
@@ -222,8 +222,8 @@ public class ProtocolIntegrationTest extends TestCase {
assertEquals(message3.getId(), it1.next());
// Read the request
assertTrue(protocolReader.hasRequest());
Request req = protocolReader.readRequest();
assertTrue(reader.hasRequest());
Request req = reader.readRequest();
BitSet requested = req.getBitmap();
assertFalse(requested.get(0));
assertTrue(requested.get(1));
@@ -233,8 +233,8 @@ public class ProtocolIntegrationTest extends TestCase {
assertEquals(2, requested.cardinality());
// Read the subscription update
assertTrue(protocolReader.hasSubscriptionUpdate());
SubscriptionUpdate s = protocolReader.readSubscriptionUpdate();
assertTrue(reader.hasSubscriptionUpdate());
SubscriptionUpdate s = reader.readSubscriptionUpdate();
Map<Group, Long> subs = s.getSubscriptions();
assertEquals(2, subs.size());
assertEquals(Long.valueOf(0L), subs.get(group));
@@ -242,8 +242,8 @@ public class ProtocolIntegrationTest extends TestCase {
assertTrue(s.getTimestamp() == timestamp);
// Read the transport update
assertTrue(protocolReader.hasTransportUpdate());
TransportUpdate t = protocolReader.readTransportUpdate();
assertTrue(reader.hasTransportUpdate());
TransportUpdate t = reader.readTransportUpdate();
assertEquals(transports, t.getTransports());
assertTrue(t.getTimestamp() == timestamp);

View File

@@ -84,7 +84,8 @@ public class ConstantsTest extends TestCase {
private void testBatchesFitIntoAck(int length) throws Exception {
// Create an ack with as many batch IDs as possible
ByteArrayOutputStream out = new ByteArrayOutputStream(length);
ProtocolWriter writer = protocolWriterFactory.createProtocolWriter(out);
ProtocolWriter writer = protocolWriterFactory.createProtocolWriter(out,
true);
int maxBatches = writer.getMaxBatchesForAck(length);
Collection<BatchId> acked = new ArrayList<BatchId>();
for(int i = 0; i < maxBatches; i++) {
@@ -116,7 +117,8 @@ public class ConstantsTest extends TestCase {
// Add the message to a batch
ByteArrayOutputStream out =
new ByteArrayOutputStream(MAX_PACKET_LENGTH);
ProtocolWriter writer = protocolWriterFactory.createProtocolWriter(out);
ProtocolWriter writer = protocolWriterFactory.createProtocolWriter(out,
true);
RawBatch b = packetFactory.createBatch(Collections.singletonList(
message.getSerialised()));
writer.writeBatch(b);
@@ -140,7 +142,8 @@ public class ConstantsTest extends TestCase {
private void testMessagesFitIntoOffer(int length) throws Exception {
// Create an offer with as many message IDs as possible
ByteArrayOutputStream out = new ByteArrayOutputStream(length);
ProtocolWriter writer = protocolWriterFactory.createProtocolWriter(out);
ProtocolWriter writer = protocolWriterFactory.createProtocolWriter(out,
true);
int maxMessages = writer.getMaxMessagesForOffer(length);
Collection<MessageId> offered = new ArrayList<MessageId>();
for(int i = 0; i < maxMessages; i++) {
@@ -165,7 +168,8 @@ public class ConstantsTest extends TestCase {
// Add the subscriptions to an update
ByteArrayOutputStream out =
new ByteArrayOutputStream(MAX_PACKET_LENGTH);
ProtocolWriter writer = protocolWriterFactory.createProtocolWriter(out);
ProtocolWriter writer = protocolWriterFactory.createProtocolWriter(out,
true);
SubscriptionUpdate s = packetFactory.createSubscriptionUpdate(subs,
Long.MAX_VALUE);
writer.writeSubscriptionUpdate(s);
@@ -194,7 +198,8 @@ public class ConstantsTest extends TestCase {
// Add the transports to an update
ByteArrayOutputStream out =
new ByteArrayOutputStream(MAX_PACKET_LENGTH);
ProtocolWriter writer = protocolWriterFactory.createProtocolWriter(out);
ProtocolWriter writer = protocolWriterFactory.createProtocolWriter(out,
true);
TransportUpdate t = packetFactory.createTransportUpdate(transports,
Long.MAX_VALUE);
writer.writeTransportUpdate(t);

View File

@@ -80,7 +80,7 @@ public class ProtocolReadWriteTest extends TestCase {
public void testWriteAndRead() throws Exception {
// Write
ByteArrayOutputStream out = new ByteArrayOutputStream();
ProtocolWriter writer = writerFactory.createProtocolWriter(out);
ProtocolWriter writer = writerFactory.createProtocolWriter(out, true);
Ack a = packetFactory.createAck(Collections.singletonList(batchId));
writer.writeAck(a);

View File

@@ -37,7 +37,8 @@ public class ProtocolWriterImplTest extends TestCase {
@Test
public void testWriteBitmapNoPadding() throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ProtocolWriter w = new ProtocolWriterImpl(serial, writerFactory, out);
ProtocolWriter w = new ProtocolWriterImpl(serial, writerFactory, out,
true);
BitSet b = new BitSet();
// 11011001 = 0xD9
b.set(0);
@@ -61,7 +62,8 @@ public class ProtocolWriterImplTest extends TestCase {
@Test
public void testWriteBitmapWithPadding() throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ProtocolWriter w = new ProtocolWriterImpl(serial, writerFactory, out);
ProtocolWriter w = new ProtocolWriterImpl(serial, writerFactory, out,
true);
BitSet b = new BitSet();
// 01011001 = 0x59
b.set(1);

View File

@@ -112,7 +112,7 @@ public class BatchConnectionReadWriteTest extends TestCase {
ProtocolWriterFactory protoFactory =
alice.getInstance(ProtocolWriterFactory.class);
TestBatchTransportWriter transport = new TestBatchTransportWriter(out,
Long.MAX_VALUE);
Long.MAX_VALUE, false);
OutgoingBatchConnection batchOut = new OutgoingBatchConnection(db,
connFactory, protoFactory, contactId, transportIndex,
transport);

View File

@@ -73,7 +73,7 @@ public class OutgoingBatchConnectionTest extends TestCase {
public void testConnectionTooShort() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
TestBatchTransportWriter transport = new TestBatchTransportWriter(out,
ProtocolConstants.MAX_PACKET_LENGTH);
ProtocolConstants.MAX_PACKET_LENGTH, true);
OutgoingBatchConnection connection = new OutgoingBatchConnection(db,
connFactory, protoFactory, contactId, transportIndex,
transport);
@@ -97,7 +97,7 @@ public class OutgoingBatchConnectionTest extends TestCase {
public void testNothingToSend() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
TestBatchTransportWriter transport = new TestBatchTransportWriter(out,
TransportConstants.MIN_CONNECTION_LENGTH);
TransportConstants.MIN_CONNECTION_LENGTH, true);
OutgoingBatchConnection connection = new OutgoingBatchConnection(db,
connFactory, protoFactory, contactId, transportIndex,
transport);
@@ -133,7 +133,7 @@ public class OutgoingBatchConnectionTest extends TestCase {
public void testSomethingToSend() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
TestBatchTransportWriter transport = new TestBatchTransportWriter(out,
TransportConstants.MIN_CONNECTION_LENGTH);
TransportConstants.MIN_CONNECTION_LENGTH, true);
OutgoingBatchConnection connection = new OutgoingBatchConnection(db,
connFactory, protoFactory, contactId, transportIndex,
transport);

View File

@@ -9,12 +9,15 @@ class TestBatchTransportWriter implements BatchTransportWriter {
private final ByteArrayOutputStream out;
private final long capacity;
private final boolean flush;
private boolean disposed = false, exception = false;
TestBatchTransportWriter(ByteArrayOutputStream out, long capacity) {
TestBatchTransportWriter(ByteArrayOutputStream out, long capacity,
boolean flush) {
this.out = out;
this.capacity = capacity;
this.flush = flush;
}
public long getCapacity() {
@@ -25,6 +28,10 @@ class TestBatchTransportWriter implements BatchTransportWriter {
return out;
}
public boolean shouldFlush() {
return flush;
}
public void dispose(boolean exception) {
assert !disposed;
disposed = true;