|
| 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