test WriteContext writing with multiple flushes

pull/7576/head
Jan Tattermusch 5 years ago
parent 53708e2f15
commit 19c0d73fb9
  1. 26
      csharp/src/Google.Protobuf.Test/CodedOutputStreamTest.cs
  2. 4
      csharp/src/Google.Protobuf/WriteBufferHelper.cs

@ -275,6 +275,32 @@ namespace Google.Protobuf
Assert.AreEqual(rawBytes, bufferWriter.WrittenSpan.ToArray());
}
}
[Test]
public void WriteContext_WritesWithFlushes()
{
TestAllTypes message = SampleMessages.CreateFullTestAllTypes();
MemoryStream expectedOutput = new MemoryStream();
CodedOutputStream output = new CodedOutputStream(expectedOutput);
output.WriteMessage(message);
output.Flush();
byte[] expectedBytes1 = expectedOutput.ToArray();
output.WriteMessage(message);
output.Flush();
byte[] expectedBytes2 = expectedOutput.ToArray();
var bufferWriter = new ArrayBufferWriter<byte>();
WriteContext.Initialize(bufferWriter, out WriteContext ctx);
ctx.WriteMessage(message);
ctx.Flush();
Assert.AreEqual(expectedBytes1, bufferWriter.WrittenSpan.ToArray());
ctx.WriteMessage(message);
ctx.Flush();
Assert.AreEqual(expectedBytes2, bufferWriter.WrittenSpan.ToArray());
}
[Test]
public void EncodeZigZag32()

@ -153,11 +153,13 @@ namespace Google.Protobuf
}
else if (state.writeBufferHelper.bufferWriter != null)
{
// calling Advance invalidates the current buffer and we must not continue writing to it,
// so we set the current buffer to point to an empty Span. If any subsequent writes happen,
// the first subsequent write will trigger refresing of the buffer.
state.writeBufferHelper.bufferWriter.Advance(state.position);
state.position = 0;
state.limit = 0;
buffer = default; // invalidate the current buffer
// TODO: add a test when we flush and then try to write more data
}
}
}

Loading…
Cancel
Save