Skip to content

Commit 8af4723

Browse files
authored
Add remove method to balancer attributes collection (#2110)
1 parent 94a8167 commit 8af4723

File tree

2 files changed

+136
-1
lines changed

2 files changed

+136
-1
lines changed

src/Grpc.Net.Client/Balancer/BalancerAttributes.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#region Copyright notice and license
1+
#region Copyright notice and license
22

33
// Copyright 2019 The gRPC Authors
44
//
@@ -113,5 +113,19 @@ public void Set<TValue>(BalancerAttributesKey<TValue> key, TValue value)
113113
{
114114
_attributes[key.Key] = value;
115115
}
116+
117+
/// <summary>
118+
/// Removes the value associated with the specified key.
119+
/// </summary>
120+
/// <typeparam name="TValue">The value type.</typeparam>
121+
/// <param name="key">The key of the value to set.</param>
122+
/// <returns>
123+
/// <c>true</c> if the element is successfully removed; otherwise, <c>false</c>.
124+
/// This method also returns <c>false</c> if key was not found.
125+
/// </returns>
126+
public bool Remove<TValue>(BalancerAttributesKey<TValue> key)
127+
{
128+
return _attributes.Remove(key.Key);
129+
}
116130
}
117131
#endif
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The gRPC Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#endregion
18+
19+
#if SUPPORT_LOAD_BALANCING
20+
using System;
21+
using System.Net.Http;
22+
using System.Threading;
23+
using System.Threading.Tasks;
24+
using Greet;
25+
using Grpc.Core;
26+
using Grpc.Net.Client.Tests.Infrastructure;
27+
using Grpc.Net.Client.Configuration;
28+
using Grpc.Tests.Shared;
29+
using NUnit.Framework;
30+
using Microsoft.Extensions.Logging.Testing;
31+
using System.Linq;
32+
using Microsoft.Extensions.Logging;
33+
using Microsoft.Extensions.DependencyInjection;
34+
using System.Net;
35+
using System.Collections.Generic;
36+
using Grpc.Net.Client.Balancer.Internal;
37+
using System.IO;
38+
using Grpc.Net.Client.Tests.Infrastructure.Balancer;
39+
using Grpc.Net.Client.Balancer;
40+
41+
namespace Grpc.Net.Client.Tests.Balancer;
42+
43+
[TestFixture]
44+
public class BalancerAttributesTests
45+
{
46+
[Test]
47+
public void TryGetValue_NotFound_ReturnFalse()
48+
{
49+
// Arrange
50+
var key = new BalancerAttributesKey<string>("key");
51+
52+
var attributes = new BalancerAttributes();
53+
54+
// Act & Assert
55+
Assert.False(attributes.TryGetValue(key, out var value));
56+
Assert.Null(value);
57+
}
58+
59+
[Test]
60+
public void TryGetValue_Found_ReturnTrue()
61+
{
62+
// Arrange
63+
var key = new BalancerAttributesKey<string>("key");
64+
65+
var attributes = new BalancerAttributes();
66+
67+
attributes.Set(key, "value");
68+
69+
// Act & Assert
70+
Assert.True(attributes.TryGetValue(key, out var value));
71+
Assert.AreEqual("value", value);
72+
}
73+
74+
[Test]
75+
public void Remove_NotFound_ReturnFalse()
76+
{
77+
// Arrange
78+
var key = new BalancerAttributesKey<string>("key");
79+
80+
var attributes = new BalancerAttributes();
81+
82+
// Act & Assert
83+
Assert.False(attributes.Remove(key));
84+
}
85+
86+
[Test]
87+
public void Remove_Found_ReturnTrue()
88+
{
89+
// Arrange
90+
var key = new BalancerAttributesKey<string>("key");
91+
92+
var attributes = new BalancerAttributes();
93+
94+
attributes.Set(key, "value");
95+
96+
// Act & Assert
97+
Assert.True(attributes.Remove(key));
98+
99+
Assert.False(attributes.TryGetValue(key, out var value));
100+
Assert.Null(value);
101+
}
102+
103+
[Test]
104+
public void Set_Null_NotRemoved()
105+
{
106+
// Arrange
107+
var key = new BalancerAttributesKey<string?>("key");
108+
109+
var attributes = new BalancerAttributes();
110+
111+
// Act
112+
attributes.Set(key, "value");
113+
// Sets value to a null value but still in collection
114+
attributes.Set(key, null);
115+
116+
// Assert
117+
Assert.True(attributes.Remove(key));
118+
}
119+
}
120+
121+
#endif

0 commit comments

Comments
 (0)