Skip to content

Commit f32cf9e

Browse files
authored
fix warnings for net8 (#1481)
* fix ca2007 * xUnit1031 * fix ca2007 * fix ca2007 * remove deprecated ctor * fix xUnit1031 * fix missing doc * fix missing dispose * fix ex
1 parent f75c4aa commit f32cf9e

18 files changed

+180
-206
lines changed

src/KubernetesClient/Autorest/HttpOperationException.cs

-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4-
using System.Runtime.Serialization;
5-
64
namespace k8s.Autorest
75
{
86
/// <summary>
97
/// Exception thrown for an invalid response with custom error information.
108
/// </summary>
11-
[Serializable]
129
public class HttpOperationException : Exception
1310
{
1411
/// <summary>
@@ -51,15 +48,5 @@ public HttpOperationException(string message, Exception innerException)
5148
: base(message, innerException)
5249
{
5350
}
54-
55-
/// <summary>
56-
/// Initializes a new instance of the <see cref="HttpOperationException"/> class.
57-
/// </summary>
58-
/// <param name="info">Serialization info.</param>
59-
/// <param name="context">Streaming context.</param>
60-
protected HttpOperationException(SerializationInfo info, StreamingContext context)
61-
: base(info, context)
62-
{
63-
}
6451
}
6552
}

src/KubernetesClient/KubernetesException.cs

-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System.Runtime.Serialization;
2-
31
namespace k8s
42
{
53
/// <summary>
@@ -72,22 +70,6 @@ public KubernetesException(string message, Exception innerException)
7270
{
7371
}
7472

75-
/// <summary>
76-
/// Initializes a new instance of the <see cref="KubernetesException"/> class with serialized data.
77-
/// </summary>
78-
/// <param name="info">
79-
/// The <see cref="SerializationInfo"/> that holds the serialized
80-
/// object data about the exception being thrown.
81-
/// </param>
82-
/// <param name="context">
83-
/// The <see cref="StreamingContext"/> that contains contextual information
84-
/// about the source or destination.
85-
/// </param>
86-
protected KubernetesException(SerializationInfo info, StreamingContext context)
87-
: base(info, context)
88-
{
89-
}
90-
9173
/// <summary>
9274
/// Gets, when this exception was raised because of a Kubernetes status message, the underlying
9375
/// Kubernetes status message.

src/KubernetesClient/LeaderElection/LeaderElector.cs

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public string GetLeader()
5454
/// Will complete the returned Task and not retry to acquire leadership again after leadership is lost once.
5555
/// </summary>
5656
/// <param name="cancellationToken">A token to cancel the operation.</param>
57+
/// <returns>A Task representing the asynchronous operation.</returns>
5758
public async Task RunUntilLeadershipLostAsync(CancellationToken cancellationToken = default)
5859
{
5960
await AcquireAsync(cancellationToken).ConfigureAwait(false);
@@ -132,6 +133,7 @@ public async Task RunAndTryToHoldLeadershipForeverAsync(CancellationToken cancel
132133
/// </summary>
133134
/// <seealso cref="RunUntilLeadershipLostAsync"/>
134135
/// <param name="cancellationToken">A token to cancel the operation.</param>
136+
/// <returns>A Task representing the asynchronous operation.</returns>
135137
[Obsolete("Replaced by RunUntilLeadershipLostAsync to encode behavior in method name.")]
136138
public Task RunAsync(CancellationToken cancellationToken = default)
137139
{

tests/E2E.Tests/MinikubeTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ void DeleteEndpoints(string name)
303303
le.OnStartedLeading += () => leader1acq.Set();
304304
le.OnStoppedLeading += () => leader1lose.Set();
305305

306-
tasks.Add(le.RunAsync(cts.Token));
306+
tasks.Add(le.RunUntilLeadershipLostAsync(cts.Token));
307307
}
308308

309309
// wait 1 become leader
@@ -325,7 +325,7 @@ void DeleteEndpoints(string name)
325325
leader2init.Set();
326326
};
327327

328-
tasks.Add(le.RunAsync());
328+
tasks.Add(le.RunUntilLeadershipLostAsync());
329329
Assert.True(leader2init.WaitOne(TimeSpan.FromSeconds(30)));
330330

331331
Assert.Equal("leader1", le.GetLeader());

tests/KubernetesClient.Classic.Tests/SimpleTests.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public void Dispose()
5252
{
5353
running = false;
5454
server.Stop();
55+
#if NET8_0_OR_GREATER
56+
server.Dispose();
57+
#endif
5558
loop.Wait();
5659
loop.Dispose();
5760
}
@@ -74,7 +77,7 @@ public async Task QueryPods()
7477
});
7578
var client = new Kubernetes(new KubernetesClientConfiguration { Host = server.Addr });
7679

77-
var pod = await client.CoreV1.ReadNamespacedPodAsync("pod", "default").ConfigureAwait(false);
80+
var pod = await client.CoreV1.ReadNamespacedPodAsync("pod", "default").ConfigureAwait(true);
7881

7982
Assert.Equal("pod0", pod.Metadata.Name);
8083
}

