SEBWIN-219: Added ignore flag for service proxy.

This commit is contained in:
dbuechel 2018-01-25 09:50:46 +01:00
parent ebc12934bf
commit 1e8eb858de
6 changed files with 59 additions and 13 deletions

View file

@ -24,11 +24,14 @@ namespace SafeExamBrowser.Configuration.Settings
public ISettings LoadDefaults() public ISettings LoadDefaults()
{ {
Current = new Settings(); var settings = new Settings();
// TODO
return Current; // TODO
settings.ServicePolicy = ServicePolicy.Optional;
Current = settings;
return settings;
} }
} }
} }

View file

@ -10,6 +10,12 @@ namespace SafeExamBrowser.Contracts.Communication
{ {
public interface IServiceProxy public interface IServiceProxy
{ {
/// <summary>
/// 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.
/// </summary>
bool Ignore { set; }
/// <summary> /// <summary>
/// Tries to connect to the service host. /// Tries to connect to the service host.
/// </summary> /// </summary>

View file

@ -96,7 +96,7 @@ namespace SafeExamBrowser.Core.Communication
private void CommunicationHostProxy_Faulted(object sender, EventArgs e) private void CommunicationHostProxy_Faulted(object sender, EventArgs e)
{ {
Logger.Error("Communication channel has faulted!"); Logger.Debug("Communication channel has faulted!");
} }
private string GetChannelState() private string GetChannelState()

View file

@ -6,7 +6,6 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/ */
using System;
using SafeExamBrowser.Contracts.Communication; using SafeExamBrowser.Contracts.Communication;
using SafeExamBrowser.Contracts.Logging; using SafeExamBrowser.Contracts.Logging;
using SafeExamBrowser.Core.Communication.Messages; using SafeExamBrowser.Core.Communication.Messages;
@ -15,20 +14,40 @@ namespace SafeExamBrowser.Core.Communication
{ {
public class ServiceProxy : BaseProxy, IServiceProxy public class ServiceProxy : BaseProxy, IServiceProxy
{ {
public bool Ignore { private get; set; }
public ServiceProxy(ILogger logger, string address) : base(logger, address) public ServiceProxy(ILogger logger, string address) : base(logger, address)
{ {
} }
public bool Connect() public bool Connect()
{ {
throw new NotImplementedException(); if (!IgnoreOperation(nameof(Connect)))
{
return base.Connect().ConnectionEstablished;
}
return false;
} }
public void Disconnect() 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;
} }
} }
} }

View file

@ -102,6 +102,7 @@ namespace SafeExamBrowser.Runtime.UnitTests.Behaviour.Operations
sut.Perform(); sut.Perform();
service.VerifySet(s => s.Ignore = true);
Assert.IsFalse(sut.AbortStartup); Assert.IsFalse(sut.AbortStartup);
} }
@ -123,6 +124,19 @@ namespace SafeExamBrowser.Runtime.UnitTests.Behaviour.Operations
service.Verify(s => s.Disconnect(), Times.Exactly(2)); 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<Exception>();
settings.SetupGet(s => s.Current.ServicePolicy).Returns(ServicePolicy.Optional);
sut.Perform();
sut.Revert();
service.Verify(s => s.Disconnect(), Times.Once);
}
[TestMethod] [TestMethod]
public void MustNotDisconnnectIfNotAvailable() public void MustNotDisconnnectIfNotAvailable()
{ {

View file

@ -60,15 +60,19 @@ namespace SafeExamBrowser.Runtime.Behaviour.Operations
} }
} }
AbortStartup = serviceMandatory && !serviceAvailable; if (serviceMandatory && !serviceAvailable)
if (AbortStartup)
{ {
AbortStartup = true;
logger.Info("Aborting startup because the service is mandatory but not available!"); 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 else
{ {
logger.Info($"The service is {(serviceMandatory ? "mandatory" : "optional")} and {(!serviceAvailable ? "not" : "")} available."); logger.Info($"The service is {(serviceMandatory ? "mandatory" : "optional")} and available.");
} }
} }