/// <summary>
        ///     Write event data without appending a newline seperator.
        /// </summary>
        /// <param name="writer">An event writer</param>
        /// <param name="eventTemplate">An event template</param>
        /// <param name="eventData">Event data</param>
        static async Task WriteEventData(EventStreamWriter writer, EventElement eventTemplate, string eventData)
        {
            var @event = eventTemplate;

            @event.Data = eventData;
            await writer.WriteEventAsync(@event);
        }
Пример #2
0
        private EntityCreatedEvent EventStreamRoundTripEvent(EntityCreatedEvent evt)
        {
            EventStreamHeader header = new EventStreamHeader(new VectorClock(), new Infrastructure.VectorClock(), Guid.NewGuid(), Guid.NewGuid());

            MemoryStream stream = new MemoryStream();

            using (EventStreamWriter writer = new EventStreamWriter(stream, header, false))
            {
                writer.WriteEvent(evt);
            }

            stream.Seek(0, SeekOrigin.Begin);

            using (stream)
                using (EventStreamReader reader = new EventStreamReader(stream))
                {
                    while (reader.Read())
                    {
                        if (reader.ItemType == ItemType.Event)
                        {
                            return((EntityCreatedEvent)reader.CurrentEvent);
                        }
                    }
                }

            throw new InvalidOperationException("The event could not be round tripped");
        }
Пример #3
0
        /// <summary>
        ///     Write event data without appending a newline seperator.
        /// </summary>
        /// <param name="writer">An event writer</param>
        /// <param name="eventTemplate">An event template</param>
        /// <param name="eventData">Event data</param>
        private static void WriteEventData(EventStreamWriter writer, EventElement eventTemplate, string eventData)
        {
            var @event = eventTemplate;

            @event.Data = eventData;
            writer.Write(@event);
        }
        /// <summary>
        ///     Write for multiple stanzas
        /// </summary>
        /// <param name="writer">An event writer</param>
        static async Task WriteMultiplex(EventStreamWriter writer)
        {
            var eventTemplate1 = new EventElement
            {
                Stanza   = "modular_input://UnitTest1",
                Unbroken = true,
            };

            var eventTemplate2 = new EventElement
            {
                Stanza   = "modular_input://UnitTest2",
                Unbroken = true,
            };

            await WriteEventDataLine(writer, eventTemplate1, "Part 1 of channel 1 with a newline");
            await WriteEventData(writer, eventTemplate2, "Part 1 of channel 2 without a newline ");

            // Mark the first channel done.
            await WriteEventDone(writer, eventTemplate1);

            await WriteEventDataLine(writer, eventTemplate1, "Part 2 of channel 1 with a newline");
            await WriteEventDataLine(writer, eventTemplate2, "Part 2 of channel 2 with a newline");

            // Mark the second channel done.
            await WriteEventDone(writer, eventTemplate2);
        }