tests/KubernetesClient.Tests/AuthTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ private X509Certificate2 OpenCertificateStore(Stream stream)
541541
var store = new Pkcs12Store();
542542
store.Load(stream, new char[] { });
543543

544-
var keyAlias = store.Aliases.Cast<string>().SingleOrDefault(a => store.IsKeyEntry(a));
544+
var keyAlias = store.Aliases.Cast<string>().SingleOrDefault(store.IsKeyEntry);
545545

546546
var key = (RsaPrivateCrtKeyParameters)store.GetKey(keyAlias).Key;
547547
var bouncyCertificate = store.GetCertificate(keyAlias).Certificate;

tests/KubernetesClient.Tests/ByteBufferTests.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public async Task ReadBlocksUntilDataAvailableTest()
255255

256256
// Kick off a read operation
257257
var readTask = Task.Run(() => read = buffer.Read(readData, 0, readData.Length));
258-
await Task.Delay(250).ConfigureAwait(false);
258+
await Task.Delay(250).ConfigureAwait(true);
259259
Assert.False(readTask.IsCompleted, "Read task completed before data was available.");
260260

261261
// Write data to the buffer
@@ -264,7 +264,7 @@ public async Task ReadBlocksUntilDataAvailableTest()
264264
await TaskAssert.Completed(
265265
readTask,
266266
TimeSpan.FromMilliseconds(1000),
267-
"Timed out waiting for read task to complete.").ConfigureAwait(false);
267+
"Timed out waiting for read task to complete.").ConfigureAwait(true);
268268

269269
Assert.Equal(3, read);
270270
Assert.Equal(0xF0, readData[0]);
@@ -411,10 +411,10 @@ public async Task ReadFirstTest()
411411
var output = new byte[buffer.Size + 1];
412412

413413
var readTask = Task.Run(() => buffer.Read(output, 0, output.Length));
414-
await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
414+
await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(true);
415415

416416
buffer.Write(data, 0, data.Length);
417-
await readTask.ConfigureAwait(false);
417+
await readTask.ConfigureAwait(true);
418418
}
419419

420420
#if NETCOREAPP2_0

tests/KubernetesClient.Tests/KubernetesClientConfigurationTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ public async Task ContextWithClusterExtensions()
437437
{
438438
var path = Path.GetFullPath("assets/kubeconfig.cluster-extensions.yml");
439439

440-
_ = await KubernetesClientConfiguration.BuildConfigFromConfigFileAsync(new FileInfo(path)).ConfigureAwait(false);
440+
_ = await KubernetesClientConfiguration.BuildConfigFromConfigFileAsync(new FileInfo(path)).ConfigureAwait(true);
441441
}
442442

443443
[Fact]

