Beispiel #1
0
        public void CancelsTheDialConnectionAttemptIfDisposed()
        {
            var handle = new IntPtr(1);

            var api = new Mock <IRasApi32>();

            api.Setup(o => o.RasDial(ref It.Ref <RASDIALEXTENSIONS> .IsAny, @"C:\Test.pbk", ref It.Ref <RASDIALPARAMS> .IsAny, Ras.NotifierType.RasDialFunc2, It.IsAny <RasDialFunc2>(), out It.Ref <IntPtr> .IsAny)).Returns(new RasDialCallback(
                                                                                                                                                                                                                                  (ref RASDIALEXTENSIONS o1, string o2, ref RASDIALPARAMS o3, Ras.NotifierType o4, RasDialFunc2 o5, out IntPtr o6) =>
            {
                o6 = handle;

                return(SUCCESS);
            }));

            var rasHangUp         = new Mock <IRasHangUp>();
            var extensionsBuilder = new Mock <IRasDialExtensionsBuilder>();
            var paramsBuilder     = new Mock <IRasDialParamsBuilder>();
            var exceptionPolicy   = new Mock <IExceptionPolicy>();

            var callbackHandler = new Mock <IRasDialCallbackHandler>();
            var connection      = new Mock <RasConnection>();

            var completionSource = new Mock <ITaskCompletionSource <RasConnection> >();

            completionSource.Setup(o => o.Task).Returns(Task.FromResult(connection.Object));

            var completionSourceFactory = new Mock <ITaskCompletionSourceFactory>();

            completionSourceFactory.Setup(o => o.Create <RasConnection>()).Returns(completionSource.Object);

            var cancellationSource        = new Mock <ITaskCancellationSource>();
            var cancellationSourceFactory = new Mock <ITaskCancellationSourceFactory>();

            cancellationSourceFactory.Setup(o => o.Create(It.IsAny <CancellationToken>())).Returns(cancellationSource.Object);

            var context = new RasDialContext
            {
                PhoneBookPath = @"C:\Test.pbk",
                EntryName     = "Entry",
                Credentials   = new NetworkCredential("User", "Password"),
                Options       = new RasDialerOptions
                {
                    InterfaceIndex = 0
                }
            };

            var target = new RasDialService(api.Object, rasHangUp.Object, extensionsBuilder.Object, paramsBuilder.Object, exceptionPolicy.Object, callbackHandler.Object, completionSourceFactory.Object, cancellationSourceFactory.Object);

            target.DialAsync(context);

            Assert.IsTrue(target.IsBusy);

            target.Dispose();

            cancellationSource.Verify(o => o.Cancel(), Times.Once);
        }
Beispiel #2
0
        public void DisposesCorrectlyWhenNotInitialized()
        {
            var api                       = new Mock <IRasApi32>();
            var rasHangUp                 = new Mock <IRasHangUp>();
            var extensionsBuilder         = new Mock <IRasDialExtensionsBuilder>();
            var paramsBuilder             = new Mock <IRasDialParamsBuilder>();
            var exceptionPolicy           = new Mock <IExceptionPolicy>();
            var completionSourceFactory   = new Mock <ITaskCompletionSourceFactory>();
            var cancellationSourceFactory = new Mock <ITaskCancellationSourceFactory>();
            var callbackHandler           = new Mock <IRasDialCallbackHandler>();

            var target = new RasDialService(api.Object, rasHangUp.Object, extensionsBuilder.Object, paramsBuilder.Object, exceptionPolicy.Object, callbackHandler.Object, completionSourceFactory.Object, cancellationSourceFactory.Object);

            target.Dispose();

            callbackHandler.Verify(o => o.Dispose(), Times.Once);
        }