public async Task SubscribeChildrenChangeTest() { var path = $"/{DateTime.Now:yyyy_MM_dd_HH_mm_ss_ff}"; var path2 = $"{path}/123"; try { if (await _client.ExistsAsync(path)) { await _client.DeleteRecursiveAsync(path); } var types = new List <Watcher.Event.EventType>(); var semaphore = new Semaphore(0, 2); await _client.SubscribeDataChange(path, (client, args) => { if (args.Type == Watcher.Event.EventType.NodeCreated) { semaphore.Release(); } return(Task.CompletedTask); }); await _client.SubscribeChildrenChange(path, (client, args) => { types.Add(args.Type); semaphore.Release(); return(Task.CompletedTask); }); await _client.CreatePersistentAsync(path, null); semaphore.WaitOne(10000); await _client.CreatePersistentAsync(path2, null); semaphore.WaitOne(10000); Assert.Equal(Watcher.Event.EventType.NodeChildrenChanged, types[0]); } finally { if (await _client.ExistsAsync(path)) { await _client.DeleteRecursiveAsync(path); } } }
public async Task CreateRecursiveAndDeleteRecursiveTest() { var pathRoot = $"/{DateTime.Now:yyyy_MM_dd_HH_mm_ss_ff}"; var path = $"{pathRoot}/1/2"; if (await _client.ExistsAsync(pathRoot)) { await _client.DeleteRecursiveAsync(pathRoot); } await _client.CreateRecursiveAsync(path, null); Assert.True(await _client.ExistsAsync(path)); await _client.DeleteRecursiveAsync(pathRoot); if (await _client.ExistsAsync(pathRoot)) { throw new Exception("删除失败"); } }
/// <summary> /// 清理指定的缓存依赖节点 /// </summary> /// <param name="node">节点名称</param> /// <param name="clearChildren">是否递归清理所有子节点</param> /// <returns>Task</returns> public Task ClearDependentNode(string node, bool clearChildren = false) { if (clearChildren) { return(_zkClient.DeleteRecursiveAsync(node)); } else { string value = GetDefaultValue(); return(_zkClient.SetDataAsync(node, Encoding.UTF8.GetBytes(value))); } }
/// <summary> /// 递归删除该节点下的所有子节点和该节点本身。 /// </summary> /// <param name="client">ZooKeeper客户端。</param> /// <param name="path">节点路径。</param> /// <returns>如果成功则返回true,false。</returns> public static async Task <bool> DeleteRecursiveAsync(this IZookeeperClient client, string path) { IEnumerable <string> children; try { children = await client.GetChildrenAsync(path); } catch (KeeperException.NoNodeException) { return(true); } foreach (var subPath in children) { if (!await client.DeleteRecursiveAsync(path + "/" + subPath)) { return(false); } } await client.DeleteAsync(path); return(true); }