tests/KubernetesClient.Tests/KubernetesExecTests.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public async Task WebSocketNamespacedPodExecAsync()
5151
{
5252
{ "X-My-Header", new List<string>() { "myHeaderValue", "myHeaderValue2" } },
5353
},
54-
cancellationToken: CancellationToken.None).ConfigureAwait(false);
54+
cancellationToken: CancellationToken.None).ConfigureAwait(true);
5555

5656
var expectedHeaders = new Dictionary<string, string>()
5757
{
@@ -91,7 +91,7 @@ public async Task WebSocketNamespacedPodPortForwardAsync()
9191
{
9292
{ "X-My-Header", new List<string>() { "myHeaderValue", "myHeaderValue2" } },
9393
},
94-
cancellationToken: CancellationToken.None).ConfigureAwait(false);
94+
cancellationToken: CancellationToken.None).ConfigureAwait(true);
9595

9696
var expectedHeaders = new Dictionary<string, string>()
9797
{
@@ -137,7 +137,7 @@ public async Task WebSocketNamespacedPodAttachAsync()
137137
{
138138
{ "X-My-Header", new List<string>() { "myHeaderValue", "myHeaderValue2" } },
139139
},
140-
cancellationToken: CancellationToken.None).ConfigureAwait(false);
140+
cancellationToken: CancellationToken.None).ConfigureAwait(true);
141141

