public void TestMultipleThreadsAddingAndRemovingCallbacks()
        {
            #region Prepare: add some callbacks first so we have something to delete
            for (int i = 0; i < numberOfTasks; ++i)
            {
                var nonCapturedI = i;
                AID.ApplicationInsights_AddCallbacks(nonCapturedI, () => nonCapturedI, null, null);
            }
            #endregion

            Task[] allTasks = new Task[numberOfTasks * 2];

            for (int i = 0; i < numberOfTasks; ++i)
            {
                int nonCapturedI = i;

                allTasks[nonCapturedI] =
                    Task.Factory.StartNew(
                        () => AID.ApplicationInsights_RemoveCallbacks(nonCapturedI, 0));
                allTasks[nonCapturedI + numberOfTasks] =
                    Task.Factory.StartNew(
                        () => AID.ApplicationInsights_AddCallbacks(nonCapturedI + numberOfTasks, () => nonCapturedI + numberOfTasks, null, null));
            }

            Task.WaitAll(allTasks);

            for (int i = numberOfTasks; i < 2 * numberOfTasks; ++i)
            {
                Assert.AreEqual(i, Convert.ToInt32(AID.ApplicationInsights_OnBegin(i), CultureInfo.InvariantCulture));
            }
        }
        public void ExtensionBasePrivateCallbacksCalled()
        {
            isOnBeginPrivateCalled = false;
            AID.ApplicationInsights_AddCallbacks(methodId, OnBeginPrivate, null, null);
            var context = AID.ApplicationInsights_OnBegin(methodId, thisObj);

            Assert.IsTrue(isOnBeginPrivateCalled);
        }
        public void TestMultipleThreadsAddingCallbacks()
        {
            var result = Parallel.For(1, numberOfTasks,
                                      (i, state) => AID.ApplicationInsights_AddCallbacks(i, () => i, null, null));

            Assert.IsTrue(result.IsCompleted);

            for (int i = 0; i < numberOfTasks; ++i)
            {
                Assert.AreEqual(i, Convert.ToInt32(AID.ApplicationInsights_OnBegin(i), CultureInfo.InvariantCulture));
            }
        }
        public void ExceptionDoesNotDeadLock()
        {
            AID.ApplicationInsights_AddCallbacks(1, () => 1, null, null);

            try
            {
                AID.ApplicationInsights_AddCallbacks(1, () => 1, null, null);
            }
            catch
            {
            }

            AID.ApplicationInsights_RemoveCallbacks(1, 0); // should not throw concurrency exception
        }
        public void ExtensionBaseCallbacksCalled()
        {
            AID.ApplicationInsights_AddCallbacks(methodId,
                                                 callbacksStub.OnBegin0,
                                                 callbacksStub.OnEnd0,
                                                 callbacksStub.OnException0);

            var context = AID.ApplicationInsights_OnBegin(methodId, thisObj);

            Assert.IsTrue(callbacksStub.isOnBeginCalled);

            Assert.AreEqual(null, AID.ApplicationInsights_OnEnd(methodId, context, returnValue, thisObj),
                            "return value was not overrided by End callback");
            Assert.IsTrue(callbacksStub.isOnEndCalled);

            AID.ApplicationInsights_OnException(methodId, context, exception, thisObj);
            Assert.IsTrue(callbacksStub.isOnExceptionCalled);
        }
 public void ExtensionBaseWrongBeginReturnContext()
 {
     AID.ApplicationInsights_AddCallbacks(methodId, () => { return(42); }, null, null);
     Assert.AreEqual(42, Convert.ToInt32(AID.ApplicationInsights_OnBegin(methodId), CultureInfo.InvariantCulture));
 }
 public void ExtensionBaseReturnValueSubstituted()
 {
     AID.ApplicationInsights_AddCallbacks(methodId, null, (ctx, retVal) => { return(42); }, null);
     Assert.AreEqual(42, Convert.ToInt32(AID.ApplicationInsights_OnEnd(methodId, null, this.returnValue), CultureInfo.InvariantCulture));
 }
 public void ExtensionBaseWrongSignatureMethodWillNotBeCalled()
 {
     AID.ApplicationInsights_AddCallbacks(methodId, callbacksStub.OnBegin1, null, null);
     AID.ApplicationInsights_OnBegin(methodId, thisObj);
     Assert.IsFalse(callbacksStub.IsAnythingCalled);
 }
 public void AddThrowsIfSameKeyIsAdded()
 {
     AID.ApplicationInsights_AddCallbacks(1, () => 1, null, null);
     AID.ApplicationInsights_AddCallbacks(1, () => 1, null, null);
 }