Esempio n. 1
0
 public void GenerateDatabase(GenerateDatabaseOptions options, Action<int> onExit = null) {
     IsCurrentReason = GeneratingReason;
     IsCurrent = false;
     if (_useUpdater) {
         if (_updater != null) {
             _updater.Dispose();
         }
         _updater = new AnalyzerStatusUpdater(_config.Id);
         _updater.WaitForWorkerStarted();
         _updater.ThrowPendingExceptions();
         _updater.UpdateStatus(0, 0);
     }
 }
Esempio n. 2
0
        public void SendUpdates() {
            Dictionary<string, AnalysisProgress> results = null;

            using (var ready = new AutoResetEvent(false))
            using (var listener = new AnalyzerStatusListener(r => { results = r; ready.Set(); })) {
                ready.Reset();
                listener.RequestUpdate();
                ready.WaitOne();
                Assert.IsNotNull(results);
                if (results.Count > 0) {
                    ready.Reset();
                    listener.RequestUpdate();
                    ready.WaitOne();
                    Assert.IsNotNull(results);
                    if (results.Count > 0) {
                        Console.WriteLine("WARNING: {0} results received from a previous test", results.Count);
                        Console.WriteLine("Keys are:");
                        foreach (var key in results.Keys) {
                            Console.WriteLine("    {0}", key);
                        }
                    }
                }

                using (var sender1 = new AnalyzerStatusUpdater("SendUpdates1"))
                using (var sender2 = new AnalyzerStatusUpdater("SendUpdates2")) {
                    // Block until workers have started
                    sender1.WaitForWorkerStarted();
                    sender1.ThrowPendingExceptions();
                    sender2.WaitForWorkerStarted();
                    sender2.ThrowPendingExceptions();

                    ready.Reset();
                    listener.RequestUpdate();
                    ready.WaitOne();
                    Assert.IsNotNull(results);
                    AssertUtil.ContainsAtLeast(results.Keys, "SendUpdates1", "SendUpdates2");
                    Assert.AreEqual(int.MaxValue, results["SendUpdates1"].Progress, "SendUpdates1.Progress not initialized to MaxValue");
                    Assert.AreEqual(0, results["SendUpdates1"].Maximum, "SendUpdates1.Maximum not initialized to 0");
                    Assert.AreEqual(string.Empty, results["SendUpdates1"].Message, "SendUpdates1.Message not initialized to empty");
                    Assert.AreEqual(int.MaxValue, results["SendUpdates2"].Progress, "SendUpdates2.Progress not initialized to MaxValue");
                    Assert.AreEqual(0, results["SendUpdates2"].Maximum, "SendUpdates2.Maximum not initialized to 0");
                    Assert.AreEqual(string.Empty, results["SendUpdates2"].Message, "SendUpdates2.Message not initialized to empty");

                    sender1.UpdateStatus(100, 200, "Message1");
                    sender1.FlushQueue(TimeSpan.FromSeconds(1.0));

                    ready.Reset();
                    listener.RequestUpdate();
                    ready.WaitOne();
                    Assert.AreEqual(100, results["SendUpdates1"].Progress, "SendUpdates1.Progress not set to 100");
                    Assert.AreEqual(200, results["SendUpdates1"].Maximum, "SendUpdates1.Maximum not set to 200");
                    Assert.AreEqual("Message1", results["SendUpdates1"].Message, "SendUpdates1.Message not set");
                    Assert.AreEqual(int.MaxValue, results["SendUpdates2"].Progress, "SendUpdates2.Progress changed from MaxValue");
                    Assert.AreEqual(0, results["SendUpdates2"].Maximum, "SendUpdates2.Maximum changed from 0");
                    Assert.AreEqual(string.Empty, results["SendUpdates2"].Message, "SendUpdates2.Message changed from empty");

                    sender2.UpdateStatus(1000, 2000, "Message2");
                    sender2.FlushQueue(TimeSpan.FromSeconds(1.0));

                    ready.Reset();
                    listener.RequestUpdate();
                    ready.WaitOne();
                    Assert.AreEqual(100, results["SendUpdates1"].Progress, "SendUpdates1.Progress changed from 100");
                    Assert.AreEqual(200, results["SendUpdates1"].Maximum, "SendUpdates1.Maximum changed from 200");
                    Assert.AreEqual("Message1", results["SendUpdates1"].Message, "SendUpdates1.Message changed");
                    Assert.AreEqual(1000, results["SendUpdates2"].Progress, "SendUpdates2.Progress not set to 1000");
                    Assert.AreEqual(2000, results["SendUpdates2"].Maximum, "SendUpdates2.Maximum not set to 2000");
                    Assert.AreEqual("Message2", results["SendUpdates2"].Message, "SendUpdates2.Message not set");
                }

                Thread.Sleep(100); // allow updaters to terminate
                ready.Reset();
                listener.RequestUpdate();
                ready.WaitOne();
                Assert.IsNotNull(results);
                if (results.Count > 0) {
                    Console.WriteLine("WARNING: {0} results exist at end of test", results.Count);
                    Console.WriteLine("Keys are:");
                    foreach (var key in results.Keys) {
                        Console.WriteLine("    {0}", key);
                    }
                }
                Assert.IsFalse(results.ContainsKey("SendUpdates1"), "results were not cleaned up");
                Assert.IsFalse(results.ContainsKey("SendUpdates2"), "results were not cleaned up");
            }
        }