Пример #5
0
    public void ScriptRun(object obj)
    {
        engine = Python.CreateEngine();
        CodeWraper wraper = (CodeWraper)obj;
        string     code   = wraper.GetCode();

        scope = engine.CreateScope();
        var streamOut = new MemoryStream();

        scope.SetVariable("WIDTH", DisplaySystem.WIDTH);
        scope.SetVariable("HEIGHT", DisplaySystem.HEIGHT);
        scope.SetVariable("__tick__", new Action(Tick));
        scope.SetVariable("set_color", new Action <int, int, int>(SetColor));
        scope.SetVariable("set_back", new Action <int, int, int>(SetBackColor));
        scope.SetVariable("set_bord", new Action <int, int, int>(SetBorderColor));
        scope.SetVariable("set_error", new Action <int, int, int>(SetErrorColor));
        scope.SetVariable("randint", new Func <int, int, int>(RandomInteger));
        scope.SetVariable("clear_screen", new Action(ClearScreen));
        scope.SetVariable("draw_circle", new Action <int, int, int>(DrawCircle));
        scope.SetVariable("draw_line", new Action <int, int, int, int>(DrawLine));
        scope.SetVariable("draw_rect", new Action <int, int, int, int>(DrawRect));
        scope.SetVariable("draw_pixel", new Action <int, int>(DrawPixel));
        scope.SetVariable("draw_pixels", new Action <int, int, int, int>(DrawPixels));
        scope.SetVariable("auto_apply", new Action <bool>(SetAutoapply));
        scope.SetVariable("apply", new Action(Apply));
        scope.SetVariable("delta_time", new Func <float>(GetDeltaTime));
        scope.SetVariable("get_keys", new Func <string[]>(GetDownedKeys));
        scope.SetVariable("char", new Func <string, int>(GetCharByNum));
        scope.SetVariable("type", new Action <int, int, int>(TyperChar));
        scope.SetVariable("input", new Func <string>(GetInput));
        scope.SetVariable("beep", new Action <float, float>(Beep));

        aborted = false;

        EventStreamWriter outputWr = new EventStreamWriter(streamOut);

        outputWr.StringWritten += new EventHandler <EventWithArgs <string> >(StringWritten);

        engine.Runtime.IO.SetOutput(streamOut, outputWr);

        try {
            engine.Execute(code, scope);
        }
        catch (Exception e) {
            _console.AddToLog(e.Message);
        }

        if (aborted)
        {
            _console.Writter.Reset();
            _console.AddToActions(_console.ResetOutput);
        }
        else
        {
            _console.AddToActions(_console.ResetOutputStopped);
        }

        stream.Abort();
    }
Пример #6
0
 private static void Write(string name, Action<EventStreamWriter> action)
 {
     var filename = Path.Combine(ResourcesPath, name);
     using (var fs = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite))
     using (var esw = new EventStreamWriter(fs, new NullEventWriter())) {
         action(esw);
     }
 }
        /// <summary>
        ///     Write a done key
        /// </summary>
        /// <param name="writer">An event writer</param>
        /// <param name="eventTemplate">An event template</param>
        static async Task WriteEventDone(EventStreamWriter writer, EventElement eventTemplate)
        {
            var @event = eventTemplate;

            @event.Unbroken = false;
            @event.Done     = true;
            await writer.WriteEventAsync(@event);
        }
Пример #8
0
        /// <summary>
        ///     Write a done key
        /// </summary>
        /// <param name="writer">An event writer</param>
        /// <param name="eventTemplate">An event template</param>
        private static void WriteEventDone(EventStreamWriter writer, EventElement eventTemplate)
        {
            var @event = eventTemplate;

            @event.Unbroken = false;
            @event.Done     = true;
            writer.Write(@event);
        }
Пример #9
0
        public async Task MoveToEndOfStreamAsync_ReadsEndOfStreamPositionFormJournal(
            [Frozen] Mock<IEventJournal> journalMock,
            EventStreamWriter writer)
        {
            await writer.MoveToEndOfStreamAsync();

            journalMock.Verify(journal => journal.ReadStreamHeaderAsync(writer.StreamName));
        }
Пример #10
0
        public async Task MoveToEndOfStreamAsync_EnsureConnectivityStateIsActive(
            [Frozen] Mock<IEventStoreConnectionState> stateMock,
            EventStreamWriter writer)
        {
            await writer.MoveToEndOfStreamAsync();

            stateMock.Verify(journal => journal.EnsureConnectionIsActive());
        }
Пример #11
0
        public async Task AppendEvents_EnsureConnectivityStateIsActive(
            [Frozen] Mock<IEventStoreConnectionState> stateMock,
            EventStreamWriter writer,
            JournaledEvent[] events)
        {
            await writer.AppendEventsAsync(events);

            stateMock.Verify(journal => journal.EnsureConnectionIsActive());
        }
