Exemple #1
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");
            }
        }
Exemple #2
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"
                    );
                }
            }
        }