Beispiel #1
0
        public void TestCoverage()
        {
            string module = GetType().Assembly.Location;
            string pdb    = Path.Combine(Path.GetDirectoryName(module), Path.GetFileNameWithoutExtension(module) + ".pdb");

            var directory = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()));

            File.Copy(module, Path.Combine(directory.FullName, Path.GetFileName(module)), true);
            File.Copy(pdb, Path.Combine(directory.FullName, Path.GetFileName(pdb)), true);

            // TODO: Mimic hits by calling ModuleTrackerTemplate.RecordHit before Unload

            // Since Coverage only instruments dependancies, we need a fake module here
            var testModule = Path.Combine(directory.FullName, "test.module.dll");

            var coverage = new Coverage(testModule, Array.Empty <string>(), Array.Empty <string>(), Array.Empty <string>(), Array.Empty <string>(), Array.Empty <string>(), string.Empty, false);

            coverage.PrepareModules();

            // The module hit tracker must signal to Coverage that it has done its job, so call it manually
            var instrumenterResult = coverage.Results.Single();

            ModuleTrackerTemplate.HitsArray         = new int[instrumenterResult.HitCandidates.Count + ModuleTrackerTemplate.HitsResultHeaderSize];
            ModuleTrackerTemplate.HitsFilePath      = instrumenterResult.HitsFilePath;
            ModuleTrackerTemplate.HitsMemoryMapName = instrumenterResult.HitsResultGuid;
            ModuleTrackerTemplate.UnloadModule(null, null);

            var result = coverage.GetCoverageResult();

            Assert.NotEmpty(result.Modules);

            directory.Delete(true);
        }
        public void HitsFileCorrectlyWritten()
        {
            ModuleTrackerTemplate.HitsArray = new[] { 1, 2, 0, 3 };
            ModuleTrackerTemplate.UnloadModule(null, null);

            var expectedHitsArray = new[] { 1, 2, 0, 3 };

            Assert.Equal(expectedHitsArray, ReadHitsFile());
        }
 public void HitsFileWithDifferentNumberOfEntriesCausesExceptionOnUnload()
 {
     FunctionExecutor.Run(() =>
     {
         using var ctx = new TrackerContext();
         WriteHitsFile(new[] { 1, 2, 3 });
         ModuleTrackerTemplate.HitsArray = new[] { 1 };
         Assert.Throws <InvalidOperationException>(() => ModuleTrackerTemplate.UnloadModule(null, null));
         return(s_success);
     });
 }
        public void MultipleSequentialUnloadsHaveCorrectTotalData()
        {
            ModuleTrackerTemplate.HitsArray = new[] { 0, 3, 2, 1 };
            ModuleTrackerTemplate.UnloadModule(null, null);

            ModuleTrackerTemplate.HitsArray = new[] { 0, 1, 2, 3 };
            ModuleTrackerTemplate.UnloadModule(null, null);

            var expectedHitsArray = new[] { 0, 4, 4, 4 };

            Assert.Equal(expectedHitsArray, ReadHitsFile());
        }
        public void HitsFileCorrectlyWritten()
        {
            FunctionExecutor.Run(() =>
            {
                using var ctx = new TrackerContext();
                ModuleTrackerTemplate.HitsArray = new[] { 1, 2, 0, 3 };
                ModuleTrackerTemplate.UnloadModule(null, null);

                int[] expectedHitsArray = new[] { 1, 2, 0, 3 };
                Assert.Equal(expectedHitsArray, ReadHitsFile());

                return(s_success);
            });
        }
        public void HitsFileCorrectlyWritten()
        {
            using var invoker = RemoteExecutor.Invoke(_ =>
            {
                using var ctx = new TrackerContext();
                ModuleTrackerTemplate.HitsArray = new[] { 1, 2, 0, 3 };
                ModuleTrackerTemplate.UnloadModule(null, null);

                var expectedHitsArray = new[] { 1, 2, 0, 3 };
                Assert.Equal(expectedHitsArray, ReadHitsFile());

                return(_success);
            });
        }
Beispiel #7
0
        public void HitsOnMultipleThreadsCorrectlyCounted()
        {
            FunctionExecutor.Run(() =>
            {
                using var ctx = new TrackerContext();
                ModuleTrackerTemplate.HitsArray = new[] { 0, 0, 0, 0 };
                for (int i = 0; i < ModuleTrackerTemplate.HitsArray.Length; ++i)
                {
                    var t = new Thread(HitIndex);
                    t.Start(i);
                }

                ModuleTrackerTemplate.UnloadModule(null, null);
                var expectedHitsArray = new[] { 4, 3, 2, 1 };
                Assert.Equal(expectedHitsArray, ReadHitsFile());
        public async void MutexBlocksMultipleWriters()
        {
            using (var mutex = new Mutex(
                       true, Path.GetFileNameWithoutExtension(ModuleTrackerTemplate.HitsMemoryMapName) + "_Mutex", out bool createdNew))
            {
                Assert.True(createdNew);

                RecordHits(0, 1, 2, 3);
                var unloadTask = Task.Run(() => ModuleTrackerTemplate.UnloadModule(null, null));

                Assert.False(unloadTask.Wait(5));

                var expectedHitsArray = new[] { 0, 0, 0, 0 };
                Assert.Equal(expectedHitsArray, ReadHits(0));

                mutex.ReleaseMutex();
                await unloadTask;

                expectedHitsArray = new[] { 0, 1, 2, 3 };
                Assert.Equal(expectedHitsArray, ReadHits());
            }
        }
        public void HitsOnMultipleThreadsCorrectlyCounted()
        {
            ModuleTrackerTemplate.HitsArray = new[] { 0, 0, 0, 0 };
            for (int i = 0; i < ModuleTrackerTemplate.HitsArray.Length; ++i)
            {
                var t = new Thread(HitIndex);
                t.Start(i);
            }

            ModuleTrackerTemplate.UnloadModule(null, null);
            var expectedHitsArray = new[] { 4, 3, 2, 1 };

            Assert.Equal(expectedHitsArray, ReadHitsFile());

            void HitIndex(object index)
            {
                var hitIndex = (int)index;

                for (int i = 0; i <= hitIndex; ++i)
                {
                    ModuleTrackerTemplate.RecordHit(i);
                }
            }
        }
 public void HitsFileWithDifferentNumberOfEntriesCausesExceptionOnUnload()
 {
     WriteHitsFile(new[] { 1, 2, 3 });
     ModuleTrackerTemplate.HitsArray = new[] { 1 };
     Assert.Throws <InvalidOperationException>(() => ModuleTrackerTemplate.UnloadModule(null, null));
 }