public InventoryItemDTO GetInventoryItem(Guid id) { InventoryItemDTO dto = null; Within.Transaction(_connectionString, transaction => { const string commandText = "SELECT Name, Description, Count, IsActive, Version FROM InventoryItems WHERE AggregateId = @aggregateId"; using (var command = new SqlCommand(commandText, transaction.Connection, transaction)) { command.Parameters.Add(new SqlParameter("@aggregateId", id)); using (var reader = command.ExecuteReader()) { while (reader != null && reader.Read()) { dto = new InventoryItemDTO { Id = id, Version = (int)reader["Version"], Name = (string)reader["Name"], Description = (string)reader["Description"], Count = (int)reader["Count"], Active = (bool)reader["IsActive"], }; } } } }); return(dto); }
public void ShouldBeAbleToCancelSchedule(WorkType workType) { var output = new List <string>(); var interval = TimeSpan.FromMilliseconds(100); var task = default(Task); switch (workType) { case WorkType.Sync: task = _scheduler.Schedule(() => output.Add("one"), interval); break; case WorkType.Async: task = _scheduler.Schedule(async() => { output.Add("one"); await Task.Yield(); }, interval); break; default: throw new Exception($"Unhandled test case {workType}."); } Within.FiveSeconds(() => output.Should().Equal(Enumerable.Repeat("one", 1))); _scheduler.CancelCurrent(); Within.FiveSeconds(() => task.IsCanceled.Should().BeTrue()); var marker = output.Count; For.OneSecond(() => output.Count.Should().Be(marker)); }
public IEnumerable <InventoryItemSummaryDTO> GetSummaries() { var dtos = new List <InventoryItemSummaryDTO>(); Within.Transaction(_connectionString, transaction => { const string commandText = "SELECT AggregateId, Name FROM InventoryItems"; using (var command = new SqlCommand(commandText, transaction.Connection, transaction)) { using (var reader = command.ExecuteReader()) { while (reader != null && reader.Read()) { dtos.Add(new InventoryItemSummaryDTO { Id = (Guid)reader["AggregateId"], Name = (string)reader["Name"] }); } } } }); return(dtos); }
public static ICriteria <T> Within <T>(this IPredicate <T> predicate, IGeometry value) { var root = (IRestorableQuery <T>)predicate; var @operator = new Within(); var criteria = new SpatialSingleCriteria <T>(root.RootQuery, predicate, @operator, value); root.RootQuery.Criterias.Add(criteria); return(criteria); }
public override void Handle(InventoryItemActivatedEvent e) { Within.Transaction(_dbConfig.ConnectionString, transaction => { const string commandText = "UPDATE InventoryItems SET IsActive = 1, Version = Version + 1 WHERE AggregateId = @aggregateId"; using (var command = new SqlCommand(commandText, transaction.Connection, transaction)) { command.Parameters.Add(new SqlParameter("@aggregateId", e.AggregateId)); command.ExecuteNonQuery(); } }); }
protected override void InitNodes(TreeNode node) { _ParentNode = AddSectionNode(node, typeof(UClass).Name); AddSimpleObjectNode(_ParentNode, Within, "Within", Within != null ? Within.GetImageName() : ""); var classFlagsNode = AddTextNode(_ParentNode, "Class Flags:" + UnrealMethods.FlagToString(ClassFlags)); classFlagsNode.ToolTipText = UnrealMethods.FlagsListToString( UnrealMethods.FlagsToList(typeof(Flags.ClassFlags), ClassFlags) ); base.InitNodes(_ParentNode); }
public override void Handle(InventoryItemDetailsChangedEvent e) { Within.Transaction(_dbConfig.ConnectionString, transaction => { const string commandText = "UPDATE InventoryItems SET Name = @name, Description = @description, Version = Version + 1 WHERE AggregateId = @aggregateId"; using (var command = new SqlCommand(commandText, transaction.Connection, transaction)) { command.Parameters.Add(new SqlParameter("@aggregateId", e.AggregateId)); command.Parameters.Add(new SqlParameter("@name", e.Name)); command.Parameters.Add(new SqlParameter("@description", e.Description)); command.ExecuteNonQuery(); } }); }
public void ShouldBeAbleToScheduleWorkToRepeatAtAFixedInterval(WorkType workType, ActorScheduleOptions actorScheduleOptions) { var barrier = new TaskCompletionSource <bool>(); var expectedInterval = TimeSpan.FromMilliseconds(100); var times = new List <DateTime>(); var sampleSize = 5; void Adder() { if (times.Count < sampleSize) { times.Add(DateTime.UtcNow); } if (times.Count == sampleSize) { // Block here so that we can assess something that's not moving barrier.Task.Wait(); } } switch (workType) { case WorkType.Sync: _scheduler.Schedule((Action)Adder, expectedInterval, actorScheduleOptions); break; case WorkType.Async: _scheduler.Schedule(async() => { await Task.Yield(); Adder(); }, expectedInterval, actorScheduleOptions); break; default: throw new Exception($"Unhandled test case {workType}."); } Within.FiveSeconds(() => times.Count.Should().Be(sampleSize)); var actualIntervals = times.Take(sampleSize - 1).Zip(times.Skip(1), (x, y) => y - x).ToList(); // Use 75ms instead of 100ms to give it a bit of latitude: mainly we just want to make sure there is some delaying going on. actualIntervals.Should().OnlyContain(x => x >= TimeSpan.FromMilliseconds(75)); barrier.SetResult(true); }
public void ShouldBeAbleToConfigureScheduleToRescheduleInCaseOfUnexpectedErrorButNotCancellation(WorkType workType) { var interval = TimeSpan.FromMilliseconds(100); var times = new List <DateTime>(); var emittedException = default(Exception); var task = default(Task); switch (workType) { case WorkType.Sync: task = _scheduler.Schedule(() => { times.Add(DateTime.UtcNow); if (times.Count == 3) { throw new InvalidOperationException("Pah!"); } }, interval, ActorScheduleOptions.Default, x => emittedException = x); break; case WorkType.Async: task = _scheduler.Schedule(async() => { times.Add(DateTime.UtcNow); await Task.Yield(); if (times.Count == 3) { throw new InvalidOperationException("Pah!"); } }, interval, ActorScheduleOptions.Default, x => emittedException = x); break; default: throw new Exception($"Unhandled test case {workType}."); } Within.FiveSeconds(() => times.Count.Should().BeGreaterOrEqualTo(4)); emittedException.Should().BeOfType <InvalidOperationException>().Which.Message.Should().Be("Pah!"); _scheduler.CancelCurrent(); Within.FiveSeconds(() => task.IsCanceled.Should().BeTrue()); }
public override void Handle(InventoryItemCreatedEvent e) { Within.Transaction(_dbConfig.ConnectionString, transaction => { const string commandText = "INSERT INTO InventoryItems VALUES(@aggregateId, @name, @description, @count, @isActive, @version)"; using (var command = new SqlCommand(commandText, transaction.Connection, transaction)) { command.Parameters.Add(new SqlParameter("@aggregateId", e.AggregateId)); command.Parameters.Add(new SqlParameter("@name", e.Name)); command.Parameters.Add(new SqlParameter("@description", e.Description)); command.Parameters.Add(new SqlParameter("@count", e.Count)); command.Parameters.Add(new SqlParameter("@isActive", e.IsActive)); command.Parameters.Add(new SqlParameter("@version", 1)); command.ExecuteNonQuery(); } }); }
public void Save(IEventProvider eventProvider) { Within.Transaction(_dbConfig.ConnectionString, transaction => { int version = GetEventProviderVersion(eventProvider.Id, transaction); if (version == -1) { StoreEventProvider(eventProvider, transaction); } if (version > eventProvider.Version) { throw new ConcurrencyViolationException(); } eventProvider.GetChanges().ForEach(e => StoreChange(e, eventProvider.Id, transaction)); UpdateEventProviderVersion(eventProvider, transaction); }); }
public IEnumerable <IEvent> GetEventsFromVersionForEventProvider(Guid id, int version) { var events = new List <IEvent>(); Within.Transaction(_dbConfig.ConnectionString, transaction => { const string commandText = "SELECT Data FROM Event WHERE EventProvider_Id = @eventProviderId AND Version > @version"; using (var command = new SqlCommand(commandText, transaction.Connection, transaction)) { command.Parameters.Add(new SqlParameter("@eventProviderId", id)); command.Parameters.Add(new SqlParameter("@version", version)); using (var reader = command.ExecuteReader()) { while (reader != null && reader.Read()) { events.Add(_serializer.Deserialize((byte[])reader["Data"])); } } } }); return(events); }
public void ShouldNotBeAbleToResumeWorkAfterStop(ResumeTestCase resumeTestCase, StopWorkOutcome stopWorkOutcome) { var actor1 = CreateActor( x => x.StopWork = new ActorStopWork( () => { if (stopWorkOutcome == StopWorkOutcome.Faults) { throw new InvalidOperationException("Never meant to be"); } })); var actor2 = CreateActor(); var pretrigger = new TaskCompletionSource <bool>(); var trigger = new TaskCompletionSource <bool>(); var suspendWork = default(Func <Task <int> >); var stages = new List <string>(); var expectedStageOrder = new List <string> { "PreSuspend", "PreTriggerWait", "PostTriggerWait" }; int OffActorWork() { stages.Add("PreTriggerWait"); pretrigger.SetResult(true); ThrowIfWaitTimesOut(trigger.Task); stages.Add("PostTriggerWait"); return(345); } switch (resumeTestCase) { case ResumeTestCase.AwaitOnSecondActor: suspendWork = () => actor2.Enqueue((Func <int>)OffActorWork); break; case ResumeTestCase.AwaitOnTaskFactoryScheduledTask: suspendWork = () => new TaskFactory(TaskScheduler.Default).StartNew(OffActorWork); break; default: throw new Exception($"Unhandled test case {resumeTestCase}."); } //var task1 = actor1.Enqueue( async() => { stages.Add("PreSuspend"); var value = await suspendWork(); stages.Add("PostSuspend"); return(value * 37); }); pretrigger.Task.AwaitingShouldCompleteIn(_waitTimeout); stages.Should().Equal(expectedStageOrder.Take(2)); var stopTask = actor1.Stop(); MarkAlreadyStopped(); switch (stopWorkOutcome) { case StopWorkOutcome.Completes: stopTask.AwaitingShouldCompleteIn(_waitTimeout); break; case StopWorkOutcome.Faults: ((Func <Task>)(async() => await stopTask)).Should().Throw <InvalidOperationException>().WithMessage("Never meant to be"); break; default: throw new Exception($"Unhandled test case {stopWorkOutcome}."); } trigger.SetResult(true); Within.OneSecond(() => stages.Should().Equal(expectedStageOrder)); For.OneSecond(() => stages.Should().Equal(expectedStageOrder)); // The below would be nice but has proved intractable to achieve. //task1.Awaiting(async x => await x).ShouldThrow<TaskCanceledException>(); actor2.Stop().Wait(); }
public void ASecondCallToScheduleShouldCancelTheWorkPreviouslyScheduled(WorkType workType1, WorkType workType2) { var output = new List <string>(); var interval = TimeSpan.FromMilliseconds(100); var task1 = default(Task); var firstTwoAddedPromise = new TaskCompletionSource <bool>(); var gotAOneAfterATwoPromise = new TaskCompletionSource <bool>(); void Adder(string x) { output.Add(x); if (x == "two") { firstTwoAddedPromise.TrySetResult(true); } else if (firstTwoAddedPromise.Task.IsCompleted && x == "one") { gotAOneAfterATwoPromise.TrySetResult(true); } } switch (workType1) { case WorkType.Sync: task1 = _scheduler.Schedule(() => { Adder("one"); }, interval); break; case WorkType.Async: task1 = _scheduler.Schedule(async() => { await Task.Delay(TimeSpan.FromMilliseconds(10)); Adder("one"); }, interval); break; default: throw new Exception($"Unhandled test case {workType1}."); } Within.FiveSeconds(() => output.Count.Should().BeGreaterOrEqualTo(1)); switch (workType2) { case WorkType.Sync: _scheduler.Schedule(() => { Adder("two"); }, interval); break; case WorkType.Async: _scheduler.Schedule(async() => { Adder("two"); await Task.Yield(); }, interval); break; default: throw new Exception($"Unhandled test case {workType2}."); } firstTwoAddedPromise.Task.AwaitingShouldCompleteIn(TimeSpan.FromSeconds(5)); For.OneSecond(() => gotAOneAfterATwoPromise.Task.IsCompleted.Should().BeFalse("The first bit of work is still being scheduled.")); task1.IsCanceled.Should().BeTrue(); }