Exemplo n.º 1
0
        public async Task <bool> IsActiveAsync(string featureName, string productName = null, CancellationToken cancellationToken = default)
        {
            var feature = await _featureStore.FindFeatureAsync(featureName, productName, cancellationToken);

            var toggle = feature.GetToggle(this.GetType().FullName);
            var data   = toggle.GetData();

            string headerName = data.HeaderName;

            if (Double.TryParse(data.Percentage.ToString(), out double percentage))
            {
                if (percentage > 0d)
                {
                    var values = _httpContextAccessor.HttpContext
                                 .Request
                                 .Headers[headerName];

                    var headerValue = values != StringValues.Empty ? values.First() : NO_HEADER_DEFAULT_VALUE;

                    var assignedPartition = Partitioner.ResolveToLogicalPartition(headerValue, Partitions);
                    return(assignedPartition <= percentage);
                }
            }

            return(false);
        }
        public async Task <bool> IsActiveAsync(string featureName, string productName = null, CancellationToken cancellationToken = default)
        {
            var feature = await _featureStore.FindFeatureAsync(featureName, productName, cancellationToken);

            var toggle = feature.GetToggle(this.GetType().FullName);
            var data   = toggle.GetData();

            if (Double.TryParse(data.Percentage.ToString(), out double percentage))
            {
                string claimType = data.ClaimType?.ToString();

                if (claimType != null && percentage > 0)
                {
                    var user = _httpContextAccessor
                               .HttpContext
                               .User;

                    if (user != null && user.Identity.IsAuthenticated)
                    {
                        var value = user.FindFirst(claimType)?.Value ?? NO_CLAIMTYPE_DEFAULT_VALUE;

                        var assignedPartition = Partitioner.ResolveToLogicalPartition(value, Partitions);
                        return(assignedPartition <= percentage);
                    }
                }
            }

            return(false);
        }
Exemplo n.º 3
0
        public async Task <bool> IsActiveAsync(string featureName, string productName = null, CancellationToken cancellationToken = default)
        {
            var feature = await _featureStore.FindFeatureAsync(featureName, productName, cancellationToken);

            var toggle = feature.GetToggle(this.GetType().FullName);
            var data   = toggle.GetData();

            if (Double.TryParse(data.Percentage.ToString(), out double percentage))
            {
                if (percentage > 0d)
                {
                    try
                    {
                        var sessionId = _httpContextAccessor
                                        .HttpContext
                                        .Session
                                        .Id;

                        var assignedPartition = Partitioner.ResolveToLogicalPartition(sessionId, Partitions);
                        return(assignedPartition <= percentage);
                    }
                    catch (InvalidOperationException)
                    {
                        _logger.LogError($"The toggle {nameof(GradualRolloutSessionToggle)} can't perform rollout on Session because  Session has not been configured for this application or request.");
                    }
                }
            }

            return(false);
        }
        public async Task be_active_when_user_partition_is_on_percent_bucket(string username, int percentage)
        {
            var partition = Partitioner.ResolveToLogicalPartition(username, 100);
            var expected  = partition <= percentage;

            var toggle = Build
                         .Toggle <GradualRolloutUserNameToggle>()
                         .AddOneParameter(Percentage, percentage)
                         .Build();

            var feature = Build
                          .Feature(Constants.FeatureName)
                          .AddOne(toggle)
                          .Build();

            var store            = new DelegatedValueFeatureStore((_, __) => feature);
            var userNameProvider = new DelegatedUserNameProviderService(() => username);

            var actual = await new GradualRolloutUserNameToggle(userNameProvider, store).IsActiveAsync(Constants.FeatureName, Constants.ProductName);

            actual.Should().Be(expected);
        }
Exemplo n.º 5
0
        public async Task <bool> IsActiveAsync(string featureName, string productName = null, CancellationToken cancellationToken = default)
        {
            var feature = await _featureStore.FindFeatureAsync(featureName, productName, cancellationToken);

            var toggle = feature.GetToggle(this.GetType().FullName);
            var data   = toggle.GetData();

            if (Double.TryParse(data.Percentage.ToString(), out double percentage))
            {
                if (percentage > 0)
                {
                    var currentUserName = await _userNameProviderService
                                          .GetCurrentUserNameAsync() ?? AnonymousUser;

                    var assignedPartition = Partitioner.ResolveToLogicalPartition(currentUserName, Partitions);

                    return(assignedPartition <= percentage);
                }
            }

            return(false);
        }