Пример #1
0
        public void Publish_GreaterRevisionExcepted()
        {
            //arrange
            var cts = new CancellationTokenSource();

            var context = new ConfigContext(new Couchbase.Configuration());

            context.Start(cts);
            context.Subscribe(this);

            var config1 = new BucketConfig
            {
                Name = "default",
                Rev  = 1
            };

            var config2 = new BucketConfig
            {
                Name = "default",
                Rev  = 3
            };

            //act
            context.Publish(config1);
            Task.Delay(1, cts.Token).GetAwaiter().GetResult();
            context.Publish(config2);
            Task.Delay(1, cts.Token).GetAwaiter().GetResult();

            //assert
            Assert.Equal(config2.Rev, context.Get("default").Rev);
        }
Пример #2
0
        public void Publish_GreaterRevisionExcepted()
        {
            //arrange
            var cts = new CancellationTokenSource();

            var context = new ConfigContext(new ClusterOptions());

            context.Start(cts);
            context.Subscribe(_bucket);

            var config1 = new BucketConfig
            {
                Name = "default",
                Rev  = 1
            };

            var config2 = new BucketConfig
            {
                Name = "default",
                Rev  = 3
            };

            //act
            context.Publish(config1);

            _event.Wait(cts.Token);

            context.Publish(config2);

            _event.Wait(cts.Token);

            //assert
            Assert.Equal(config2.Rev, context.Get("default").Rev);
        }
Пример #3
0
        public void Can_Start_Stop_Start_Subscribe()
        {
            //arrange
            var cts = new CancellationTokenSource();

            var context = new ConfigContext(new Couchbase.Configuration());

            context.Start(cts);
            context.Subscribe(this);

            context.Stop();

            cts = new CancellationTokenSource();
            context.Start(cts);

            var config = new BucketConfig
            {
                Name = "default",
                Rev  = 1
            };

            context.Publish(config);
            Task.Delay(10, cts.Token).GetAwaiter().GetResult();

            //assert
            Assert.Equal(1u, context.Get("default").Rev);
        }
Пример #4
0
        public void Can_Start_Stop_Start_Subscribe()
        {
            //arrange
            var cts = new CancellationTokenSource();

            var context = new ConfigContext(new ClusterOptions());

            context.Start(cts);
            context.Subscribe(_bucket);

            context.Stop();

            cts = new CancellationTokenSource();
            context.Start(cts);

            var config = new BucketConfig
            {
                Name = "default",
                Rev  = 1
            };

            context.Publish(config);
            _event.Wait(cts.Token);

            //assert
            Assert.Equal(1u, context.Get("default").Rev);
        }
Пример #5
0
        public void Get_When_Stopped_Throw_ObjectDisposedException()
        {
            //arrange
            var cts = new CancellationTokenSource();

            var context = new ConfigContext(new Couchbase.Configuration());

            context.Start(cts);
            context.Subscribe(this);
            context.Stop();

            var config = new BucketConfig
            {
                Name = "default",
                Rev  = 1
            };

            //act
            Assert.Throws <ObjectDisposedException>(() =>
            {
                context.Publish(config);
                Task.Delay(10, cts.Token).GetAwaiter().GetResult();
            });
        }
Пример #6
0
        public void Get_When_Stopped_Throw_ObjectDisposedException()
        {
            //arrange
            var cts = new CancellationTokenSource();

            var context = new ConfigContext(new ClusterOptions());

            context.Start(cts);
            context.Subscribe(_bucket);
            context.Stop();

            var config = new BucketConfig
            {
                Name = "default",
                Rev  = 1
            };

            //act
            Assert.Throws <ObjectDisposedException>(() =>
            {
                context.Publish(config);
                _event.Wait(cts.Token);
            });
        }
        public void StartListening()
        {
            var streamingUriPath = "/pools/default/bs/" + _bucketName;

            Task.Run(async() =>
            {
                using (_httpClient)
                {
                    _httpClient.Timeout = Timeout.InfiniteTimeSpan;

                    var servers = _clusterOptions.Servers.ToList().Shuffle();
                    while (servers.Any())
                    {
                        try
                        {
                            var server = servers.First();
                            servers.Remove(server);

                            var streamingUri = new UriBuilder(server)
                            {
                                Scheme = _clusterOptions.UseSsl ? Uri.UriSchemeHttps : Uri.UriSchemeHttp,
                                Port   = _clusterOptions.MgmtPort,
                                Path   = streamingUriPath
                            };

                            var response = await _httpClient.GetAsync(streamingUri.Uri,
                                                                      HttpCompletionOption.ResponseHeadersRead,
                                                                      _cancellationToken).ConfigureAwait(false);

                            response.EnsureSuccessStatusCode();

                            using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
                            {
                                _cancellationToken.Register(Dispose);

                                if (stream.CanTimeout)
                                {
                                    //the stream itself can timeout if CanTimeout is true on a platform
                                    stream.ReadTimeout = Timeout.Infinite;
                                }

                                using (var reader = new StreamReader(stream, Encoding.UTF8, false))
                                {
                                    string config;
                                    while (!_cancellationToken.IsCancellationRequested &&
                                           (config = await reader.ReadLineAsync().ConfigureAwait(false)) != null)
                                    {
                                        if (config != string.Empty)
                                        {
                                            config           = config.Replace("$HOST", server.Host);
                                            var bucketConfig = JsonConvert.DeserializeObject <BucketConfig>(config);
                                            _couchbaseContext.Publish(bucketConfig);
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Log.LogError(e, "HTTP Streaming error.");
                        }
                    }
                }
            }, _cancellationToken);
        }