예제 #1
0
        public void VerifyMaliciousApplicationIdIsTruncated()
        {
            // 50 character string.
            var testApplicationId = "a123456789b123546789c123456789d123456798e123456789";

            // An arbitrary string that is expected to be truncated.
            var malicious = "00000000000000000000000000000000000000000000000000000000000";

            var testFormattedApplicationId = ApplicationIdHelper.ApplyFormatting(testApplicationId);

            var mockProfileServiceWrapper = GenerateMockServiceWrapper(HttpStatusCode.OK, testApplicationId + malicious);
            var aiApplicationIdProvider   = new ApplicationInsightsApplicationIdProvider(mockProfileServiceWrapper);

            // first request expected to fail
            Assert.IsFalse(aiApplicationIdProvider.TryGetApplicationId(testInstrumentationKey, out string ignore));

            // wait for async tasks to complete
            while (aiApplicationIdProvider.IsFetchAppInProgress(testInstrumentationKey))
            {
                Console.WriteLine("wait");
                Thread.Sleep(taskWaitMilliseconds);
            }

            Assert.IsTrue(aiApplicationIdProvider.TryGetApplicationId(testInstrumentationKey, out string actual));
            Assert.AreEqual(testFormattedApplicationId, actual);
        }
예제 #2
0
        public void VerifyWhenRequestFailsWillWaitBeforeRetry()
        {
            mockMethodFailOnceStateBool = false;
            var mockProfileServiceWrapper = GenerateMockServiceWrapper(this.MockMethodFailOnce);
            var aiApplicationIdProvider   = new ApplicationInsightsApplicationIdProvider(mockProfileServiceWrapper);
            var stopWatch = new Stopwatch();

            Console.WriteLine($"first request: {DateTime.UtcNow.ToString("HH:mm:ss:fffff")}");
            stopWatch.Start();
            Assert.IsFalse(aiApplicationIdProvider.TryGetApplicationId(testInstrumentationKey, out string ignore1)); // first request will fail, will create internal failure-timeout

            // wait for async tasks to complete
            while (aiApplicationIdProvider.IsFetchAppInProgress(testInstrumentationKey))
            {
                Console.WriteLine("wait for task");
                Thread.Sleep(taskWaitMilliseconds);
            }

            Console.WriteLine($"\nsecond request: {DateTime.UtcNow.ToString("HH:mm:ss:fffff")}");
            Assert.IsFalse(aiApplicationIdProvider.TryGetApplicationId(testInstrumentationKey, out string ignore2)); // first retry should fail because timeout (too soon)

            while (!mockProfileServiceWrapper.FailedRequestsManager.CanRetry(testInstrumentationKey))
            {
                Console.WriteLine("wait for retry");
                Thread.Sleep(taskWaitMilliseconds);
            }
            stopWatch.Stop();
            Assert.IsTrue(stopWatch.Elapsed >= failedRequestRetryWaitTime, "too fast, did not wait timeout");

            Console.WriteLine($"\nthird request: {DateTime.UtcNow.ToString("HH:mm:ss:fffff")}");
            Assert.IsFalse(aiApplicationIdProvider.TryGetApplicationId(testInstrumentationKey, out string ignore3)); // second retry should fail (because no matching value), but will create new request task

            // wait for async tasks to complete
            while (aiApplicationIdProvider.IsFetchAppInProgress(testInstrumentationKey))
            {
                Console.WriteLine("wait for task");
                Thread.Sleep(taskWaitMilliseconds);
            }

            Console.WriteLine($"\nfourth request: {DateTime.UtcNow.ToString("HH:mm:ss:fffff")}");
            Assert.IsTrue(aiApplicationIdProvider.TryGetApplicationId(testInstrumentationKey, out string actual)); // third retry should resolve

            Assert.AreEqual(testFormattedApplicationId, actual);
        }
예제 #3
0
        public void VerifySucceedsOnSecondRequest()
        {
            var mockProfileServiceWrapper = GenerateMockServiceWrapper(HttpStatusCode.OK, testApplicationId);
            var aiApplicationIdProvider   = new ApplicationInsightsApplicationIdProvider(mockProfileServiceWrapper);

            // first request expected to fail
            Assert.IsFalse(aiApplicationIdProvider.TryGetApplicationId(testInstrumentationKey, out string ignore));

            // wait for async tasks to complete
            while (aiApplicationIdProvider.IsFetchAppInProgress(testInstrumentationKey))
            {
                Console.WriteLine("wait");
                Thread.Sleep(taskWaitMilliseconds);
            }

            Assert.IsTrue(aiApplicationIdProvider.TryGetApplicationId(testInstrumentationKey, out string actual));
            Assert.AreEqual(testFormattedApplicationId, actual);
        }
예제 #4
0
        public void VerifyWhenRequestHardFailsWillNotRetry()
        {
            var mockProfileServiceWrapper = GenerateMockServiceWrapper(HttpStatusCode.NotFound);
            var aiApplicationIdProvider   = new ApplicationInsightsApplicationIdProvider(mockProfileServiceWrapper);

            Console.WriteLine("first request");
            Assert.IsFalse(aiApplicationIdProvider.TryGetApplicationId(testInstrumentationKey, out string ignore1));

            // wait for async tasks to complete
            while (aiApplicationIdProvider.IsFetchAppInProgress(testInstrumentationKey))
            {
                Console.WriteLine("wait");
                Thread.Sleep(taskWaitMilliseconds);
            }

            Console.WriteLine("second request");
            Assert.IsFalse(aiApplicationIdProvider.TryGetApplicationId(testInstrumentationKey, out string ignore2)); // retry should fail, fatal error
            Thread.Sleep(failedRequestRetryWaitTime + failedRequestRetryWaitTime);                                   // wait for timeout to expire (2x timeout).

            Console.WriteLine("third request");
            Assert.IsFalse(aiApplicationIdProvider.TryGetApplicationId(testInstrumentationKey, out string ignore3)); // retry should still fail, fatal error
        }