Exemplo n.º 1
0
 public OneTimeFetcher(ICrispinClient client)
 {
     _toggles = new Lazy <Task <IReadOnlyDictionary <Guid, Toggle> > >(async() =>
     {
         var toggles = await client.GetAllToggles();
         return(toggles.ToDictionary(t => t.ID));
     });
 }
Exemplo n.º 2
0
 private async Task SafeReadToggles(ICrispinClient client)
 {
     try
     {
         _toggles = (await client.GetAllToggles()).ToDictionary(t => t.ID);
     }
     catch (Exception)
     {
     }
 }
Exemplo n.º 3
0
        public OneTimeFetcherTests()
        {
            _toggles = Enumerable.Range(0, 5).Select(i => new Toggle {
                ID = Guid.NewGuid(), Name = i.ToString()
            }).ToArray();

            _client = Substitute.For <ICrispinClient>();
            _client.GetAllToggles().Returns(_toggles);

            _fetcher = new OneTimeFetcher(_client);
        }
Exemplo n.º 4
0
        public BatchingWriterTests()
        {
            var timeControl = Substitute.For <ITimeControl>();

            timeControl
            .When(t => t.Every(Arg.Any <TimeSpan>(), Arg.Any <Func <Task> >()))
            .Do(ci => _timer = ci.Arg <Func <Task> >());

            _client  = Substitute.For <ICrispinClient>();
            _writer  = new BatchingWriter(_client, time: timeControl, batchSize: BatchSize);
            _fixture = new Fixture();
        }
Exemplo n.º 5
0
        public BatchingWriter(ICrispinClient client, int batchSize = 5, ITimeControl time = null, TimeSpan interval = default(TimeSpan))
        {
            time     = time ?? new RealTimeControl();
            interval = interval != TimeSpan.Zero
                                ? interval
                                : TimeSpan.FromSeconds(5);

            _client    = client;
            _batchSize = batchSize;
            _pending   = new ConcurrentQueue <Statistic>();

            _cancel = time.Every(interval, Send);
        }
Exemplo n.º 6
0
        public PeriodicFetcherTests()
        {
            _toggles = Enumerable.Range(0, 5).Select(i => new Toggle {
                ID = Guid.NewGuid(), Name = i.ToString()
            }).ToArray();

            _client = Substitute.For <ICrispinClient>();

            _timeControl = Substitute.For <ITimeControl>();
            _timeControl
            .Delay(Arg.Any <TimeSpan>(), Arg.Any <CancellationToken>())
            .Returns(Task.CompletedTask);
        }
Exemplo n.º 7
0
        public PeriodicFetcher(ICrispinClient client, TimeSpan frequency, ITimeControl timeControl = null)
        {
            timeControl = timeControl ?? new RealTimeControl();

            _toggles         = new Dictionary <Guid, Toggle>();
            _source          = new CancellationTokenSource();
            _initialLoadDone = new TaskCompletionSource <bool>();

            _backgroundFetch = Task.Run(async() =>
            {
                await SafeReadToggles(client);
                _initialLoadDone.SetResult(true);

                while (_source.IsCancellationRequested == false)
                {
                    await timeControl.Delay(frequency, _source.Token);
                    await SafeReadToggles(client);
                }
            }, _source.Token);
        }