public void it_should_insert_the_consumption_logs_into_the_store()
        {
            deleteConsumptionLogTable();

            var subject = new TableEventStore(CloudStorageAccount.DevelopmentStorageAccount, "test");

            var expectedEvent = new RaisedEvent(
                Guid.NewGuid(),
                new DummyEvent {Prop = "Property"},
                new DateTimeOffset(2013, 01, 17, 12, 06, 39, 967, TimeZoneInfo.Utc.BaseUtcOffset),
                "txid");

            var expectedConsumptionLog = new ConsumptionLog(
                expectedEvent,
                expectedEvent.RaisedTimestamp.AddSeconds(1),
                new AggregateInfo(typeof (DummyAggregate), 123));

            subject.LogConsumption(expectedEvent, expectedConsumptionLog);

            var actual = storedConsumptionLogEntity(expectedConsumptionLog);

            actual.ShouldNotBeNull();
            actual.ConsumedTimestamp.ShouldEqual(expectedConsumptionLog.ConsumedTimestamp);
            actual.ConsumptionException.ShouldEqual(expectedConsumptionLog.ConsumptionException);
            actual.Event.ShouldEqual(JsonEvent.ConvertToJson(expectedEvent));
            actual.EventThumbprint.ShouldEqual(expectedConsumptionLog.EventThumbprint);
        }
        public async void it_should_insert_the_event_into_the_store()
        {
            deleteEventStoreTable();

            var subject = new TableEventStore(CloudStorageAccount.DevelopmentStorageAccount, "test");

            var expected = new RaisedEvent(
                Guid.NewGuid(),
                new DummyEvent {Prop = "Property"},
                new DateTimeOffset(2013, 01, 17, 12, 06, 39, 967, TimeZoneInfo.Utc.BaseUtcOffset),
                "txid");

            var eventEntity = new EventEntity(expected);
            var insert = TableOperation.Insert(eventEntity);
            var inserted = Task.Factory.FromAsync<TableOperation, TableResult>(subject.EventStoreTable.BeginExecute,
                                                                               subject.EventStoreTable.EndExecute, insert, null);
            await inserted;

            var actual = storedEventEntity(expected);

            actual.ShouldNotBeNull();
            actual.Event.ShouldEqual(JsonEvent.ConvertToJson(expected));
            actual.RaisedTimestamp.ShouldEqual(expected.RaisedTimestamp);
            actual.TransactionId.ShouldEqual(expected.TransactionId);
        }
Beispiel #3
0
 public EventEntity(RaisedEvent raisedEvent)
 {
     PartitionKey = raisedEvent.Thumbprint.ToString();
     RowKey = raisedEvent.Event.GetType().FullName;
     TransactionId = raisedEvent.TransactionId;
     RaisedTimestamp = raisedEvent.RaisedTimestamp;
     Event = JsonEvent.ConvertToJson(raisedEvent);
 }
 public ConsumptionLogEntity(ConsumptionLog consumptionLog, RaisedEvent raisedEvent)
 {
     PartitionKey = consumptionLog.AffectedAggregate.Type.FullName + "@" + consumptionLog.AffectedAggregate.Key;
     RowKey = consumptionLog.ConsumedTimestamp.Ticks.ToString(longPad);
     EventThumbprint = raisedEvent.Thumbprint;
     ConsumedTimestamp = consumptionLog.ConsumedTimestamp;
     ExecutionTime = consumptionLog.ExecutionTime;
     ConsumptionException = consumptionLog.ConsumptionException;
     Event = JsonEvent.ConvertToJson(raisedEvent);
 }
        [TestMethod] // Debug this method to be able to see debug output etc.
        public async Task WriteData()
        {
            try
            {
                await InitApp();

                var version = -1;

                var dbId = GetRandomString(15);
                await _db.CreateDbAsync(dbId); // here we create a random db

                var streamKey = $"{dbId}@{0}";

                while (true)
                {
                    try
                    {
                        var evt = new RaisedEvent(new NoteAdded(0, "someNote")) // create some data, in form of an event
                        {
                            SequenceNumber = ++version                          // protocol way of managing concurrent write to the stream
                        };

                        var events = new List <RaisedEvent> {
                            evt
                        };
                        var data = events.Select(e => new EventData(
                                                     e.Payload,
                                                     Guid.NewGuid(),
                                                     Guid.NewGuid(),
                                                     e.EventClrType,
                                                     e.Id,
                                                     e.Name,
                                                     e.SequenceNumber,
                                                     e.TimeStamp))
                                   .ToList();                                        // protocol way of how to serialize and package the event data

                        var batch = new EventBatch(streamKey, Guid.NewGuid(), data); // another protocol way of packaging the data

                        var res = await _db.StoreBatchAsync(dbId, batch);            // store the data to the db

                        if (res.Error)
                        {
                        }

                        Debug.WriteLine(version); // so we expect to reach ~22-30 entries before sudden crash. Sometimes more, sometimes less.
                        //await Task.Delay(1000);
                    }
                    catch (Exception ex)
                    { } // you can put breakpoint here, however, the big problem (and mystery) is that these do not catch anything, program just dies OR a NullReferenceException is reported in logs; "occurred in Unknown Module".
                }
            }
            catch (Exception ex)
            { } // you can put breakpoint here, however, the big problem (and mystery) is that these do not catch anything, program just dies OR a NullReferenceException is reported in logs; "occurred in Unknown Module".
        }
