public void PacketCacheTest() { m_PacketReader.RegisterType(typeof(WrapperPacket)); m_PacketReader.RegisterType(typeof(LogPacket)); m_PacketReader.RegisterType(typeof(ThreadInfo)); //since timestamp length varies by time zone we have to measure it first (sigh..) var timestampLength = DateTimeOffsetSerializedLength(); bool haveTimeZone = timestampLength == 12; //The first message has to write out a bunch of stuff - definitions, threads, etc. LogPacket.Write("message 1", 101, m_PacketWriter); Assert.AreEqual(133 + timestampLength, m_MemoryStream.Position, "Serialized value isn't the expected length. Position is: {1}, expected {0}.\r\nSerialized Value: {2}", m_MemoryStream.Position, 133 + timestampLength, m_MemoryStream.ToArray().ToDisplayString()); Thread.Sleep(50); //having now written that these messages will be smaller because we don't have to write out threads and timestamps are smaller. var baseline = m_MemoryStream.Position; LogPacket.Write("message 2", 101, m_PacketWriter); Assert.AreEqual(haveTimeZone ? 20 : 19, m_MemoryStream.Position - baseline); Thread.Sleep(50); baseline = m_MemoryStream.Position; LogPacket.Write("message 3", 101, m_PacketWriter); Assert.AreEqual(haveTimeZone ? 20 : 19, m_MemoryStream.Position - baseline); baseline = m_MemoryStream.Position; LogPacket.Write("message 1", 101, m_PacketWriter); Assert.AreEqual(haveTimeZone ? 20 : 19, m_MemoryStream.Position - baseline); m_MemoryStream.Position = 0; ThreadInfo threadInfo = (ThreadInfo)m_PacketReader.Read(); LogPacket message1 = (LogPacket)m_PacketReader.Read(); LogPacket message2 = (LogPacket)m_PacketReader.Read(); LogPacket message3 = (LogPacket)m_PacketReader.Read(); LogPacket message4 = (LogPacket)m_PacketReader.Read(); Assert.AreEqual(101, threadInfo.ThreadId); Assert.AreEqual(101, message1.ThreadId); Assert.AreEqual("message 1", message1.Caption); Assert.LessOrEqual(message1.TimeStamp, DateTime.Now); Assert.AreEqual(101, message2.ThreadId); Assert.AreEqual("message 2", message2.Caption); Assert.LessOrEqual(message1.TimeStamp, message2.TimeStamp); Assert.AreEqual(101, message3.ThreadId); Assert.AreEqual("message 3", message3.Caption); Assert.LessOrEqual(message2.TimeStamp, message3.TimeStamp); Assert.AreEqual("message 1", message4.Caption); Assert.LessOrEqual(message3.TimeStamp, message4.TimeStamp); }
public void PacketCacheTest() { m_PacketReader.RegisterType(typeof(WrapperPacket)); m_PacketReader.RegisterType(typeof(LogPacket)); m_PacketReader.RegisterType(typeof(ThreadInfo)); LogPacket.Write("message 1", m_PacketWriter); Assert.AreEqual(141, m_MemoryStream.Position); Thread.Sleep(50); LogPacket.Write("message 2", m_PacketWriter); Assert.AreEqual(160, m_MemoryStream.Position); Thread.Sleep(50); LogPacket.Write("message 3", m_PacketWriter); Assert.AreEqual(179, m_MemoryStream.Position); LogPacket.Write("message 1", m_PacketWriter); Assert.AreEqual(198, m_MemoryStream.Position); m_MemoryStream.Position = 0; ThreadInfo threadInfo = (ThreadInfo)m_PacketReader.Read(); LogPacket message1 = (LogPacket)m_PacketReader.Read(); LogPacket message2 = (LogPacket)m_PacketReader.Read(); LogPacket message3 = (LogPacket)m_PacketReader.Read(); LogPacket message4 = (LogPacket)m_PacketReader.Read(); int threadId = Thread.CurrentThread.ManagedThreadId; Assert.AreEqual(threadId, threadInfo.ThreadId); Assert.AreEqual(threadId, message1.ThreadId); Assert.AreEqual("message 1", message1.Caption); Assert.LessOrEqual(message1.TimeStamp, DateTime.Now); Assert.AreEqual(message2.ThreadId, threadId); Assert.AreEqual("message 2", message2.Caption); Assert.LessOrEqual(message1.TimeStamp, message2.TimeStamp); Assert.AreEqual(message3.ThreadId, threadId); Assert.AreEqual("message 3", message3.Caption); Assert.LessOrEqual(message2.TimeStamp, message3.TimeStamp); Assert.AreEqual("message 1", message4.Caption); Assert.LessOrEqual(message3.TimeStamp, message4.TimeStamp); }