public virtual async Task<CodeAction> GetFixAsync(
            ImmutableDictionary<Document, ImmutableArray<Diagnostic>> documentsAndDiagnosticsToFixMap,
            FixAllContext fixAllContext)
        {
            if (documentsAndDiagnosticsToFixMap != null && documentsAndDiagnosticsToFixMap.Any())
            {
                FixAllLogger.LogDiagnosticsStats(documentsAndDiagnosticsToFixMap);

                var fixesBag = new ConcurrentBag<CodeAction>();

                using (Logger.LogBlock(FunctionId.CodeFixes_FixAllOccurrencesComputation_Fixes, fixAllContext.CancellationToken))
                {
                    fixAllContext.CancellationToken.ThrowIfCancellationRequested();

                    var documents = documentsAndDiagnosticsToFixMap.Keys;
                    var tasks = documents.Select(d => AddDocumentFixesAsync(d, documentsAndDiagnosticsToFixMap[d], fixesBag.Add, fixAllContext))
                                         .ToArray();
                    await Task.WhenAll(tasks).ConfigureAwait(false);
                }

                if (fixesBag.Any())
                {
                    using (Logger.LogBlock(FunctionId.CodeFixes_FixAllOccurrencesComputation_Merge, fixAllContext.CancellationToken))
                    {
                        FixAllLogger.LogFixesToMergeStats(fixesBag);
                        return await TryGetMergedFixAsync(fixesBag, fixAllContext).ConfigureAwait(false);
                    }
                }
            }

            return null;
        }
Пример #2
0
        public virtual async Task<CodeAction> GetFixAsync(
            ImmutableDictionary<Document, ImmutableArray<Diagnostic>> documentsAndDiagnosticsToFixMap,
            FixAllContext fixAllContext)
        {
            if (documentsAndDiagnosticsToFixMap != null && documentsAndDiagnosticsToFixMap.Any())
            {
                FixAllLogger.LogDiagnosticsStats(documentsAndDiagnosticsToFixMap);

                var fixesBag = new ConcurrentBag<CodeAction>();

                using (Logger.LogBlock(FunctionId.CodeFixes_FixAllOccurrencesComputation_Fixes, fixAllContext.CancellationToken))
                {
                    fixAllContext.CancellationToken.ThrowIfCancellationRequested();

                    var documents = documentsAndDiagnosticsToFixMap.Keys.ToImmutableArray();
                    var options = new ParallelOptions() { CancellationToken = fixAllContext.CancellationToken };
                    Parallel.ForEach(documents, options, document =>
                    {
                        fixAllContext.CancellationToken.ThrowIfCancellationRequested();
                        AddDocumentFixesAsync(document, documentsAndDiagnosticsToFixMap[document], fixesBag.Add, fixAllContext).Wait(fixAllContext.CancellationToken);
                    });
                }

                if (fixesBag.Any())
                {
                    using (Logger.LogBlock(FunctionId.CodeFixes_FixAllOccurrencesComputation_Merge, fixAllContext.CancellationToken))
                    {
                        FixAllLogger.LogFixesToMergeStats(fixesBag);
                        return await TryGetMergedFixAsync(fixesBag, fixAllContext).ConfigureAwait(false);
                    }
                }
            }

            return null;
        }
Пример #3
0
        static int GetPort()
        {
            var port = random.Next(minValue: 40000, maxValue: 65535);

            if (usedPorts.Any(p => p == port))
            {
                port = GetPort();
            }
            else
            {
                usedPorts.Add(port);
            }

            return(port);
        }
        //产生队列数据
        public void InitQueue(DateTime startTime, DateTime endTime)
        {
            roteLimitConfig = logsController.GetLimitConfig();
            this.startTime  = startTime;
            this.endTime    = endTime;
            //var settings = settingsAppService.GetAll().ToDictionary(key => key.Key, value => value.Value);
            var settings = logsController.GetSettings();

            timeSpan     = Convert.ToInt32(settings["timeSpan"]);
            sample       = Convert.ToDouble(settings["sample"]);
            taskCount    = Convert.ToInt32(settings["taskCount"]);
            resultSaveDb = Convert.ToBoolean(settings["resultSaveDb"]);
            updateRateLimitForCloundflare = Convert.ToBoolean(settings["UpdateRateLimitForCloundflare"]);

            if (keyValuePairs == null)
            {
                keyValuePairs = new ConcurrentQueue <KeyValuePair <DateTime, DateTime> >();
            }
            DateTime dateTime = startTime;

            OnMessage(new MessageEventArgs("产生队列数据开始"));
            while (true)
            {
                string time = string.Format("{0}-{1}", dateTime.ToString("yyyyMMddHHmmss"), dateTime.AddSeconds(timeSpan).ToString("yyyyMMddHHmmss"));

                if (cloudflareLogReports != null)
                {
                    if (!cloudflareLogReports.Any(a => a.Time == time))
                    {
                        OnMessage(new MessageEventArgs(time));
                        keyValuePairs.Enqueue(new KeyValuePair <DateTime, DateTime>(dateTime, dateTime.AddSeconds(timeSpan)));
                    }
                }
                else
                {
                    OnMessage(new MessageEventArgs(time));
                    keyValuePairs.Enqueue(new KeyValuePair <DateTime, DateTime>(dateTime, dateTime.AddSeconds(timeSpan)));
                }

                dateTime = dateTime.AddSeconds(timeSpan);

                if (dateTime >= endTime)
                {
                    break;
                }
            }
            OnMessage(new MessageEventArgs("产生队列数据结束"));
        }
Пример #5
0
        public async Task UsingJsonFileProgressRepository()
        {
            var web3 = new Web3.Web3(TestConfiguration.BlockchainUrls.Infura.Mainnet);

            //cancellation token to enable the listener to be stopped
            //passing in a time limit as a safety valve for the unit test
            var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromMinutes(1));

            //somewhere to put matching events
            //using ConcurrentBag because we'll be referencing the collection on different threads
            var erc20Transfers = new ConcurrentBag <EventLog <TransferEventDto> >();

            // define a progress repository
            // this sample uses an out of the box simple json file implementation
            // if you want your own - the IBlockProgressRepository interface is easy to implement
            // the processor will use this repo to define which block to start at and update it after each batch is complete
            // it can prevent duplicate processing that could occur after a restart
            var jsonFilePath = Path.Combine(Path.GetTempPath(), "EventProcessingBlockProgress.json");

            //initialise the builder
            var builder = web3.Eth.LogsProcessor <TransferEventDto>()
                          .OnEvents((events) => erc20Transfers.AddRange(events)) // transfer events
                          .SetBlocksPerBatch(1)                                  //optional: restrict batches to one block at a time
                          .SetMinimumBlockNumber(7540102)                        //optional: default is to start at current block on chain
                                                                                 // for test purposes we'll stop after processing a batch
                          .OnBatchProcessed((args) => cancellationTokenSource.Cancel())
                                                                                 // tell the processor to use a Json File based Block Progress Repository
                                                                                 // for test purposes only we delete any existing file to ensure we start afresh with no previous state
                          .UseJsonFileForBlockProgress(jsonFilePath, deleteExistingFile: true);

            var processor = builder.Build();

            //we should have a BlockProgressRepository
            Assert.NotNull(builder.BlockProgressRepository);
            //there should be no prior progress
            Assert.Null(await builder.BlockProgressRepository.GetLastBlockNumberProcessedAsync());

            //run the processor for a while
            var rangesProcessed = await processor.ProcessContinuallyAsync(cancellationTokenSource.Token);

            //the last block processed should have been saved
            Assert.NotNull(await builder.BlockProgressRepository.GetLastBlockNumberProcessedAsync());

            //we should have captured some events
            Assert.True(erc20Transfers.Any());
            //clean up
            File.Delete(jsonFilePath);
        }