142142
var expectedHeaders = new Dictionary<string, string>()
143143
{

tests/KubernetesClient.Tests/KubernetesMetricsTests.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public async Task NodesMetrics()
3535
{
3636
var client = new Kubernetes(new KubernetesClientConfiguration { Host = server.Uri.ToString() });
3737

38-
var nodesMetricsList = await client.GetKubernetesNodesMetricsAsync().ConfigureAwait(false);
38+
var nodesMetricsList = await client.GetKubernetesNodesMetricsAsync().ConfigureAwait(true);
3939

4040
Assert.Single(nodesMetricsList.Items);
4141

@@ -56,7 +56,7 @@ public async Task NodesMetricsOptionalProperty()
5656
var client = new Kubernetes(new KubernetesClientConfiguration { Host = server.Uri.ToString() });
5757

5858
// Should not throw with timespan optional property
59-
var exception = await Record.ExceptionAsync(async () => await client.GetKubernetesNodesMetricsAsync().ConfigureAwait(false)).ConfigureAwait(false);
59+
var exception = await Record.ExceptionAsync(client.GetKubernetesNodesMetricsAsync).ConfigureAwait(true);
6060

6161
Assert.Null(exception);
6262
}
@@ -69,7 +69,7 @@ public async Task PodsMetrics()
6969
{
7070
var client = new Kubernetes(new KubernetesClientConfiguration { Host = server.Uri.ToString() });
7171

72-
var podsMetricsList = await client.GetKubernetesPodsMetricsAsync().ConfigureAwait(false);
72+
var podsMetricsList = await client.GetKubernetesPodsMetricsAsync().ConfigureAwait(true);
7373

7474
Assert.Single(podsMetricsList.Items);
7575

@@ -94,7 +94,7 @@ public async Task PodsMetricsOptionalProperty()
9494
var client = new Kubernetes(new KubernetesClientConfiguration { Host = server.Uri.ToString() });
9595

9696
// Should not throw with timespan optional property
97-
var exception = await Record.ExceptionAsync(async () => await client.GetKubernetesPodsMetricsAsync().ConfigureAwait(false)).ConfigureAwait(false);
97+
var exception = await Record.ExceptionAsync(client.GetKubernetesPodsMetricsAsync).ConfigureAwait(true);
9898

9999
Assert.Null(exception);
100100
}
@@ -107,7 +107,7 @@ public async Task PodsMetricsEmptyResponse()
107107
{
108108
var client = new Kubernetes(new KubernetesClientConfiguration { Host = server.Uri.ToString() });
109109

110-
var podsMetricsList = await client.GetKubernetesPodsMetricsByNamespaceAsync("empty").ConfigureAwait(false);
110+
var podsMetricsList = await client.GetKubernetesPodsMetricsByNamespaceAsync("empty").ConfigureAwait(true);
111111

112112
Assert.Empty(podsMetricsList.Items);
113113
}
@@ -122,7 +122,7 @@ public async Task PodsMetricsByNamespace()
122122
{
123123
var client = new Kubernetes(new KubernetesClientConfiguration { Host = server.Uri.ToString() });
124124

125-
var podsMetricsList = await client.GetKubernetesPodsMetricsByNamespaceAsync(namespaceName).ConfigureAwait(false);
125+
var podsMetricsList = await client.GetKubernetesPodsMetricsByNamespaceAsync(namespaceName).ConfigureAwait(true);
126126

127127
Assert.Single(podsMetricsList.Items);
128128

@@ -147,7 +147,7 @@ public async Task PodsMetricsNonExistingNamespaceResponse()
147147
{
148148
var client = new Kubernetes(new KubernetesClientConfiguration { Host = server.Uri.ToString() });
149149

150-
var podsMetricsList = await client.GetKubernetesPodsMetricsByNamespaceAsync("nonexisting").ConfigureAwait(false);
150+
var podsMetricsList = await client.GetKubernetesPodsMetricsByNamespaceAsync("nonexisting").ConfigureAwait(true);
151151

152152
Assert.Empty(podsMetricsList.Items);
153153
}

tests/KubernetesClient.Tests/KubernetesYamlTests.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ public async Task LoadAllFromFile()
136136
var tempFileName = Path.GetTempFileName();
137137
try
138138
{
139-
await File.WriteAllTextAsync(tempFileName, content).ConfigureAwait(false);
139+
await File.WriteAllTextAsync(tempFileName, content).ConfigureAwait(true);
140140

141-
var objs = await KubernetesYaml.LoadAllFromFileAsync(tempFileName).ConfigureAwait(false);
141+
var objs = await KubernetesYaml.LoadAllFromFileAsync(tempFileName).ConfigureAwait(true);
142142
Assert.Equal(2, objs.Count);
143143
Assert.IsType<V1Pod>(objs[0]);
144144
Assert.IsType<V1Namespace>(objs[1]);
@@ -175,9 +175,9 @@ public async Task LoadAllFromFileWithTypes()
175175
var tempFileName = Path.GetTempFileName();
176176
try
177177
{
178-
await File.WriteAllTextAsync(tempFileName, content).ConfigureAwait(false);
178+
await File.WriteAllTextAsync(tempFileName, content).ConfigureAwait(true);
179179

180-
var objs = await KubernetesYaml.LoadAllFromFileAsync(tempFileName, types).ConfigureAwait(false);
180+
var objs = await KubernetesYaml.LoadAllFromFileAsync(tempFileName, types).ConfigureAwait(true);
181181
Assert.Equal(2, objs.Count);
182182
Assert.IsType<MyPod>(objs[0]);
183183
Assert.IsType<V1Namespace>(objs[1]);
@@ -282,7 +282,7 @@ public void LoadPropertyNamedReadOnlyFromString()
282282
}
283283

284284
[Fact]
285-
public void LoadFromStream()
285+
public async Task LoadFromStream()
286286
{
287287
var content = @"apiVersion: v1
288288
kind: Pod
@@ -292,7 +292,7 @@ public void LoadFromStream()
292292

293293
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(content));
294294

295-
var obj = KubernetesYaml.LoadFromStreamAsync<V1Pod>(stream).Result;
295+
var obj = await KubernetesYaml.LoadFromStreamAsync<V1Pod>(stream).ConfigureAwait(true);
296296

297297
Assert.Equal("foo", obj.Metadata.Name);
298298
}
@@ -309,9 +309,9 @@ public async Task LoadFromFile()
309309
var tempFileName = Path.GetTempFileName();
310310
try
311311
{
312-
await File.WriteAllTextAsync(tempFileName, content).ConfigureAwait(false);
312+
await File.WriteAllTextAsync(tempFileName, content).ConfigureAwait(true);
313313

314-
var obj = await KubernetesYaml.LoadFromFileAsync<V1Pod>(tempFileName).ConfigureAwait(false);
314+
var obj = await KubernetesYaml.LoadFromFileAsync<V1Pod>(tempFileName).ConfigureAwait(true);
315315
Assert.Equal("foo", obj.Metadata.Name);
316316
}
317317
finally

tests/KubernetesClient.Tests/LeaderElection/LeaderElectionTests.cs

+11-11
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void SimpleLeaderElection()
5555
};
5656

5757
var countdown = new CountdownEvent(2);
58-
Task.Run(() =>
58+
Task.Run(async () =>
5959
{
6060
var leaderElector = new LeaderElector(leaderElectionConfig);
6161

@@ -71,7 +71,7 @@ public void SimpleLeaderElection()
7171
countdown.Signal();
7272
};
7373

74-
leaderElector.RunUntilLeadershipLostAsync().Wait();
74+
await leaderElector.RunUntilLeadershipLostAsync().ConfigureAwait(true);
7575
});
7676

7777
countdown.Wait(TimeSpan.FromSeconds(10));
@@ -147,7 +147,7 @@ public void LeaderElection()
147147
var lockAStopLeading = new ManualResetEvent(false);
148148
var testLeaderElectionLatch = new CountdownEvent(4);
149149

150-
Task.Run(() =>
150+
Task.Run(async () =>
151151
{
152152
var leaderElector = new LeaderElector(leaderElectionConfigA);
153153

@@ -164,13 +164,13 @@ public void LeaderElection()
164164
lockAStopLeading.Set();
165165
};
166166

167-
leaderElector.RunUntilLeadershipLostAsync().Wait();
167+
await leaderElector.RunUntilLeadershipLostAsync().ConfigureAwait(true);
168168
});
169169

170170

171171
lockAStopLeading.WaitOne(TimeSpan.FromSeconds(3));
172172

173-
Task.Run(() =>
173+
Task.Run(async () =>
174174
{
175175
var leaderElector = new LeaderElector(leaderElectionConfigB);
176176

@@ -186,7 +186,7 @@ public void LeaderElection()
186186
testLeaderElectionLatch.Signal();
187187
};
188188

189-
leaderElector.RunUntilLeadershipLostAsync().Wait();
189+
await leaderElector.RunUntilLeadershipLostAsync().ConfigureAwait(true);
190190
});
191191

192192
testLeaderElectionLatch.Wait(TimeSpan.FromSeconds(15));
@@ -256,7 +256,7 @@ public void LeaderElectionWithRenewDeadline()
256256
};
257257