Beispiel #6
0
        static RaisedEvent <T> RaisesInternal <T>(Action <EventHandler <T> > attach, Action <EventHandler <T> > detach, Action testCode)
            where T : EventArgs
        {
            RaisedEvent <T>  raisedEvent = null;
            EventHandler <T> handler     = delegate(object s, T args) { raisedEvent = new RaisedEvent <T>(s, args); };

            attach(handler);
            testCode();
            detach(handler);
            return(raisedEvent);
        }
Beispiel #7
0
        static async Task <RaisedEvent <T> > RaisesAsyncInternal <T>(Action <EventHandler <T> > attach, Action <EventHandler <T> > detach, Func <Task> testCode)
            where T : EventArgs
        {
            RaisedEvent <T>  raisedEvent = null;
            EventHandler <T> handler     = delegate(object s, T args) { raisedEvent = new RaisedEvent <T>(s, args); };

            attach(handler);
            await testCode();

            detach(handler);
            return(raisedEvent);
        }
Beispiel #8
0
        public virtual void Consume(RaisedEvent raisedEvent)
        {
            var consumerTasks =
                (
                    from aggregateInfo in applyTo(raisedEvent.Event).ToList()
                    let consumptionLog = new ConsumptionLog(raisedEvent, DateTimeOffset.UtcNow, aggregateInfo)
                    select new Task(() => consumeEventOnAggregate(raisedEvent, aggregateInfo, consumptionLog))
                ).ToList();

            //Store the event.
            Store.RecordEvent(raisedEvent);

            foreach (var task in consumerTasks)
            {
                task.Start();
            }
        }
Beispiel #9
0
        public static RaisedEvent Raises(Action <EventHandler> attach, Action <EventHandler> detach, Action testAction)
        {
            RaisedEvent raised = null;

            attach(OnEventRaised);
            testAction();
            detach(OnEventRaised);

            if (raised is null)
            {
                throw new RaisesException(typeof(EventArgs));
            }

            return(raised);

            void OnEventRaised(object sender, EventArgs e) => raised = new RaisedEvent(sender, e);
        }
Beispiel #10
0
        public static RaisedEvent <PropertyChangedEventArgs> Raises <T>(
            Action <PropertyChangedEventHandler> attach,
            Action <PropertyChangedEventHandler> detach,
            Action testCode)
            where T : PropertyChangedEventArgs
        {
            RaisedEvent <PropertyChangedEventArgs> raisedEvent = null;
            PropertyChangedEventHandler            handler     = (sender, e)
                                                                 => raisedEvent = new RaisedEvent <PropertyChangedEventArgs>(sender, e);

            attach(handler);
            testCode();
            detach(handler);

            Assert.IsNotNull(raisedEvent);

            return(raisedEvent);
        }
Beispiel #11
0
        private IList <RaisedEvent> RaiseEvents()
        {
            List <RaisedEvent> raisedEvents = new List <RaisedEvent>();

            if (this.Events != null)
            {
                foreach (string eventName in this.Events)
                {
                    AcmaInternalExitEvent internalEvent = new AcmaInternalExitEvent();
                    internalEvent.AllowDuplicateIDs = true;
                    internalEvent.ID = eventName;
                    RaisedEvent raisedEvent = new RaisedEvent(internalEvent);
                    raisedEvents.Add(raisedEvent);
                }
            }

            return(raisedEvents);
        }
Beispiel #12
0
        static RaisedEvent <T> RaisesInternal <T>(Action <EventHandler <T> > attach, Action <EventHandler <T> > detach, Action testCode)