Пример #6
0
        public Task <Cashier> CreateCashier(string name)
        {
            var cashierId = string.Empty;

            do
            {
                cashierId = GenerateCashierId();
            } while(_cashiers.Any(o => string.Equals(o.Id, cashierId, StringComparison.OrdinalIgnoreCase)));

            var cashier = new Cashier {
                Id   = cashierId,
                Name = name
            };

            return(Task.FromResult(cashier));
        }
Пример #7
0
 private void CleanUp()
 {
     if (_createdTemporaryTableNames.Any())
     {
         if (DataContext.Database.Connection.State == ConnectionState.Closed)
         {
             DataContext.Database.Connection.Open();
         }
         foreach (var tmpName in _createdTemporaryTableNames.Distinct().ToList())
         {
             string sqlCommand = $"IF OBJECT_ID('tempdb..{tmpName}') IS NOT NULL BEGIN DROP TABLE {tmpName} END";
             ExecuteSqlCommand(sqlCommand);
         }
         DataContext.Database.Connection.Close();
     }
 }
Пример #8
0
        /// <summary>
        /// Adds a resource to the cache.
        /// </summary>
        /// <param name="key">
        /// The filename of the item to add.
        /// </param>
        /// <param name="contents">
        /// The contents of the file to cache.
        /// </param>
        /// <param name="fileMonitors">
        /// The file monitors to keep track of.
        /// </param>
        protected void AddItemToCache(string key, string contents, ConcurrentBag <string> fileMonitors)
        {
            if (!string.IsNullOrWhiteSpace(contents))
            {
                CacheItemPolicy cacheItemPolicy = new CacheItemPolicy {
                    Priority = CacheItemPriority.NotRemovable
                };

                if (fileMonitors.Any())
                {
                    cacheItemPolicy.ChangeMonitors.Add(new HostFileChangeMonitor(fileMonitors.ToList()));
                }

                CacheManager.AddItem(key, contents, cacheItemPolicy);
            }
        }
Пример #9
0
        public Status AddNewList(ToDoList list)
        {
            if (list == null || !list.Validate())
            {
                return(Status.Invalid);
            }

            if (TodoLists.Any(tdl => tdl.Id == list.Id))
            {
                return(Status.AlreadyExist);
            }

            TodoLists.Add(list);

            return(Status.Created);
        }
Пример #10
0
        public void Run()
        {
            new Thread(() =>
            {
                var udpClient = new UdpClient(32228);
                while (players.IsEmpty || players.Any(x => !x.Ready))
                {
                    Thread.Sleep(100);
                }

                while (true)
                {
                    StartNewGame(udpClient);
                }
            }).Start();
        }
Пример #11
0
        // other options for perf.
        // 1) only grab certain directories either with dockerfiles or as specified by build.yaml
        // 2) Prioritize large files or files with lots of copies.
        // 3) parallelize copy with buffer first attempt at that with _contentClient.GetBufferAsync failed. Also lots of memory.
        // 4) multistream copyasync
        public async Task <Dictionary <string, double> > Materialize(string localDestination)
        {
            var uniqueblobs = _files.GroupBy(keySelector: file => file.Blob.Id, resultSelector: (key, file) => file).ToList();
            var metrics     = new Dictionary <string, double>
            {
                ["files"]       = _files.Count,
                ["uniqueblobs"] = uniqueblobs.Count
            };

            if (_computeDockerHashes)
            {
                ComputeDockerHashes(localDestination, metrics);
            }

            var dltimes   = new ConcurrentBag <double>();
            var copytimes = new ConcurrentBag <double>();
            var filesize  = new ConcurrentBag <double>();
            var throttler = new ActionBlock <IEnumerable <VstsFile> >(list => DownloadGrouping(list, localDestination, dltimes, copytimes, filesize), new ExecutionDataflowBlockOptions {
                MaxDegreeOfParallelism = _concurrentDownloads
            });

            foreach (var grouping in uniqueblobs)
            {
                throttler.Post(grouping);
            }

            throttler.Complete();
            await throttler.Completion;

            if (dltimes.Any())
            {
                metrics["DownloadCount"]       = dltimes.Count();
                metrics["AverageDownloadSecs"] = dltimes.Average();
                metrics["MaxDownloadSecs"]     = dltimes.Max();
                CalculatePercentile(metrics, dltimes, "Download");
            }

            if (copytimes.Any())
            {
                metrics["CopyCount"]       = copytimes.Count();
                metrics["AverageCopySecs"] = copytimes.Average();
                metrics["MaxCopySecs"]     = copytimes.Max();
                CalculatePercentile(metrics, copytimes, "CopyTime");
            }

            return(metrics);
        }
        private async Task RunTenantsAsync(Func <IServiceProvider, Task> actionAsync, IConfiguration config)
        {
            var output = new ConcurrentBag <IList <string> >();
            var done   = new ConcurrentBag <string>();

            var tenants = Settings.GetInstance(config).Tenants;

            await tenants
            .Where(x => Tenant == null || x == Tenant)
            .OrderBy(x => x)
            .ForEachAsync(7,
                          async tenant =>
            {
                var tenantOutput = new List <string>();

                using (var serviceScope = GetServiceScope(config, tenant))
                {
                    LogHelper.Debug($"Starting tenant: {tenant}");

                    try
                    {
                        var provider = serviceScope.ServiceProvider;

                        await actionAsync(provider);
                    }
                    catch (Exception e)
                    {
                        LogHelper.Error($"Error: {e.Message}", e);
                        tenantOutput.Add($"Error: {e.Message}");
                    }
                }

                if (tenantOutput.Any())
                {
                    tenantOutput.Insert(0, $"\r\nTenant: {tenant}");
                    output.Add(tenantOutput);
                }

                done.Add(tenant);
                LogHelper.Debug($"End tenant: {tenant}. Missing: {tenants.Count - done.Count}");
            });

            if (output.Any())
            {
                LogHelper.Debug(string.Join(Environment.NewLine, output.SelectMany(x => x)));
            }
        }
Пример #13
0
        public void Subscribe(ISubscriber subscriber)
        {
            if (ReferenceEquals(null, subscriber))
            {
                throw new ArgumentNullException(nameof(subscriber));
            }
            if (subscriber.GetInvolvedMessageTypes().Any() == false)
            {
                throw new ArgumentException($"Subscirber '{subscriber.Id}' does not care about any message types. Any reason?");
            }
            if (subscribers.Any(x => x.Id == subscriber.Id))
            {
                throw new ArgumentException($"There is already subscriber with id '{subscriber.Id}'");
            }

            subscribers.Add(subscriber);
        }
        private void AssertNoViolationsOfUnsupportedTermInRazorFiles(string unsupportedTerm)
        {
            var violations = new ConcurrentBag <Tuple <int, string, string> >();
            var razorFiles = GetRazorFiles();

            // Catch working directory issues.
            Assert.NotEmpty(razorFiles);

            Parallel.ForEach(
                razorFiles,
                file =>
            {
                var lineNumber = 0;
                foreach (var line in File.ReadLines(file))
                {
                    lineNumber++;

                    if (line.Contains(unsupportedTerm))
                    {
                        violations.Add(Tuple.Create(lineNumber, file, line.TrimStart(' ').TrimEnd(' ')));
                    }
                }
            });

            if (violations.Any())
            {
                _testOutputHelper.WriteLine(
                    $"Avoid usage of '{unsupportedTerm}' in .cshtml files! Consider using a method from 'UrlExtensions.cs' to ensure usage of configured 'SiteRoot' setting.");

                // Pretty-print any violations: group by file
                foreach (var violationsInFile in violations.GroupBy(t => t.Item2).OrderBy(g => g.Key))
                {
                    _testOutputHelper.WriteLine($"Violation(s) in file '{violationsInFile.Key}':");

                    // Order by line number
                    foreach (var violation in violationsInFile.OrderBy(v => v.Item1))
                    {
                        _testOutputHelper.WriteLine(
                            $"  Line #{violation.Item1}: \"{violation.Item3}\"");
                    }
                }

                // Fail the test
                Assert.Empty(violations);
            }
        }
        public override void Notify(ConcurrentBag<AMQPQueueMetric> busyQueues,
            ConcurrentBag<AMQPQueueMetric> quietQueues) {
            if (busyQueues.Any()) {
                if (!amqpAdapter.IsConnected)
                    amqpAdapter.Connect();

                Parallel.ForEach(busyQueues, amqpQueueMetric => amqpAdapter.Publish(ScaleMessage.Create(ScaleDirective.Out),
                    exchangeName, amqpQueueMetric.QueueName));
            }

            if (!quietQueues.Any()) return;
            if (!amqpAdapter.IsConnected)
                amqpAdapter.Connect();

            Parallel.ForEach(quietQueues.Where(q => !q.AMQPQueueMetricAnalysisResult.Equals(AMQPQueueMetricAnalysisResult.Stable)), amqpQueueMetric => amqpAdapter.Publish(ScaleMessage.Create(ScaleDirective.In),
                exchangeName, amqpQueueMetric.QueueName));
        }