Esempio n. 3
0
        public void InitializeWithoutCrashing() {
            using (var updater = new AnalyzerStatusUpdater("InitializeWithoutCrashing")) { }

            using (var updater = new AnalyzerStatusListener(x => { })) { }
        }
Esempio n. 4
0
        public void MessageMaximumLength() {
            Dictionary<string, AnalysisProgress> results = null;

            using (var ready = new AutoResetEvent(false))
            using (var listener = new AnalyzerStatusListener(r => { results = r; ready.Set(); })) {
                listener.WaitForWorkerStarted();
                listener.ThrowPendingExceptions();

                // Ensure that updates are being received
                listener.RequestUpdate();
                ready.WaitOne();
                Assert.IsNotNull(results);
                if (results.Count > 0) {
                    ready.Reset();
                    listener.RequestUpdate();
                    ready.WaitOne();
                    Assert.IsNotNull(results);
                    if (results.Count > 0) {
                        Console.WriteLine("WARNING: {0} results received from a previous test", results.Count);
                        Console.WriteLine("Keys are:");
                        foreach (var key in results.Keys) {
                            Console.WriteLine("    {0}", key);
                        }
                    }
                }

                using (var sender = new AnalyzerStatusUpdater("MessageMaximumLength")) {
                    sender.WaitForWorkerStarted();
                    sender.ThrowPendingExceptions();

                    // Create a message that is deliberately too long
                    string message = new string('x', AnalyzerStatusUpdater.MAX_MESSAGE_LENGTH * 2);
                    sender.UpdateStatus(0, 0, message);
                    sender.FlushQueue(TimeSpan.FromSeconds(1.0));

                    listener.RequestUpdate();
                    ready.WaitOne();
                    Assert.IsNotNull(results);
                    AssertUtil.Contains(results.Keys, "MessageMaximumLength");
                    var receivedMessage = results["MessageMaximumLength"].Message;
                    Console.WriteLine("Message: <{0}>", receivedMessage);
                    Assert.AreEqual(
                        AnalyzerStatusUpdater.MAX_MESSAGE_LENGTH,
                        receivedMessage.Length,
                        "Message length was not limited"
                    );
                }
            }
        }
Esempio n. 5
0
        public void IdentifierInUse() {
            using (var updater = new AnalyzerStatusUpdater("IdentifierInUse")) {
                updater.UpdateStatus(1, 100);
                updater.WaitForWorkerStarted();
                // Should not throw
                updater.ThrowPendingExceptions();

                using (var updater2 = new AnalyzerStatusUpdater("IdentifierInUse")) {
                    updater2.WaitForWorkerStarted();
                    updater2.UpdateStatus(99, 100);

                    try {
                        updater2.ThrowPendingExceptions();
                        Assert.Fail("Expected IdentifierInUseException");
                    } catch (IdentifierInUseException) {
                    }
                }
            }
        }
