Пример #1
0
        static void Main(string[] args)
        {
            var unleashConfig = new UnleashConfig.Builder()
                                .AppName("c-sharp-test")
                                .InstanceId("instance y")
                                .UnleashAPI("https://unleash.herokuapp.com/api/")
                                .FetchTogglesInterval(1)
                                .SendMetricsInterval(2)
                                .Build();

            var context = UnleashContext.CreateBuilder()
                          .SessionId(new Random().Next(10000) + "")
                          .UserId(new Random().Next(10000) + "")
                          .RemoteAddress("192.168.1.1")
                          .Build();

            // var executor = new UnleashScheduledExecutorImpl();
            // var repository = new FeatureToggleRepository(
            //         unleashConfig,
            //         executor,
            //         new HttpToggleFetcher(unleashConfig),
            //         new ToggleBackupHandlerFile(unleashConfig));
            var unleash = new DefaultUnleash(unleashConfig,
                                             //repository,
                                             new ActiveForUserWithIdStrategy()
                                             );


            //var test =  executor.SetInterval(s => repository.UpdateToggles(s).Wait(), 0, unleashConfig.FetchTogglesInterval);

            Run(unleash, context, "test", 200);
        }
Пример #2
0
        public static IUnleash CreateUnleash(TestDefinition testDefinition, UnleashContextDefinition contextDefinition)
        {
            var fakeHttpClientFactory  = A.Fake <IHttpClientFactory>();
            var fakeHttpMessageHandler = new TestHttpMessageHandler();
            var httpClient             = new HttpClient(fakeHttpMessageHandler)
            {
                BaseAddress = new Uri("http://localhost")
            };
            var fakeScheduler  = A.Fake <IUnleashScheduledTaskManager>();
            var fakeFileSystem = new MockFileSystem();

            A.CallTo(() => fakeHttpClientFactory.Create(A <Uri> ._)).Returns(httpClient);
            A.CallTo(() => fakeScheduler.Configure(A <IEnumerable <IUnleashScheduledTask> > ._, A <CancellationToken> ._)).Invokes(action =>
            {
                var task = ((IEnumerable <IUnleashScheduledTask>)action.Arguments[0]).First();
                task.ExecuteAsync((CancellationToken)action.Arguments[1]).Wait();
            });

            fakeHttpMessageHandler.Response = new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(testDefinition.State.ToString(), Encoding.UTF8, "application/json"),
                Headers    =
                {
                    ETag = new EntityTagHeaderValue("\"123\"")
                }
            };

            var contextBuilder = new UnleashContext.Builder()
                                 .UserId(contextDefinition.UserId)
                                 .SessionId(contextDefinition.SessionId)
                                 .RemoteAddress(contextDefinition.RemoteAddress)
                                 .Environment(contextDefinition.Environment)
                                 .AppName(contextDefinition.AppName);

            if (contextDefinition.Properties != null)
            {
                foreach (var property in contextDefinition.Properties)
                {
                    contextBuilder.AddProperty(property.Key, property.Value);
                }
            }

            var settings = new UnleashSettings
            {
                AppName = testDefinition.Name,
                UnleashContextProvider = new DefaultUnleashContextProvider(contextBuilder.Build()),
                HttpClientFactory      = fakeHttpClientFactory,
                ScheduledTaskManager   = fakeScheduler,
                FileSystem             = fakeFileSystem
            };

            var unleash = new DefaultUnleash(settings);

            return(unleash);
        }
Пример #3
0
        /// <summary>
        /// Initializes a new instance of Unleash client.
        /// </summary>
        /// <param name="synchronousInitialization">If true, fetch and cache toggles before returning. If false, allow the unleash client schedule an initial poll of features in the background</param>
        /// <param name="strategies">Custom strategies, added in addtion to builtIn strategies.</param>
        public async Task <IUnleash> CreateClientAsync(UnleashSettings settings, bool synchronousInitialization = false, params IStrategy[] strategies)
        {
            if (synchronousInitialization)
            {
                settings.ScheduleFeatureToggleFetchImmediatly = false;
                var unleash = new DefaultUnleash(settings, strategies);
                await unleash.services.FetchFeatureTogglesTask.ExecuteAsync(CancellationToken.None).ConfigureAwait(false);

                return(unleash);
            }
            return(new DefaultUnleash(settings, strategies));
        }
        ///// <summary>
        ///// Initializes a new instance of Unleash client.
        ///// </summary>
        ///// <param name="SynchronousInitialization">If true, fetch and cache toggles before returning. If false, allow the unleash client schedule an initial poll of features in the background</param>
        public async Task <IUnleash> Generate(UnleashSettings settings, bool SynchronousInitialization = false)
        {
            if (SynchronousInitialization)
            {
                settings.ScheduleFeatureToggleFetchImmediatly = false;
                var unleash = new DefaultUnleash(settings);
                await unleash.services.FetchFeatureTogglesTask.ExecuteAsync(CancellationToken.None);

                return(unleash);
            }
            return(new DefaultUnleash(settings));
        }
Пример #5
0
        /// <summary>
        /// Initializes a new instance of Unleash client.
        /// </summary>
        /// <param name="synchronousInitialization">If true, fetch and cache toggles before returning. If false, allow the unleash client schedule an initial poll of features in the background</param>
        /// <param name="strategies">Custom strategies, added in addtion to builtIn strategies.</param>
        public IUnleash CreateClient(UnleashSettings settings, bool synchronousInitialization = false, params IStrategy[] strategies)
        {
            if (synchronousInitialization)
            {
                settings.ScheduleFeatureToggleFetchImmediatly = false;
                var unleash = new DefaultUnleash(settings, strategies);
                TaskFactory
                .StartNew(() => unleash.services.FetchFeatureTogglesTask.ExecuteAsync(CancellationToken.None))
                .Unwrap()
                .GetAwaiter()
                .GetResult();

                return(unleash);
            }
            return(new DefaultUnleash(settings, strategies));
        }
Пример #6
0
        private static void Run(DefaultUnleash unleash, UnleashContext context, string name, int maxRounds)
        {
            int currentRound = 0;
            var sw           = new Stopwatch();

            while (currentRound < maxRounds)
            {
                currentRound++;

                sw.Start();
                bool enabled = unleash.IsEnabled("Demo", context);
                sw.Stop();

                Console.WriteLine(name + "\t" + "Demo" + ":" + enabled + "\t " + sw.ElapsedMilliseconds + "ms");
                sw.Reset();

                Thread.Sleep(new Random().Next(10000));
            }
        }
Пример #7
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            UnleashSettings = new UnleashSettings()
            {
                UnleashApi = new Uri("http://unleash.herokuapp.com/api/"),
                //UnleashApi = new Uri("http://localhost:4242/api/"),
                AppName                = "dotnet-api-test",
                InstanceTag            = "instance 1",
                SendMetricsInterval    = TimeSpan.FromSeconds(20),
                UnleashContextProvider = new AspNetContextProvider(),
                //JsonSerializer = new JsonNetSerializer()
            };

            UnleashInfo = UnleashSettings.ToString();

            Unleash = new DefaultUnleash(UnleashSettings);
        }