Skip to content

Commit

Permalink
fix warnings for net8 (#1481)
Browse files Browse the repository at this point in the history
* fix ca2007

* xUnit1031

* fix ca2007

* fix ca2007

* remove deprecated ctor

* fix xUnit1031

* fix missing doc

* fix missing dispose

* fix ex
  • Loading branch information
tg123 authored Jan 9, 2024
1 parent f75c4aa commit f32cf9e
Show file tree
Hide file tree
Showing 18 changed files with 180 additions and 206 deletions.
13 changes: 0 additions & 13 deletions src/KubernetesClient/Autorest/HttpOperationException.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Runtime.Serialization;

namespace k8s.Autorest
{
/// <summary>
/// Exception thrown for an invalid response with custom error information.
/// </summary>
[Serializable]
public class HttpOperationException : Exception
{
/// <summary>
Expand Down Expand Up @@ -51,15 +48,5 @@ public HttpOperationException(string message, Exception innerException)
: base(message, innerException)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="HttpOperationException"/> class.
/// </summary>
/// <param name="info">Serialization info.</param>
/// <param name="context">Streaming context.</param>
protected HttpOperationException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
}
18 changes: 0 additions & 18 deletions src/KubernetesClient/KubernetesException.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Runtime.Serialization;

namespace k8s
{
/// <summary>
Expand Down Expand Up @@ -72,22 +70,6 @@ public KubernetesException(string message, Exception innerException)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="KubernetesException"/> class with serialized data.
/// </summary>
/// <param name="info">
/// The <see cref="SerializationInfo"/> that holds the serialized
/// object data about the exception being thrown.
/// </param>
/// <param name="context">
/// The <see cref="StreamingContext"/> that contains contextual information
/// about the source or destination.
/// </param>
protected KubernetesException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}

/// <summary>
/// Gets, when this exception was raised because of a Kubernetes status message, the underlying
/// Kubernetes status message.
Expand Down
2 changes: 2 additions & 0 deletions src/KubernetesClient/LeaderElection/LeaderElector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public string GetLeader()
/// Will complete the returned Task and not retry to acquire leadership again after leadership is lost once.
/// </summary>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>A Task representing the asynchronous operation.</returns>
public async Task RunUntilLeadershipLostAsync(CancellationToken cancellationToken = default)
{
await AcquireAsync(cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -132,6 +133,7 @@ public async Task RunAndTryToHoldLeadershipForeverAsync(CancellationToken cancel
/// </summary>
/// <seealso cref="RunUntilLeadershipLostAsync"/>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>A Task representing the asynchronous operation.</returns>
[Obsolete("Replaced by RunUntilLeadershipLostAsync to encode behavior in method name.")]
public Task RunAsync(CancellationToken cancellationToken = default)
{
Expand Down
4 changes: 2 additions & 2 deletions tests/E2E.Tests/MinikubeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ void DeleteEndpoints(string name)
le.OnStartedLeading += () => leader1acq.Set();
le.OnStoppedLeading += () => leader1lose.Set();

tasks.Add(le.RunAsync(cts.Token));
tasks.Add(le.RunUntilLeadershipLostAsync(cts.Token));
}

// wait 1 become leader
Expand All @@ -325,7 +325,7 @@ void DeleteEndpoints(string name)
leader2init.Set();
};

tasks.Add(le.RunAsync());
tasks.Add(le.RunUntilLeadershipLostAsync());
Assert.True(leader2init.WaitOne(TimeSpan.FromSeconds(30)));

Assert.Equal("leader1", le.GetLeader());
Expand Down
5 changes: 4 additions & 1 deletion tests/KubernetesClient.Classic.Tests/SimpleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public void Dispose()
{
running = false;
server.Stop();
#if NET8_0_OR_GREATER
server.Dispose();
#endif
loop.Wait();
loop.Dispose();
}
Expand All @@ -74,7 +77,7 @@ public async Task QueryPods()
});
var client = new Kubernetes(new KubernetesClientConfiguration { Host = server.Addr });

var pod = await client.CoreV1.ReadNamespacedPodAsync("pod", "default").ConfigureAwait(false);
var pod = await client.CoreV1.ReadNamespacedPodAsync("pod", "default").ConfigureAwait(true);

Assert.Equal("pod0", pod.Metadata.Name);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/KubernetesClient.Tests/AuthTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ private X509Certificate2 OpenCertificateStore(Stream stream)
var store = new Pkcs12Store();
store.Load(stream, new char[] { });

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

var key = (RsaPrivateCrtKeyParameters)store.GetKey(keyAlias).Key;
var bouncyCertificate = store.GetCertificate(keyAlias).Certificate;
Expand Down
8 changes: 4 additions & 4 deletions tests/KubernetesClient.Tests/ByteBufferTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public async Task ReadBlocksUntilDataAvailableTest()

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

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

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

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

buffer.Write(data, 0, data.Length);
await readTask.ConfigureAwait(false);
await readTask.ConfigureAwait(true);
}

#if NETCOREAPP2_0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ public async Task ContextWithClusterExtensions()
{
var path = Path.GetFullPath("assets/kubeconfig.cluster-extensions.yml");

_ = await KubernetesClientConfiguration.BuildConfigFromConfigFileAsync(new FileInfo(path)).ConfigureAwait(false);
_ = await KubernetesClientConfiguration.BuildConfigFromConfigFileAsync(new FileInfo(path)).ConfigureAwait(true);
}

[Fact]
Expand Down
6 changes: 3 additions & 3 deletions tests/KubernetesClient.Tests/KubernetesExecTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public async Task WebSocketNamespacedPodExecAsync()
{
{ "X-My-Header", new List<string>() { "myHeaderValue", "myHeaderValue2" } },
},
cancellationToken: CancellationToken.None).ConfigureAwait(false);
cancellationToken: CancellationToken.None).ConfigureAwait(true);