Esempio n. 6
0
        public void LotsOfUpdaters() {
            var updaters = new List<AnalyzerStatusUpdater>();

            // We should stop creating new entries well before 10000
            for (int j = 0; j < 10000; ++j) {
                Console.WriteLine("Creating LotsOfUpdaters{0}", j);
                var newUpdater = new AnalyzerStatusUpdater("LotsOfUpdaters" + j.ToString());
                updaters.Add(newUpdater);
            }
            // Give the updaters a chance to start
            foreach (var updater in updaters) {
                updater.WaitForWorkerStarted();
            }

            // Make sure that we got failures.
            int succeeded = 0;
            try {
                foreach (var u in updaters) {
                    u.ThrowPendingExceptions();
                    succeeded += 1;
                }
                Assert.Fail("Should not have been able to create 10000 updaters");
            } catch (InvalidOperationException) {
            } finally {
                foreach (var u in updaters) {
                    u.Dispose();
                }
            }
            Console.WriteLine("{0} updaters were created.", succeeded);
            updaters.Clear();
        }
Esempio n. 7
0
        public PyLibAnalyzer(
            Guid id,
            Version langVersion,
            string interpreter,
            IEnumerable<PythonLibraryPath> library,
            List<string> baseDb,
            string outDir,
            string logPrivate,
            string logGlobal,
            string logDiagnostic,
            bool rescanAll,
            bool dryRun,
            string waitForAnalysis,
            int repeatCount
        ) {
            _id = id;
            _version = langVersion;
            _interpreter = interpreter;
            _baseDb = baseDb;
            _outDir = outDir;
            _logPrivate = logPrivate;
            _logGlobal = logGlobal;
            _logDiagnostic = logDiagnostic;
            _all = rescanAll;
            _dryRun = dryRun;
            _waitForAnalysis = waitForAnalysis;
            _repeatCount = repeatCount;

            _scrapeFileGroups = new List<List<ModulePath>>();
            _analyzeFileGroups = new List<List<ModulePath>>();
            _treatPathsAsStandardLibrary = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
            _library = library != null ? library.ToList() : new List<PythonLibraryPath>();

            if (_id != Guid.Empty) {
                var identifier = AnalyzerStatusUpdater.GetIdentifier(_id, _version);
                _updater = new AnalyzerStatusUpdater(identifier);
                // We worry about initialization exceptions here, specifically
                // that our identifier may already be in use.
                _updater.WaitForWorkerStarted();
                try {
                    _updater.ThrowPendingExceptions();
                    // Immediately inform any listeners that we've started running
                    // successfully.
                    _updater.UpdateStatus(0, 0, "Initializing");
                } catch (InvalidOperationException) {
                    // Thrown when we run out of space in our shared memory
                    // block. Disable updates for this run.
                    _updater.Dispose();
                    _updater = null;
                }
            }
            // TODO: Link cancellation into the updater
            _cancel = CancellationToken.None;
        }
Esempio n. 8
0
        public void EndGenerateCompletionDatabase(string id, bool success) {
            if (_updater != null) {
                for (int i = 0; i <= 100; i += 30) {
                    _updater.UpdateStatus(i, 100);
                    // Need to sleep to allow the update to go through.
                    Thread.Sleep(500);
                }

                _updater.Dispose();
                _updater = null;
            }
            // Also have to sleep after disposing to make sure that the
            // completion is picked up.
            Thread.Sleep(500);

            if (success) {
                IsCurrent = true;
            } else {
                IsCurrentReason = MissingModulesReason; // won't raise an event
                IsCurrent = false;
            }
        }
Esempio n. 9
0
 public void Dispose() {
     if (_updater != null) {
         _updater.Dispose();
         _updater = null;
     }
 }