コード例 #1
0
        public async Task TestGetContentWaitTillCreated()
        {
            LOG.Info("--- testGetContentWaitTillCreated");
            Holder <string> contentHolder = new Holder <string>();

            Task task = new Task(() =>
            {
                ContentWatcher <string> watcher = new ContentWatcher <string>(_zkClient, FILE_NAME);
                try
                {
                    watcher.Start();
                    contentHolder.value = watcher.GetContent();
                    watcher.Stop();
                }
                catch (Exception)
                {
                }
            });

            task.Start();

            // create content after 200ms
            await Task.Delay(200);

            await _zkClient.CreatePersistentAsync(FILE_NAME, "aaa");

            // we give the thread some time to pick up the change
            await Task.Delay(1000);

            Assert.True("aaa" == contentHolder.value);
        }
コード例 #2
0
        public virtual ContentWatcher Watcher(params string[] name) {
            if (_watchers == null) {
                _watchers = new Dictionary<string, ContentWatcher>();
            }

            string key;
            string local;

            if (name.Length == 0) {
                key = "";
                local = null;
            } else {
                key = Path.Combine(name);
                local = key;
            }

            ContentWatcher result;
            if (_watchers.TryGetValue(key, out result)) return result;

            var directory = GetSubdirectoryFilename(local);
            if (!Directory.Exists(directory)) {
                Directory.CreateDirectory(directory);
            }

            _watchers[key] = new ContentWatcher(directory);
            return _watchers[key];
        }
コード例 #3
0
        public void TestHandlingNullContent()
        {
            LOG.Info("--- testHandlingNullContent");
            ContentWatcher <string> watcher = new ContentWatcher <string>(_zkClient, FILE_NAME);

            watcher.Start();
            _zkClient.CreatePersistent(FILE_NAME, null);
            Assert.True(string.IsNullOrEmpty(watcher.GetContent()));
            watcher.Stop();
        }
コード例 #4
0
        protected ContentWatcher GetWatcher(string directory)
        {
            var key = FileUtils.NormalizePath(directory);

            if (Watchers.TryGetValue(key, out var result))
            {
                return(result);
            }

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            _watchers[key] = new ContentWatcher(directory);
            return(_watchers[key]);
        }
コード例 #5
0
        public void TestGetContent()
        {
            LOG.Info("--- testGetContent");
            ContentWatcher <string> watcher = new ContentWatcher <string>(_zkClient, FILE_NAME);

            watcher.Start();
            _zkClient.CreatePersistent(FILE_NAME, "a");
            Assert.True("a" == watcher.GetContent());

            // update the content
            _zkClient.WriteData(FILE_NAME, "b");

            string contentFromWatcher = TestUtil.WaitUntil("b", () => { return(watcher.GetContent()); }, new TimeSpan(0, 0, 0, 5));

            Assert.True("b" == contentFromWatcher);
            watcher.Stop();
        }
コード例 #6
0
        public async Task TestGetContent()
        {
            LOG.Info("--- testGetContent");
            ContentWatcher <string> watcher = new ContentWatcher <string>(_zkClient, FILE_NAME);

            watcher.Start();
            await _zkClient.CreatePersistentAsync(FILE_NAME, "a");

            Assert.True("a" == watcher.GetContent());

            // update the content
            await _zkClient.SetDataAsync(FILE_NAME, "b");

            string contentFromWatcher = TestUtil.WaitUntil(
                "b",
                () => { return(watcher.GetContent()); },
                TimeSpan.FromSeconds(5));

            Assert.True("b" == contentFromWatcher);
            watcher.Stop();
        }
コード例 #7
0
        public virtual ContentWatcher Watcher(params string[] name) {
            if (_watchers == null) {
                _watchers = new Dictionary<string, ContentWatcher>();
            }

            var nameCombined = Path.Combine(name);

            if (_watchers.ContainsKey(nameCombined)) return _watchers[nameCombined];

            var directory = GetSubdirectoryFilename(nameCombined);
            if (!Directory.Exists(directory)) {
                Directory.CreateDirectory(directory);
            }
            _watchers[nameCombined] = new ContentWatcher(directory);
            return _watchers[nameCombined];
        }