From 1e8eb858de8890460c61bb8b279e4394e42d0705 Mon Sep 17 00:00:00 2001 From: dbuechel Date: Thu, 25 Jan 2018 09:50:46 +0100 Subject: [PATCH] SEBWIN-219: Added ignore flag for service proxy. --- .../Settings/SettingsRepository.cs | 11 +++++--- .../Communication/IServiceProxy.cs | 6 +++++ .../Communication/BaseProxy.cs | 2 +- .../Communication/ServiceProxy.cs | 27 ++++++++++++++++--- .../Operations/ServiceOperationTests.cs | 14 ++++++++++ .../Behaviour/Operations/ServiceOperation.cs | 12 ++++++--- 6 files changed, 59 insertions(+), 13 deletions(-) diff --git a/SafeExamBrowser.Configuration/Settings/SettingsRepository.cs b/SafeExamBrowser.Configuration/Settings/SettingsRepository.cs index 596a24e0..6343b131 100644 --- a/SafeExamBrowser.Configuration/Settings/SettingsRepository.cs +++ b/SafeExamBrowser.Configuration/Settings/SettingsRepository.cs @@ -24,11 +24,14 @@ namespace SafeExamBrowser.Configuration.Settings public ISettings LoadDefaults() { - Current = new Settings(); - - // TODO + var settings = new Settings(); - return Current; + // TODO + settings.ServicePolicy = ServicePolicy.Optional; + + Current = settings; + + return settings; } } } diff --git a/SafeExamBrowser.Contracts/Communication/IServiceProxy.cs b/SafeExamBrowser.Contracts/Communication/IServiceProxy.cs index 257bfab9..4cc033b6 100644 --- a/SafeExamBrowser.Contracts/Communication/IServiceProxy.cs +++ b/SafeExamBrowser.Contracts/Communication/IServiceProxy.cs @@ -10,6 +10,12 @@ namespace SafeExamBrowser.Contracts.Communication { public interface IServiceProxy { + /// + /// Instructs the proxy to ignore all operations or simulate a connection, where applicable. To be set e.g. when the service + /// policy is optional and the service is not available. + /// + bool Ignore { set; } + /// /// Tries to connect to the service host. /// diff --git a/SafeExamBrowser.Core/Communication/BaseProxy.cs b/SafeExamBrowser.Core/Communication/BaseProxy.cs index 78515813..55aa203b 100644 --- a/SafeExamBrowser.Core/Communication/BaseProxy.cs +++ b/SafeExamBrowser.Core/Communication/BaseProxy.cs @@ -96,7 +96,7 @@ namespace SafeExamBrowser.Core.Communication private void CommunicationHostProxy_Faulted(object sender, EventArgs e) { - Logger.Error("Communication channel has faulted!"); + Logger.Debug("Communication channel has faulted!"); } private string GetChannelState() diff --git a/SafeExamBrowser.Core/Communication/ServiceProxy.cs b/SafeExamBrowser.Core/Communication/ServiceProxy.cs index 5df74154..7af22a74 100644 --- a/SafeExamBrowser.Core/Communication/ServiceProxy.cs +++ b/SafeExamBrowser.Core/Communication/ServiceProxy.cs @@ -6,7 +6,6 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -using System; using SafeExamBrowser.Contracts.Communication; using SafeExamBrowser.Contracts.Logging; using SafeExamBrowser.Core.Communication.Messages; @@ -15,20 +14,40 @@ namespace SafeExamBrowser.Core.Communication { public class ServiceProxy : BaseProxy, IServiceProxy { + public bool Ignore { private get; set; } + public ServiceProxy(ILogger logger, string address) : base(logger, address) { } public bool Connect() { - throw new NotImplementedException(); + if (!IgnoreOperation(nameof(Connect))) + { + return base.Connect().ConnectionEstablished; + } + + return false; } public void Disconnect() { - FailIfNotConnected(nameof(Disconnect)); + if (!IgnoreOperation(nameof(Disconnect))) + { + FailIfNotConnected(nameof(Disconnect)); - Disconnect(new Message { CommunicationToken = CommunicationToken.Value }); + Disconnect(new Message { CommunicationToken = CommunicationToken.Value }); + } + } + + private bool IgnoreOperation(string operationName) + { + if (Ignore) + { + Logger.Debug($"Skipping {operationName} because the ignore flag is set."); + } + + return Ignore; } } } diff --git a/SafeExamBrowser.Runtime.UnitTests/Behaviour/Operations/ServiceOperationTests.cs b/SafeExamBrowser.Runtime.UnitTests/Behaviour/Operations/ServiceOperationTests.cs index 5601250f..02953f5a 100644 --- a/SafeExamBrowser.Runtime.UnitTests/Behaviour/Operations/ServiceOperationTests.cs +++ b/SafeExamBrowser.Runtime.UnitTests/Behaviour/Operations/ServiceOperationTests.cs @@ -102,6 +102,7 @@ namespace SafeExamBrowser.Runtime.UnitTests.Behaviour.Operations sut.Perform(); + service.VerifySet(s => s.Ignore = true); Assert.IsFalse(sut.AbortStartup); } @@ -123,6 +124,19 @@ namespace SafeExamBrowser.Runtime.UnitTests.Behaviour.Operations service.Verify(s => s.Disconnect(), Times.Exactly(2)); } + [TestMethod] + public void MustNotFailWhenDisconnecting() + { + service.Setup(s => s.Connect()).Returns(true); + service.Setup(s => s.Disconnect()).Throws(); + settings.SetupGet(s => s.Current.ServicePolicy).Returns(ServicePolicy.Optional); + + sut.Perform(); + sut.Revert(); + + service.Verify(s => s.Disconnect(), Times.Once); + } + [TestMethod] public void MustNotDisconnnectIfNotAvailable() { diff --git a/SafeExamBrowser.Runtime/Behaviour/Operations/ServiceOperation.cs b/SafeExamBrowser.Runtime/Behaviour/Operations/ServiceOperation.cs index 84153154..48437fd6 100644 --- a/SafeExamBrowser.Runtime/Behaviour/Operations/ServiceOperation.cs +++ b/SafeExamBrowser.Runtime/Behaviour/Operations/ServiceOperation.cs @@ -60,15 +60,19 @@ namespace SafeExamBrowser.Runtime.Behaviour.Operations } } - AbortStartup = serviceMandatory && !serviceAvailable; - - if (AbortStartup) + if (serviceMandatory && !serviceAvailable) { + AbortStartup = true; logger.Info("Aborting startup because the service is mandatory but not available!"); } + else if (!serviceAvailable) + { + service.Ignore = true; + logger.Info("All service-related operations will be ignored, since the service is optional and not available."); + } else { - logger.Info($"The service is {(serviceMandatory ? "mandatory" : "optional")} and {(!serviceAvailable ? "not" : "")} available."); + logger.Info($"The service is {(serviceMandatory ? "mandatory" : "optional")} and available."); } }