示例#1
0
    public async Task Invoke_SuccessfulFlow_CallNext(AffinityStatus status, string foundDestinationId)
    {
        var cluster  = GetCluster();
        var endpoint = GetEndpoint(cluster);
        DestinationState foundDestination = null;

        if (foundDestinationId != null)
        {
            cluster.Destinations.TryGetValue(foundDestinationId, out foundDestination);
        }
        var          invokedMode  = string.Empty;
        const string expectedMode = "Policy-B";
        var          policies     = RegisterAffinityPolicies(
            true,
            cluster.Destinations.Values.ToList(),
            ("Policy-A", AffinityStatus.DestinationNotFound, (DestinationState)null, (Action <ISessionAffinityPolicy>)(p => throw new InvalidOperationException($"Policy {p.Name} call is not expected."))),
            (expectedMode, status, foundDestination, p => invokedMode = p.Name));
        var nextInvoked = false;
        var middleware  = new SessionAffinityMiddleware(c =>
        {
            nextInvoked = true;
            return(Task.CompletedTask);
        },
                                                        policies.Select(p => p.Object), new IAffinityFailurePolicy[0],
                                                        new Mock <ILogger <SessionAffinityMiddleware> >().Object);
        var context = new DefaultHttpContext();

        context.SetEndpoint(endpoint);
        var destinationFeature = GetReverseProxyFeature(cluster);

        context.Features.Set(destinationFeature);

        await middleware.Invoke(context);

        Assert.Equal(expectedMode, invokedMode);
        Assert.True(nextInvoked);
        policies[0].VerifyGet(p => p.Name, Times.Once);
        policies[0].VerifyNoOtherCalls();
        policies[1].VerifyAll();

        if (foundDestinationId != null)
        {
            Assert.Equal(1, destinationFeature.AvailableDestinations.Count);
            Assert.Equal(foundDestinationId, destinationFeature.AvailableDestinations[0].DestinationId);
        }
        else
        {
            Assert.True(cluster.Destinations.Values.SequenceEqual(destinationFeature.AvailableDestinations));
        }
    }
示例#2
0
    public async Task Invoke_ErrorFlow_CallFailurePolicy(AffinityStatus affinityStatus, bool keepProcessing)
    {
        var          cluster         = GetCluster();
        var          endpoint        = GetEndpoint(cluster);
        var          policies        = RegisterAffinityPolicies(true, cluster.Destinations.Values.ToList(), ("Policy-B", affinityStatus, null, _ => { }));
        var          invokedPolicy   = string.Empty;
        const string expectedPolicy  = "Policy-1";
        var          failurePolicies = RegisterFailurePolicies(
            affinityStatus,
            ("Policy-0", false, p => throw new InvalidOperationException($"Policy {p.Name} call is not expected.")),
            (expectedPolicy, keepProcessing, p => invokedPolicy = p.Name));
        var nextInvoked = false;
        var logger      = AffinityTestHelper.GetLogger <SessionAffinityMiddleware>();
        var middleware  = new SessionAffinityMiddleware(c =>
        {
            nextInvoked = true;
            return(Task.CompletedTask);
        },
                                                        policies.Select(p => p.Object), failurePolicies.Select(p => p.Object),
                                                        logger.Object);
        var context            = new DefaultHttpContext();
        var destinationFeature = GetReverseProxyFeature(cluster);

        context.SetEndpoint(endpoint);
        context.Features.Set(destinationFeature);

        await middleware.Invoke(context);

        Assert.Equal(expectedPolicy, invokedPolicy);
        Assert.Equal(keepProcessing, nextInvoked);
        failurePolicies[0].VerifyGet(p => p.Name, Times.Once);
        failurePolicies[0].VerifyNoOtherCalls();
        failurePolicies[1].VerifyAll();
        if (!keepProcessing)
        {
            logger.Verify(
                l => l.Log(LogLevel.Warning, EventIds.AffinityResolutionFailedForCluster, It.IsAny <It.IsAnyType>(), null, (Func <It.IsAnyType, Exception, string>)It.IsAny <object>()),
                Times.Once);
        }
    }