Exemplo n.º 1
0
        public async Task <StoredMutantInfo> StoreMutant(Mutant mutant)
        {
            mutant.State = MutantResultState.Creating;

            var mutationResult = await _mutantsCache.GetMutatedModulesAsync(mutant);

            mutant.State = MutantResultState.Writing;

            var clone = await _clonesManager.CreateCloneAsync("InitTestEnvironment");

            var info = new StoredMutantInfo(clone);


            if (mutationResult.MutatedModules != null)
            {
                //AKB

                /*foreach (ICciModuleSource mutatedModule in mutationResult.MutatedModules)
                 * {*/
                var singleMutated = mutationResult.MutatedModules.Modules.SingleOrDefault();
                if (singleMutated != null)
                {
                    //TODO: remove: assemblyDefinition.Name.Name + ".dll", use factual original file name
                    string file = Path.Combine(info.Directory, singleMutated.Name + ".dll");
                    //
                    //                    var memory = mutationResult.MutatedModules.WriteToStream(singleMutated);
                    //                    // _mutantsCache.Release(mutationResult);
                    //
                    //                    using (FileStream peStream = File.Create(file))
                    //                    {
                    //                        await memory.CopyToAsync(peStream);
                    //                    }
                    using (FileStream peStream = File.Create(file))
                    {
                        mutationResult.MutatedModules.WriteToStream(singleMutated, peStream, file);

                        //  await memory.CopyToAsync(peStream);
                    }

                    info.AssembliesPaths.Add(file);
                }

                var otherModules = _originalCodebase.Modules
                                   .Where(_ => singleMutated == null || _.Module.Name != singleMutated.Name);

                foreach (var otherModule in otherModules)
                {
                    string file = Path.Combine(info.Directory, otherModule.Module.Name + ".dll");
                    info.AssembliesPaths.Add(file);
                }
                //}
            }
            else
            {
                foreach (var cciModuleSource in mutationResult.Old)
                {
                    var module = cciModuleSource.Modules.Single();
                    //TODO: remove: assemblyDefinition.Name.Name + ".dll", use factual original file name
                    string file = Path.Combine(info.Directory, module.Name + ".dll");


                    // _mutantsCache.Release(mutationResult);

                    using (FileStream peStream = File.Create(file))
                    {
                        cciModuleSource.WriteToStream(module, peStream, file);
                        //  await memory.CopyToAsync(peStream);
                    }

                    info.AssembliesPaths.Add(file);
                }
            }


            return(info);
        }
Exemplo n.º 2
0
        public async Task Initialize()
        {
            _error = null;
            _paths = new BlockingCollection <ProjectFilesClone>();
            ProjectFilesClone[] projectFilesClones = await Task.WhenAll(
                Enumerable.Range(0, _threadsCount)
                .Select(i => _fileManager.CreateCloneAsync("WhiteCache-" + i)));

            _filesPool = projectFilesClones.ToList();
            foreach (var projectFilesClone in _filesPool)
            {
                _paths.Add(projectFilesClone);
            }

            ProjectFilesClone filesClone = _paths.Take();

            _mainModules = Task.Run(() =>
            {
                _referenceStrings = new List <string>();
                var task          = filesClone.Assemblies.Select(a =>
                {
                    var cci  = new CciModuleSource(a.Path);
                    cci.Guid = Guid.NewGuid();
                    _log.Debug("Whitecache#" + a.FileName + ": Created initial source: " + cci.Guid);
                    return(cci);
                }).ToList();

                _paths.Add(filesClone);
                return(task);
            });


            new Thread(() =>
            {
                try
                {
                    InitializeModuleNames();


                    foreach (ProjectFilesClone item in _paths.GetConsumingEnumerable())
                    {
                        lock (this)
                        {
                            while (_whiteCaches.All(_ => _.Value.Count >= _maxCount) && !_paths.IsAddingCompleted)
                            {
                                Monitor.Wait(this);
                            }
                        }

                        if (_paths.IsAddingCompleted)
                        {
                            return;
                        }
                        ProjectFilesClone item1 = item;
                        Task.Run(() => TryAdd(item1))
                        .ContinueWith(task =>
                        {
                            _paths.TryAdd(item1);
                            NotifyClients();
                        }).LogErrors();
                    }
                }
                catch (Exception e)
                {
                    _log.Error("Read assembly failed. ", e);
                    _error = e;
                    _paths.CompleteAdding();
                }
            }).Start();
        }