Skip to content

Commit

Permalink
use Array.Empty (#6801)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Jun 19, 2023
1 parent 4e97b5a commit 731ec92
Show file tree
Hide file tree
Showing 23 changed files with 38 additions and 38 deletions.
8 changes: 4 additions & 4 deletions src/contrib/cluster/Akka.Cluster.Metrics.Tests/MetricSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,16 @@ public class NodeMetricSpec
[Fact]
public void NodeMetrics_Should_return_correct_result_for_2_same_nodes()
{
new NodeMetrics(_node1, 0, new NodeMetrics.Types.Metric[0])
.SameAs(new NodeMetrics(_node1, 0, new NodeMetrics.Types.Metric[0]))
new NodeMetrics(_node1, 0, Array.Empty<NodeMetrics.Types.Metric>())
.SameAs(new NodeMetrics(_node1, 0, Array.Empty<NodeMetrics.Types.Metric>()))
.Should().BeTrue();
}

[Fact]
public void NodeMetrics_Should_return_correct_result_for_2_not_same_nodes()
{
new NodeMetrics(_node1, 0, new NodeMetrics.Types.Metric[0])
.SameAs(new NodeMetrics(_node2, 0, new NodeMetrics.Types.Metric[0]))
new NodeMetrics(_node1, 0, Array.Empty<NodeMetrics.Types.Metric>())
.SameAs(new NodeMetrics(_node2, 0, Array.Empty<NodeMetrics.Types.Metric>()))
.Should().BeFalse();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public override byte[] ToBinary(object obj)
case MetricsGossipEnvelope m: return Compress(MetricsGossipEnvelopeToProto(m)); // TODO: Add compression here
case Metrics.AdaptiveLoadBalancingPool alb: return AdaptiveLoadBalancingPoolToBinary(alb);
case Metrics.MixMetricsSelector mms: return MixMetricsSelectorToBinary(mms);
case CpuMetricsSelector _: return new byte[0];
case MemoryMetricsSelector _: return new byte[0];
case CpuMetricsSelector _: return Array.Empty<byte>();
case MemoryMetricsSelector _: return Array.Empty<byte>();
default:
throw new ArgumentException($"Can't serialize object of type ${obj.GetType().Name} in [${GetType().Name}]");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void Setup_should_be_converted_to_settings_correctly()
false,
typeof(DummyTypesProvider),
new Func<string, string>[] { s => $"{s}.." },
new Surrogate[0],
Array.Empty<Surrogate>(),
true);
var appliedSettings = setup.ApplySettings(settings);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public static HyperionSerializerSettings Create(Config config)
/// <exception>Raised when `known-types-provider` type doesn't implement <see cref="IKnownTypesProvider"/> interface.</exception>
[Obsolete]
public HyperionSerializerSettings(bool preserveObjectReferences, bool versionTolerance, Type knownTypesProvider)
: this(preserveObjectReferences, versionTolerance, knownTypesProvider, new List<Func<string, string>>(), new Surrogate[0], true, DisabledTypeFilter.Instance)
: this(preserveObjectReferences, versionTolerance, knownTypesProvider, new List<Func<string, string>>(), Array.Empty<Surrogate>(), true, DisabledTypeFilter.Instance)
{ }

/// <summary>
Expand All @@ -330,7 +330,7 @@ public HyperionSerializerSettings(
bool versionTolerance,
Type knownTypesProvider,
IEnumerable<Func<string, string>> packageNameOverrides)
: this(preserveObjectReferences, versionTolerance, knownTypesProvider, packageNameOverrides, new Surrogate[0], true, DisabledTypeFilter.Instance)
: this(preserveObjectReferences, versionTolerance, knownTypesProvider, packageNameOverrides, Array.Empty<Surrogate>(), true, DisabledTypeFilter.Instance)
{ }

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/core/Akka.Cluster/SplitBrainResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public IEnumerable<Member> Apply(NetworkPartitionContext context)

if (remaining.Count < unreachable.Count) return context.Remaining;
if (remaining.Count > unreachable.Count) return context.Unreachable;
if (remaining.IsEmpty && unreachable.IsEmpty) return new Member[0];
if (remaining.IsEmpty && unreachable.IsEmpty) return Array.Empty<Member>();

// if the parts are of equal size the part containing the node with the lowest address is kept.
var oldest = remaining.Union(unreachable).First();
Expand Down
2 changes: 1 addition & 1 deletion src/core/Akka.Persistence.Query/PersistenceQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private IReadJournalProvider CreateType(Type pluginType, object[] parameters)
if (ctor != null) return (IReadJournalProvider)ctor.Invoke(new[] { parameters[0] });

ctor = pluginType.GetConstructor(new Type[0]);
if (ctor != null) return (IReadJournalProvider)ctor.Invoke(new object[0]);
if (ctor != null) return (IReadJournalProvider)ctor.Invoke(Array.Empty<object>());

throw new ArgumentException($"Unable to create read journal plugin instance type {pluginType}!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public virtual void SnapshotStore_should_serialize_AtLeastOnceDeliverySnapshot_w
{
var probe = CreateTestProbe();

var unconfirmed = new UnconfirmedDelivery[0];
var unconfirmed = Array.Empty<UnconfirmedDelivery>();
var atLeastOnceDeliverySnapshot = new AtLeastOnceDeliverySnapshot(13, unconfirmed);

var metadata = new SnapshotMetadata(Pid, 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public void Serializer_must_reject_invalid_manifest()
public void Serializer_must_reject_deserialization_with_invalid_manifest()
{
var serializer = new MiscMessageSerializer(Sys.AsInstanceOf<ExtendedActorSystem>());
Action comparison = () => serializer.FromBinary(new byte[0], "INVALID");
Action comparison = () => serializer.FromBinary(Array.Empty<byte>(), "INVALID");
comparison.Should().Throw<SerializationException>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public override byte[] ToBinary(object obj)
case ToBinaryIllegal _:
throw new ArgumentException();
default:
return new byte[0];
return Array.Empty<byte>();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/Akka.Streams.Tests/Dsl/FlowIntersperseSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void A_Intersperse_must_surround_single_element_stream_with_start_and_end
public void A_Intersperse_must_not_surround_empty_stream_with_null_start_and_stop()
{
var probe =
Source.From(new int[0])
Source.From(Array.Empty<int>())
.Select(x => x.ToString())
.Intersperse(",")
.RunWith(this.SinkProbe<string>(), Materializer);
Expand Down
2 changes: 1 addition & 1 deletion src/core/Akka.Streams.Tests/Dsl/FlowSelectManySpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public FlowSelectManySpec(ITestOutputHelper output) : base(output)
public async Task SelectMany_should_map_and_concat()
{
var script = Script.Create(
(new[] { 0 }, new int[0]),
(new[] { 0 }, Array.Empty<int>()),
(new[] { 1 }, new[] { 1 }),
(new[] { 2 }, new[] { 2, 2 }),
(new[] { 3 }, new[] { 3, 3, 3 }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void A_StatefulSelectMany_must_work_in_happy_case()
{
var phases = new[]
{
((ICollection<int>)new[] {2}, (ICollection<int>)new int[0]),
((ICollection<int>)new[] {2}, (ICollection<int>)Array.Empty<int>()),
((ICollection<int>)new[] {1}, (ICollection<int>)new[] {1, 1}),
((ICollection<int>)new[] {3}, (ICollection<int>)new[] {3}),
((ICollection<int>)new[] {6}, (ICollection<int>)new[] {6, 6, 6})
Expand Down
2 changes: 1 addition & 1 deletion src/core/Akka.Streams.Tests/Dsl/RetrySpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public void RetryConcat_should_concat_incremented_ints_and_modulo_3_incremented_
{
var s = (os + 1) % 3;
if (os < 42) return new[] { (os + 1, os + 1), (s, s) };
if (os == 42) return new (int, int)[0];
if (os == 42) return Array.Empty<(int, int)>();
return null;
}))
.ToMaterialized(this.SinkProbe<(Result<int>, int)>(), Keep.Both)
Expand Down
2 changes: 1 addition & 1 deletion src/core/Akka.Streams.Tests/IO/InputStreamSinkSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ await this.AssertAllStagesStoppedAsync(() => {
Action(() => inputStream.Read(buf, -1, 2)).Should().Throw<ArgumentException>();
Action(() => inputStream.Read(buf, 0, 5)).Should().Throw<ArgumentException>();
Action(() => inputStream.Read(new byte[0], 0, 1)).Should().Throw<ArgumentException>();
Action(() => inputStream.Read(Array.Empty<byte>(), 0, 1)).Should().Throw<ArgumentException>();
Action(() => inputStream.Read(buf, 0, 0)).Should().Throw<ArgumentException>();
return Task.CompletedTask;
}, _materializer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ public PortTestSetup(ActorSystem system, bool chasing = false) : base(system)
var propagateStage = new EventPropagateStage();

var assembly = !chasing
? new GraphAssembly(new IGraphStageWithMaterializedValue<Shape, object>[0], new Attributes[0],
? new GraphAssembly(Array.Empty<IGraphStageWithMaterializedValue<Shape, object>>(), Array.Empty<Attributes>(),
new Inlet[] {null}, new[] {-1}, new Outlet[] {null}, new[] {-1})
: new GraphAssembly(new[] {propagateStage}, new[] {Attributes.None},
new Inlet[] {propagateStage.In, null}, new[] {0, -1}, new Outlet[] {null, propagateStage.Out},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void Interpreter_should_implement_chain_of_maps_correctly()
[Fact]
public void Interpreter_should_work_with_only_boundary_ops()
{
WithOneBoundedSetup(new IStage<int, int>[0],
WithOneBoundedSetup(Array.Empty<IStage<int, int>>(),
(lastEvents, upstream, downstream) =>
{
lastEvents().Should().BeEmpty();
Expand Down
2 changes: 1 addition & 1 deletion src/core/Akka.Streams/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public AfterDelay(TimeSpan delay, IStrategy strategy)
/// <param name="attributes">TBD</param>
public Attributes(params IAttribute[] attributes)
{
_attributes = attributes ?? new IAttribute[0];
_attributes = attributes ?? Array.Empty<IAttribute>();
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/core/Akka.Tests/Actor/PropsSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void Props_created_with_strategy_must_have_it_set()
public void Props_created_with_null_type_must_throw()
{
Type missingType = null;
object[] args = new object[0];
object[] args = Array.Empty<object>();
var argsEnumerable = Enumerable.Empty<object>();
var defaultStrategy = SupervisorStrategy.DefaultStrategy;
var defaultDeploy = Deploy.Local;
Expand Down
9 changes: 4 additions & 5 deletions src/core/Akka.Tests/MatchHandler/CachedMatchCompilerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class CachedMatchCompilerTests
public void When_compiling_first_time_correct_calls_are_made_to_MatchExpressionBuilder_and_PartialActionBuilder()
{
//Arrange
object[] argumentValues = new object[0];
object[] argumentValues = Array.Empty<object>();
Expression<Func<object, bool>> lambdaExpression = _ => true;
var matchExpressionBuilder = new DummyMatchExpressionBuilder()
{
Expand Down Expand Up @@ -60,12 +60,11 @@ public void When_compiling_first_time_correct_calls_are_made_to_MatchExpressionB
public void When_compiling_second_time_with_same_signature_the_cached_version_should_be_used()
{
//Arrange
object[] argumentValues = new object[0];
Expression<Func<object, bool>> lambdaExpression = _ => true;
var matchExpressionBuilder = new DummyMatchExpressionBuilder()
{
BuildLambdaExpressionResult = new MatchExpressionBuilderResult(lambdaExpression, argumentValues),
CreateArgumentValuesArrayResult = argumentValues,
BuildLambdaExpressionResult = new MatchExpressionBuilderResult(lambdaExpression, Array.Empty<object>()),
CreateArgumentValuesArrayResult = Array.Empty<object>(),
};

Func<object, bool> deleg = _ => true;
Expand Down Expand Up @@ -95,7 +94,7 @@ public void When_compiling_second_time_with_same_signature_the_cached_version_sh
//Assert

AssertOneCall(to: matchExpressionBuilder.CreateArgumentValuesArrayCalls, withArgument: arguments, description: "CreateArgumentValuesArray");
AssertOneCall(to: partialActionBuilder.BuildCalls, description: "Build", check: i => ReferenceEquals(i.CompiledDelegate, deleg) && ReferenceEquals(i.DelegateArguments, argumentValues));
AssertOneCall(to: partialActionBuilder.BuildCalls, description: "Build", check: i => ReferenceEquals(i.CompiledDelegate, deleg) && ReferenceEquals(i.DelegateArguments, Array.Empty<object>()));
Assert.Same(partialAction, resultPartialAction);

AssertNoCall(to: matchExpressionBuilder.BuildLambdaExpressionCalls, description: "BuildLambdaExpression");
Expand Down
11 changes: 6 additions & 5 deletions src/core/Akka.Tests/MatchHandler/MatchBuilderSignatureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// </copyright>
//-----------------------------------------------------------------------

using System;
using Akka.Tools.MatchHandler;
using Xunit;

Expand All @@ -16,7 +17,7 @@ public class MatchBuilderSignatureTests
public void GetHashCode_should_return_same_value_for_empty_and_null_signatures()
{
var nullSignature = new MatchBuilderSignature(null);
var emptySignature = new MatchBuilderSignature(new object[0]);
var emptySignature = new MatchBuilderSignature(Array.Empty<object>());

Assert.Equal(nullSignature.GetHashCode(), emptySignature.GetHashCode());
}
Expand Down Expand Up @@ -83,7 +84,7 @@ public void GetHashCode_should_be_different_when_same_elements_but_fewer()
[Fact]
public void Equals_with_null_should_be_false()
{
var signature = new MatchBuilderSignature(new object[0]);
var signature = new MatchBuilderSignature(Array.Empty<object>());
Assert.False(signature.Equals((object)null));
Assert.False(signature.Equals(null));
}
Expand All @@ -92,15 +93,15 @@ public void Equals_with_null_should_be_false()
[Fact]
public void Equals_with_same_should_be_true()
{
var signature = new MatchBuilderSignature(new object[0]);
var signature = new MatchBuilderSignature(Array.Empty<object>());
Assert.True(signature.Equals((object)signature));
Assert.True(signature.Equals(signature));
}

[Fact]
public void Equals_with_different_type_should_be_false()
{
var signature = new MatchBuilderSignature(new object[0]);
var signature = new MatchBuilderSignature(Array.Empty<object>());
Assert.False(signature.Equals(new object()));
}

Expand All @@ -116,7 +117,7 @@ public void Equals_with_two_null_element_signatures_should_be_true()
public void Equals_with_one_null_element_signature_and_one_empty_element_signature_should_be_true()
{
var signature1 = new MatchBuilderSignature(null);
var signature2 = new MatchBuilderSignature(new object[0]);
var signature2 = new MatchBuilderSignature(Array.Empty<object>());
Assert.True(signature1.Equals(signature2));
Assert.True(signature2.Equals(signature1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void Given_a_0_arguments_delegate_When_building_and_invoking_Then_the_sup
object updatedValue = null;
Func<object, bool> deleg = value => { updatedValue = value; return true; };

var partialAction = builder.Build<object>(new CompiledMatchHandlerWithArguments(deleg, new object[0]));
var partialAction = builder.Build<object>(new CompiledMatchHandlerWithArguments(deleg, Array.Empty<object>()));
partialAction("value");
Assert.Same(updatedValue, "value");
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/Akka/Routing/RouterConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public abstract class Group : RouterConfig, IEquatable<Group>
protected Group(IEnumerable<string> paths, string routerDispatcher) : base(routerDispatcher)
{
// equivalent of turning the paths into an immutable sequence
InternalPaths = paths?.ToArray() ?? new string[0];
InternalPaths = paths?.ToArray() ?? Array.Empty<string>();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ static void Main(string[] args)
_clusterConfig = section.AkkaConfig;
LaunchBackend(new []{ "2551" });
LaunchBackend(new[] { "2552" });
LaunchBackend(new string[0]);
LaunchFrontend(new string[0]);
LaunchFrontend(new string[0]);
LaunchBackend(Array.Empty<string>());
LaunchFrontend(Array.Empty<string>());
LaunchFrontend(Array.Empty<string>());
//starting 2 frontend nodes and 3 backend nodes
Console.WriteLine("Press any key to exit.");
Console.ReadLine();
Expand Down

0 comments on commit 731ec92

Please sign in to comment.