public async Task SerializeAsync_ResultBufferMatchesExpected(int totItems, int totThreads, int maxQueuedItems) { var cfg = GetConfig(totThreads, maxQueuedItems); TestItemMP[] originalArray = new TestItemMP[totItems]; List <TestItemMP> asyncArray = new List <TestItemMP>(); for (int f = 0; f < totItems; f++) { originalArray[f] = new TestItemMP() { f = (byte)(1 + f % 32) } } ; MemoryStream ms = new MemoryStream(); await using (var ser = new CollectionSerializerAsync <TestItemMP>(ms, cfg)) foreach (var item in originalArray) { await ser.SerializeAsync(item, CancellationToken.None); } byte[] originalArraySerialized = new ArraySegment <byte>(ms.GetBuffer(), 0, (int)ms.Position).ToArray(); using (var streamReader = new MessagePackStreamReader(new MemoryStream(originalArraySerialized))) while (await streamReader.ReadAsync(CancellationToken.None) is ReadOnlySequence <byte> msgpack) { asyncArray.Add(MessagePackSerializer.Deserialize <Frame <TestItemMP> >(msgpack, cancellationToken: CancellationToken.None)); } Assert.AreEqual(originalArray.Select(f => f.f).ToArray(), asyncArray.Select(t => t.f).ToArray()); }
public async Task SerializeAsync_FrameHeadersAreCorrect(int totItems, int totThreads, int maxQueuedItems) { var cfg = GetConfig(totThreads, maxQueuedItems); TestItemMP[] originalArray = new TestItemMP[totItems]; List <byte> syncSerializedWithFrameData = new List <byte>(); for (int f = 0; f < totItems; f++) { originalArray[f] = new TestItemMP() { f = (byte)(1 + f % 32) } } ; MemoryStream ms = new MemoryStream(); foreach (var item in originalArray) { int bodylen = MessagePackSerializer.Serialize(item, MessagePackSerializerOptions.Standard).Length; //bodylen = bodylen | (1 << 24); // (A) trick to force writing Int32 Frame <TestItemMP> framedItem = new Frame <TestItemMP>(bodylen, item); syncSerializedWithFrameData.AddRange(MessagePackSerializer.Serialize(framedItem, cancellationToken: CancellationToken.None)); } //for (int i = 0; i < syncSerializedWithFrameData.Count - 1; i++) //{ // // undo (A) // if (syncSerializedWithFrameData[i] == MessagePackCode.UInt32 && syncSerializedWithFrameData[i + 1] == 1) // syncSerializedWithFrameData[i + 1] = 0; //} await using (var ser = new CollectionSerializerAsync <TestItemMP>(ms, cfg)) foreach (var item in originalArray) { await ser.SerializeAsync(item, CancellationToken.None); } byte[] originalArraySerialized = new ArraySegment <byte>(ms.GetBuffer(), 0, (int)ms.Position).ToArray(); Assert.AreEqual(syncSerializedWithFrameData.ToArray(), originalArraySerialized); }