when_send_queue_size_is_smaller_than_threshold_should_not_close_connection() { var mre = new ManualResetEventSlim(); var messageSize = _connectionPendingSendBytesThreshold; var evnt = new EventRecord(0, 0, Guid.NewGuid(), Guid.NewGuid(), 0, 0, "testStream", 0, DateTime.Now, PrepareFlags.None, "eventType", new byte[messageSize], new byte[0]); var record = ResolvedEvent.ForUnresolvedEvent(evnt, null); var message = new ClientMessage.ReadEventCompleted(Guid.NewGuid(), "testStream", ReadEventResult.Success, record, StreamMetadata.Empty, false, ""); var dummyConnection = new DummyTcpConnection(); dummyConnection.SendQueueSize = ESConsts.MaxConnectionQueueSize - 1; var tcpConnectionManager = new TcpConnectionManager( Guid.NewGuid().ToString(), TcpServiceType.External, new ClientTcpDispatcher(2000), InMemoryBus.CreateTest(), dummyConnection, InMemoryBus.CreateTest(), new InternalAuthenticationProvider(InMemoryBus.CreateTest(), new Core.Helpers.IODispatcher(InMemoryBus.CreateTest(), new NoopEnvelope()), null, 1, false), new AuthorizationGateway(new TestAuthorizationProvider()), TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10), (man, err) => { mre.Set(); }, ESConsts.UnrestrictedPendingSendBytes, ESConsts.MaxConnectionQueueSize); tcpConnectionManager.SendMessage(message); var data = dummyConnection.ReceivedData.Last(); var receivedPackage = TcpPackage.FromArraySegment(data); Assert.AreEqual(receivedPackage.Command, TcpCommand.ReadEventCompleted, "Expected ReadEventCompleted but got {0}", receivedPackage.Command); }
when_send_queue_size_is_larger_than_threshold_should_close_connection() { var mre = new ManualResetEventSlim(); var messageSize = _connectionPendingSendBytesThreshold; var evnt = new EventRecord(0, 0, Guid.NewGuid(), Guid.NewGuid(), 0, 0, "testStream", 0, DateTime.Now, PrepareFlags.None, "eventType", new byte[messageSize], new byte[0]); var record = ResolvedEvent.ForUnresolvedEvent(evnt, null); var message = new ClientMessage.ReadEventCompleted(Guid.NewGuid(), "testStream", ReadEventResult.Success, record, StreamMetadata.Empty, false, ""); var dummyConnection = new DummyTcpConnection(); dummyConnection.SendQueueSize = ESConsts.MaxConnectionQueueSize + 1; var tcpConnectionManager = new TcpConnectionManager( Guid.NewGuid().ToString(), TcpServiceType.External, new ClientTcpDispatcher(2000), InMemoryBus.CreateTest(), dummyConnection, InMemoryBus.CreateTest(), new InternalAuthenticationProvider(InMemoryBus.CreateTest(), new Core.Helpers.IODispatcher(InMemoryBus.CreateTest(), new NoopEnvelope()), null, 1, false), new AuthorizationGateway(new TestAuthorizationProvider()), TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10), (man, err) => { mre.Set(); }, ESConsts.UnrestrictedPendingSendBytes, ESConsts.MaxConnectionQueueSize); tcpConnectionManager.SendMessage(message); if (!mre.Wait(2000)) { Assert.Fail("Timed out waiting for connection to close"); } }
public static string SmartFormat(ClientMessage.ReadEventCompleted completed, ICodec targetCodec) { var dto = new HttpClientMessageDto.ReadEventCompletedText(completed); if (completed.Record.Flags.HasFlag(PrepareFlags.IsJson)) { var deserializedData = Codec.Json.From <object>((string)dto.Data); var deserializedMetadata = Codec.Json.From <object>((string)dto.Metadata); if (deserializedData != null) { dto.Data = deserializedData; } if (deserializedMetadata != null) { dto.Metadata = deserializedMetadata; } } switch (targetCodec.ContentType) { case ContentType.Xml: case ContentType.ApplicationXml: case ContentType.Atom: { var serializeObject = JsonConvert.SerializeObject(dto); var deserializeXmlNode = JsonConvert.DeserializeXmlNode(serializeObject, "read-event-result"); return(deserializeXmlNode.InnerXml); } default: return(targetCodec.To(dto)); } }
when_limit_pending_and_sending_message_larger_than_pending_bytes_threshold_but_no_bytes_pending_should_not_close_connection() { var messageSize = _connectionPendingSendBytesThreshold + 1000; var evnt = new EventRecord(0, 0, Guid.NewGuid(), Guid.NewGuid(), 0, 0, "testStream", 0, DateTime.Now, PrepareFlags.None, "eventType", new byte[messageSize], new byte[0]); var record = ResolvedEvent.ForUnresolvedEvent(evnt, null); var message = new ClientMessage.ReadEventCompleted(Guid.NewGuid(), "testStream", ReadEventResult.Success, record, StreamMetadata.Empty, false, ""); var dummyConnection = new DummyTcpConnection(); dummyConnection.PendingSendBytes = 0; var tcpConnectionManager = new TcpConnectionManager( Guid.NewGuid().ToString(), TcpServiceType.External, new ClientTcpDispatcher(), InMemoryBus.CreateTest(), dummyConnection, InMemoryBus.CreateTest(), new InternalAuthenticationProvider( new Core.Helpers.IODispatcher(InMemoryBus.CreateTest(), new NoopEnvelope()), null, 1, false), TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10), (man, err) => { }, _connectionPendingSendBytesThreshold, _connectionQueueSizeThreshold); tcpConnectionManager.SendMessage(message); var data = dummyConnection.ReceivedData.Last(); var receivedPackage = TcpPackage.FromArraySegment(data); Assert.AreEqual(receivedPackage.Command, TcpCommand.ReadEventCompleted, "Expected ReadEventCompleted but got {0}", receivedPackage.Command); }
private static TcpPackage WrapReadEventCompleted(ClientMessage.ReadEventCompleted msg) { var dto = new TcpClientMessageDto.ReadEventCompleted( (TcpClientMessageDto.ReadEventCompleted.ReadEventResult)msg.Result, new TcpClientMessageDto.ResolvedIndexedEvent(msg.Record.Event, msg.Record.Link), msg.Error); return(new TcpPackage(TcpCommand.ReadEventCompleted, msg.CorrelationId, dto.Serialize())); }
public static string GetXmlReadResult(ClientMessage.ReadEventCompleted completed, bool dataJson = true, bool metadataJson = true) { return(string.Format(XmlReadFormat, completed.Record.EventStreamId, completed.Record.EventNumber, completed.Record.EventType, dataJson ? XmlData : AsString(completed.Record.Data), metadataJson ? XmlMetadata : AsString(completed.Record.Metadata))); }
public static string GetJsonReadResult(ClientMessage.ReadEventCompleted completed, bool dataJson = true, bool metadataJson = true) { return(string.Format(JsonReadFormat, WrapIntoQuotes(completed.Record.EventStreamId), completed.Record.EventNumber, WrapIntoQuotes(completed.Record.EventType), dataJson ? JsonData : WrapIntoQuotes(AsString(completed.Record.Data)), metadataJson ? JsonMetadata : WrapIntoQuotes(AsString(completed.Record.Metadata)))); }
public void Handle(ClientMessage.ReadEventCompleted message) { IEnvelope envelope; if (_correlations.TryGetValue(message.CorrelationId, out envelope)) { envelope.ReplyWith(message); _correlations.Remove(message.CorrelationId); } }
private static TcpPackage WrapReadEventsCompleted(ClientMessage.ReadEventCompleted msg) { var dto = new ClientMessageDto.ReadEventCompleted(msg.CorrelationId, msg.EventStreamId, msg.EventNumber, msg.Result, msg.Record == null ? null : msg.Record.EventType, msg.Record == null ? null : msg.Record.Data, msg.Record == null ? null : msg.Record.Metadata); return(new TcpPackage(TcpCommand.ReadEventCompleted, msg.CorrelationId, dto.Serialize())); }
public static string ConvertOnRead(ClientMessage.ReadEventCompleted completed, ICodec responseCodec) { var dto = new ClientMessageDto.ReadEventCompletedText(completed); if (completed.Record.Flags.HasFlag(PrepareFlags.IsJson)) { dto.Data = Codec.Json.From <object>((string)dto.Data); dto.Metadata = Codec.Json.From <object>((string)dto.Metadata); } var type = responseCodec.GetType(); type = type == typeof(CustomCodec) ? ((CustomCodec)responseCodec).BaseCodec.GetType() : type; return(type == typeof(XmlCodec) ? Codec.Json.ToXmlUsingJson(dto) : responseCodec.To(dto)); }
public static ClientMessage.ReadEventCompleted ReadEvent(MiniClusterNode node, string streamId, long eventNumber) { ClientMessage.ReadEventCompleted readResult = null; var resetEvent = new ManualResetEventSlim(); var read = new ClientMessage.ReadEvent(Guid.NewGuid(), Guid.NewGuid(), new CallbackEnvelope(msg => { readResult = (ClientMessage.ReadEventCompleted)msg; resetEvent.Set(); }), streamId, eventNumber, false, false, SystemAccount.Principal); node.Node.MainQueue.Publish(read); if (!resetEvent.Wait(_timeout)) { Assert.Fail("Timed out waiting for the event to be read"); return(null); } return(readResult); }
// Used when we need to resolve a link event to see if it points to an event that no longer exists. If that // event no longer exists, we skip it and resume the recovery process. private void OnEmittedLinkEventResolved(bool anyFound, EmittedEvent eventToWrite, Tuple <CheckpointTag, string, long> topAlreadyCommitted, ClientMessage.ReadEventCompleted resp) { if (resp.Result != ReadEventResult.StreamDeleted && resp.Result != ReadEventResult.NotFound && resp.Result != ReadEventResult.NoStream && resp.Result != ReadEventResult.Success) { throw CreateSequenceException(topAlreadyCommitted, eventToWrite); } if (resp.Result == ReadEventResult.Success) { anyFound = true; NotifyEventCommitted(eventToWrite, topAlreadyCommitted.Item3); } else { Log.Verbose($"Emitted event ignored after resolution because it links to an event that no longer exists: eventId: {eventToWrite.EventId}, eventType: {eventToWrite.EventId}, checkpoint: {eventToWrite.CorrelationId}, causedBy: {eventToWrite.CausedBy}"); } _pendingWrites.Dequeue(); _awaitingLinkToResolution = false; SubmitWriteEventsInRecoveryLoop(anyFound); }
// Used when we need to resolve a link event to see if it points to an event that no longer exists. If that // event no longer exists, we skip it and resume the recovery process. private void OnEmittedLinkEventResolved(bool anyFound, EmittedEvent eventToWrite, ClientMessage.ReadEventCompleted resp) { var topAlreadyCommitted = _alreadyCommittedEvents.Pop(); if (resp.Result != ReadEventResult.StreamDeleted && resp.Result != ReadEventResult.NotFound) { throw CreateSequenceException(topAlreadyCommitted, eventToWrite); } Log.Verbose($"Emitted event ignored after resolution because it links to an event that no longer exists: eventId: {eventToWrite.EventId}, eventType: {eventToWrite.EventId}, checkpoint: {eventToWrite.CorrelationId}, causedBy: {eventToWrite.CausedBy}"); _pendingWrites.Dequeue(); SubmitWriteEventsInRecoveryLoop(anyFound); }