Пример #12
0
 public void Given_first_set_when_read_back_then_output_is_as_expected()
 {
     using (var ms = new MemoryStream()) {
         using (var esw = new EventStreamWriter(ms, new NullEventWriter())) {
             esw.Write(MockEventStreams.First);
             Assert.AreEqual(ms.ReadStartToEnd(), ResourceProvider.Get("First.e"));
         }
     }
 }
 /// <summary>
 ///     Write an event data line.
 /// </summary>
 /// <param name="writer">An event writer</param>
 /// <param name="eventTemplate">An event template</param>
 /// <param name="eventData">Event data</param>
 static async Task WriteEventDataLine(
     EventStreamWriter writer,
     EventElement eventTemplate,
     string eventData)
 {
     await WriteEventData(
         writer,
         eventTemplate,
         eventData + Environment.NewLine);
 }
Пример #14
0
 /// <summary>
 ///     Write an event data line.
 /// </summary>
 /// <param name="writer">An event writer</param>
 /// <param name="eventTemplate">An event template</param>
 /// <param name="eventData">Event data</param>
 private static void WriteEventDataLine(
     EventStreamWriter writer,
     EventElement eventTemplate,
     string eventData)
 {
     WriteEventData(
         writer,
         eventTemplate,
         eventData + Environment.NewLine);
 }
Пример #15
0
        public void Store(Guid identity, IEnumerable<IStreamedEvent> eventsToAppend)
        {
            var filename = _repositoryHierarchy.For(identity, true);
            using (var fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 4096, FileOptions.SequentialScan)) {
                fs.Position = fs.Length;

                using (var esw = new EventStreamWriter(fs, _eventWriter))
                    esw.Write(eventsToAppend);
            }
        }
Пример #16
0
 public void Given_first_set_when_appended_to_with_second_set_then_hashes_continue_from_previous_hash()
 {
     using (var ms = new MemoryStream()) {
         using (var esw = new EventStreamWriter(ms, new NullEventWriter())) {
             esw.Write(MockEventStreams.First);
             esw.Write(MockEventStreams.Second);
             Assert.AreEqual(ms.ReadStartToEnd(), ResourceProvider.Get("First_and_second.e"));
         }
     }
 }
Пример #17
0
        public void Given_first_set_when_artificially_truncated_and_appended_to_with_second_set_then_it_will_throw_on_write()
        {
            using (var ms = new MemoryStream()) {
                ResourceProvider.AppendTo(ms, "First.e", 7 /* magic sauce to truncate by an arbitrary 7 bytes */);

                using (var esw = new EventStreamWriter(ms, new NullEventWriter())) {
                    Assert.Throws<DataVerificationPersistenceException>(() => esw.Write(MockEventStreams.Second));
                }
            }
        }
Пример #18
0
        public async Task AppendEvents_ShouldUseEventMutationPipeline(
            [Frozen] Mock<IEventMutationPipeline> pipelineMock,
            EventStreamWriter writer,
            JournaledEvent[] events)
        {
            await writer.AppendEventsAsync(events);

            pipelineMock.Verify(
                journal => journal.Mutate(It.Is<JournaledEvent>(e => events.Contains(e))),
                Times.Exactly(events.Length));
        }
Пример #19
0
        public void Given_first_set_when_appended_to_with_first_set_again_then_hashing_continues_from_item_two_in_the_file()
        {
            using (var ms = new MemoryStream()) {
                ResourceProvider.AppendTo(ms, "First.e");

                using (var esw = new EventStreamWriter(ms, new NullEventWriter())) {
                    esw.Write(MockEventStreams.First);
                    Assert.AreEqual(ms.ReadStartToEnd(), ResourceProvider.Get("First_with_hash_seed.e"));
                }
            }
        }
        /// <summary>
        ///     Write events using EventStreamWriter
        /// </summary>
        // This method can be used by manual testing thus is public
        public static async Task WriteEvents()
        {
            using (var writer = new EventStreamWriter())
            {
                var eventTemplate = new EventElement
                {
                    Index      = "sdk-tests2",
                    Host       = "test host",
                    SourceType = "test sourcetype",
                    Source     = "test source",
                };

                await WriteEventData(
                    writer,
                    eventTemplate,
                    "Event with all default fields set");

                await WriteEventData(
                    writer,
                    eventTemplate,
                    "Letter O with double acute: \u0150");

                eventTemplate.Unbroken = true;

                await WriteEventData(
                    writer,
                    eventTemplate,
                    "Part 1 of an unbroken event ");

                await WriteEventData(
                    writer,
                    eventTemplate,
                    "Part 2 of an unbroken event ending with newline" + Environment.NewLine);

                await WriteEventDone(
                    writer,
                    eventTemplate);

                eventTemplate.Unbroken = false;

                await WriteEventData(
                    writer,
                    eventTemplate,
                    "Event after done key");

                var timedEvent = eventTemplate;
                timedEvent.Time = new DateTime(2013, 1, 1, 0, 0, 0, 1, DateTimeKind.Utc);
                timedEvent.Data = "Event with fixed time";
                await writer.WriteEventAsync(timedEvent);

                await WriteMultiplex(writer);
            }
        }
