public void Should_stop_on_first_violation_and_return_2_results() { // Arrange var context = TestDataFactory.CreateSecurityContext(false); var firstPolicy = new TestPolicy(); var secondPolicy = new Mock <ISecurityPolicy>(); secondPolicy.Setup(x => x.Enforce(It.IsAny <ISecurityContext>())).Returns(PolicyResult.CreateFailureResult(secondPolicy.Object, "Failure occured")); var thirdPolicy = new TestPolicy(); var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender()); policyContainer.SecurityConfigurationProvider = TestDataFactory.CreateValidSecurityConfiguration; policyContainer.AddPolicy(firstPolicy).AddPolicy(secondPolicy.Object).AddPolicy(thirdPolicy); // Act var results = policyContainer.EnforcePolicies(context); // Assert Assert.That(results.Count(), Is.EqualTo(2)); Assert.That(results.First().ViolationOccured, Is.False); Assert.That(results.Last().ViolationOccured, Is.True); }
protected override ISyncPolicy GetSyncPolicy(CallContextBase context, PolicyAttribute attribute) { var testAttribute = (TestPolicyAttribute)attribute; var policy = new TestPolicy(testAttribute.Name, testAttribute.Order); return(policy); }
public void Serializer_Qubscribable_WithPolicy() { var input = new QuotedSubscribable <int>(Subscribable.Empty <int>(), ((Expression <Func <ISubscribable <int> > >)(() => Subscribable.Empty <int>())).Body); var policy = new TestPolicy(); Assert.AreEqual(0, policy.ConstantHoisterCalls); Assert.AreEqual(0, policy.DelegateCacheCalls); Assert.AreEqual(0, policy.InMemoryCacheCalls); Assert.AreEqual(0, policy.OutlineCompilationCalls); Assert.AreEqual(0, policy.ReflectionProviderCalls); Assert.AreEqual(0, policy.ExpressionFactoryCalls); var quotedResult = Roundtrip(input, new Serializer(policy, SerializerVersioning.v1)); Assert.AreEqual(1, policy.ConstantHoisterCalls); Assert.AreEqual(1, policy.DelegateCacheCalls); Assert.AreEqual(1, policy.InMemoryCacheCalls); Assert.AreEqual(1, policy.OutlineCompilationCalls); Assert.AreEqual(1, policy.ReflectionProviderCalls); Assert.AreEqual(1, policy.ExpressionFactoryCalls); Assert.IsNotNull(quotedResult); Assert.AreEqual(input.Value.GetType(), quotedResult.Value.GetType()); }
public void EpsilonGreedyFixedActionUsingVariableActionInterface() { int numActions = 10; float epsilon = 0f; var policy = new TestPolicy <VariableActionTestContext>(); var testContext = new VariableActionTestContext(numActions); var explorer = new EpsilonGreedyExplorer(epsilon); EpsilonGreedyWithContext(numActions, testContext, policy, explorer); }
public void ExpressionPolicy_WithConstantHoisting() { var policy = new TestPolicy { DelegateCache = new SimpleCompiledDelegateCache(), ConstantHoister = new TestHoister() }; Assert.AreEqual(42, policy.Evaluate <int>(Expression.Constant(42))); }
public void EpsilonGreedy() { int numActions = 10; float epsilon = 0f; var policy = new TestPolicy <RegularTestContext>(); var testContext = new RegularTestContext(); var explorer = new EpsilonGreedyExplorer(epsilon); EpsilonGreedyWithContext(numActions, testContext, policy, explorer); }
public void Should_be_PolicyResult_star_star_TestPolicy() { // Arrange var policy = new TestPolicy(); var strategy = new PolicyResultCacheStrategy("AdminController", "Login", policy.GetType(), Cache.DoNotCache, By.Policy); var context = TestDataFactory.CreateSecurityContext(true); // Act var cacheKey = PolicyResultCacheKeyBuilder.CreateFromStrategy(strategy, policy, context); // Assert Assert.That(cacheKey, Is.EqualTo("PolicyResult_*_*_" + NameHelper.Policy <TestPolicy>())); }
public void TauFirstFixedActionUsingVariableActionInterface() { int numActions = 10; int tau = 0; var testContext = new VariableActionTestContext(numActions) { Id = 100 }; var policy = new TestPolicy <VariableActionTestContext>(); var explorer = new TauFirstExplorer(tau); TauFirstWithContext(numActions, testContext, policy, explorer); }
public void TauFirst() { int numActions = 10; int tau = 0; RegularTestContext testContext = new RegularTestContext() { Id = 100 }; var policy = new TestPolicy <RegularTestContext>(); var explorer = new TauFirstExplorer(tau); TauFirstWithContext(numActions, testContext, policy, explorer); }
public void Should_be_PolicyResult_star_star_TestPolicy() { // Arrange var policy = new TestPolicy(); var strategy = new PolicyResultCacheStrategy("AdminController", "Login", policy.GetType(), Cache.DoNotCache, By.Policy); var context = TestDataFactory.CreateSecurityContext(true); // Act var cacheKey = PolicyResultCacheKeyBuilder.CreateFromStrategy(strategy, policy, context); // Assert Assert.That(cacheKey, Is.EqualTo("PolicyResult_*_*_" + NameHelper.Policy<TestPolicy>())); }
public void QuotedOfT_WithExpressionPolicy() { var policy = new TestPolicy { InMemoryCache = new ExpressionHeap(), DelegateCache = new SimpleCompiledDelegateCache(), ConstantHoister = ConstantHoister.Create(useDefaultForNull: false) }; var expr = Expression.Constant(42, typeof(int)); var q = new Quoted <int>(expr, policy); Assert.AreEqual(42, q.Value); Assert.AreNotSame(expr, q.Expression); Assert.IsTrue(new ExpressionEqualityComparer().Equals(expr, q.Expression)); }
static void ExploreTauFirst <TContext> ( string appId, int policyType, JToken configPolicy, int tau, int numActions, string[] experimentalUnitIdList, TContext[] contextList, string outputFile ) { var recorder = new StringRecorder <TContext>(); bool isVariableActionContext = typeof(IVariableActionContext).IsAssignableFrom(typeof(TContext)); switch (policyType) { case 0: // fixed policy { var policyAction = configPolicy["Action"].Value <uint>(); var policy = new TestPolicy <TContext> { ActionToChoose = policyAction }; var explorer = new TauFirstExplorer(tau); var mwt = isVariableActionContext ? MwtExplorer.Create(appId, new VariableActionProvider <TContext>(), recorder, explorer, policy) : MwtExplorer.Create(appId, numActions, recorder, explorer, policy); for (int i = 0; i < experimentalUnitIdList.Length; i++) { int numActionsVariable = isVariableActionContext ? ((IVariableActionContext)contextList[i]).GetNumberOfActions() : int.MaxValue; mwt.ChooseAction(experimentalUnitIdList[i], contextList[i]); } File.AppendAllText(outputFile, recorder.GetRecording()); break; } } }
public async Task Run_WhenTrainingWithPredefinedDemo_ThenItFindsTheCorrectSolution() { // Prepare var testEnvironment = new TestEnvironment(); var testPolicy = new TestPolicy(); var trainGoal = new TrainGoal(); var rlTrainer = new RLTrainer <int>(new RLTrainerOptions <int>(testEnvironment, testPolicy, trainGoal)); // Execute await rlTrainer.Run(epochs : 50, maximumActions : 100); var pathFinder = new RLPathFinder <int>(testEnvironment, testPolicy); var result = await pathFinder.FindRoute(new TestState(8), trainGoal); result.State.ShouldBe(PathFindResultState.GoalReached); result.Steps.ShouldNotBeNull(); result.Steps.ShouldNotBeEmpty(); result.Steps[0].ShouldBe(new StateAndActionPair <int>(new TestState(8), new TestAction(new TestState(9)))); result.Steps[1].ShouldBe(new StateAndActionPair <int>(new TestState(9), new TestAction(new TestState(5)))); result.Steps[2].ShouldBe(new StateAndActionPair <int>(new TestState(5), new TestAction(new TestState(6)))); result.Steps[3].ShouldBe(new StateAndActionPair <int>(new TestState(6), new TestAction(new TestState(7)))); result.Steps[4].ShouldBe(new StateAndActionPair <int>(new TestState(7), new TestAction(new TestState(11)))); }
public void Should_stop_on_first_violation_and_return_2_results() { // Arrange var context = TestDataFactory.CreateSecurityContext(false); var firstPolicy = new TestPolicy(); var secondPolicy = new Mock<ISecurityPolicy>(); secondPolicy.Setup(x => x.Enforce(It.IsAny<ISecurityContext>())).Returns(PolicyResult.CreateFailureResult(secondPolicy.Object, "Failure occured")); var thirdPolicy = new TestPolicy(); var policyContainer = new PolicyContainer(TestDataFactory.ValidControllerName, TestDataFactory.ValidActionName, TestDataFactory.CreateValidPolicyAppender()); policyContainer.AddPolicy(firstPolicy).AddPolicy(secondPolicy.Object).AddPolicy(thirdPolicy); // Act var results = policyContainer.EnforcePolicies(context); // Assert Assert.That(results.Count(), Is.EqualTo(2)); Assert.That(results.First().ViolationOccured, Is.False); Assert.That(results.Last().ViolationOccured, Is.True); }
public void should_register_object_def_for_value() { var policy = new TestPolicy(null); _expression.ApplyPolicy(policy); _policies.ShouldContain(p => p.Value == policy); }
private static void EpsilonGreedyWithContext <TContext>(int numActions, TContext testContext, TestPolicy <TContext> policy, IExplorer <int, int> explorer) where TContext : RegularTestContext { string uniqueKey = "ManagedTestId"; var uniqueId = uniqueKey; TestRecorder <TContext> recorder = new TestRecorder <TContext>(); //MwtExplorer<TContext> mwtt = new MwtExplorer<TContext>("mwt", recorder); var mwtt = MwtExplorer.Create("mwt", numActions, recorder, explorer, policy); testContext.Id = 100; int expectedAction = policy.MapContext(testContext).Value; int chosenAction = mwtt.ChooseAction(uniqueId, testContext); Assert.AreEqual(expectedAction, chosenAction); chosenAction = mwtt.ChooseAction(uniqueId, testContext); Assert.AreEqual(expectedAction, chosenAction); var interactions = recorder.GetAllInteractions(); Assert.AreEqual(2, interactions.Count); Assert.AreEqual(testContext.Id, interactions[0].Context.Id); // Verify that policy action is chosen all the time explorer.EnableExplore(false); for (int i = 0; i < 1000; i++) { chosenAction = mwtt.ChooseAction(uniqueId, testContext); Assert.AreEqual(expectedAction, chosenAction); } }
public void Arrange() { _logger = new Mock <ILogger>(); _policy = new TestPolicy(_logger.Object); }