public async Task ListenerConfig_Should_Succeed() { var request = new AddListenerRequest { DataId = "dataId", //Group = "DEFAULT_GROUP", //Tenant = "tenant", Callbacks = new List <Action <string> > { x => { Console.WriteLine(x); }, } }; await _configClient.AddListenerAsync(request); Assert.True(true); await Task.Delay(1000); var rRequest = new RemoveListenerRequest { DataId = "dataId", }; await _configClient.RemoveListenerAsync(rRequest); await Task.Delay(50000); }
/// <summary> /// /// </summary> /// <param name="request"></param> /// <returns></returns> public Task RemoveListenerAsync(RemoveListenerRequest request) { if (request == null) { throw new NacosException(ConstValue.CLIENT_INVALID_PARAM, "request param invalid"); } if (string.IsNullOrWhiteSpace(request.Tenant)) { request.Tenant = _options.Namespace; } if (string.IsNullOrWhiteSpace(request.Group)) { request.Group = ConstValue.DefaultGroup; } request.CheckParam(); var name = BuildName(request.Tenant, request.Group, request.DataId); if (!listeners.Any(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase))) { _logger.LogWarning($"[remove-listener] error, {name} was not added."); return(Task.CompletedTask); } var list = listeners.Where(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase)).ToList(); // clean timer foreach (var item in list) { item.Timer.Dispose(); item.Timer = null; } // remove listeners listeners.RemoveAll(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); foreach (var cb in request.Callbacks) { try { cb(); } catch (Exception ex) { _logger.LogError(ex, $"[remove-listener] call back throw exception, dataId={request.DataId}, group={request.Group}, tenant={request.Tenant}"); } } return(Task.CompletedTask); }