Пример #21
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="report"></param>
 public override void VisitReport(ReportObject report)
 {
     using (EventStreamWriter writer = new EventStreamWriter())
     {
         writer.Write(new EventElement
         {
             Time   = report.Date,
             Source = this.streamName,
             Data   = report.ConvertToXml()
         });
     }
 }
Пример #22
0
        public void Given_a_malformed_supposedly_hash_seeded_stream_when_appended_to_with_first_set_then_it_will_throw()
        {
            using (var ms = new MemoryStream()) {
                // This is just some malformed junk that is of sufficient length and structure to produce the behaviour.
                ms.Write(new byte[20], 0, 20); // hash
                ms.Write(new byte[4], 0, 4); // tail record length
                ms.Write(new byte[] { 0x02 }, 0, 1); // (an incorrect) tail indicator byte

                using (var esw = new EventStreamWriter(ms, new NullEventWriter())) {
                    Assert.Throws<DataVerificationPersistenceException>(() => esw.Write(MockEventStreams.First));
                }
            }
        }
Пример #23
0
        public async Task AppendEvents_AppendEventsToJournal(
            [Frozen] EventStreamHeader header,
            [Frozen] Mock<IEventJournal> journalMock,
            EventStreamWriter writer,
            JournaledEvent[] events)
        {
            await writer.AppendEventsAsync(events);

            journalMock.Verify(journal => journal.AppendEventsAsync(
                writer.StreamName,
                header,
                It.Is<IReadOnlyCollection<JournaledEvent>>(e => e.Count == events.Length)));
        }
Пример #24
0
        public async Task AppendEvents_NotifyAboutStreamUpdates(
            [Frozen] Mock<INotificationHub> hubMock,
            EventStreamWriter writer,
            JournaledEvent[] events)
        {
            var fromVersion = writer.StreamVersion;
            await writer.AppendEventsAsync(events);
            var toVersion = writer.StreamVersion;

            hubMock.Verify(journal => journal.NotifyAsync(
                It.Is<EventStreamUpdated>(notification =>
                    notification.StreamName == writer.StreamName &&
                    notification.FromVersion == fromVersion &&
                    notification.ToVersion == toVersion)));
        }
Пример #25
0
        public async Task StreamPosition_AfterWriteAsyncCall_UpdatedStreamVersionValue(
            [Frozen] Mock<IEventJournal> journalMock,
            EventStreamHeader header,
            EventStreamWriter writer,
            JournaledEvent[] events)
        {
            journalMock
                .Setup(self => self.AppendEventsAsync(
                    It.IsAny<string>(),
                    It.IsAny<EventStreamHeader>(),
                    It.IsAny<IReadOnlyCollection<JournaledEvent>>()))
                .Returns(header.YieldTask());

            await writer.AppendEventsAsync(events);

            Assert.Equal(header.Version, writer.StreamVersion);
        }
