[c# epoxy] Clean up EpoxyProtocol.
This commit is contained in:
Родитель
30137da7de
Коммит
5e673765aa
|
@ -160,7 +160,7 @@ namespace Bond.Comm.Epoxy
|
|||
return $"{nameof(EpoxyConnection)}(local: {LocalEndPoint}, remote: {RemoteEndPoint})";
|
||||
}
|
||||
|
||||
internal static Frame MessageToFrame(ulong conversationId, string methodName, PayloadType type, IMessage payload, IBonded layerData, Logger logger)
|
||||
internal static Frame MessageToFrame(ulong conversationId, string methodName, PayloadType type, IMessage message, IBonded layerData, Logger logger)
|
||||
{
|
||||
var frame = new Frame(logger);
|
||||
|
||||
|
@ -172,9 +172,9 @@ namespace Bond.Comm.Epoxy
|
|||
method_name = methodName ?? string.Empty, // method_name is not nullable
|
||||
};
|
||||
|
||||
if (payload.IsError)
|
||||
if (message.IsError)
|
||||
{
|
||||
headers.error_code = payload.Error.Deserialize<Error>().error_code;
|
||||
headers.error_code = message.Error.Deserialize<Error>().error_code;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -194,21 +194,17 @@ namespace Bond.Comm.Epoxy
|
|||
const int initialLayerDataBufferSize = 150;
|
||||
var outputBuffer = new OutputBuffer(initialLayerDataBufferSize);
|
||||
var compactWriter = new CompactBinaryWriter<OutputBuffer>(outputBuffer);
|
||||
// TODO: See TODO below about issues with IBonded Marshal.TO(...)
|
||||
compactWriter.WriteVersion();
|
||||
layerData.Serialize(compactWriter);
|
||||
frame.Add(new Framelet(FrameletType.LayerData, outputBuffer.Data));
|
||||
}
|
||||
|
||||
{
|
||||
var userData = payload.IsError ? (IBonded)payload.Error : (IBonded)payload.RawPayload;
|
||||
|
||||
var userData = message.IsError ? (IBonded)message.Error : (IBonded)message.RawPayload;
|
||||
|
||||
const int initialPayloadBufferSize = 1024;
|
||||
var outputBuffer = new OutputBuffer(initialPayloadBufferSize);
|
||||
var compactWriter = new CompactBinaryWriter<OutputBuffer>(outputBuffer);
|
||||
// TODO: marshal dies on IBonded Marshal.To(compactWriter, request)
|
||||
// understand more deeply why and consider fixing
|
||||
compactWriter.WriteVersion();
|
||||
userData.Serialize(compactWriter);
|
||||
|
||||
|
@ -234,7 +230,7 @@ namespace Bond.Comm.Epoxy
|
|||
var protocolError = new ProtocolError
|
||||
{
|
||||
error_code = errorCode,
|
||||
details = details == null ? null : new Bonded<Error>(details)
|
||||
details = (details == null ? null : new Bonded<Error>(details))
|
||||
};
|
||||
|
||||
var outputBuffer = new OutputBuffer(16);
|
||||
|
|
|
@ -182,11 +182,11 @@ namespace Bond.Comm.Epoxy
|
|||
logger.Site().Debug("Processing {0} framelets.", frame.Count);
|
||||
|
||||
var state = ClassifyState.ExpectFirstFramelet;
|
||||
var disposition = FrameDisposition.Indeterminate;
|
||||
EpoxyHeaders headers = null;
|
||||
var layerData = new ArraySegment<byte>();
|
||||
var payload = new ArraySegment<byte>();
|
||||
ProtocolError error = null;
|
||||
var disposition = FrameDisposition.Indeterminate;
|
||||
ProtocolErrorCode? errorCode = null;
|
||||
uint transitions = 0;
|
||||
while (true)
|
||||
|
@ -336,7 +336,6 @@ namespace Bond.Comm.Epoxy
|
|||
}
|
||||
|
||||
var framelet = frame.Framelets[0];
|
||||
|
||||
var inputBuffer = new InputBuffer(framelet.Contents);
|
||||
var fastBinaryReader = new FastBinaryReader<InputBuffer>(inputBuffer, version: 1);
|
||||
switch (headersDeserializer.TryDeserialize(fastBinaryReader, out headers))
|
||||
|
@ -369,7 +368,7 @@ namespace Bond.Comm.Epoxy
|
|||
|
||||
if (frame.Count < 2)
|
||||
{
|
||||
logger.Site().Error("Frame did not continue with LayerData or PayloadData.");
|
||||
logger.Site().Error("Frame had headers but no payload.");
|
||||
errorCode = ProtocolErrorCode.MALFORMED_DATA;
|
||||
return ClassifyState.MalformedFrame;
|
||||
}
|
||||
|
@ -398,10 +397,9 @@ namespace Bond.Comm.Epoxy
|
|||
}
|
||||
|
||||
int payloadDataIndex = (layerData.Array == null ? 1 : 2);
|
||||
|
||||
if (payloadDataIndex >= frame.Count)
|
||||
{
|
||||
logger.Site().Error("Frame did not continue with PayloadData.");
|
||||
logger.Site().Error("Frame had headers but no payload.");
|
||||
errorCode = ProtocolErrorCode.MALFORMED_DATA;
|
||||
return ClassifyState.MalformedFrame;
|
||||
}
|
||||
|
@ -409,7 +407,7 @@ namespace Bond.Comm.Epoxy
|
|||
var framelet = frame.Framelets[payloadDataIndex];
|
||||
if (framelet.Type != FrameletType.PayloadData)
|
||||
{
|
||||
logger.Site().Error("Frame did not continue with PayloadData.");
|
||||
logger.Site().Error("Frame had headers but no payload.");
|
||||
errorCode = ProtocolErrorCode.MALFORMED_DATA;
|
||||
return ClassifyState.MalformedFrame;
|
||||
}
|
||||
|
@ -424,13 +422,13 @@ namespace Bond.Comm.Epoxy
|
|||
ClassifyState state, Frame frame, ArraySegment<byte> layerData, ref ProtocolErrorCode? errorCode,
|
||||
Logger logger)
|
||||
{
|
||||
// FIXME: Change all of these to asserts.
|
||||
if (state != ClassifyState.ExpectEndOfFrame || frame == null)
|
||||
{
|
||||
return ClassifyState.InternalStateError;
|
||||
}
|
||||
|
||||
int validFrameSize = (layerData.Array == null ? 2 : 3);
|
||||
|
||||
var validFrameSize = (layerData.Array == null ? 2 : 3);
|
||||
if (frame.Count == validFrameSize)
|
||||
{
|
||||
return ClassifyState.FrameComplete;
|
||||
|
@ -457,6 +455,7 @@ namespace Bond.Comm.Epoxy
|
|||
case PayloadType.Response:
|
||||
case PayloadType.Event:
|
||||
return ClassifyState.ValidFrame;
|
||||
|
||||
default:
|
||||
logger.Site().Warning("Received unrecognized payload type {0}.", headers.payload_type);
|
||||
errorCode = ProtocolErrorCode.NOT_SUPPORTED;
|
||||
|
@ -541,6 +540,12 @@ namespace Bond.Comm.Epoxy
|
|||
return ClassifyState.InternalStateError;
|
||||
}
|
||||
|
||||
if (frame.Count > 1)
|
||||
{
|
||||
logger.Site().Error("Protocol error frame had trailing framelets.");
|
||||
return ClassifyState.ErrorInErrorFrame;
|
||||
}
|
||||
|
||||
var framelet = frame.Framelets[0];
|
||||
|
||||
var inputBuffer = new InputBuffer(framelet.Contents);
|
||||
|
@ -556,15 +561,7 @@ namespace Bond.Comm.Epoxy
|
|||
}
|
||||
|
||||
logger.Site().Debug("Deserialized {0} with code {1}.", nameof(ProtocolError), error.error_code);
|
||||
|
||||
if (frame.Count > 1)
|
||||
{
|
||||
logger.Site().Error("Frame had trailing framelets.");
|
||||
return ClassifyState.ErrorInErrorFrame;
|
||||
}
|
||||
|
||||
disposition = FrameDisposition.HandleProtocolError;
|
||||
|
||||
return ClassifyState.ClassifiedValidFrame;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,8 @@ namespace UnitTest.Epoxy
|
|||
{
|
||||
using Bond.Comm;
|
||||
using Bond.Comm.Epoxy;
|
||||
using Bond.IO.Safe;
|
||||
using Bond.Protocols;
|
||||
using NUnit.Framework;
|
||||
using UnitTest.Comm;
|
||||
using UnitTest.Interfaces;
|
||||
|
@ -18,11 +20,10 @@ namespace UnitTest.Epoxy
|
|||
private const uint GoodRequestId = 1;
|
||||
private const uint GoodResponseId = 1;
|
||||
private const string GoodMethod = "ShaveYaks";
|
||||
private static readonly Error goodPayload = new Error();
|
||||
private static readonly IMessage<Error> meaninglessMessage = new Message<Error>(goodPayload);
|
||||
private static readonly ProtocolErrorCode meaninglessErrorCode = ProtocolErrorCode.GENERIC_ERROR;
|
||||
private const ProtocolErrorCode MeaninglessErrorCode = ProtocolErrorCode.GENERIC_ERROR;
|
||||
private static readonly IMessage<Dummy> meaninglessMessage = new Message<Dummy>(new Dummy());
|
||||
private static readonly ArraySegment<byte> emptyLayerData = new ArraySegment<byte>();
|
||||
private static readonly ArraySegment<byte> nonEmptyLayerData = new ArraySegment<byte>(new byte[] { 3 });
|
||||
private static ArraySegment<byte> goodLayerData;
|
||||
|
||||
private static readonly EpoxyHeaders goodRequestHeaders = new EpoxyHeaders
|
||||
{
|
||||
|
@ -52,11 +53,10 @@ namespace UnitTest.Epoxy
|
|||
payload_type = (PayloadType)(-100),
|
||||
conversation_id = GoodRequestId
|
||||
};
|
||||
private static readonly Dummy DummyObject = new Dummy
|
||||
private static readonly Dummy dummyObject = new Dummy
|
||||
{
|
||||
int_value = 0x1234
|
||||
};
|
||||
private static readonly Bond.IBonded GoodLayerData = new Bond.Bonded<Dummy>(DummyObject);
|
||||
|
||||
private static Frame goodRequestFrame;
|
||||
private static Frame goodRequestLayerDataFrame;
|
||||
|
@ -77,11 +77,19 @@ namespace UnitTest.Epoxy
|
|||
[TestFixtureSetUp]
|
||||
public static void CreateFrames()
|
||||
{
|
||||
// Set up the non-empty layer data we'll use.
|
||||
Bond.IBonded goodLayerObject = new Bond.Bonded<Dummy>(dummyObject);
|
||||
var outputBuffer = new OutputBuffer();
|
||||
var compactWriter = new CompactBinaryWriter<OutputBuffer>(outputBuffer);
|
||||
compactWriter.WriteVersion();
|
||||
goodLayerObject.Serialize(compactWriter);
|
||||
goodLayerData = outputBuffer.Data;
|
||||
|
||||
// Good frames, from which we can pull good framelets to build bad frames.
|
||||
goodRequestFrame = EpoxyConnection.MessageToFrame(
|
||||
GoodRequestId, GoodMethod, PayloadType.Request, meaninglessMessage, null, LoggerTests.BlackHole);
|
||||
goodRequestLayerDataFrame = EpoxyConnection.MessageToFrame(
|
||||
GoodRequestId, GoodMethod, PayloadType.Request, meaninglessMessage, GoodLayerData, LoggerTests.BlackHole);
|
||||
GoodRequestId, GoodMethod, PayloadType.Request, meaninglessMessage, goodLayerObject, LoggerTests.BlackHole);
|
||||
|
||||
goodResponseFrame = EpoxyConnection.MessageToFrame(
|
||||
GoodResponseId, GoodMethod, PayloadType.Response, meaninglessMessage, null, LoggerTests.BlackHole);
|
||||
|
@ -89,7 +97,7 @@ namespace UnitTest.Epoxy
|
|||
GoodRequestId, GoodMethod, PayloadType.Event, meaninglessMessage, null, LoggerTests.BlackHole);
|
||||
|
||||
configFrame = EpoxyConnection.MakeConfigFrame(LoggerTests.BlackHole);
|
||||
protocolErrorFrame = EpoxyConnection.MakeProtocolErrorFrame(meaninglessErrorCode, null, LoggerTests.BlackHole);
|
||||
protocolErrorFrame = EpoxyConnection.MakeProtocolErrorFrame(MeaninglessErrorCode, null, LoggerTests.BlackHole);
|
||||
|
||||
var goodFrameletCount = goodRequestFrame.Count;
|
||||
|
||||
|
@ -107,7 +115,7 @@ namespace UnitTest.Epoxy
|
|||
doubleHeadersRequestFrame.Add(goodRequestFrame.Framelets[i]);
|
||||
}
|
||||
|
||||
headersConfigRequestFrame = new Frame(1, LoggerTests.BlackHole);
|
||||
headersConfigRequestFrame = new Frame(2, LoggerTests.BlackHole);
|
||||
headersConfigRequestFrame.Add(goodRequestFrame.Framelets[0]);
|
||||
headersConfigRequestFrame.Add(configFrame.Framelets[0]);
|
||||
|
||||
|
@ -124,7 +132,7 @@ namespace UnitTest.Epoxy
|
|||
backwardsRequestFrame.Add(framelet);
|
||||
}
|
||||
|
||||
doubleProtocolErrorFrame = EpoxyConnection.MakeProtocolErrorFrame(meaninglessErrorCode, null, LoggerTests.BlackHole);
|
||||
doubleProtocolErrorFrame = EpoxyConnection.MakeProtocolErrorFrame(MeaninglessErrorCode, null, LoggerTests.BlackHole);
|
||||
doubleProtocolErrorFrame.Add(doubleProtocolErrorFrame.Framelets[0]);
|
||||
|
||||
configFrameExtra = EpoxyConnection.MakeConfigFrame(LoggerTests.BlackHole);
|
||||
|
@ -224,7 +232,7 @@ namespace UnitTest.Epoxy
|
|||
}
|
||||
|
||||
[Test]
|
||||
public void TransitionOptionalExpectLayerData_Valid()
|
||||
public void TransitionExpectOptionalLayerData_Valid()
|
||||
{
|
||||
var layerData = new ArraySegment<byte>();
|
||||
ProtocolErrorCode? errorCode = null;
|
||||
|
@ -240,12 +248,12 @@ namespace UnitTest.Epoxy
|
|||
EpoxyProtocol.ClassifyState.ExpectOptionalLayerData, goodRequestLayerDataFrame, goodRequestHeaders,
|
||||
ref layerData, ref errorCode, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.ClassifyState.ExpectPayload, after);
|
||||
Assert.NotNull(layerData.Array);
|
||||
CollectionAssert.AreEqual(goodLayerData, layerData);
|
||||
Assert.Null(errorCode);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TransitionOptionalExpectLayerData_InvalidPreconditions()
|
||||
public void TransitionExpectOptionalLayerData_InvalidPreconditions()
|
||||
{
|
||||
var layerData = new ArraySegment<byte>();
|
||||
ProtocolErrorCode? errorCode = null;
|
||||
|
@ -259,23 +267,16 @@ namespace UnitTest.Epoxy
|
|||
}
|
||||
|
||||
[Test]
|
||||
public void TransitionOptionalExpectLayerData_MalformedFrame()
|
||||
public void TransitionExpectOptionalLayerData_MalformedFrame()
|
||||
{
|
||||
var payload = new ArraySegment<byte>();
|
||||
var layerData = new ArraySegment<byte>();
|
||||
ProtocolErrorCode? errorCode = null;
|
||||
|
||||
var after = EpoxyProtocol.TransitionExpectPayload(
|
||||
EpoxyProtocol.ClassifyState.ExpectPayload, emptyFrame, goodRequestHeaders, emptyLayerData,
|
||||
ref payload, ref errorCode, LoggerTests.BlackHole);
|
||||
var after = EpoxyProtocol.TransitionExpectOptionalLayerData(
|
||||
EpoxyProtocol.ClassifyState.ExpectOptionalLayerData, shortRequestFrame, goodRequestHeaders,
|
||||
ref layerData, ref errorCode, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.ClassifyState.MalformedFrame, after);
|
||||
Assert.Null(payload.Array);
|
||||
Assert.AreEqual(ProtocolErrorCode.MALFORMED_DATA, errorCode);
|
||||
|
||||
after = EpoxyProtocol.TransitionExpectPayload(
|
||||
EpoxyProtocol.ClassifyState.ExpectPayload, shortRequestFrame, goodRequestHeaders, emptyLayerData,
|
||||
ref payload, ref errorCode, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.ClassifyState.MalformedFrame, after);
|
||||
Assert.Null(payload.Array);
|
||||
Assert.Null(layerData.Array);
|
||||
Assert.AreEqual(ProtocolErrorCode.MALFORMED_DATA, errorCode);
|
||||
}
|
||||
|
||||
|
@ -293,7 +294,7 @@ namespace UnitTest.Epoxy
|
|||
Assert.Null(errorCode);
|
||||
|
||||
after = EpoxyProtocol.TransitionExpectPayload(
|
||||
EpoxyProtocol.ClassifyState.ExpectPayload, goodRequestLayerDataFrame, goodRequestHeaders, nonEmptyLayerData,
|
||||
EpoxyProtocol.ClassifyState.ExpectPayload, goodRequestLayerDataFrame, goodRequestHeaders, goodLayerData,
|
||||
ref payload, ref errorCode, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.ClassifyState.ExpectEndOfFrame, after);
|
||||
Assert.NotNull(payload.Array);
|
||||
|
@ -342,7 +343,7 @@ namespace UnitTest.Epoxy
|
|||
Assert.AreEqual(ProtocolErrorCode.MALFORMED_DATA, errorCode);
|
||||
|
||||
after = EpoxyProtocol.TransitionExpectPayload(
|
||||
EpoxyProtocol.ClassifyState.ExpectPayload, goodRequestFrame, goodRequestHeaders, nonEmptyLayerData,
|
||||
EpoxyProtocol.ClassifyState.ExpectPayload, goodRequestFrame, goodRequestHeaders, goodLayerData,
|
||||
ref payload, ref errorCode, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.ClassifyState.MalformedFrame, after);
|
||||
Assert.Null(payload.Array);
|
||||
|
@ -360,7 +361,7 @@ namespace UnitTest.Epoxy
|
|||
Assert.Null(errorCode);
|
||||
|
||||
after = EpoxyProtocol.TransitionExpectEndOfFrame(
|
||||
EpoxyProtocol.ClassifyState.ExpectEndOfFrame, goodRequestLayerDataFrame, nonEmptyLayerData, ref errorCode, LoggerTests.BlackHole);
|
||||
EpoxyProtocol.ClassifyState.ExpectEndOfFrame, goodRequestLayerDataFrame, goodLayerData, ref errorCode, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.ClassifyState.FrameComplete, after);
|
||||
Assert.Null(errorCode);
|
||||
}
|
||||
|
@ -390,6 +391,11 @@ namespace UnitTest.Epoxy
|
|||
EpoxyProtocol.ClassifyState.FrameComplete, goodResponseHeaders, ref errorCode, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.ClassifyState.ValidFrame, after);
|
||||
Assert.Null(errorCode);
|
||||
|
||||
after = EpoxyProtocol.TransitionFrameComplete(
|
||||
EpoxyProtocol.ClassifyState.FrameComplete, goodEventHeaders, ref errorCode, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.ClassifyState.ValidFrame, after);
|
||||
Assert.Null(errorCode);
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
@ -419,6 +425,13 @@ namespace UnitTest.Epoxy
|
|||
EpoxyProtocol.ClassifyState.ValidFrame, goodResponseHeaders, ref disposition);
|
||||
Assert.AreEqual(EpoxyProtocol.ClassifyState.ClassifiedValidFrame, after);
|
||||
Assert.AreEqual(EpoxyProtocol.FrameDisposition.DeliverResponseToProxy, disposition);
|
||||
|
||||
disposition = EpoxyProtocol.FrameDisposition.Indeterminate;
|
||||
|
||||
after = EpoxyProtocol.TransitionValidFrame(
|
||||
EpoxyProtocol.ClassifyState.ValidFrame, goodEventHeaders, ref disposition);
|
||||
Assert.AreEqual(EpoxyProtocol.ClassifyState.ClassifiedValidFrame, after);
|
||||
Assert.AreEqual(EpoxyProtocol.FrameDisposition.DeliverEventToService, disposition);
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
@ -479,11 +492,15 @@ namespace UnitTest.Epoxy
|
|||
var after = EpoxyProtocol.TransitionExpectConfig(
|
||||
EpoxyProtocol.ClassifyState.ExpectConfig, emptyFrame, ref errorCode, ref disposition, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.ClassifyState.InternalStateError, after);
|
||||
Assert.AreEqual(EpoxyProtocol.FrameDisposition.Indeterminate, disposition);
|
||||
Assert.IsNull(errorCode);
|
||||
|
||||
// not a config frame
|
||||
// Non-empty, non-config frame.
|
||||
after = EpoxyProtocol.TransitionExpectConfig(
|
||||
EpoxyProtocol.ClassifyState.ExpectConfig, protocolErrorFrame, ref errorCode, ref disposition, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.ClassifyState.InternalStateError, after);
|
||||
Assert.AreEqual(EpoxyProtocol.FrameDisposition.Indeterminate, disposition);
|
||||
Assert.IsNull(errorCode);
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
@ -495,12 +512,12 @@ namespace UnitTest.Epoxy
|
|||
var after = EpoxyProtocol.TransitionExpectProtocolError(
|
||||
EpoxyProtocol.ClassifyState.ExpectProtocolError, protocolErrorFrame, ref error, ref disposition, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.ClassifyState.ClassifiedValidFrame, after);
|
||||
Assert.AreEqual(meaninglessErrorCode, error.error_code);
|
||||
Assert.AreEqual(MeaninglessErrorCode, error.error_code);
|
||||
Assert.AreEqual(EpoxyProtocol.FrameDisposition.HandleProtocolError, disposition);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TransitionExpectProtocolError_Error()
|
||||
public void TransitionExpectProtocolError_ErrorInError()
|
||||
{
|
||||
ProtocolError error = null;
|
||||
var disposition = EpoxyProtocol.FrameDisposition.Indeterminate;
|
||||
|
@ -508,6 +525,8 @@ namespace UnitTest.Epoxy
|
|||
var after = EpoxyProtocol.TransitionExpectProtocolError(
|
||||
EpoxyProtocol.ClassifyState.ExpectProtocolError, doubleProtocolErrorFrame, ref error, ref disposition, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.ClassifyState.ErrorInErrorFrame, after);
|
||||
Assert.Null(error);
|
||||
Assert.AreEqual(EpoxyProtocol.FrameDisposition.Indeterminate, disposition);
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
@ -517,7 +536,7 @@ namespace UnitTest.Epoxy
|
|||
var disposition = EpoxyProtocol.FrameDisposition.Indeterminate;
|
||||
|
||||
var after = EpoxyProtocol.TransitionExpectProtocolError(
|
||||
EpoxyProtocol.ClassifyState.ExpectProtocolError, backwardsRequestFrame, ref error, ref disposition, LoggerTests.BlackHole);
|
||||
EpoxyProtocol.ClassifyState.ExpectProtocolError, goodRequestFrame, ref error, ref disposition, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.ClassifyState.InternalStateError, after);
|
||||
Assert.Null(error);
|
||||
Assert.AreEqual(EpoxyProtocol.FrameDisposition.Indeterminate, disposition);
|
||||
|
@ -525,51 +544,72 @@ namespace UnitTest.Epoxy
|
|||
|
||||
// These end-to-end tests cover states that don't fit in functions.
|
||||
|
||||
private static void AssertHeadersEqual(EpoxyHeaders expected, EpoxyHeaders actual)
|
||||
{
|
||||
Assert.AreEqual(expected.error_code, actual.error_code);
|
||||
Assert.AreEqual(expected.method_name, actual.method_name);
|
||||
Assert.AreEqual(expected.payload_type, actual.payload_type);
|
||||
Assert.AreEqual(expected.conversation_id, actual.conversation_id);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Classify_Valid()
|
||||
{
|
||||
var requestResult = EpoxyProtocol.Classify(goodRequestFrame, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.FrameDisposition.DeliverRequestToService, requestResult.Disposition);
|
||||
Assert.AreEqual(goodRequestHeaders.error_code, requestResult.Headers.error_code);
|
||||
Assert.AreEqual(goodRequestHeaders.method_name, requestResult.Headers.method_name);
|
||||
Assert.AreEqual(goodRequestHeaders.payload_type, requestResult.Headers.payload_type);
|
||||
Assert.AreEqual(goodRequestHeaders.conversation_id, requestResult.Headers.conversation_id);
|
||||
Assert.Null(requestResult.LayerData.Array);
|
||||
AssertHeadersEqual(goodRequestHeaders, requestResult.Headers);
|
||||
Assert.AreEqual(goodRequestFrame.Framelets[goodRequestFrame.Count - 1].Contents, requestResult.Payload);
|
||||
Assert.Null(requestResult.Error);
|
||||
Assert.Null(requestResult.ErrorCode);
|
||||
|
||||
var requestLayerResult = EpoxyProtocol.Classify(goodRequestLayerDataFrame, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.FrameDisposition.DeliverRequestToService, requestLayerResult.Disposition);
|
||||
AssertHeadersEqual(goodRequestHeaders, requestLayerResult.Headers);
|
||||
CollectionAssert.AreEqual(goodLayerData, requestLayerResult.LayerData);
|
||||
Assert.AreEqual(goodRequestLayerDataFrame.Framelets[goodRequestLayerDataFrame.Count - 1].Contents, requestLayerResult.Payload);
|
||||
Assert.Null(requestLayerResult.Error);
|
||||
Assert.Null(requestLayerResult.ErrorCode);
|
||||
|
||||
var responseResult = EpoxyProtocol.Classify(goodResponseFrame, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.FrameDisposition.DeliverResponseToProxy, responseResult.Disposition);
|
||||
Assert.AreEqual(goodResponseHeaders.error_code, responseResult.Headers.error_code);
|
||||
Assert.AreEqual(goodResponseHeaders.method_name, responseResult.Headers.method_name);
|
||||
Assert.AreEqual(goodResponseHeaders.payload_type, responseResult.Headers.payload_type);
|
||||
Assert.AreEqual(goodResponseHeaders.conversation_id, responseResult.Headers.conversation_id);
|
||||
AssertHeadersEqual(goodResponseHeaders, responseResult.Headers);
|
||||
Assert.Null(requestResult.LayerData.Array);
|
||||
Assert.AreEqual(goodResponseFrame.Framelets[goodResponseFrame.Count - 1].Contents, responseResult.Payload);
|
||||
Assert.Null(requestResult.Error);
|
||||
Assert.Null(requestResult.ErrorCode);
|
||||
|
||||
var eventResult = EpoxyProtocol.Classify(goodEventFrame, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.FrameDisposition.DeliverEventToService, eventResult.Disposition);
|
||||
Assert.Null(eventResult.LayerData.Array);
|
||||
AssertHeadersEqual(goodEventHeaders, eventResult.Headers);
|
||||
Assert.AreEqual(goodEventFrame.Framelets[goodEventFrame.Count - 1].Contents, eventResult.Payload);
|
||||
Assert.Null(eventResult.Error);
|
||||
Assert.Null(eventResult.ErrorCode);
|
||||
|
||||
var configResult = EpoxyProtocol.Classify(configFrame, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.FrameDisposition.ProcessConfig, configResult.Disposition);
|
||||
Assert.Null(configResult.Headers);
|
||||
Assert.Null(configResult.LayerData.Array);
|
||||
Assert.Null(configResult.Payload.Array);
|
||||
Assert.Null(configResult.Error);
|
||||
Assert.Null(configResult.ErrorCode);
|
||||
|
||||
var protocolErrorResult = EpoxyProtocol.Classify(protocolErrorFrame, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.FrameDisposition.HandleProtocolError, protocolErrorResult.Disposition);
|
||||
Assert.Null(protocolErrorResult.Headers);
|
||||
Assert.Null(protocolErrorResult.LayerData.Array);
|
||||
Assert.Null(protocolErrorResult.Payload.Array);
|
||||
Assert.AreEqual(meaninglessErrorCode, protocolErrorResult.Error.error_code);
|
||||
Assert.AreEqual(MeaninglessErrorCode, protocolErrorResult.Error.error_code);
|
||||
Assert.Null(protocolErrorResult.ErrorCode);
|
||||
|
||||
var doubleProtocolErrorResult = EpoxyProtocol.Classify(doubleProtocolErrorFrame, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.FrameDisposition.HangUp, doubleProtocolErrorResult.Disposition);
|
||||
Assert.Null(doubleProtocolErrorResult.Headers);
|
||||
Assert.Null(doubleProtocolErrorResult.LayerData.Array);
|
||||
Assert.Null(doubleProtocolErrorResult.Payload.Array);
|
||||
Assert.AreEqual(ProtocolErrorCode.ERROR_IN_ERROR, doubleProtocolErrorResult.Error.error_code);
|
||||
|
||||
var eventResult = EpoxyProtocol.Classify(goodEventFrame, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.FrameDisposition.DeliverEventToService, eventResult.Disposition);
|
||||
Assert.AreEqual(goodEventHeaders.error_code, eventResult.Headers.error_code);
|
||||
Assert.AreEqual(goodEventHeaders.method_name, eventResult.Headers.method_name);
|
||||
Assert.AreEqual(goodEventHeaders.payload_type, eventResult.Headers.payload_type);
|
||||
Assert.AreEqual(goodEventHeaders.conversation_id, eventResult.Headers.conversation_id);
|
||||
Assert.AreEqual(goodEventFrame.Framelets[goodEventFrame.Count - 1].Contents, eventResult.Payload);
|
||||
Assert.Null(doubleProtocolErrorResult.ErrorCode);
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
@ -578,7 +618,9 @@ namespace UnitTest.Epoxy
|
|||
var nullResult = EpoxyProtocol.Classify(null, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.FrameDisposition.Indeterminate, nullResult.Disposition);
|
||||
Assert.Null(nullResult.Headers);
|
||||
Assert.Null(nullResult.LayerData.Array);
|
||||
Assert.Null(nullResult.Payload.Array);
|
||||
Assert.Null(nullResult.Error);
|
||||
Assert.Null(nullResult.ErrorCode);
|
||||
}
|
||||
|
||||
|
@ -588,42 +630,57 @@ namespace UnitTest.Epoxy
|
|||
var emptyResult = EpoxyProtocol.Classify(emptyFrame, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.FrameDisposition.SendProtocolError, emptyResult.Disposition);
|
||||
Assert.Null(emptyResult.Headers);
|
||||
Assert.Null(emptyResult.LayerData.Array);
|
||||
Assert.Null(emptyResult.Payload.Array);
|
||||
Assert.Null(emptyResult.Error);
|
||||
Assert.AreEqual(ProtocolErrorCode.MALFORMED_DATA, emptyResult.ErrorCode);
|
||||
|
||||
var shortResult = EpoxyProtocol.Classify(shortRequestFrame, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.FrameDisposition.SendProtocolError, shortResult.Disposition);
|
||||
Assert.Null(shortResult.Headers);
|
||||
Assert.Null(shortResult.LayerData.Array);
|
||||
Assert.Null(shortResult.Payload.Array);
|
||||
Assert.Null(shortResult.Error);
|
||||
Assert.AreEqual(ProtocolErrorCode.MALFORMED_DATA, shortResult.ErrorCode);
|
||||
|
||||
var doubleHeadersResult = EpoxyProtocol.Classify(doubleHeadersRequestFrame, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.FrameDisposition.SendProtocolError, doubleHeadersResult.Disposition);
|
||||
Assert.Null(doubleHeadersResult.Headers);
|
||||
Assert.Null(doubleHeadersResult.LayerData.Array);
|
||||
Assert.Null(doubleHeadersResult.Payload.Array);
|
||||
Assert.Null(doubleHeadersResult.Error);
|
||||
Assert.AreEqual(ProtocolErrorCode.MALFORMED_DATA, doubleHeadersResult.ErrorCode);
|
||||
|
||||
var headersConfigRequestResult = EpoxyProtocol.Classify(headersConfigRequestFrame, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.FrameDisposition.SendProtocolError, headersConfigRequestResult.Disposition);
|
||||
Assert.Null(headersConfigRequestResult.Headers);
|
||||
Assert.Null(headersConfigRequestResult.LayerData.Array);
|
||||
Assert.Null(headersConfigRequestResult.Payload.Array);
|
||||
Assert.Null(headersConfigRequestResult.Error);
|
||||
Assert.AreEqual(ProtocolErrorCode.MALFORMED_DATA, headersConfigRequestResult.ErrorCode);
|
||||
|
||||
var doublePayloadResult = EpoxyProtocol.Classify(doublePayloadRequestFrame, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.FrameDisposition.SendProtocolError, doublePayloadResult.Disposition);
|
||||
Assert.Null(doublePayloadResult.Headers);
|
||||
Assert.Null(doublePayloadResult.LayerData.Array);
|
||||
Assert.Null(doublePayloadResult.Payload.Array);
|
||||
Assert.Null(doublePayloadResult.Error);
|
||||
Assert.AreEqual(ProtocolErrorCode.MALFORMED_DATA, doublePayloadResult.ErrorCode);
|
||||
|
||||
var backwardsResult = EpoxyProtocol.Classify(backwardsRequestFrame, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.FrameDisposition.SendProtocolError, backwardsResult.Disposition);
|
||||
Assert.Null(backwardsResult.Headers);
|
||||
Assert.Null(backwardsResult.LayerData.Array);
|
||||
Assert.Null(backwardsResult.Payload.Array);
|
||||
Assert.Null(backwardsResult.Error);
|
||||
Assert.AreEqual(ProtocolErrorCode.MALFORMED_DATA, backwardsResult.ErrorCode);
|
||||
|
||||
var configExtraResult = EpoxyProtocol.Classify(configFrameExtra, LoggerTests.BlackHole);
|
||||
Assert.AreEqual(EpoxyProtocol.FrameDisposition.SendProtocolError, configExtraResult.Disposition);
|
||||
Assert.Null(configExtraResult.Headers);
|
||||
Assert.Null(configExtraResult.LayerData.Array);
|
||||
Assert.Null(configExtraResult.Payload.Array);
|
||||
Assert.Null(configExtraResult.Error);
|
||||
Assert.AreEqual(ProtocolErrorCode.MALFORMED_DATA, configExtraResult.ErrorCode);
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче