Ejemplo n.º 1
0
        public void MultiThread_NumbersAreUnique()
        {
            var collection = new BlockingCollection <int>();

            Parallel.ForEach(Enumerable.Range(0, 1000), i =>
            {
                var random = GameRandom.Between(int.MinValue, int.MaxValue);
                collection.Add(random);
            });
            output.WriteLine($"Repeated values: {collection.Count() - collection.Distinct().Count()}");
            Assert.True(collection.Distinct().Count() == collection.Count());
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            string conString = "127.0.0.1:2181";

            new IdHelperBootstrapper()
            .UseZookeeper(conString, 200, "Test")
            .Boot();

            Console.WriteLine($"WorkerId:{IdHelper.WorkerId}");

            Stopwatch watch = new Stopwatch();

            watch.Start();
            List <Task> tasks = new List <Task>();
            BlockingCollection <string> ids = new BlockingCollection <string>();

            for (int i = 0; i < 4; i++)
            {
                tasks.Add(Task.Run(() =>
                {
                    for (int j = 0; j < 1000000; j++)
                    {
                        ids.Add(IdHelper.GetId());
                    }
                }));
            }
            Task.WaitAll(tasks.ToArray());
            watch.Stop();
            Console.WriteLine($"耗时:{watch.ElapsedMilliseconds}ms,是否有重复:{ids.Count != ids.Distinct().Count()}");
        }
Ejemplo n.º 3
0
        public void Should_never_return_different_boards_for_same_name_when_used_from_multiple_threads()
        {
            const int threadCount = 5;
            const int getCount    = 100;
            var       boards      = new BlockingCollection <DrawingBoard>();
            var       tasks       = new List <Task>();

            var    allStarted   = new SemaphoreSlim(threadCount);
            var    startGetting = new ManualResetEventSlim();
            Action getBoard     = () =>
            {
                allStarted.Release();
                startGetting.Wait();
                for (var j = 0; j < getCount; j++)
                {
                    boards.Add(_service.Get("test"));
                }
            };

            for (var i = 0; i < threadCount; i++)
            {
                var task = Task.Factory.StartNew(getBoard);
                tasks.Add(task);
            }
            allStarted.Wait();
            startGetting.Set();
            Task.WaitAll(tasks.ToArray());

            var distinctBoardCount = boards.Distinct().Count();

            Assert.That(distinctBoardCount, Is.EqualTo(1));
        }
Ejemplo n.º 4
0
        public int SortAndWriteToFile()
        {
            var sortedCollection = _collection.Distinct().OrderBy(x => x.Key);

            File.WriteAllLines(Common.Common.OutputFileName, sortedCollection.Select(s => s.Value.ToString()).ToList());
            return(sortedCollection.Count());
        }
Ejemplo n.º 5
0
        public static List <TokenString> NewTokenStrings(int tokenMask, int Capacity)
        {
            var tokens = new BlockingCollection <TokenString>();

            Parallel.For(1, (int)(Capacity * 1.08), (i, state) => { tokens.Add(NewTokenString(tokenMask)); });

            return(tokens.Distinct().Take(Capacity).ToList());
        }
Ejemplo n.º 6
0
        public void Can_GenerateUniqueAplhaNumericStrings_ForMillionIterations_MultiThreaded()
        {
            var fragments = new BlockingCollection <string>();

            Parallel.For(0, 1000000, i =>
            {
                var generatedFragment = GenerateFragment(fragments);

                ValidateGeneratedFragment(generatedFragment);
            });

            bool success = fragments.Count == fragments.Distinct().Count();

            if (!success)
            {
                Debug.WriteLine(fragments.Count);
                Debug.WriteLine(fragments.Distinct().Count());
            }

            Assert.IsTrue(success);
        }
        public void GetDouble_ManyThreads_DiffRandomResult()
        {
            var target = new FastRandomRandomization();
            var actual = new BlockingCollection <int>();

            Parallel.For(0, 1000, (i) =>
            {
                actual.Add(target.GetInt(0, int.MaxValue));
            });

            Assert.AreEqual(1000, actual.Count);
            Assert.AreEqual(1000, actual.Distinct().Count());
        }
Ejemplo n.º 8
0
            public void should_return_mostly_unique_values()
            {
                BlockingCollection <int> values = new BlockingCollection <int>();

                Parallel.For(0, 100,
                             new ParallelOptions {
                    MaxDegreeOfParallelism = 10
                },
                             (i, loop) => values.Add(ARandom.IntBetween(int.MinValue, int.MaxValue)));

                // Allow for occasional duplicate
                values.Distinct().Count().Should().BeInRange(99, 100);
            }
        public void GetDouble_ManyThreads_DiffRandomResult()
        {
            var target = new BasicRandomization();
            var actual = new BlockingCollection<int>();

            Parallel.For(0, 1000, (i) =>
            {
                actual.Add(target.GetInt(0, int.MaxValue));
            });

            Assert.AreEqual(1000, actual.Count);
            Assert.AreEqual(1000, actual.Distinct().Count());
        }
Ejemplo n.º 10
0
        public static void GetIdTest()
        {
            string conString = "127.0.0.1:2181";

            new IdHelperBootstrapper()
            .UseZookeeper(conString, 200, "Test")
            //.SetWorkderId(1)
            .Boot();

            //Console.WriteLine($"WorkerId:{IdHelper.WorkerId}");
            //while (true)
            //{
            //    try
            //    {
            //        Console.WriteLine($"WorkerId:{IdHelper.WorkerId},Id:{IdHelper.GetId()}");
            //    }
            //    catch (Exception ex)
            //    {
            //        Console.WriteLine(ex.Message);
            //    }
            //    finally
            //    {
            //        Thread.Sleep(1000);
            //    }
            //}

            Stopwatch watch = new Stopwatch();

            watch.Restart();
            List <Task> tasks = new List <Task>();
            BlockingCollection <string> ids = new BlockingCollection <string>();

            for (int i = 0; i < 4; i++)
            {
                tasks.Add(Task.Run(() =>
                {
                    for (int j = 0; j < 1000000; j++)
                    {
                        ids.Add(IdHelper.GetId());
                    }
                }));
            }
            Task.WaitAll(tasks.ToArray());
            watch.Stop();
            Console.WriteLine($"耗时:{watch.ElapsedMilliseconds}ms,是否有重复:{ids.Count != ids.Distinct().Count()}");
        }
Ejemplo n.º 11
0
        public void SequentialGuid()
        {
            List <Task> tasks = new List <Task>();
            BlockingCollection <Guid> guids = new BlockingCollection <Guid>();

            for (int i = 0; i < 4; i++)
            {
                tasks.Add(Task.Run(() =>
                {
                    for (int j = 0; j < 1000000; j++)
                    {
                        guids.Add(GuidHelper.NewGuid());
                    }
                }));
            }

            Task.WaitAll(tasks.ToArray());

            Assert.AreEqual(guids.Distinct().Count(), 4000000);
        }
Ejemplo n.º 12
0
        public void NextId_Duplicate_issues_2_Test()
        {
            var snowflakeIdWorker  = new SnowflakeIdWorker(1, 1);
            var blockingCollection = new BlockingCollection <long>();
            var tasks = new List <Task>();

            for (int i = 0; i < 100; i++)
            {
                var task = Task.Run(() =>
                {
                    for (int j = 0; j < 10000; j++)
                    {
                        blockingCollection.Add(snowflakeIdWorker.NextId());
                    }
                });

                tasks.Add(task);
            }

            Task.WaitAll(tasks.ToArray());

            Assert.AreEqual(1000000, blockingCollection.Distinct().Count());
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Service entry point
        /// </summary>
        /// <param name="tvMazeApiService">TVMazeApi HttpClient Service</param>
        /// <param name="dbContext">MS SQL Db Context</param>
        /// <param name="cancellationToken"></param>
        /// <param name="logger">Logger service</param>
        /// <returns></returns>
        public async Task ExecuteAsync(TVMazeAPIService tvMazeApiService, TVMazeDBContext dbContext, CancellationToken cancellationToken, ILogger logger)
        {
            DateTime startTime = DateTime.Now;
            //We need to check last pack of shows, maybe we have a new ones in it
            bool tryFirstPack = true;

            _tvMazeApiService = tvMazeApiService;

            //HttpClient Retry Policy
            var maxRetryAttempts     = 3;
            var pauseBetweenFailures = TimeSpan.FromSeconds(10);

            var retryPolicy = Policy
                              .Handle <HttpRequestException>()
                              .WaitAndRetryAsync(maxRetryAttempts, i => pauseBetweenFailures);

            //Select max show id into local database
            var maxShowId = dbContext.TVMazeShow.Select(s => s.Id).DefaultIfEmpty(0).Max();
            //Calc next pageIndex for TVMazeAPI
            var pageIndex = (maxShowId == 0) ? 0 : (int)Math.Floor((double)maxShowId / 250) + 1;

            while (true)
            {
                IEnumerable <TVMazeShow> showsList = null;
                //need to check pack pack for new shows
                if (tryFirstPack && maxShowId != 0 && maxShowId < 250 * pageIndex - 1)
                {
                    logger.Log(LogLevel.None, $"Pack with index={pageIndex - 1} loading started");
                    //Get Shows list by page index
                    showsList = await retryPolicy.ExecuteAsync(async() =>
                    {
                        return(await LoadShowsPack(pageIndex - 1));
                    });

                    showsList = showsList.Where(show => show.Id > maxShowId);
                    //New shows into last pack not found - check the next pack
                    if (!showsList.Any())
                    {
                        logger.Log(LogLevel.None, $"Pack with index={pageIndex - 1} doesn't contain new shows");
                        tryFirstPack = false;
                        continue;
                    }
                }
                else
                {
                    tryFirstPack = false;
                    logger.Log(LogLevel.None, $"Pack with index={pageIndex} loading started");
                    //Get Shows list by page index
                    showsList = await retryPolicy.ExecuteAsync(async() =>
                    {
                        return(await LoadShowsPack(pageIndex));
                    });

                    //Show page request return NotFound, Nothing to synchronize
                    if (showsList == null)
                    {
                        logger.Log(LogLevel.None, $"Pack with index={pageIndex} Not Found");
                        logger.Log(LogLevel.None, "Loading process totaly finished");
                        return;
                    }
                }

                using (var personsList = new BlockingCollection <TVMazePerson>())
                    using (var castsList = new BlockingCollection <TVMazeCast>())
                    {
                        foreach (var show in showsList)
                        {
                            IEnumerable <TVMazePerson> showCastsList;
                            //Get casts for each show
                            showCastsList = await retryPolicy.ExecuteAsync(async() =>
                            {
                                return(await LoadShowCasts(show));
                            });


                            showCastsList.ToList().ForEach(cast =>
                            {
                                personsList.Add(cast);
                                castsList.Add(new TVMazeCast()
                                {
                                    ShowId = show.Id, PersonId = cast.Id
                                });
                            });
                        }
                        ;
                        //Insert downloaded data into database
                        using (var transaction = dbContext.Database.BeginTransaction())
                        {
                            try
                            {
                                dbContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT " + typeof(TVMazePerson).Name + " ON;");
                                var insertedPersonsCount = dbContext.TVMazePerson.AddNoDublicate(personsList.Distinct().ToList());
                                dbContext.SaveChanges();
                                dbContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT " + typeof(TVMazePerson).Name + " OFF;");
                                dbContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT " + typeof(TVMazeShow).Name + " ON;");
                                var insetredShowsCount = showsList.Count();
                                dbContext.TVMazeShow.AddRange(showsList);
                                dbContext.SaveChanges();
                                dbContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT " + typeof(TVMazeShow).Name + " OFF;");
                                var castsForInsert     = castsList.Distinct();
                                var insertedCastsCount = castsForInsert.Count();
                                dbContext.TVMazeCast.AddRange(castsForInsert);
                                dbContext.SaveChanges();
                                transaction.Commit();
                                logger.Log(LogLevel.None, $"{insetredShowsCount} shows inserted, {insertedCastsCount} casts inserted, {insertedPersonsCount} persons inserted");
                                logger.Log(LogLevel.None, $"Pack with index={(tryFirstPack ? pageIndex - 1 : pageIndex)} loading finished");
                            }
                            catch (Exception ex)
                            {
                                logger.LogError(ex, "An error occurred inserting the DB.");
                            }
                        }
                        if (!tryFirstPack)
                        {
                            pageIndex++;
                        }
                        tryFirstPack = false;
                    }
            }
        }