Пример #26
0
        /// <summary>
        ///
        /// </summary>
        public override void VisitBatchReport()
        {
            SystemLogger.Write(string.Format("VisitBatchReport: {0}", this.reportObjectList.Count));

            using (EventStreamWriter writer = new EventStreamWriter())
            {
                foreach (ReportObject report in reportObjectList)
                {
                    writer.Write(new EventElement
                    {
                        Time   = report.Date,
                        Source = this.streamName,
                        Data   = report.ConvertToXml()
                    });
                }
            }
        }
Пример #27
0
        public async Task AppendEvents_AppendsMutatedEventsToStream(
            [Frozen] Mock<IEventMutationPipeline> pipelineMock,
            [Frozen] Mock<IEventJournal> journalMock,
            EventStreamWriter writer,
            JournaledEvent[] events,
            JournaledEvent[] mutatedEvents)
        {
            var callNumber = 0;
            pipelineMock
                .Setup(self => self.Mutate(It.IsAny<JournaledEvent>()))
                .Returns(() => mutatedEvents[callNumber++]);

            await writer.AppendEventsAsync(events);

            journalMock.Verify(
                self => self.AppendEventsAsync(
                    It.IsAny<string>(),
                    It.IsAny<EventStreamHeader>(),
                    mutatedEvents));
        }
Пример #28
0
        public async Task AppendEvents_DeletesPendingNotification(
            [Frozen] Mock<IEventJournal> journalMock,
            [Frozen] Mock<IPendingNotifications> pendingNotificationMock,
            [Frozen] EventStreamHeader header,
            EventStreamWriter writer,
            JournaledEvent[] events)
        {
            journalMock
                .Setup(self => self.AppendEventsAsync(
                    It.IsAny<string>(),
                    It.IsAny<EventStreamHeader>(),
                    It.IsAny<IReadOnlyCollection<JournaledEvent>>()))
                .Returns(header.YieldTask());

            await writer.AppendEventsAsync(events);

            pendingNotificationMock.Verify(notifications => notifications.DeleteAsync(
                writer.StreamName,
                header.Version));
        }
Пример #29
0
        public void PublishEvents(IEnumerable <ModelEvent> events)
        {
            UpdateEventCache();

            var eventsToPublish = events.Where(e => !_publishedEvents.Contains(e.EventID)).OrderBy(e => e.EventVector).ToList();

            if (eventsToPublish.Count == 0)
            {
                return;
            }

            var streamId             = Guid.NewGuid();
            EventStreamHeader header = new EventStreamHeader(eventsToPublish[0].EventVector, eventsToPublish.Last().EventVector, _deviceId, streamId);

            string filePath = Path.Combine(_deviceDirectory, streamId.ToString() + ".eventstream");

            using (Stream file = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write))
                using (EventStreamWriter writer = new EventStreamWriter(file, header, true))
                {
                    writer.WriteEvents(eventsToPublish);
                }

            FileEventStream stream = new FileEventStream(filePath);

            _cachedEventStreams.Add(filePath, stream);

            var streamEventIDs = eventsToPublish.Select(e => e.EventID).ToList();

            foreach (var eventId in streamEventIDs)
            {
                if (!_publishedEvents.Add(eventId))
                {
                    throw new InvalidOperationException("This eventID already exists!");
                }
            }
            _eventsInStreams.Add(filePath, streamEventIDs);
        }
Пример #30
0
 public EventStreamWriterTests()
 {
     _stream = new MemoryStream();
     _writer = new EventStreamWriter(_stream);
 }