Пример #16
0
        /// <summary> Whenever a <see cref="Packet"/> is received without a <see cref="PacketReceivedHandler{T}"/> already registered for it, it is placed in a timeout in the sad, lonely corner that is <see cref="receivedUnknownPacketHandlerPackets"/>. Whenever a <see cref="PacketReceivedHandler{P}"/> is registered via the 'Connection.RegisterXXX' methods, we need to search for any lonely packets that can now be handled. </summary>
        private void SearchAndInvokeUnknownHandlerPackets(Delegate del)
        {
            //Retreive the packettype for the given handler.
            Type delegateForPacketType = del.GetType().GenericTypeArguments.FirstOrDefault();

            if (receivedUnknownPacketHandlerPackets.Any(p => p.GetType().Equals(delegateForPacketType)))
            {
                var forwardToDelegatePackets = receivedUnknownPacketHandlerPackets.Where(p => p.GetType().Equals(delegateForPacketType));

                foreach (Packet currentForwardPacket in forwardToDelegatePackets)
                {
                    Logger.Log($"Buffered packet {currentForwardPacket.GetType().Name} received a handler => Forwarding", LogLevel.Information);
                    receivedUnknownPacketHandlerPackets.Remove(currentForwardPacket);
                    HandleDefaultPackets(currentForwardPacket);
                }
            }
        }
Пример #17
0
 /// <summary>
 /// Compress list of file entity objects into array of byte.
 /// </summary>
 /// <param name="files">list of files to compress.</param>
 /// <returns></returns>
 public static byte[] Compress(this ConcurrentBag <FileEntity> files)
 {
     if (files.Any())
     {
         var ms = new MemoryStream();
         using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, leaveOpen: true))
         {
             foreach (var file in files)
             {
                 var entry = archive.Add(file);
             }
         }
         ms.Position = 0; //reset memory stream position.
         return(ms.ReadBytes());
     }
     return(null);
 }
Пример #18
0
        private static void PurgeAccessAttempts()
        {
            var banDate = DateTime.Now.AddMinutes(-1).Ticks;

            foreach (var k in AccessAttempts.Keys)
            {
                var recentAttempts = new ConcurrentBag <long>(AccessAttempts[k].Where(x => x >= banDate));
                if (!recentAttempts.Any())
                {
                    AccessAttempts.TryRemove(k, out _);
                }
                else
                {
                    Interlocked.Exchange(ref recentAttempts, AccessAttempts[k]);
                }
            }
        }