#endif
        {
            GuardArgumentNotNull(nameof(attach), attach);
            GuardArgumentNotNull(nameof(detach), detach);
            GuardArgumentNotNull(nameof(testCode), testCode);

#if XUNIT_NULLABLE
            RaisedEvent <T>?raisedEvent = null;
            void handler(object?s, T args) => raisedEvent = new RaisedEvent <T>(s, args);
#else
            RaisedEvent <T>  raisedEvent = null;
            EventHandler <T> handler     = (object s, T args) => raisedEvent = new RaisedEvent <T>(s, args);
#endif
            attach(handler);
            testCode();
            detach(handler);
            return(raisedEvent);
        }
Beispiel #13
0
        public void EvaluateOnSimpleEvent()
        {
            Guid newId = Guid.NewGuid();

            try
            {
                MAObjectHologram maObject = ActiveConfig.DB.CreateMAObject(newId, "person");

                AcmaEvent maevent = GetAccountNameChangedEvent();

                RaisedEvent exitEvent = new RaisedEvent(maevent, maObject);
                maObject.IncomingEvents = new List <RaisedEvent>()
                {
                    exitEvent
                };

                // Positive Tests
                EventRule target = new EventRule();
                target.EventName = "accountNameChanged";
                Assert.IsTrue(target.Evaluate(maObject));

                // Negative Tests

                maevent   = GetSupervisorChangedEvent();
                exitEvent = new RaisedEvent(maevent, maObject);
                maObject.IncomingEvents = new List <RaisedEvent>()
                {
                    exitEvent
                };

                target.EventName = "accountNameChanged";
                Assert.IsFalse(target.Evaluate(maObject));

                target.EventName   = "accountNameChanged";
                target.EventSource = ActiveConfig.DB.GetAttribute("supervisor");
                Assert.IsFalse(target.Evaluate(maObject));
            }
            finally
            {
                ActiveConfig.DB.DeleteMAObjectPermanent(newId);
            }
        }
        public void it_will_serialise_raised_events_to_a_brokered_message()
        {
            var expected = new RaisedEvent(
                new DummyEvent {Prop = "value"},
                new DateTimeOffset(2013, 01, 14, 21, 46, 22, TimeZoneInfo.Utc.BaseUtcOffset));

            var actual = expected.ConvertToMessage();

            using (var reader = new StreamReader(actual.GetBody()))
            {
                var json = reader.ReadToEnd();

                var match =
                    new Regex(
                        @"\{""Thumbprint"":"".*?"",""Event"":\{""\$type"":""Test[.]Honeycomb[.]Azure[.]DummyEvent,\sTest[.]Honeycomb[.]Azure"",""Prop"":""value""\},""RaisedTimestamp"":""2013-01-14T21:46:22[+]00:00"",""TransactionId"":null\}");
                //Console.WriteLine(match);
                //Console.WriteLine(json);
                match.IsMatch(json).ShouldBeTrue();
            }
        }
Beispiel #15
0
        protected void RaiseEvent(Event @event, bool isHistoric = false)
        {
            // ANTI DATA CORRUPTION MEASURE // For bulk applying and exporting events, since we cannot guarantee they wont contain any objects that are stored to AR state but not deepcloned
            var typeName  = @event.GetType().AssemblyQualifiedName;
            var safeEvent = (Event)@event.AsBytes().Parse(typeName);

            // ANTI DATA CORRUPTION MEASURE

            Apply(safeEvent);
            Version++;

            if (!isHistoric)
            {
                var raised = new RaisedEvent(@event)
                {
                    SequenceNumber = Version
                };
                _uncommittedEvents.Add(raised);
            }
        }
Beispiel #16
0
        static async Task <RaisedEvent <T> > RaisesAsyncInternal <T>(Action <EventHandler <T> > attach, Action <EventHandler <T> > detach, Func <Task> testCode)
#endif
            where T : EventArgs
        {
            GuardArgumentNotNull(nameof(attach), attach);
            GuardArgumentNotNull(nameof(detach), detach);
            GuardArgumentNotNull(nameof(testCode), testCode);

#if XUNIT_NULLABLE
            RaisedEvent <T>?raisedEvent = null;
            void handler(object?s, T args) => raisedEvent = new RaisedEvent <T>(s, args);
#else
            RaisedEvent <T>  raisedEvent = null;
            EventHandler <T> handler     = (object s, T args) => raisedEvent = new RaisedEvent <T>(s, args);
#endif
            attach(handler);
            await testCode();

            detach(handler);
            return(raisedEvent);
        }