Пример #31
0
        /// <summary>
        ///     Stream events into stdout
        /// </summary>
        /// <param name="inputDefinition">Input definition from Splunk</param>
        public override void StreamEvents(InputDefinition inputDefinition)
        {
            string lastVarValue = null;

            using (var writer = new EventStreamWriter())
            {
                var stanza = inputDefinition.Stanza;

                // Gets input name. It is also the env var name.
                const string Seperator      = @"://";
                var          indexInputName = stanza.Name.IndexOf(Seperator) + Seperator.Length;
                var          varName        = stanza.Name.Substring(indexInputName);

                SystemLogger.Write(
                    string.Format(
                        "Name of the var to monitor is : {0}",
                        varName));

                var interval = 1000;

                string intervalParam;
                if (stanza.SingleValueParameters.TryGetValue(
                        PollingInterval,
                        out intervalParam))
                {
                    interval = int.Parse(intervalParam);
                }

                SystemLogger.Write(
                    string.Format(
                        "Polling interval is : {0}",
                        interval));

                while (true)
                {
                    var varValue = Environment.GetEnvironmentVariable(
                        varName,
                        EnvironmentVariableTarget.Machine);

                    // Event data can't be null for real events.
                    varValue = varValue ?? "(not exist)";

                    // Splunk does not record lines with only white spaces.
                    varValue = string.IsNullOrWhiteSpace(varValue)
                                   ? "(white space)"
                                   : varValue;

                    if (varValue != lastVarValue)
                    {
                        writer.Write(
                            new EventElement
                        {
                            Source = varName,
                            Data   = varValue,
                        });
                        lastVarValue = varValue;
                    }
                    Thread.Sleep(interval);
                }
            }
        }
Пример #32
0
 public void StreamPosition_ReturnsStreamVersionValue([Frozen] EventStreamHeader header, EventStreamWriter writer)
 {
     Assert.Equal(header.Version, writer.StreamVersion);
 }
Пример #33
0
        private void CreateBudget()
        {
            string budgetPath = _budgetLoader.GetBudgetSavePath(Budget.Name);

            if (budgetPath == null)
            {
                return;
            }

            InitializeDefaultCategories();

            BudgetModel budgetModel = _budgetLoader.CreateNewBudget(budgetPath, Budget);

            Account manyTransactions = new Account();

            manyTransactions.AccountType   = AccountTypes.Checking;
            manyTransactions.BudgetingType = BudgetingTypes.OnBudget;
            manyTransactions.Name          = "Many Transactions";

            Account manyMoreTransactions = new Account();

            manyMoreTransactions.AccountType   = AccountTypes.Checking;
            manyMoreTransactions.BudgetingType = BudgetingTypes.OnBudget;
            manyMoreTransactions.Name          = "Many More Transactions";

            Payee payee = new Payee();

            payee.Name = "Subway";

            Budget.Payees.Add(payee);
            budgetModel.SaveChanges();

            /*for (int i = 1; i <= 1000; i++)
             * {
             *  OpenBudget.Model.Entities.Transaction transaction = new OpenBudget.Model.Entities.Transaction();
             *  transaction.Amount = -i;
             *  transaction.Category = Budget.MasterCategories[0].Categories[0];
             *  transaction.TransactionDate = DateTime.Today;
             *  transaction.Payee = payee;
             *  if (i % 2 == 0)
             *  {
             *      manyTransactions.Transactions.Add(transaction);
             *  }
             *  else
             *  {
             *      manyMoreTransactions.Transactions.Add(transaction);
             *  }
             * }
             *
             * Budget.Accounts.Add(manyTransactions);
             * Budget.Accounts.Add(manyMoreTransactions);
             * budgetModel.SaveChanges();*/

            List <ModelEvent> events = budgetModel.BudgetStore.EventStore.GetEvents().ToList();
            EventStreamHeader header = new EventStreamHeader(events[0].EventVector, events.Last().EventVector, default(Guid), default(Guid));

            using (var stream = File.Create(Path.Combine(Path.GetDirectoryName(budgetPath), "events.json")))
                using (var writer = new EventStreamWriter(stream, header, true))
                {
                    writer.WriteEvents(events);
                }

            var        deviceSettings = _settingsProvider.Get <Device>();
            BudgetStub budgetStub     = new BudgetStub()
            {
                BudgetName = Budget.Name, BudgetPath = budgetPath, LastEdited = DateTime.Now
            };

            deviceSettings.AddRecentBudgetToTop(budgetStub);
            deviceSettings.Save();

            _mainViewModel.MountBudget(budgetModel);
            _budget.ErrorsChanged -= Budget_ErrorsChanged;
            _navigationService.NavigateTo <MainBudgetViewModel>();
        }