public VerificationResult VerifyThat(Event actualEvent, int actualVersion) { var sb = new StringBuilder(); if (ShouldBeVersion != Core.Data.ExpectedVersion.Any && actualVersion != ShouldBeVersion) sb.AppendFormat("- Actual version is wrong. Expected: {0}, actual: {1}.\n", ShouldBeVersion, actualVersion); if (actualEvent == null) { sb.AppendFormat("- Actual event is NULL!\n"); return new VerificationResult(ComparisonStatus.Fail, sb.ToString()); } if (actualEvent.EventId != Event.EventId) sb.AppendFormat("- Wrong EventId. Expected: {0}, actual: {1}.\n", Event.EventId, actualEvent.EventId); if (actualEvent.EventType != Event.EventType) sb.AppendFormat("- Wrong EventType. Expected: {0}, actual: {1}.\n", Event.EventType, actualEvent.EventType); if (actualEvent.Data.Length != Event.Data.Length) sb.AppendFormat("- Wrong length of data. Expected length: {0}, actual length: {1}.\n", Event.Data.Length, actualEvent.Data.Length); else if (actualEvent.Data.Zip(Event.Data, Tuple.Create).Any(x => x.Item1 != x.Item2)) sb.AppendFormat("- Wrong data.\n"); if (actualEvent.Metadata.Length != Event.Metadata.Length) sb.AppendFormat("- Wrong length of metadata. Expected length: {0}, actual length: {1}.\n", Event.Metadata.Length, actualEvent.Metadata.Length); else if (actualEvent.Metadata.Zip(Event.Metadata, Tuple.Create).Any(x => x.Item1 != x.Item2)) sb.AppendFormat("- Wrong metadata.\n"); return new VerificationResult(sb.Length == 0 ? ComparisonStatus.Success : ComparisonStatus.Fail, sb.ToString()); }
public void when_handling_trusted_write_on_internal_service() { ManualResetEvent waiter = new ManualResetEvent(false); ClientMessage.WriteEvents publishedWrite = null; var evnt = new Event(Guid.NewGuid(), "TestEventType", true, new byte[] { }, new byte[] { }); var write = new TcpClientMessageDto.WriteEvents( Guid.NewGuid().ToString(), ExpectedVersion.Any, new[] { new TcpClientMessageDto.NewEvent(evnt.EventId.ToByteArray(), evnt.EventType, evnt.IsJson ? 1 : 0, 0, evnt.Data, evnt.Metadata) }, false); var package = new TcpPackage(TcpCommand.WriteEvents, Guid.NewGuid(), write.Serialize()); var dummyConnection = new DummyTcpConnection(); var publisher = InMemoryBus.CreateTest(); publisher.Subscribe(new AdHocHandler<ClientMessage.WriteEvents>(x => { publishedWrite = x; waiter.Set(); })); var tcpConnectionManager = new TcpConnectionManager( Guid.NewGuid().ToString(), TcpServiceType.Internal, new ClientTcpDispatcher(), publisher, dummyConnection, publisher, new InternalAuthenticationProvider(new Core.Helpers.IODispatcher(publisher, new NoopEnvelope()), new StubPasswordHashAlgorithm(), 1), TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10), (man, err) => { }); tcpConnectionManager.ProcessPackage(package); if (!waiter.WaitOne(TimeSpan.FromSeconds(5))) { throw new Exception("Timed out waiting for events."); } Assert.AreEqual(evnt.EventId, publishedWrite.Events.First().EventId, "Expected the published write to be the event that was sent through the tcp connection manager to be the event {0} but got {1}", evnt.EventId, publishedWrite.Events.First().EventId); }
private void PublishCheckpoint(int state) { Log.Debug("publishing checkpoint " + state); _outstandingWrite = true; var evnt = new Event(Guid.NewGuid(), "SubscriptionCheckpoint", true, state.ToJson(), null); _ioDispatcher.WriteEvent(_subscriptionStateStream, _version, evnt, SystemAccount.Principal, WriteStateCompleted); }
public void Initialize() { _checkpointEventToBePublished = null; _inCheckpointWriteAttempt = 0; _ioDispatcher.Writer.Cancel(_writeRequestId); _lastWrittenCheckpointEventNumber = ExpectedVersion.Invalid; }
public Event Create(int version) { var accountObject = BankAccountEventFactory.CreateAccountObject(version); var serializedObject = Codec.Json.To(accountObject); var @event = new Event(Guid.NewGuid(), accountObject.GetType().FullName, false, Encoding.UTF8.GetBytes(serializedObject), new byte[0]); return @event; }
public Event Create(int version) { var accountObject = BankAccountEventFactory.CreateAccountObject(version); var serializedObject = Codec.Json.To(accountObject); var @event = new Event(Guid.NewGuid(), accountObject.GetType().Name, true, Helper.UTF8NoBom.GetBytes(serializedObject), new byte[0]); return @event; }
public void BeginParkMessage(ResolvedEvent ev,string reason, Action<ResolvedEvent, OperationResult> completed) { var metadata = new ParkedMessageMetadata {Added = DateTime.Now, Reason = reason, SubscriptionEventNumber = ev.OriginalEventNumber}; string data = GetLinkToFor(ev); var parkedEvent = new Event(Guid.NewGuid(), SystemEventTypes.LinkTo, false, data, metadata.ToJson()); _ioDispatcher.WriteEvent(_parkedStreamId, ExpectedVersion.Any, parkedEvent, SystemAccount.Principal, x => WriteStateCompleted(completed, ev, x)); }
public VerificationEvent Next() { var expected = _expected == -1 ? _expected : _expected + 1; var shouldBe = _shouldBe; _expected++; _shouldBe++; var evnt = new Event(Guid.NewGuid(), "TEST", false, Encoding.UTF8.GetBytes(string.Format("TEST-DATA exp: {0}. shd: {1}.", expected, shouldBe)), Encoding.UTF8.GetBytes(string.Format("TEST-METADATA exp: {0}. shd: {1}.", expected, shouldBe))); return new VerificationEvent(evnt, StreamId, expected, shouldBe); }
public WritePrepares(Guid correlationId, IEnvelope envelope, string eventStreamId, int expectedVersion, Event[] events, DateTime liveUntil) { CorrelationId = correlationId; Envelope = envelope; EventStreamId = eventStreamId; ExpectedVersion = expectedVersion; Events = events; LiveUntil = liveUntil; }
protected override void BeginWriteCheckpoint( CheckpointTag requestedCheckpointPosition, string requestedCheckpointState) { _requestedCheckpointPosition = requestedCheckpointPosition; _inCheckpointWriteAttempt = 1; //TODO: pass correct expected version _checkpointEventToBePublished = new Event( Guid.NewGuid(), "ProjectionCheckpoint", true, requestedCheckpointState == null ? null : Encoding.UTF8.GetBytes(requestedCheckpointState), requestedCheckpointPosition.ToJsonBytes()); PublishWriteCheckpointEvent(); }
public void BeginWriteCheckpoint(IEnvelope envelope, CheckpointTag requestedCheckpointPosition, string requestedCheckpointState) { _envelope = envelope; _requestedCheckpointPosition = requestedCheckpointPosition; _inCheckpointWriteAttempt = 1; //TODO: pass correct expected version _checkpointEventToBePublished = new Event( Guid.NewGuid(), ProjectionNamesBuilder.EventType_ProjectionCheckpoint, true, requestedCheckpointState == null ? null : Helper.UTF8NoBom.GetBytes(requestedCheckpointState), requestedCheckpointPosition.ToJsonBytes(projectionVersion: _projectionVersion)); PublishWriteStreamMetadataAndCheckpointEvent(); }
public VerificationEvent(Event @event, string eventStreamId, int expectedVersion, int shouldBeVersion) { Ensure.NotNull(@event, "event"); Ensure.NotNullOrEmpty(eventStreamId, "eventStreamId"); Event = @event; EventStreamId = eventStreamId; ExpectedVersion = expectedVersion; ShouldBeVersion = shouldBeVersion; }
public WritePrepares(Guid correlationId, IEnvelope envelope, string eventStreamId, int expectedVersion, Event[] events, DateTime liveUntil) { Ensure.NotEmptyGuid(correlationId, "correlationId"); Ensure.NotNull(envelope, "envelope"); Ensure.NotNull(eventStreamId, "eventStreamId"); Ensure.NotNull(events, "events"); CorrelationId = correlationId; Envelope = envelope; EventStreamId = eventStreamId; ExpectedVersion = expectedVersion; Events = events; LiveUntil = liveUntil; }
private void OnWriteComplete(ClientMessage.WriteEventsCompleted completed, Event evnt, string streamId, int retryCount) { if (completed.Result != OperationResult.Success) { if (retryCount > 0) { Log.Error("PROJECTIONS: Failed to write a tracked stream id of {0} to the {1} stream. Retrying {2}/{3}. Reason: {4}", streamId, _projectionNamesBuilder.GetEmittedStreamsName(), (MaxRetryCount - retryCount) + 1, MaxRetryCount, completed.Result); WriteEvent(evnt, retryCount - 1); } else { Log.Error("PROJECTIONS: Failed to write a tracked stream id of {0} to the {1} stream. Retry limit of {2} reached. Reason: {3}", streamId, _projectionNamesBuilder.GetEmittedStreamsName(), MaxRetryCount, completed.Result); } } }
//NOTE: committed event with null event _data means - end of the source reached. // Current last available TF commit position is in _position.CommitPosition // TODO: separate message? public CommittedEventDistributed( Guid correlationId, EventPosition position, string positionStreamId, int positionSequenceNumber, string eventStreamId, int eventSequenceNumber, bool resolvedLinkTo, Event data, long? safeTransactionFileReaderJoinPosition, float progress) { _correlationId = correlationId; _data = data; _safeTransactionFileReaderJoinPosition = safeTransactionFileReaderJoinPosition; _progress = progress; _position = position; _positionStreamId = positionStreamId; _positionSequenceNumber = positionSequenceNumber; _eventStreamId = eventStreamId; _eventSequenceNumber = eventSequenceNumber; _resolvedLinkTo = resolvedLinkTo; }
public void TrackEmittedStream(EmittedEvent[] emittedEvents) { if (!_projectionConfig.TrackEmittedStreams) return; foreach (var emittedEvent in emittedEvents) { string streamId; if (!_streamIdCache.TryGetRecord(emittedEvent.StreamId, out streamId)) { var trackEvent = new Event(Guid.NewGuid(), ProjectionEventTypes.StreamTracked, false, Helper.UTF8NoBom.GetBytes(emittedEvent.StreamId), null); lock (_locker) { _streamIdCache.PutRecord(emittedEvent.StreamId, emittedEvent.StreamId, false); } WriteEvent(trackEvent, MaxRetryCount); } } }
private Task NextForStream(string stream) { var expected = _currentExpected == -1 ? _currentExpected : _currentExpected + 1; var shouldBe = _currentShouldBe; _currentExpected++; _currentShouldBe++; var evnt = new Event( Guid.NewGuid(), "TEST", false, Encoding.UTF8.GetBytes(string.Format("TEST-DATA exp: {0}. shd: {1}.", expected, shouldBe)), Encoding.UTF8.GetBytes(string.Format("TEST-METADATA exp: {0}. shd: {1}.", expected, shouldBe))); return new WriteTask(new VerificationEvent(evnt, stream, expected, shouldBe)); }
public Task Next() { var expected = _expected == -1 ? _expected : _expected + 1; var shouldBe = _shouldBe; _expected++; _shouldBe++; var accountObject = BankAccountEventFactory.CreateAccountObject(shouldBe); var metadata = new Dictionary<string, string> {{"Source", "National Bank"}}; var serializedObject = Codec.Json.To(accountObject); var serializedMetadata = Codec.Json.To(metadata); var @event = new Event(Guid.NewGuid(), accountObject.GetType().FullName, false, Encoding.UTF8.GetBytes(serializedObject), Encoding.UTF8.GetBytes(serializedMetadata)); var verificationEvent = new VerificationEvent(@event, "AdvBankAccount", expected, shouldBe); return new WriteTask(verificationEvent); }
public TransactionWriteRequestCreated(Guid correlationId, IEnvelope envelope, long transactionId, string eventStreamId, Event[] events) { Ensure.NotEmptyGuid(correlationId, "correlationId"); Ensure.NotNull(envelope, "envelope"); Ensure.Nonnegative(transactionId, "transactionId"); Ensure.NotNull(eventStreamId, "eventStreamId"); Ensure.NotNull(events, "events"); CorrelationId = correlationId; Envelope = envelope; TransactionId = transactionId; EventStreamId = eventStreamId; Events = events; }
public WriteTransactionData(Guid correlationId, IEnvelope envelope, long transactionId, Event[] events) { CorrelationId = correlationId; Envelope = envelope; TransactionId = transactionId; Events = events; }
private static Event[] Parse(HttpClientMessageDto.ClientEventDynamic[] dynamicEvents) { var events = new Event[dynamicEvents.Length]; for (int i = 0, n = dynamicEvents.Length; i < n; ++i) { var textEvent = dynamicEvents[i]; bool dataIsJson; bool metadataIsJson; var data = AsBytes(textEvent.Data, out dataIsJson); var metadata = AsBytes(textEvent.Metadata, out metadataIsJson); events[i] = new Event(textEvent.EventId, textEvent.EventType, dataIsJson || metadataIsJson, data, metadata); } return events.ToArray(); }
public TransactionWrite(Guid internalCorrId, Guid correlationId, IEnvelope envelope, bool requireMaster, long transactionId, Event[] events, IPrincipal user, string login = null, string password = null) : base(internalCorrId, correlationId, envelope, requireMaster, user, login, password) { Ensure.Nonnegative(transactionId, "transactionId"); Ensure.NotNull(events, "events"); TransactionId = transactionId; Events = events; }
public WriteEvents(Guid internalCorrId, Guid correlationId, IEnvelope envelope, bool requireMaster, string eventStreamId, int expectedVersion, Event @event, IPrincipal user, string login = null, string password = null) : this(internalCorrId, correlationId, envelope, requireMaster, eventStreamId, expectedVersion, @event == null ? null : new[] { @event }, user, login, password) { }
public WriteEvents(Guid internalCorrId, Guid correlationId, IEnvelope envelope, bool requireMaster, string eventStreamId, int expectedVersion, Event[] events, IPrincipal user, string login = null, string password = null) : base(internalCorrId, correlationId, envelope, requireMaster, user, login, password) { Ensure.NotNullOrEmpty(eventStreamId, "eventStreamId"); if (expectedVersion < Data.ExpectedVersion.Any) throw new ArgumentOutOfRangeException("expectedVersion"); Ensure.NotNull(events, "events"); EventStreamId = eventStreamId; ExpectedVersion = expectedVersion; Events = events; }
public override void Initialize() { base.Initialize(); _writeDispatcher.Cancel(_writeRequestId); _readDispatcher.Cancel(_readRequestId); foreach (var requestId in _loadStateRequests) _readDispatcher.Cancel(requestId); _loadStateRequests.Clear(); _inCheckpointWriteAttempt = 0; _lastWrittenCheckpointEventNumber = 0; _nextStateIndexToRequest = 0; _checkpointEventToBePublished = null; _requestedCheckpointPosition = null; _readRequestsInProgress = 0; }
public void BeginParkMessage(ResolvedEvent @event,string reason, Action<ResolvedEvent, OperationResult> completed) { var metadata = new ParkedMessageMetadata() {Added = DateTime.Now, Reason = reason}; var evnt = new Event(Guid.NewGuid(), SystemEventTypes.LinkTo, false, GetLinkToFor(@event), metadata.ToJson()); _ioDispatcher.WriteEvent(_parkedStreamId, ExpectedVersion.Any, evnt, SystemAccount.Principal, x => WriteStateCompleted(completed, @event, x)); }
public WriteRequestCreated(Guid correlationId, IEnvelope envelope, string eventStreamId, int expectedVersion, Event[] events) { Ensure.NotEmptyGuid(correlationId, "correlationId"); Ensure.NotNull(envelope, "envelope"); Ensure.NotNullOrEmpty(eventStreamId, "eventStreamId"); Ensure.NotNull(events, "events"); CorrelationId = correlationId; Envelope = envelope; EventStreamId = eventStreamId; ExpectedVersion = expectedVersion; Events = events; }
private static Event[] LoadRaw(string data, bool isJson, Guid includedId, string includedType) { var ret = new Event[1]; ret[0] = new Event(includedId, includedType, isJson, data, null); return ret; }
private static Event[] LoadRaw(byte[] data, Guid includedId, string includedType) { var ret = new Event[1]; ret[0] = new Event(includedId, includedType, false, data, null); return ret; }
private static ClientMessage.WriteEvents UnwrapWriteEvents(TcpPackage package, IEnvelope envelope, IPrincipal user, string login, string password) { var dto = package.Data.Deserialize<TcpClientMessageDto.WriteEvents>(); if (dto == null) return null; var events = new Event[dto.Events == null ? 0 : dto.Events.Length]; for (int i = 0; i < events.Length; ++i) { // ReSharper disable PossibleNullReferenceException var e = dto.Events[i]; // ReSharper restore PossibleNullReferenceException events[i] = new Event(new Guid(e.EventId), e.EventType, e.DataContentType == 1, e.Data, e.Metadata); } return new ClientMessage.WriteEvents(Guid.NewGuid(), package.CorrelationId, envelope, dto.RequireMaster, dto.EventStreamId, dto.ExpectedVersion, events, user, login, password); }