Пример #19
0
        private void CompressFiles(IReadOnlyList <FileInfo> files, bool randomizeCab)
        {
            if (!SB3UGS_Initializer.CheckIsAvailable())
            {
                MessageBox.Show(
                    "SB3UGS has not been found in KK Manager directory or it failed to be loaded. Reinstall KK Manager and try again.",
                    "Compress files", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            LoadingDialog.ShowDialog(this, "Compressing asset bundle files", dialogInterface =>
            {
                dialogInterface.SetMaximum(files.Count);

                var excs            = new ConcurrentBag <Exception>();
                long totalSizeSaved = 0;
                var count           = 0;

                Parallel.ForEach(files, file =>
                {
                    dialogInterface.SetProgress(count++, "Compressing " + file.Name);

                    try
                    {
                        var origSize = file.Length;
                        SB3UGS_Utils.CompressBundle(file.FullName, randomizeCab);
                        file.Refresh();
                        totalSizeSaved += origSize - file.Length;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Failed to compress file {file.FullName} - {ex.Message}");
                        excs.Add(ex);
                    }
                });

                if (excs.Any())
                {
                    MessageBox.Show($"Successfully compressed {files.Count - excs.Count} out of {files.Count} files, see log for details. Saved {FileSize.FromBytes(totalSizeSaved).ToString()}.", "Compress files", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    MessageBox.Show($"Successfully compressed {files.Count} files. Saved {FileSize.FromBytes(totalSizeSaved).ToString()}.", "Compress files", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            });
        }
        public Task <Order> CreateOrder()
        {
            var orderId = string.Empty;

            do
            {
                orderId = GenerateOrderId();
            } while(_orders.Any(o => string.Equals(o.Id, orderId, StringComparison.OrdinalIgnoreCase)));

            var order = new Order {
                Id = orderId
            };

            this._orders.Add(order);

            return(Task.FromResult(order));
        }
Пример #21
0
        private IEnumerable <BcrLine> RunBcr(IEnumerable <Report> reports)
        {
            var bag = new ConcurrentBag <BcrLine>();

            var extraReportsToRun = new ConcurrentBag <Report>();

            Parallel.ForEach(
                reports,
                new ParallelOptions {
                MaxDegreeOfParallelism = 3
            },
                t =>
            {
                try
                {
                    var bcrLines = RunBcr(t);
                    foreach (var line in bcrLines)
                    {
                        bag.Add(line);
                    }
                }
                catch (Exception)
                {
                    if (t.ShouldFallBack)
                    {
                        var fallbackReports = t.FallbackReports().ToList();
                        _log.Info(
                            string.Format(
                                "Error getting BCR for {0}. Will fallback to {1}:{2}",
                                t.Parameter,
                                string.Join(
                                    Environment.NewLine,
                                    fallbackReports.Select(x => x.Parameter).ToArray()),
                                Environment.NewLine));
                        fallbackReports.ForEach(r => extraReportsToRun.Add(r));
                    }
                }
            });

            if (extraReportsToRun.Any())
            {
                return(bag.Concat(RunBcr(extraReportsToRun)));
            }

            return(bag);
        }
 /// <summary>
 /// Return a list of found local Assemblies excluding the known assemblies we don't want to scan
 /// and exluding the ones passed in and excluding the exclusion list filter, the results of this are
 /// cached for perforance reasons.
 /// </summary>
 /// <param name="excludeFromResults"></param>
 /// <returns></returns>
 internal static IEnumerable <Assembly> GetAssembliesWithKnownExclusions(
     IEnumerable <Assembly> excludeFromResults = null)
 {
     if (LocalFilteredAssemblyCache.Any())
     {
         return(LocalFilteredAssemblyCache);
     }
     using (new WriteLock(LocalFilteredAssemblyCacheLocker))
     {
         var assemblies = GetFilteredAssemblies(excludeFromResults, KnownAssemblyExclusionFilter);
         foreach (var assembly in assemblies)
         {
             LocalFilteredAssemblyCache.Add(assembly);
         }
     }
     return(LocalFilteredAssemblyCache);
 }
Пример #23
0
        private static void ExtractStandaloneLibraries(string path)
        {
            var result = CyPhyComponentAuthoring.Modules
                         .EDAModelImport
                         .GetDevicesInEagleModel(path);

            ConcurrentBag <Tuple <String, Exception> > cb_exceptions = new ConcurrentBag <Tuple <String, Exception> >();
            ConcurrentBag <Tuple <String, avm.schematic.SchematicModel> > cb_schematics = new ConcurrentBag <Tuple <string, avm.schematic.SchematicModel> >();

            Parallel.ForEach(result, device =>
            {
                var parts  = device.Split('\\');
                var lib    = parts[0];
                var devSet = parts[1];
                var dev    = parts[2];

                try
                {
                    var deviceXML = CyPhyComponentAuthoring.Modules
                                    .EDAModelImport
                                    .GetEagleDevice(path, lib, devSet, dev);

                    var standaloneXML = CyPhyComponentAuthoring.Modules
                                        .EDAModelImport
                                        .ExtractStandaloneLibrary(deviceXML);
                }
                catch (Exception e)
                {
                    cb_exceptions.Add(new Tuple <String, Exception>(device, e));
                }
            });

            if (cb_exceptions.Any())
            {
                String msg = String.Format("Exceptions encountered when extracting libraries for {0} device(s):"
                                           + Environment.NewLine + Environment.NewLine,
                                           cb_exceptions.Count);
                foreach (var tuple in cb_exceptions)
                {
                    msg += String.Format("{0}: {1}" + Environment.NewLine + Environment.NewLine,
                                         tuple.Item1,
                                         tuple.Item2);
                }
                Assert.True(false, msg);
            }
        }
Пример #24
0
        private void ProcessUpdatedMerchandiseTypeProductPids()
        {
            if (!_updatedMerchandiseTypeProductPids.Any())
            {
                Log.Info("There were no products that required merch types to be updated inside CJ table.");
                return;
            }

            var          query = string.Empty;
            var          time  = DateTime.Now;
            const string insertStatementFormat = "UPDATE [dbo].[tbl_CJ_Products] SET [CJ_MERCHANDISETYPE] = '{0}', [DATECHANGED] = '{1}' WHERE [PID] = {2} ";

            query = _updatedMerchandiseTypeProductPids.Aggregate(query, (current, updatedMerchandiseTypeProductPid) => current + string.Format(insertStatementFormat, ZeroCommissionListName, time.ToString("yyyy-MM-dd HH:mm:ss"), updatedMerchandiseTypeProductPid));

            DbUtils.ExecuteTransaction("BatchDB", query, null);
            Log.InfoFormat("There were {0} products whose merch types were updated inside CJ table.", _updatedMerchandiseTypeProductPids.Count);
        }
Пример #25
0
        private void InitializeTestClassLookup()
        {
            _testCompilationUnitSyntaxList = SelectTestProject.GetCSharpClassDeclarationsFromProject();

            if (!_testCompilationUnitSyntaxList.Any())
            {
                TestClassLookup.Visibility = Visibility.Hidden;
                MessageBoxService.Show(NoAnyClassFound);
            }
            else
            {
                var id = 0;
                _testLookUpDataSource = _testCompilationUnitSyntaxList
                                        .SelectMany(cu => cu.CompilationUnitSyntax.DescendantNodes <ClassDeclarationSyntax>(),
                                                    (cu, classDeclarationSyntax) => new TestClassDetail
                {
                    Id                   = 0,
                    FullName             = $"{cu.CompilationUnitSyntax.NameSpace()}.{classDeclarationSyntax.Identifier.Text}",
                    FilePath             = cu.FileName,
                    TotalNumberOfMethods = classDeclarationSyntax.DescendantNodes <MethodDeclarationSyntax>().Count,
                    Claz                 = new ClassDeclaration(classDeclarationSyntax)
                }).Where(x => x.TotalNumberOfMethods > 0)
                                        .OrderByDescending(x => x.TotalNumberOfMethods)
                                        .ToList();

                foreach (var testClassDetail in _testLookUpDataSource)
                {
                    testClassDetail.Id = id++;
                }

                TestClassLookup.ItemsSource = new BindingSource
                {
                    DataSource = _testLookUpDataSource
                };

                ResetSelectedIndex(TestClassLookup);

                var selected = _testLookUpDataSource.FirstOrDefault(x => x.FilePath.Equals(_selectedTestClassPath));
                if (!string.IsNullOrWhiteSpace(_selectedTestClassPath) && selected != null)
                {
                    TestClassLookup.SelectedIndex = selected.Id;
                }

                TestClassLookup.Visibility = Visibility.Visible;
            }
        }
            public async Task ExpectForEmitted(Expression <Predicate <DiagnosticMessage> > predicate)
            {
                var asCompiledPredicate = predicate.Compile();

                // May seem hacky but nothing is more painfull to debug than infinite hanging test ...
                for (int i = 0; i < 100; i++)
                {
                    if (Messages.Any(m => asCompiledPredicate(m)))
                    {
                        return;
                    }

                    await Task.Delay(250);
                }

                throw new InvalidOperationException($"Timeout reached before expected event count reached before prediction {predicate} came true, current diagnostics '{String.Join(";", Messages.SelectMany(x => x.Results))}'");
            }
Пример #27
0
        /// <summary>
        /// 释放所有数据库上下文
        /// </summary>
        public void CloseAll()
        {
            if (!dbContexts.Any())
            {
                return;
            }

            foreach (var dbContext in dbContexts)
            {
                var conn = dbContext.Database.GetDbConnection();
                if (conn.State == ConnectionState.Open)
                {
                    var wrapConn = InjectMiniProfiler ? new ProfiledDbConnection(conn, MiniProfiler.Current) : conn;
                    wrapConn.Close();
                }
            }
        }
        public BidirectionalGraph<AssemblyVertex, EquatableEdge<AssemblyVertex>> GenerateAssemblyReferenceGraph(IEnumerable<Regex> exclusions, ConcurrentBag<string> files, bool verbose)
        {
            if (verbose) Console.WriteLine("Processing {0} files.", files.Count);
            var edges = new ConcurrentBag<EquatableEdge<AssemblyVertex>>();
            var current = 0;
            var total = files.Count;
            Parallel.ForEach(files, file =>
                {
                    if (verbose) Console.Write("\rProcessing file: {0} of {1}", ++current, total);
                    AssemblyDefinition assembly;
                    try
                    {
                        assembly = AssemblyDefinition.ReadAssembly(new MemoryStream(_fileSystem.File.ReadAllBytes(file)));
                    }
                    catch (Exception)
                    {
                        if (verbose) Console.WriteLine("Skipping file as it does not appear to be a .Net assembly: {0}", file);
                        return;
                    }
                    foreach (var reference in assembly.MainModule.AssemblyReferences)
                    {
                        var exists = files.Any(f =>
                            {
                                var fileInfo = new FileInfo(f);
                                return reference.Name.Equals(fileInfo.Name.Replace(fileInfo.Extension, ""), StringComparison.OrdinalIgnoreCase);
                            });
                        if (!exists)
                        {
                            string assemblyPath;
                            exists = _gacResolver.AssemblyExists(reference.FullName, out assemblyPath);
                        }
                        var assemblyName = new AssemblyName(assembly.FullName);
                        edges.Add(CreateNewEdge(reference, exists, assemblyName, exclusions));
                    }
                });

            if (verbose) Console.WriteLine();
            if (verbose) Console.WriteLine("Creating Graph...");
            var graph = new BidirectionalGraph<AssemblyVertex, EquatableEdge<AssemblyVertex>>();
            var allVertices = edges.Select(e => e.Source).Concat(edges.Select(e => e.Target));
            var distinctVertices = allVertices.Distinct();
            graph.AddVertexRange(distinctVertices);
            graph.AddEdgeRange(edges);
            return graph;
        }
Пример #29
0
        private void InitializeClassLookup()
        {
            _compilationUnitSyntaxList = SelectSourceCodeProject.GetCSharpClassDeclarationsFromProject();

            if (!_compilationUnitSyntaxList.Any())
            {
                ClassLookup.Visibility = Visibility.Hidden;
                MessageBoxService.Show(NoAnyClassFound);
            }
            else
            {
                var sourceClassDetails = (from cu in _compilationUnitSyntaxList
                                          from classDeclarationSyntax in cu.CompilationUnitSyntax.DescendantNodes <ClassDeclarationSyntax>()
                                          select new SourceClassDetail
                {
                    Id = 0,
                    FullName = $"{cu.CompilationUnitSyntax.NameSpace()}.{classDeclarationSyntax.Identifier.Text}",
                    FilePath = cu.FileName,
                    TotalNumberOfMethods = classDeclarationSyntax.DescendantNodes <MethodDeclarationSyntax>().Count,
                    Claz = new ClassDeclaration(classDeclarationSyntax)
                })
                                         .OrderByDescending(x => x.TotalNumberOfMethods)
                                         .ToList();

                var id = 0;
                foreach (var classDetail in sourceClassDetails)
                {
                    classDetail.Id = id++;
                }

                ClassLookup.ItemsSource = new BindingSource
                {
                    DataSource = sourceClassDetails
                };

                ResetSelectedIndex(ClassLookup);

                var selected = sourceClassDetails.FirstOrDefault(x => x.FilePath.Equals(_selectedSourceClassPath));
                if (!string.IsNullOrWhiteSpace(_selectedSourceClassPath) && selected != null)
                {
                    ClassLookup.SelectedIndex = selected.Id;
                }
                ClassLookup.Visibility = Visibility.Visible;
            }
        }
Пример #30
0
        /// <summary>
        /// Gets subtitles meta.
        /// </summary>
        /// <param name="releaseName">Name of the release.</param>
        /// <param name="language"></param>
        /// <returns>The query result.</returns>
        public override QueryResult <Subtitles> GetSubtitlesMeta(string releaseName, Language language)
        {
            var    subtitles = new Subtitles();
            var    statuses  = new ConcurrentBag <Status>();
            Status status    = Status.Fatal;
            var    tasks     = new List <Task>(Handlers.Count);
            var    sb        = new StringBuilder();

            if (Handlers.Count > 0)
            {
                foreach (var subtitleDb in Handlers)
                {
                    var db     = subtitleDb;
                    var dbTask = Task.Run(
                        () =>
                    {
                        Status dbStatus;
                        try
                        {
                            var meta = db.GetSubtitlesMeta(releaseName, language);
                            dbStatus = meta.Status;
                            if (dbStatus == Status.Success && meta.Data != null && meta.Data.Count > 0)
                            {
                                subtitles.AddRange(meta.Data);
                            }
                        }
                        catch (Exception ex)
                        {
                            Trace.TraceError(Literals.Data_Failed_to_get_subtitles_meta, releaseName, db, ex);
                            dbStatus = Status.Fatal;
                            sb.AppendLine(string.Format(Literals.Data_Failed_to_get_subtitles_meta, releaseName, db, ex.Message));
                        }

                        statuses.Add(dbStatus);
                    });

                    tasks.Add(dbTask);
                }

                Task.WaitAll(tasks.ToArray());
                status = statuses.Any(s => s == Status.Success) ? Status.Success : Status.Failure;
            }

            return(new QueryResult <Subtitles>(status, subtitles, sb.ToString()));
        }
Пример #31
0
        public void Bfs(int start)
        {
            _vertexes[start].Value = 0;
            _currentLayer.Add(start);

            _depth = 0;
            while (_currentLayer.Any())
            {
                _currentLayer
                .AsParallel()
                .ForAll(v => BfsRoutine(_vertexes[v]));

                _currentLayer = _nextLayer;
                _nextLayer    = new ConcurrentBag <int>();

                _depth++;
            }
        }
Пример #32
0
        public void AddReference(IdentifierReference reference)
        {
            if (reference == null || reference.Declaration.Context == reference.Context)
            {
                return;
            }

            if (reference.Context.Parent != _context &&
                !_references.Select(r => r.Context).Contains(reference.Context.Parent) &&
                !_references.Any(r => r.QualifiedModuleName == reference.QualifiedModuleName &&
                                 r.Selection.StartLine == reference.Selection.StartLine &&
                                 r.Selection.EndLine == reference.Selection.EndLine &&
                                 r.Selection.StartColumn == reference.Selection.StartColumn &&
                                 r.Selection.EndColumn == reference.Selection.EndColumn))
            {
                _references.Add(reference);
            }
        }
Пример #33
0
        int GetNextUnprocessedOffset()
        {
            lock (_locker)
            {
                if (!_processingOffsets.Any())
                {
                    _processingOffsets.Add(_offset);

                    return(_offset);
                }

                var next = _processingOffsets.Max() + _limit;

                _processingOffsets.Add(_offset);

                return(next);
            }
        }
Пример #34
0
 protected static string GetKey(ConcurrentBag <string> keys, string key)
 {
     lock (keys)
     {
         // add the contributor with a -n appended to the id
         if (keys.Any(k => k.Equals(key)))
         {
             var newKey = string.Concat(key, "-", keys.Count(k => k.StartsWith(key)));
             keys.Add(newKey);
             return(newKey);
         }
         else
         {
             keys.Add(key);
             return(key);
         }
     }
 }
        private static List<Tuple<String, avm.schematic.eda.EDAModel>> ConvertAllDevices(string path)
        {
            var result = CyPhyComponentAuthoring.Modules
                                                .EDAModelImport
                                                .GetDevicesInEagleModel(path);

            ConcurrentBag<Tuple<String, Exception>> cb_exceptions = new ConcurrentBag<Tuple<String, Exception>>();
            var cb_schematics = new ConcurrentBag<Tuple<string, avm.schematic.eda.EDAModel>>();

            Parallel.ForEach(result, device =>
            {
                var parts = device.Split('\\');
                var lib = parts[0];
                var devSet = parts[1];
                var dev = parts[2];

                try
                {
                    var deviceXML = CyPhyComponentAuthoring.Modules
                                                           .EDAModelImport
                                                           .GetEagleDevice(path, lib, devSet, dev);

                    var avm_schematic = CyPhyComponentAuthoring.Modules
                                                               .EDAModelImport
                                                               .ConvertEagleDeviceToAvmEdaModel(deviceXML);

                    cb_schematics.Add(
                        new Tuple<String, avm.schematic.eda.EDAModel>(
                            device,
                            avm_schematic
                            ));
                }
                catch (Exception e)
                {
                    cb_exceptions.Add(new Tuple<String, Exception>(device, e));
                }
            });

            if (cb_exceptions.Any())
            {
                String msg = String.Format("Exceptions encountered when converting {0} device(s):"
                                           + Environment.NewLine + Environment.NewLine,
                                           cb_exceptions.Count);
                foreach (var tuple in cb_exceptions)
                {
                    msg += String.Format("{0}: {1}" + Environment.NewLine + Environment.NewLine,
                                         tuple.Item1,
                                         tuple.Item2);
                }
                Assert.True(false, msg);
            }

            return cb_schematics.ToList();
        }
Пример #36
0
		private static void TestSendReceiveRequestCore( IPEndPoint endPoint, int count, int concurrency )
		{
			_SetUpFixture.EnsureThreadPoolCapacity();

			using ( var clientTransportManager = new UdpClientTransportManager( new RpcClientConfiguration() { PreferIPv4 = true } ) )
			{
				var connectTask = clientTransportManager.ConnectAsync( endPoint );

				if ( !connectTask.Wait( Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds ) )
				{
					throw new TimeoutException();
				}

				using ( var clientTransport = connectTask.Result )
				{
					for ( int i = 0; i < count; i++ )
					{
						using ( var latch = new CountdownEvent( concurrency ) )
						{
							var ids = Enumerable.Range( i * concurrency, concurrency ).ToArray();
							var args = Enumerable.Repeat( 0, concurrency ).Select( _ => Guid.NewGuid().ToString() ).ToArray();
							var idAndArgs = ids.Zip( args, ( id, arg ) => new { MessageId = id, Guid = arg.ToString() } );
							var requestTable = new ConcurrentDictionary<int, string>();
							var responseTable = new ConcurrentDictionary<int, string>();
							var exceptions = new ConcurrentBag<Exception>();

							if ( !Task.Factory.ContinueWhenAll(
								idAndArgs.Select(
									idAndArg =>
										Task.Factory.StartNew(
											() =>
											{
												var requestContext = clientTransport.GetClientRequestContext();
												requestTable[ idAndArg.MessageId ] = idAndArg.Guid;
												requestContext.SetRequest(
													idAndArg.MessageId,
													"Dummy",
													( responseContext, exception, completedSynchronously ) =>
													{
														try
														{
															if ( exception != null )
															{
																exceptions.Add( exception );
															}
															else
															{
																// Server returns args as array, so store only first element.
																responseTable[ responseContext.MessageId.Value ] = Unpacking.UnpackArray( responseContext.ResultBuffer )[ 0 ].AsString();
															}
														}
														finally
														{
															latch.Signal();
														}
													}
												);
												requestContext.ArgumentsPacker.PackArrayHeader( 1 );
												requestContext.ArgumentsPacker.Pack( idAndArg.Guid );

												return requestContext;
											}
										)
								).ToArray(),
								previouses =>
								{
									var contexts = previouses.Select( previous => previous.Result ).ToArray();
									foreach ( var context in contexts )
									{
										clientTransport.Send( context );
									}
								}
							).ContinueWith(
								previous =>
								{
									if ( previous.IsFaulted )
									{
										throw previous.Exception;
									}

									// receive
									if ( !latch.Wait( Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds ) )
									{
										throw new TimeoutException( "Receive" );
									}

									if ( exceptions.Any() )
									{
										throw new AggregateException( exceptions );
									}

									Assert.That( requestTable.Count, Is.EqualTo( concurrency ) );
									Assert.That( requestTable, Is.EquivalentTo( responseTable ) );
								}
							).Wait( Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds ) )
							{
								throw new TimeoutException();
							}
						}
					}
				}
			}
		}
        public BidirectionalGraph<AssemblyVertex, EquatableEdge<AssemblyVertex>> GenerateAssemblyReferenceGraph(IEnumerable<Regex> exclusions, IEnumerable<Regex> ignoring, ConcurrentBag<string> files, bool verbose, bool checkAssemblyVersionMatch = true)
        {
            if (verbose) Console.WriteLine("Processing {0} files.", files.Count);
            var edges = new ConcurrentBag<EquatableEdge<AssemblyVertex>>();
            var vertices = new List<AssemblyVertex>();
            var ignoreList = ignoring.ToList();
            var excludeList = exclusions.ToList();

            var current = 0;
            var total = files.Count;
            Parallel.ForEach(files, file =>
                {
                    if (verbose) Console.Write("\rProcessing file: {0} of {1}", ++current, total);
                    AssemblyDefinition assembly;
                    try
                    {
                        assembly = AssemblyDefinition.ReadAssembly(new MemoryStream(_fileSystem.File.ReadAllBytes(file)));
                    }       
                    catch (Exception)
                    {
                        if (verbose) Console.WriteLine("Skipping file as it does not appear to be a .Net assembly: {0}", file);
                        return;
                    }
                    //We need to load the assembly to ensure that the assembly name is the same as the file name (to be exact)
                    if (ignoreList.Any(i => i.IsMatch(assembly.Name.Name.ToLowerInvariant())))
                    {
                        if (verbose) Console.WriteLine("Ignoring file: {0}", file);
                        return;
                    }

                    //Check for 32bitness on our source assembly
                    var required32Bit = Required32BitSet(assembly);
                    var assemblyName = new AssemblyName(assembly.FullName);
 
                    //If we dont have references (I know, unlikely right?)
                    if (!assembly.MainModule.AssemblyReferences.Any())
                    {
                        vertices.Add(CreateAssemblyVertex(assemblyName.FullName, assemblyName.Name, true, Required32BitSet(assembly), excludeList));
                        return;
                    }

                    //Otherwise create edges for them...
                    foreach (var reference in assembly.MainModule.AssemblyReferences)
                    {
                        var foundFileMatch = files.Any(f => CheckDependentFileMatchesManifest(f, reference, checkAssemblyVersionMatch, required32Bit));

                        if (!foundFileMatch)
                        {
                            foundFileMatch = _gacResolver.AssemblyExists(reference.FullName);
                        }
                        if (!ignoreList.Any(i => i.IsMatch(reference.Name.ToLowerInvariant())))
                            edges.Add(CreateNewEdge(reference, foundFileMatch, required32Bit, assemblyName, excludeList));
                        else
                            if (verbose) Console.WriteLine("Ignoring: {0}",assemblyName.Name);
                    }
                });

            if (verbose) Console.WriteLine();
            if (verbose) Console.WriteLine("Creating Graph...");
            var graph = new BidirectionalGraph<AssemblyVertex, EquatableEdge<AssemblyVertex>>();
            var allVertices = edges.Select(e => e.Source).Concat(edges.Select(e => e.Target)).Concat(vertices);
            var distinctVertices = allVertices.Distinct();
            graph.AddVertexRange(distinctVertices);
            graph.AddEdgeRange(edges);
            return graph;
        }
        private static void ExtractStandaloneLibraries(string path)
        {
            var result = CyPhyComponentAuthoring.Modules
                                                .EDAModelImport
                                                .GetDevicesInEagleModel(path);
                        
            ConcurrentBag<Tuple<String, Exception>> cb_exceptions = new ConcurrentBag<Tuple<String, Exception>>();
            ConcurrentBag<Tuple<String, avm.schematic.SchematicModel>> cb_schematics = new ConcurrentBag<Tuple<string, avm.schematic.SchematicModel>>();

            Parallel.ForEach(result, device =>
            {
                var parts = device.Split('\\');
                var lib = parts[0];
                var devSet = parts[1];
                var dev = parts[2];

                try
                {
                    var deviceXML = CyPhyComponentAuthoring.Modules
                                                           .EDAModelImport
                                                           .GetEagleDevice(path, lib, devSet, dev);

                    var standaloneXML = CyPhyComponentAuthoring.Modules
                                                               .EDAModelImport
                                                               .ExtractStandaloneLibrary(deviceXML);
                }
                catch (Exception e)
                {
                    cb_exceptions.Add(new Tuple<String, Exception>(device, e));
                }
            });
            
            if (cb_exceptions.Any())
            {
                String msg = String.Format("Exceptions encountered when extracting libraries for {0} device(s):"
                                           + Environment.NewLine + Environment.NewLine,
                                           cb_exceptions.Count);
                foreach (var tuple in cb_exceptions)
                {
                    msg += String.Format("{0}: {1}" + Environment.NewLine + Environment.NewLine,
                                         tuple.Item1,
                                         tuple.Item2);
                }
                Assert.True(false, msg);
            }
        }
Пример #39
0
        /// <summary>
        /// Renvoi la list des departements avec leur employer
        /// </summary>
        /// <returns></returns>
        public List<DepStaffCard> GetDepStaffsCard()
        {
            using (var db = new StationContext())
            {
                var depStaffCardList = new ConcurrentBag<DepStaffCard>();

                var nd = new DepStaffCard("");
                if (nd.StaffsList.Any()) { depStaffCardList.Add(nd); }

                var deps = (db.Staffs.Where(s => !s.Person.IsDeleted).ToList()
                    .Where(s => string.IsNullOrEmpty(s.DepartementPrincipale) == false)
                    .Select(s => s.DepartementPrincipale)).Distinct().ToList();

                Parallel.ForEach(deps, dep =>
                {
                    depStaffCardList.Add(new DepStaffCard(dep));
                });

                return depStaffCardList.Any() ? depStaffCardList.OrderBy(d => d.DepartementName).ToList() : null;
            }
        }
Пример #40
0
        public async void GetMetrics()
        {
            try
            {
                Scope.Connect();
                var datapoints = new ConcurrentBag<DataPoint>();

                if (Counters == null) return; //no work to do.

                Parallel.ForEach(Counters, counter =>
                {
                    var datapoint = GetDataPoints(counter);
                    if (datapoint != null)
                    {
                        datapoint.ForEach(datapoints.Add);
                    }
                });


                if (datapoints.Any())
                {
                    await _stackDriverPoster.SendBatchMetricsAsync(datapoints);
                }
                else
                {
                    Log.Debug("No metrics were reported during this cycle.");
                }
            }
            catch (Exception ex)
            {
                Log.Error("Exception in metric timer ", ex);
            }
        }
        /// <summary>
        /// Converts some given data into a listing instance.
        /// </summary>
        /// <param name="data">some data source, like Xml data or json data.</param>
        /// <param name="areBadCharactersRemoved">Help clean up the data.</param>
        /// <param name="isClearAllIsModified">After the data is loaded, do we clear all IsModified fields so it looks like the listing(s) are all ready to be used and/or compared against other listings.</param>
        /// /// <returns>List of listings and any unhandled data.</returns>
        public ConvertToResult ConvertTo(string data, 
            bool areBadCharactersRemoved = false,
            bool isClearAllIsModified = false)
        {
            data.ShouldNotBeNullOrEmpty();

            // Remove any BOM if one exists.
            // REF: http://stackoverflow.com/questions/5098757/how-to-tell-asciiencoding-class-not-to-decode-the-byte-order-mark
            data = RemoveByteOrderMark(data);

            var validationErrorMessage = ValidateXmlString(data);
            if (!string.IsNullOrWhiteSpace(validationErrorMessage))
            {
                if (!areBadCharactersRemoved)
                {
                    return new ConvertToResult
                    {
                        Errors = new List<ParsedError>
                    {
                        new ParsedError(validationErrorMessage, "The entire data source.")}
                    };
                }

                // Some bad data occurs, so lets clean any bad data out.
                data = RemoveInvalidXmlChars(data);
            }

            // Now split it up into the known listing types.
            SplitElementResult elements;

            try
            {
                elements = SplitReaXmlIntoElements(data);
            }
            catch (Exception exception)
            {
                return new ConvertToResult
                {
                    Errors = new List<ParsedError>
                    {
                        new ParsedError(exception.Message, 
                            "Failed to parse the provided xml data because it contains some invalid data. Pro Tip: This is usually because a character is not encoded. Like an ampersand.")
                    }
                };
            }
            
            if (!elements.KnownXmlData.Any() &&
                !elements.UnknownXmlData.Any())
            {
                return null;
            }

            // Finally, we convert each segment into a listing.
            var successfullyParsedListings = new ConcurrentBag<ListingResult>();
            var invalidData = new ConcurrentBag<ParsedError>();

            Parallel.ForEach(elements.KnownXmlData, element =>
            {
                try
                {
                    successfullyParsedListings.Add(new ListingResult
                    {
                        Listing = ConvertFromReaXml(element, DefaultCultureInfo, AddressDelimeter, isClearAllIsModified),
                        SourceData = element.ToString()
                    });
                }
                catch (Exception exception)
                {
                    invalidData.Add(new ParsedError(exception.Message, element.ToString()));
                }
            });

            return new ConvertToResult
            {
                Listings = successfullyParsedListings.Any()
                    ? successfullyParsedListings.ToList()
                    : null,
                UnhandledData = elements.UnknownXmlData != null &&
                                elements.UnknownXmlData.Any()
                    ? elements.UnknownXmlData.Select(x => x.ToString()).ToList()
                    : null,
                Errors = invalidData.Any()
                    ? invalidData.ToList()
                    : null
            };
        }
Пример #42
0
        internal override async Task<CodeAction> GetFixAsync(
            ImmutableDictionary<Project, ImmutableArray<Diagnostic>> projectsAndDiagnosticsToFixMap,
            FixAllState fixAllState, CancellationToken cancellationToken)
        {
            if (projectsAndDiagnosticsToFixMap != null && projectsAndDiagnosticsToFixMap.Any())
            {
                FixAllLogger.LogDiagnosticsStats(projectsAndDiagnosticsToFixMap);

                var fixesBag = new ConcurrentBag<CodeAction>();

                using (Logger.LogBlock(FunctionId.CodeFixes_FixAllOccurrencesComputation_Fixes, cancellationToken))
                {
                    var projects = projectsAndDiagnosticsToFixMap.Keys;
                    var tasks = projects.Select(p => AddProjectFixesAsync(p, projectsAndDiagnosticsToFixMap[p], fixesBag.Add, fixAllState, cancellationToken))
                                        .ToArray();
                    await Task.WhenAll(tasks).ConfigureAwait(false);
                }

                if (fixesBag.Any())
                {
                    using (Logger.LogBlock(FunctionId.CodeFixes_FixAllOccurrencesComputation_Merge, cancellationToken))
                    {
                        FixAllLogger.LogFixesToMergeStats(fixesBag);
                        return await TryGetMergedFixAsync(fixesBag, fixAllState, cancellationToken).ConfigureAwait(false);
                    }
                }
            }

            return null;
        }
Пример #43
0
		private static void TestSendNotifyCore( IPEndPoint endPoint, CountdownEvent arrivalLatch, IProducerConsumerCollection<string> arrivedIds, int count )
		{
			using ( var clientTransportManager = new UdpClientTransportManager( new RpcClientConfiguration() { PreferIPv4 = true } ) )
			using ( var connectTask = clientTransportManager.ConnectAsync( endPoint ) )
			{
				if ( !connectTask.Wait( Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds ) )
				{
					throw new TimeoutException();
				}

				using ( var clientTransport = connectTask.Result )
				{
					for ( int i = 0; i < count; i++ )
					{
						if ( arrivalLatch != null )
						{
							arrivalLatch.Reset();
						}

						var args = Enumerable.Repeat( 0, arrivalLatch.InitialCount ).Select( _ => Guid.NewGuid().ToString() ).ToArray();
						var exceptions = new ConcurrentBag<Exception>();

						if ( !Task.Factory.ContinueWhenAll(
							args.Select(
								arg =>
									Task.Factory.StartNew(
										() =>
										{
											var requestContext = clientTransport.GetClientRequestContext();
											requestContext.SetNotification(
												"Dummy",
												( exception, completedSynchronously ) =>
												{
													if ( exception != null )
													{
														exceptions.Add( exception );
													}

													arrivalLatch.Signal();
												}
											);
											requestContext.ArgumentsPacker.PackArrayHeader( 1 );
											requestContext.ArgumentsPacker.Pack( arg );

											return requestContext;
										}
									)
							).ToArray(),
							previouses =>
							{
								var contexts = previouses.Select( previous => previous.Result ).ToArray();
								foreach ( var context in contexts )
								{
									clientTransport.Send( context );
								}
							}
						).ContinueWith(
							previous =>
							{
								if ( previous.IsFaulted )
								{
									throw previous.Exception;
								}

								// receive
								if ( !arrivalLatch.Wait( Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds ) )
								{
									throw new TimeoutException( "Receive" );
								}

								if ( exceptions.Any() )
								{
									throw new AggregateException( exceptions );
								}
							}
						).Wait( Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds ) )
						{
							throw new TimeoutException();
						}
					}
				}
			}
		}
Пример #44
0
        public async Task<List<ColonneCard>> GetColonnesCard()
        {
            return await Task.Run(() =>
            {
                using (var db = new StationContext())
                {
                    var cardList = new ConcurrentBag<ColonneCard>();

                    var nd = new ColonneCard("");
                    if (nd.Pompes.Any())
                        cardList.Add(nd);

                    var cols = (db.Pompes.Where(s => !s.IsDeleted).ToList()
                        .Where(s => !string.IsNullOrEmpty(s.Colonne))
                        .Select(s => s.Colonne.ToLower())).Distinct().ToList();

                    Parallel.ForEach(cols, dep => cardList.Add(new ColonneCard(dep)));

                    return cardList.Any() ? cardList.OrderBy(d => d.Libel).ToList() : null;
                }
            });
        }
Пример #45
0
        public override async Task ListContacts(ContactQuery query)
        {

            ContactLite[] returnUsers = null;
            Task.Run(async () =>
            {
                var bExceptionRaised = false;
                try
                {
                    var finallist = new ConcurrentBag<ContactLite>();
                    var agenda = await ContactManager.RequestStoreAsync();
                    var foundContactList = await agenda.FindContactsAsync();

                    if (query != null)
                    {
                        WindowsPhoneUtils.Log("Listing contacts by query: " + query);
                        FilterReturnedContacts(foundContactList, finallist, query);
                    }
                    else
                    {
                        WindowsPhoneUtils.Log("Listing ALL contacts...");
                        foundContactList.AsParallel()
                        .ForAll(contact => finallist.Add(new ContactLite
                        {
                            ID = contact.Id.Replace("{", "").Replace(".", "").Replace("}", ""),
                            DisplayName = contact.DisplayName,
                            Name = contact.FirstName,
                            Firstname = contact.MiddleName,
                            Lastname = contact.LastName,
                            Phones = GetContactPhonesArray(contact),
                            Emails = GetContactEmailArray(contact)
                        }));
                    }
                    returnUsers = finallist.ToArray();
                }
                catch (UnauthorizedAccessException ex)
                {
                    WindowsPhoneUtils.Log("Not enough privileges to access Contacts");
                    WindowsPhoneUtils.InvokeCallback(AccessDeniedContactsCallback, WindowsPhoneUtils.CALLBACKID, null);
                    return;
                }
                catch (Exception ex)
                {
                    //error
                    bExceptionRaised = true;
                }

                if (bExceptionRaised)
                {
                    try
                    {
                        _faultyLetters = new ConcurrentBag<string>();
                        _startingLetters = new ConcurrentBag<string>();
                        _finalUsers.Clear();
                        await StartContactSeach(String.Empty, query);
                        returnUsers = _finalUsers.Values.ToArray();


                        _startingLetters.AsParallel().ForAll(startingLetter =>
                        {
                            if (!_faultyLetters.Any(
                                    faultySilabe =>
                                        faultySilabe.StartsWith(startingLetter,
                                            StringComparison.CurrentCultureIgnoreCase)))
                                _faultyLetters.Add(startingLetter);
                        });

                        WindowsPhoneUtils.InvokeCallback(WpContactsFailedCallback, WindowsPhoneUtils.CALLBACKID, JsonConvert.SerializeObject(_faultyLetters.OrderBy(x => x).ToArray()));
                    }
                    catch (Exception ex)
                    {
                        //UNHANDLED ERROR
                        WindowsPhoneUtils.Log("Unhandled error recovering contacts:" + ex.Message);
                    }
                }
                WindowsPhoneUtils.InvokeCallback(ListContactsCallback, WindowsPhoneUtils.CALLBACKID, JsonConvert.SerializeObject(returnUsers));
            });
        }
Пример #46
0
        /// <summary>
        /// Yes, copying the byte array to here. But given we'll not have many of these tasks going to parallel
        /// and each byte array is AT MOST 4M, I think I can live with the memory overhead.
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="bytesRead"></param>
        /// <param name="bytesToRead"></param>
        /// <param name="blob"></param>
        /// <param name="uploadedBlockList"></param>
        /// <param name="testMode"></param>
        /// <returns></returns>
        private Task WriteBytes(long offset, int bytesRead, byte[] bytesToRead, CloudBlockBlob blob, ConcurrentBag<UploadedBlock> uploadedBlockList, bool testMode)
        {
            var t = Task.Factory.StartNew(() =>
                {
                    var sig = CommonOps.GenerateBlockSig(bytesToRead, offset, (int)bytesRead, 0);
                    var blockId = Convert.ToBase64String(sig.MD5Signature);

                    bool isDupe = false;
                    lock (parallelLock)
                    {

                        isDupe = uploadedBlockList.Any(ub => ub.BlockId == blockId);

                        // store the block id that is associated with this byte range.
                        uploadedBlockList.Add(new UploadedBlock()
                        {
                            BlockId = blockId,
                            Offset = offset,
                            Sig = sig,
                            Size = bytesRead,
                            IsNew = true,
                            IsDuplicate = isDupe
                        });

                    }

                    if (!testMode)
                    {
                        if (!isDupe)
                        {
                            // yes, putting into memory stream is probably a waste here.
                            using (var ms = new MemoryStream(bytesToRead))
                            {
                                var options = new BlobRequestOptions() { ServerTimeout = new TimeSpan(0, 90, 0) };
                                blob.PutBlock(blockId, ms, null, null, options);

                            }
                        }
                    }
              });

            return t;
        }
Пример #47
0
        public void DCAwareRoundRobinPolicyWithNodesChanging()
        {
            var hostList = new ConcurrentBag<Host>
            {
                TestHelper.CreateHost("0.0.0.1", "dc1"),
                TestHelper.CreateHost("0.0.0.2", "dc2"),
                TestHelper.CreateHost("0.0.0.3", "dc1"),
                TestHelper.CreateHost("0.0.0.4", "dc2"),
                TestHelper.CreateHost("0.0.0.5", "dc1"),
                TestHelper.CreateHost("0.0.0.6", "dc2"),
                TestHelper.CreateHost("0.0.0.7", "dc1"),
                TestHelper.CreateHost("0.0.0.8", "dc2"),
                TestHelper.CreateHost("0.0.0.9", "dc1"),
                TestHelper.CreateHost("0.0.0.10", "dc2")
            };
            const string localDc = "dc1";
            //to remove the host 3
            var hostToRemove = hostList.First(h => TestHelper.GetLastAddressByte(h) == 3);
            var clusterMock = new Mock<ICluster>();
            clusterMock
                .Setup(c => c.AllHosts())
                .Returns(() =>
                {
                    return hostList.ToList();
                });
            //Initialize the balancing policy
            var policy = new DCAwareRoundRobinPolicy(localDc, 1);
            policy.Initialize(clusterMock.Object);

            var hostYielded = new ConcurrentBag<IEnumerable<Host>>();
            Action action = () => hostYielded.Add(policy.NewQueryPlan(null, null).ToList());

            //Invoke without nodes changing
            TestHelper.ParallelInvoke(action, 100);
            Assert.True(hostYielded.Any(hl => hl.Any(h => h == hostToRemove)));

            var actionList = new List<Action>(Enumerable.Repeat<Action>(action, 1000));


            actionList.Insert(200, () =>
            {
                var host = TestHelper.CreateHost("0.0.0.11", "dc1");
                //raise event and then add
                clusterMock.Raise(c => c.HostAdded += null, host);
                hostList.Add(host);
            });
            actionList.Insert(400, () =>
            {
                var host = TestHelper.CreateHost("0.0.0.12", "dc1");
                //first add and then raise event
                hostList.Add(host);
                clusterMock.Raise(c => c.HostAdded += null, host);
            });
            
            actionList.Insert(400, () =>
            {
                var host = hostToRemove;
                hostList = new ConcurrentBag<Host>(hostList.Where(h => h != hostToRemove));
                clusterMock.Raise(c => c.HostRemoved += null, host);
            });

            //Invoke it with nodes being modified
            TestHelper.ParallelInvoke(actionList);
            //Clear the host yielded so far
            hostYielded = new ConcurrentBag<IEnumerable<Host>>();
            //Invoke it a some of times more in parallel
            TestHelper.ParallelInvoke(action, 100);
            //The removed node should not be returned
            Assert.False(hostList.Any(h => h == hostToRemove));
            Assert.False(hostYielded.Any(hl => hl.Any(h => h == hostToRemove)));
        }