Beispiel #17
0
        public void EvaluateOnSourceEvent()
        {
            Guid supervisorId = Guid.NewGuid();
            Guid targetId     = Guid.NewGuid();

            try
            {
                MAObjectHologram supervisorObject = ActiveConfig.DB.CreateMAObject(supervisorId, "person");
                MAObjectHologram targetObject     = ActiveConfig.DB.CreateMAObject(targetId, "person");

                AcmaSchemaAttribute supervisorAttribute = ActiveConfig.DB.GetAttribute("supervisor");
                targetObject.SetAttributeValue(supervisorAttribute, supervisorObject.ObjectID);
                targetObject.CommitCSEntryChange();

                AcmaEvent   maevent   = GetAccountNameChangedEvent();
                RaisedEvent exitEvent = new RaisedEvent(maevent, supervisorObject);
                targetObject.IncomingEvents = new List <RaisedEvent>()
                {
                    exitEvent
                };

                // Positive Tests
                EventRule target = new EventRule();
                target.EventName   = "accountNameChanged";
                target.EventSource = ActiveConfig.DB.GetAttribute("supervisor");
                Assert.IsTrue(target.Evaluate(targetObject));

                // Negative Tests
                target             = new EventRule();
                target.EventName   = "accountNameChanged";
                target.EventSource = ActiveConfig.DB.GetAttribute("directReports");
                Assert.IsFalse(target.Evaluate(targetObject));
            }
            finally
            {
                ActiveConfig.DB.DeleteMAObjectPermanent(targetId);
                ActiveConfig.DB.DeleteMAObjectPermanent(supervisorId);
            }
        }
 public void Emit(RaisedEvent @event)
 {
     buffer.Enqueue(@event);
     nudge.Set();
 }
 public void LogConsumption(RaisedEvent raisedEvent, ConsumptionLog consumptionLog)
 {
     var logEntity = new ConsumptionLogEntity(consumptionLog, raisedEvent);
     var insert = TableOperation.Insert(logEntity);
     logTable.Execute(insert);
 }
 public void RecordEvent(RaisedEvent raisedEvent)
 {
     var eventEntity = new EventEntity(raisedEvent);
     var insert = TableOperation.Insert(eventEntity);
     EventStoreTable.Execute(insert);
 }
Beispiel #21
0
 public static string ConvertToJson(RaisedEvent @event)
 {
     return JsonConvert.SerializeObject(@event, jsonSerializerSettings);
 }
Beispiel #22
0
        private void consumeEventOnAggregate(RaisedEvent raisedEvent, AggregateInfo aggregateInfo, ConsumptionLog consumptionLog)
        {
            //If it's not tracked, then select it from the domain.
            if (aggregateInfo.Lifestate == AggregateLifestate.Untracked)
                selectAggregate(aggregateInfo);

            using (var scope = new TransactionScope())
            {
                try
                {
                    try
                    {
                        //If we haven't found it in the domain, then create it, otherwise consume the event.
                        if (aggregateInfo.Lifestate == AggregateLifestate.Untracked)
                            AggregateFactory.Create(aggregateInfo, raisedEvent.Event);
                        else
                            aggregateInfo.Instance.AsDynamic().Receive(raisedEvent);
                    }
                    catch (ApplicationException e)
                    {
                        if (e.Source == "ReflectionMagic")
                            throw new MissingMethodException(
                                aggregateInfo.Type.FullName,
                                string.Format(
                                    "{0}({1})",
                                    aggregateInfo.Lifestate == AggregateLifestate.Untracked ? "_ctor" : "Receive",
                                    raisedEvent.GetType()));

                        throw;
                    }
                }
                catch (Exception e)
                {
                    consumptionLog.RecordExceptionForConsumer(e);
                }

                consumptionLog.RecordConsumptionComplete();

                Store.LogConsumption(raisedEvent, consumptionLog);
                scope.Complete();
            }
        }
 private EventEntity storedEventEntity(RaisedEvent raisedEvent)
 {
     var cloudStorageAccount = CloudStorageAccount.DevelopmentStorageAccount;
     var cloudTableClient = cloudStorageAccount.CreateCloudTableClient();
     var table = cloudTableClient.GetTableReference("testEventStore");
     var retrieve = TableOperation.Retrieve<EventEntity>(raisedEvent.Thumbprint.ToString(), raisedEvent.Event.GetType().FullName);
     var result = table.Execute(retrieve);
     return result.Result as EventEntity;
 }