var expectedHeaders = new Dictionary<string, string>()
{
Expand Down Expand Up @@ -91,7 +91,7 @@ public async Task WebSocketNamespacedPodPortForwardAsync()
{
{ "X-My-Header", new List<string>() { "myHeaderValue", "myHeaderValue2" } },
},
cancellationToken: CancellationToken.None).ConfigureAwait(false);
cancellationToken: CancellationToken.None).ConfigureAwait(true);

var expectedHeaders = new Dictionary<string, string>()
{
Expand Down Expand Up @@ -137,7 +137,7 @@ public async Task WebSocketNamespacedPodAttachAsync()
{
{ "X-My-Header", new List<string>() { "myHeaderValue", "myHeaderValue2" } },
},
cancellationToken: CancellationToken.None).ConfigureAwait(false);
cancellationToken: CancellationToken.None).ConfigureAwait(true);

var expectedHeaders = new Dictionary<string, string>()
{
Expand Down
14 changes: 7 additions & 7 deletions tests/KubernetesClient.Tests/KubernetesMetricsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public async Task NodesMetrics()
{
var client = new Kubernetes(new KubernetesClientConfiguration { Host = server.Uri.ToString() });

var nodesMetricsList = await client.GetKubernetesNodesMetricsAsync().ConfigureAwait(false);
var nodesMetricsList = await client.GetKubernetesNodesMetricsAsync().ConfigureAwait(true);

Assert.Single(nodesMetricsList.Items);

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

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

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

var podsMetricsList = await client.GetKubernetesPodsMetricsAsync().ConfigureAwait(false);
var podsMetricsList = await client.GetKubernetesPodsMetricsAsync().ConfigureAwait(true);

Assert.Single(podsMetricsList.Items);

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

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

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

var podsMetricsList = await client.GetKubernetesPodsMetricsByNamespaceAsync("empty").ConfigureAwait(false);
var podsMetricsList = await client.GetKubernetesPodsMetricsByNamespaceAsync("empty").ConfigureAwait(true);

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

var podsMetricsList = await client.GetKubernetesPodsMetricsByNamespaceAsync(namespaceName).ConfigureAwait(false);
var podsMetricsList = await client.GetKubernetesPodsMetricsByNamespaceAsync(namespaceName).ConfigureAwait(true);

Assert.Single(podsMetricsList.Items);

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

var podsMetricsList = await client.GetKubernetesPodsMetricsByNamespaceAsync("nonexisting").ConfigureAwait(false);
var podsMetricsList = await client.GetKubernetesPodsMetricsByNamespaceAsync("nonexisting").ConfigureAwait(true);

Assert.Empty(podsMetricsList.Items);
}
Expand Down
16 changes: 8 additions & 8 deletions tests/KubernetesClient.Tests/KubernetesYamlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ public async Task LoadAllFromFile()
var tempFileName = Path.GetTempFileName();
try
{
await File.WriteAllTextAsync(tempFileName, content).ConfigureAwait(false);
await File.WriteAllTextAsync(tempFileName, content).ConfigureAwait(true);

var objs = await KubernetesYaml.LoadAllFromFileAsync(tempFileName).ConfigureAwait(false);
var objs = await KubernetesYaml.LoadAllFromFileAsync(tempFileName).ConfigureAwait(true);
Assert.Equal(2, objs.Count);
Assert.IsType<V1Pod>(objs[0]);
Assert.IsType<V1Namespace>(objs[1]);
Expand Down Expand Up @@ -175,9 +175,9 @@ public async Task LoadAllFromFileWithTypes()
var tempFileName = Path.GetTempFileName();
try
{
await File.WriteAllTextAsync(tempFileName, content).ConfigureAwait(false);
await File.WriteAllTextAsync(tempFileName, content).ConfigureAwait(true);

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

[Fact]
public void LoadFromStream()
public async Task LoadFromStream()
{
var content = @"apiVersion: v1
kind: Pod
Expand All @@ -292,7 +292,7 @@ public void LoadFromStream()

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

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

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

var obj = await KubernetesYaml.LoadFromFileAsync<V1Pod>(tempFileName).ConfigureAwait(false);
var obj = await KubernetesYaml.LoadFromFileAsync<V1Pod>(tempFileName).ConfigureAwait(true);
Assert.Equal("foo", obj.Metadata.Name);
}
finally
Expand Down
22 changes: 11 additions & 11 deletions tests/KubernetesClient.Tests/LeaderElection/LeaderElectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void SimpleLeaderElection()
};

var countdown = new CountdownEvent(2);
Task.Run(() =>
Task.Run(async () =>
{
var leaderElector = new LeaderElector(leaderElectionConfig);
Expand All @@ -71,7 +71,7 @@ public void SimpleLeaderElection()
countdown.Signal();
};
leaderElector.RunUntilLeadershipLostAsync().Wait();
await leaderElector.RunUntilLeadershipLostAsync().ConfigureAwait(true);
});

countdown.Wait(TimeSpan.FromSeconds(10));
Expand Down Expand Up @@ -147,7 +147,7 @@ public void LeaderElection()
var lockAStopLeading = new ManualResetEvent(false);
var testLeaderElectionLatch = new CountdownEvent(4);

Task.Run(() =>
Task.Run(async () =>
{
var leaderElector = new LeaderElector(leaderElectionConfigA);
Expand All @@ -164,13 +164,13 @@ public void LeaderElection()
lockAStopLeading.Set();
};
leaderElector.RunUntilLeadershipLostAsync().Wait();
await leaderElector.RunUntilLeadershipLostAsync().ConfigureAwait(true);
});


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

Task.Run(() =>
Task.Run(async () =>
{
var leaderElector = new LeaderElector(leaderElectionConfigB);
Expand All @@ -186,7 +186,7 @@ public void LeaderElection()
testLeaderElectionLatch.Signal();
};
leaderElector.RunUntilLeadershipLostAsync().Wait();
await leaderElector.RunUntilLeadershipLostAsync().ConfigureAwait(true);
});

testLeaderElectionLatch.Wait(TimeSpan.FromSeconds(15));
Expand Down Expand Up @@ -256,7 +256,7 @@ public void LeaderElectionWithRenewDeadline()
};

var countdown = new CountdownEvent(2);
Task.Run(() =>
Task.Run(async () =>
{
var leaderElector = new LeaderElector(leaderElectionConfig);
Expand All @@ -272,7 +272,7 @@ public void LeaderElectionWithRenewDeadline()
countdown.Signal();
};
leaderElector.RunUntilLeadershipLostAsync().Wait();
await leaderElector.RunUntilLeadershipLostAsync().ConfigureAwait(true);
});

countdown.Wait(TimeSpan.FromSeconds(15));
Expand All @@ -290,7 +290,7 @@ public void LeaderElectionWithRenewDeadline()
}

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

try
{
leaderElector.RunUntilLeadershipLostAsync().Wait();
await leaderElector.RunUntilLeadershipLostAsync().ConfigureAwait(true);
}
catch (Exception e)
{
Assert.Equal("noxu", e.InnerException?.Message);
Assert.Equal("noxu", e.Message);
return;
}

Expand Down
Loading

0 comments on commit f32cf9e

Please sign in to comment.