static void Main(string[] args) { var optionsBuilder = new DbContextOptionsBuilder <AppDataContext>(); optionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=ChangeManagementDB;Trusted_Connection=True;MultipleActiveResultSets=true"); AppDataContext context = new AppDataContext(optionsBuilder.Options); ChangeRepository _changeRepository = new ChangeRepository(context); //ChangeRequestTask task = new ChangeRequestTask(); //task.Name = "Task1"; //task.Summary = "Test Task for Pessimistic Concurrency Check"; //task.ChangeRequestID = 5016; //int taskid = _changeRepository.CreateChangeRequestTask(task, "user0"); //// place pessimistic lock on record and lock for user0 //_changeRepository.GetChangeRequestTaskbyIdForEdit(taskid, "user0"); // sleep so the lock can expire (3 seconds) //Thread.Sleep(4000); int taskid = 13; for (int i = 0; i < numberOfThreads; i++) { ThreadPool.QueueUserWorkItem(ThreadProc, new ConcurrencyTestOptions(taskid, "user" + i)); } Console.ReadKey(); }
private async Task <SyncResponseViewModel> GetChanges(IApplication app, SyncRequestViewModel request) { var response = new SyncResponseViewModel() { Groups = new List <SyncResponseViewModel.GroupViewModel>() }; if (request.Groups != null) { foreach (var group in request.Groups) { List <IChange> results = await ChangeRepository.ListChangesAsync(app.Id, group.Group, group.Tidemark); if (results != null && results.Any()) { response.Groups.Add(new SyncResponseViewModel.GroupViewModel() { Group = group.Group, Tidemark = results.Last().Id, Changes = results.Select(r => new SyncResponseViewModel.ChangeViewModel() { Modified = r.ClientModified, Value = r.RecordValue, Entity = r.Entity, RecordId = r.RecordId, Property = r.Property }).ToList() }); } } } return(response); }
static void ThreadProc(Object stateInfo) { var optionsBuilder = new DbContextOptionsBuilder <AppDataContext>(); optionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=ChangeManagementDB;Trusted_Connection=True;MultipleActiveResultSets=true"); ConcurrencyTestOptions testOptions = (ConcurrencyTestOptions)stateInfo; try { using (var context = new AppDataContext(optionsBuilder.Options)) { ChangeRepository _changeRepository = new ChangeRepository(context); //Console.WriteLine("Before Barrier, user={0}", testOptions.UserName); Console.WriteLine("Before get for edit: {0}", testOptions.UserName); _changeRepository.GetChangeRequestTaskbyIdForEdit(testOptions.RecordId, testOptions.UserName); //Console.WriteLine("After Barrier, user={0}", testOptions.UserName); Console.WriteLine("After get for edit: {0}", testOptions.UserName); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
public void ShouldTotalChangeCards() { //Arrange var hashSet = new HashSet <ChanceCard>(); var changeRepository = new ChangeRepository( new List <Player>()); //Act for (var i = 0; i < TotalCards * 3; i++) { var result = changeRepository.Next(); if (!hashSet.Contains(result)) { hashSet.Add(result); } } //Assert hashSet.Count.Should().Be(TotalCards); var groups = hashSet. GroupBy(_ => _.GetType()). ToList(); groups.Count.Should().Be(5); Assert(groups, typeof(EarnChangeCard), EarnTotalCards); Assert(groups, typeof(SpendChangeCard), SpendTotalCards); Assert(groups, typeof(PrisonExitCard), PrisonExitCards); Assert(groups, typeof(Earn50FromEveryOne), Earn50FromEveryOneCards); Assert(groups, typeof(GoToPrisonChangeCard), GoToPrisonChangeCards); }
private async Task ProcessChanges(IApplication app, SyncRequestViewModel request) { if (request.Changes != null && request.Changes.Any()) { var dbChanges = new List <IChange>(); Stopwatch sw = new Stopwatch(); sw.Start(); // Dedup any changes with the same Record, Entity and Property var uniqueChanges = new Dictionary <string, SyncRequestViewModel.ChangeViewModel>(); foreach (var change in request.Changes) { string key = $"{change.RecordId}-{change.Entity}-{change.Property}"; if (uniqueChanges.TryGetValue(key, out var existingChange)) { if (change.MillisecondsAgo < existingChange.MillisecondsAgo) { uniqueChanges[key] = change; } } else { uniqueChanges.Add(key, change); } } Logger.LogInformation($"Deduped changes in {sw.ElapsedMilliseconds}ms count: {dbChanges.Count}"); sw.Restart(); foreach (var change in uniqueChanges.Values) { if (change != null) { DateTime modifiedUTC = requestStartTimeUTC.AddMilliseconds(-change.MillisecondsAgo); var dbChange = ChangeRepository.CreateChange(app.AccountId, app.Id, change.RecordId, change.Group, change.Entity, change.Property, modifiedUTC.Ticks, change.Value); dbChanges.Add(dbChange); } } Logger.LogInformation($"Generated changes in {sw.ElapsedMilliseconds}ms count: {dbChanges.Count}"); sw.Restart(); if (dbChanges.Any()) { sw.Restart(); await ChangeRepository.UpsertChangesAsync(app.Id, dbChanges); Logger.LogInformation($"Saved changes to database in {sw.ElapsedMilliseconds}ms count: {dbChanges.Count}"); } sw.Stop(); } }
public IEnumerable <ChangeViewModel> Get(int clientId) { try { return(ChangeRepository.GetChangesForClient(clientId).Select(CreateChangeViewModel)); } catch (ClientNotFoundException) { throw new HttpResponseException(HttpStatusCode.Unauthorized); } }
private EntityPermission Read(int parentId, int?userId, int?groupId) { EntityPermission permission = null; if (!userId.HasValue && !groupId.HasValue) { return(null); } if (userId.HasValue) { permission = ChangeRepository.ReadForUser(parentId, userId.Value); } else if (groupId.HasValue) { permission = ChangeRepository.ReadForGroup(parentId, groupId.Value); } return(permission); }
// methods private async Task <bool> LoadRepositoriesAsync() { LoadStatus = "Loading Speakers..."; await SpeakerRepository.LoadAsync(); LoadStatus = "Loading Sessions..."; await SessionRepository.LoadAsync(); LoadStatus = "Getting Changes..."; await ChangeRepository.LoadAsync(); var changes = ChangeRepository.GetAll(); if (changes.Count > 0) { LoadStatus = "Applying Changes..."; ChangeService.ApplyChanges(changes); } return(true); }
public IEnumerable <ChangeViewModel> Get() { return(ChangeRepository.GetAll().Select(CreateChangeViewModel)); }