public void AuditDispatcherCallsCorrectMethods() { // Arrange var repoMock = new Mock <IRepository>(MockBehavior.Strict); repoMock.Setup(repo => repo.Insert(It.IsAny <SerializedEvent>())); var e = new SerializedEvent { Body = "AuditDispatcher.Test", TimeReceived = DateTime.Now }; repoMock.Setup(repo => repo.FindBy(It.IsAny <Expression <Func <SerializedEvent, bool> > >())) .Returns(() => new List <SerializedEvent>() { e }); var pubMock = new Mock <IPublisher>(MockBehavior.Strict); pubMock.Setup(pub => pub.Publish(It.IsAny <string>(), It.IsAny <SerializedEvent>())); options.ExchangeName = "TestExchange1"; using (var target = new AuditDispatcher(repoMock.Object, pubMock.Object, options)) { // Act var command = new SendAllEventCommand { returnQueueName = "Test.QueueName", StartTime = DateTime.MinValue, EndTime = DateTime.MaxValue }; target.OnReceived(command); // Assert repoMock.Verify(repo => repo.FindBy(It.IsAny <Expression <Func <SerializedEvent, bool> > >()), Times.Once()); pubMock.Verify(pub => pub.Publish(It.IsAny <string>(), It.IsAny <SerializedEvent>()), Times.Once()); } }
private void InsertEvent(SerializedEvent e, SqlConnection connection, SqlTransaction transaction) { const string query = @" INSERT INTO logs.Event ( AggregateIdentifier, AggregateVersion, EventClass, EventType, EventData, IdentityTenant, IdentityUser, EventTime ) VALUES ( @AggregateIdentifier, @AggregateVersion, @EventClass, @EventType, @EventData, @IdentityTenant, @IdentityUser, @EventTime )"; using (var command = new SqlCommand(query, connection, transaction)) { var parameters = command.Parameters; parameters.AddWithValue("AggregateIdentifier", e.AggregateIdentifier); parameters.AddWithValue("AggregateVersion", e.AggregateVersion); parameters.AddWithValue("EventClass", e.EventClass); parameters.AddWithValue("EventType", e.EventType); parameters.AddWithValue("EventData", e.EventData); parameters.AddWithValue("IdentityTenant", e.IdentityTenant); parameters.AddWithValue("IdentityUser", e.IdentityUser); parameters.AddWithValue("EventTime", e.EventTime); try { command.ExecuteNonQuery(); } catch (SqlException ex) { throw new SqlInsertException($"The event ({e.EventType}) could not be saved.", ex); } } }
public void AddEvent(byte eventType, string eventSerialized) { SerializedEvent se = new SerializedEvent(eventType, eventSerialized); events.Add(se); }
public static Result <SerializedEventEntity> Create(SerializedEvent serializedEvent) { return(Result.Create(serializedEvent != null, SerializedEventEntityErrors.UnableToCreateEntityForEmptyEvent.Build()) .OnSuccess(() => new SerializedEventEntity(serializedEvent))); }
private SerializedEventEntity(SerializedEvent serializedEvent) { SerializedEvent = serializedEvent; MessageId = Guid.NewGuid(); }
/// <remarks> /// Timeout implementation inspired by: http://stackoverflow.com/questions/5018921/implement-c-sharp-timeout /// </remarks> public PollResponse PollEvents(long consumerVersion, string consumerName) { var ecv = this.store.CurrentEventCollectionVersion; var newEvents = new SerializedEvent[0]; // last received version could be somehow less than 0. I found once that was -1, // and was always pushing "0 events", as the signal r tracing showed (27/10/2015) if (consumerVersion < 0) { consumerVersion = 0; } // the consumer says that is more updated than the source. That is an error. Maybe the publisher did not started yet! if (ecv < consumerVersion) { return(PollResponse.CreateSerializedResponse(true, false, this.streamType, newEvents, consumerVersion, ecv)); } bool newEventsWereFound = false; var errorDetected = false; var stopwatch = Stopwatch.StartNew(); while (!this.stopping && stopwatch.Elapsed < this.longPollingTimeout) { if (ecv == consumerVersion) { // consumer is up to date, and now is waiting until something happens! Thread.Sleep(1); ecv = this.store.CurrentEventCollectionVersion; // I Forgot to update! } // weird error, but is crash proof. Once i had an error where in an infinite loop there was an error saying: Pushing 0 events to.... // A Charly le paso. Sucede que limpio la base de datos y justo queria entregar un evento y no devolvia nada. else if (ecv > consumerVersion) { newEvents = this.store.FindEventsForConsumer(consumerVersion, ecv, this.eventsToPushMaxCount, consumerName); if (newEvents.Length > 0) { newEventsWereFound = true; this.log.Trace($"{this.streamType} publisher is pushing {newEvents.Length} event/s to {consumerName}"); break; } else { // Lo que le paso a charly. newEventsWereFound = false; errorDetected = true; var errorMessage = $"There is an error in the event store or a racy condition. The consumer [{consumerName}] version is {consumerVersion} and the local event collection version should be {ecv} but it is not."; this.log.Error(errorMessage); this.bus.Publish(new FatalErrorOcurred(new FatalErrorException(errorMessage))); break; } } else { // bizzare, but helpful to avoid infinite loops break; } } return(PollResponse.CreateSerializedResponse(errorDetected, newEventsWereFound, this.streamType, newEvents, consumerVersion, ecv)); }
public void Insert(SerializedEvent serializedEvent) { _context.SerializedEvents.Add(serializedEvent); _context.SaveChanges(); }
public void Add(SerializedEvent @event) { this.Events.Add(@event); }
public object Deserialize(SerializedEvent @event) { var type = Type.GetType(@event.Type); return(JsonConvert.DeserializeObject(@event.Payload, type)); }
public IDomainEvent Deserialize(SerializedEvent serializedEvent) { var type = eventNameLookup.Single(l => l.Value == serializedEvent.EventName).Key; return((IDomainEvent)JsonConvert.DeserializeObject(serializedEvent.EventData, type)); }