public void CopyOnWriteDictionary_GetOrAdd_Should_Return_The_Current_Value() { var map = new CopyOnWriteDictionary<string, int>(); Assert.AreEqual(0, map.Count); var value = map.GetOrAdd("key1", 1); Assert.AreEqual(1, value); value = map.GetOrAdd("key1", 2); //not modified Assert.AreEqual(1, value); Assert.AreEqual(1, map.Count); Assert.AreEqual(1, map["key1"]); value = map.GetOrAdd("key2", 10); Assert.AreEqual(10, value); Assert.AreEqual(2, map.Count); Assert.AreEqual(10, map["key2"]); }
/// <inheritdoc /> public INodeMetrics GetOrCreateNodeMetrics(Host host) { var value = _nodeMetricsCollection.GetOrAdd(host, h => { var nodeBucket = $"{_sessionBucket}.nodes.{MetricsManager.BuildHostAddressMetricPath(host.Address)}"; var newRegistry = new NodeMetrics(_driverMetricsProvider, _metricsOptions, _metricsEnabled, nodeBucket); _nodeMetricsRegistryCollection.Add(host, newRegistry.MetricsRegistry); return(newRegistry); }); return(value); }
/// <summary> /// Adds the host if not exists /// </summary> public Host Add(IPEndPoint key, IContactPoint contactPoint) { var newHost = new Host(key, contactPoint); var host = _hosts.GetOrAdd(key, newHost); if (!ReferenceEquals(newHost, host)) { //The host was not added, return the existing host return(host); } //The node was added host.Down += OnHostDown; host.Up += OnHostUp; Added?.Invoke(newHost); return(host); }
/// <summary> /// Adds the host if not exists /// </summary> public Host Add(IPEndPoint key) { var newHost = new Host(key); var host = _hosts.GetOrAdd(key, newHost); if (!ReferenceEquals(newHost, host)) { //The host was not added, return the existing host return(host); } //The node was added host.Down += OnHostDown; host.Up += OnHostUp; if (Added != null) { Added(newHost); } return(host); }