258258
var countdown = new CountdownEvent(2);
259-
Task.Run(() =>
259+
Task.Run(async () =>
260260
{
261261
var leaderElector = new LeaderElector(leaderElectionConfig);
262262

@@ -272,7 +272,7 @@ public void LeaderElectionWithRenewDeadline()
272272
countdown.Signal();
273273
};
274274

275-
leaderElector.RunUntilLeadershipLostAsync().Wait();
275+
await leaderElector.RunUntilLeadershipLostAsync().ConfigureAwait(true);
276276
});
277277

278278
countdown.Wait(TimeSpan.FromSeconds(15));
@@ -290,7 +290,7 @@ public void LeaderElectionWithRenewDeadline()
290290
}
291291

292292
[Fact]
293-
public void LeaderElectionThrowException()
293+
public async Task LeaderElectionThrowException()
294294
{
295295
var l = new Mock<ILock>();
296296
l.Setup(obj => obj.GetAsync(CancellationToken.None))
@@ -305,11 +305,11 @@ public void LeaderElectionThrowException()
305305

306306
try
307307
{
308-
leaderElector.RunUntilLeadershipLostAsync().Wait();
308+
await leaderElector.RunUntilLeadershipLostAsync().ConfigureAwait(true);
309309
}
310310
catch (Exception e)
311311
{
312-
Assert.Equal("noxu", e.InnerException?.Message);
312+
Assert.Equal("noxu", e.Message);
313313
return;
314314
}
315315

0 commit comments

Comments
 (0)