public void TestNestedInterning() { ServerProtocol.Serializers.Register(InterningNestedTestModel.Read, InterningNestedTestModel.Write); ClientProtocol.Serializers.Register(InterningNestedTestModel.Read, InterningNestedTestModel.Write); var serverProperty = new RdProperty <InterningNestedTestModel>(InterningNestedTestModel.Read.Interned("Test"), InterningNestedTestModel.Write.Interned("Test")) { IsMaster = true } .Static(1); var clientProperty = new RdProperty <InterningNestedTestModel>(InterningNestedTestModel.Read.Interned("Test"), InterningNestedTestModel.Write.Interned("Test")) { IsMaster = false } .Static(1); var serverPropertyWrapper = new InterningTestPropertyWrapper <InterningNestedTestModel>(serverProperty, ServerProtocol.SerializationContext); var clientPropertyWrapper = new InterningTestPropertyWrapper <InterningNestedTestModel>(clientProperty, ClientProtocol.SerializationContext); serverPropertyWrapper.mySerializationContext = ServerProtocol.SerializationContext.WithInternRootsHere(serverPropertyWrapper, "Test"); clientPropertyWrapper.mySerializationContext = ClientProtocol.SerializationContext.WithInternRootsHere(clientPropertyWrapper, "Test"); serverPropertyWrapper.Bind(LifetimeDefinition.Lifetime, ServerProtocol, "top"); clientPropertyWrapper.Bind(LifetimeDefinition.Lifetime, ClientProtocol, "top"); var testValue = new InterningNestedTestModel("extremelyLongString", new InterningNestedTestModel("middle", new InterningNestedTestModel("bottom", null))); var firstSendBytes = MeasureBytes(ServerProtocol, () => { serverProperty.Value = testValue; Assertion.Assert(Equals(testValue, clientProperty.Value), "Received value should be the same as sent one"); }); var secondSendBytes = MeasureBytes(ServerProtocol, () => { serverProperty.Value = testValue.Inner; Assertion.Assert(Equals(testValue.Inner, clientProperty.Value), "Received value should be the same as sent one"); }); var thirdSendBytes = MeasureBytes(ServerProtocol, () => { serverProperty.Value = testValue; Assertion.Assert(Equals(testValue, clientProperty.Value), "Received value should be the same as sent one"); }); Assertion.Assert(secondSendBytes == thirdSendBytes, "Sending a single interned object should take the same amount of bytes"); Assertion.Assert(thirdSendBytes <= firstSendBytes - SumLengths(testValue), "Interning should save data"); }
private static int SumLengths(InterningNestedTestModel value) { return(value.Value.Length * 2 + 4 + (value.Inner == null ? 0 : SumLengths(value.Inner))); }