Skip to content

Commit 6915e55

Browse files
committed
reduce duplicate codes
1 parent 42dc7ee commit 6915e55

File tree

1 file changed

+75
-190
lines changed

1 file changed

+75
-190
lines changed

tests/Smdn.Net.MuninNode/Smdn.Net.MuninNode/NodeBase.cs

Lines changed: 75 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,40 @@ await StartSession(
538538
);
539539
}
540540

541+
private static Task GetCommandResponseAsync(
542+
IReadOnlyList<IPlugin> plugins,
543+
Func<string> getCommand,
544+
Action<IReadOnlyList<string>> assertResponseLines
545+
)
546+
=> StartSession(
547+
plugins: plugins,
548+
action: async (node, client, writer, reader, cancellationToken) => {
549+
await writer.WriteLineAsync(getCommand(), cancellationToken);
550+
await writer.FlushAsync(cancellationToken);
551+
552+
var lines = new List<string>();
553+
554+
try {
555+
for (; ; ) {
556+
var line = await reader.ReadLineAsync(cancellationToken);
557+
558+
if (line is null)
559+
break;
560+
561+
lines.Add(line);
562+
563+
if (line == ".")
564+
break;
565+
}
566+
}
567+
catch (IOException ex) when (ex.InnerException is SocketException) {
568+
// ignore
569+
}
570+
571+
assertResponseLines(lines);
572+
}
573+
);
574+
541575
private static System.Collections.IEnumerable YieldTestCases_ProcessCommandAsync_FetchCommand()
542576
{
543577
var graphAttrs = new PluginGraphAttributes(
@@ -595,29 +629,22 @@ private static System.Collections.IEnumerable YieldTestCases_ProcessCommandAsync
595629
}
596630

597631
[TestCaseSource(nameof(YieldTestCases_ProcessCommandAsync_FetchCommand))]
598-
public async Task ProcessCommandAsync_FetchCommand(
632+
public Task ProcessCommandAsync_FetchCommand(
599633
IReadOnlyList<IPlugin> plugins,
600634
string command,
601635
string[] expectedResponseLines
602636
)
603-
{
604-
await StartSession(
637+
=> GetCommandResponseAsync(
605638
plugins: plugins,
606-
action: async (node, client, writer, reader, cancellationToken) => {
607-
await writer.WriteLineAsync(command, cancellationToken);
608-
await writer.FlushAsync(cancellationToken);
609-
610-
var totalLineCount = 0;
639+
getCommand: () => command,
640+
assertResponseLines: lines => {
641+
Assert.That(lines.Count, Is.EqualTo(expectedResponseLines.Length), nameof(lines.Count));
611642

612643
foreach (var (expectedResponseLine, lineNumber) in expectedResponseLines.Select(static (line, index) => (line, index))) {
613-
Assert.That(await reader.ReadLineAsync(cancellationToken), Is.EqualTo(expectedResponseLine), $"line #{lineNumber}");
614-
totalLineCount++;
644+
Assert.That(lines[lineNumber], Is.EqualTo(expectedResponseLine), $"line #{lineNumber}");
615645
}
616-
617-
Assert.That(totalLineCount, Is.EqualTo(expectedResponseLines.Length), nameof(totalLineCount));
618646
}
619647
);
620-
}
621648

622649
[Test]
623650
public async Task ProcessCommandAsync_ConfigCommand_UnknownService()
@@ -638,14 +665,13 @@ public async Task ProcessCommandAsync_ConfigCommand_UnknownService()
638665
),
639666
};
640667

641-
await StartSession(
668+
await GetCommandResponseAsync(
642669
plugins: plugins,
643-
action: async static (node, client, writer, reader, cancellationToken) => {
644-
await writer.WriteLineAsync("config nonexistentplugin", cancellationToken);
645-
await writer.FlushAsync(cancellationToken);
646-
647-
Assert.That(await reader.ReadLineAsync(cancellationToken), Is.EqualTo("# Unknown service"), "line #1");
648-
Assert.That(await reader.ReadLineAsync(cancellationToken), Is.EqualTo("."), "line #2");
670+
getCommand: () => "config nonexistentplugin",
671+
assertResponseLines: lines => {
672+
Assert.That(lines.Count, Is.EqualTo(2), "# of lines");
673+
Assert.That(lines[0], Is.EqualTo("# Unknown service"), "line #1");
674+
Assert.That(lines[1], Is.EqualTo("."), "line #2");
649675
}
650676
);
651677
}
@@ -726,36 +752,16 @@ PluginGraphAttributes attributes
726752
}
727753

728754
[TestCaseSource(nameof(YieldTestCases_ProcessCommandAsync_ConfigCommand))]
729-
public async Task ProcessCommandAsync_ConfigCommand(
755+
public Task ProcessCommandAsync_ConfigCommand(
730756
IReadOnlyList<IPlugin> plugins,
731757
string command,
732758
Action<IReadOnlyList<string>> assertResponseLines
733759
)
734-
{
735-
await StartSession(
760+
=> GetCommandResponseAsync(
736761
plugins: plugins,
737-
action: async (node, client, writer, reader, cancellationToken) => {
738-
await writer.WriteLineAsync(command, cancellationToken);
739-
await writer.FlushAsync(cancellationToken);
740-
741-
var lines = new List<string>();
742-
743-
for (; ; ) {
744-
var line = await reader.ReadLineAsync(cancellationToken);
745-
746-
if (line is null)
747-
break;
748-
749-
lines.Add(line);
750-
751-
if (line == ".")
752-
break;
753-
}
754-
755-
assertResponseLines(lines);
756-
}
762+
getCommand: () => command,
763+
assertResponseLines: assertResponseLines
757764
);
758-
}
759765

760766
private static System.Collections.IEnumerable YieldTestCases_ProcessCommandAsync_ConfigCommand_OptionalAttributes()
761767
{
@@ -845,35 +851,15 @@ private static System.Collections.IEnumerable YieldTestCases_ProcessCommandAsync
845851
}
846852

847853
[TestCaseSource(nameof(YieldTestCases_ProcessCommandAsync_ConfigCommand_OptionalAttributes))]
848-
public async Task ProcessCommandAsync_ConfigCommand_OptionalAttributes(
854+
public Task ProcessCommandAsync_ConfigCommand_OptionalAttributes(
849855
IPlugin plugin,
850856
Action<IReadOnlyList<string>> assertResponseLines
851857
)
852-
{
853-
await StartSession(
858+
=> GetCommandResponseAsync(
854859
plugins: new[] { plugin },
855-
action: async (node, client, writer, reader, cancellationToken) => {
856-
await writer.WriteLineAsync($"config {plugin.Name}", cancellationToken);
857-
await writer.FlushAsync(cancellationToken);
858-
859-
var lines = new List<string>();
860-
861-
for (; ; ) {
862-
var line = await reader.ReadLineAsync(cancellationToken);
863-
864-
if (line is null)
865-
break;
866-
867-
lines.Add(line);
868-
869-
if (line == ".")
870-
break;
871-
}
872-
873-
assertResponseLines(lines);
874-
}
860+
getCommand: () => $"config {plugin.Name}",
861+
assertResponseLines: assertResponseLines
875862
);
876-
}
877863

878864
private static System.Collections.IEnumerable YieldTestCases_ProcessCommandAsync_ConfigCommand_GraphOrder()
879865
{
@@ -920,35 +906,15 @@ private static System.Collections.IEnumerable YieldTestCases_ProcessCommandAsync
920906
}
921907

922908
[TestCaseSource(nameof(YieldTestCases_ProcessCommandAsync_ConfigCommand_GraphOrder))]
923-
public async Task ProcessCommandAsync_ConfigCommand_GraphOrder(
909+
public Task ProcessCommandAsync_ConfigCommand_GraphOrder(
924910
IPlugin plugin,
925911
Action<IReadOnlyList<string>> assertResponseLines
926912
)
927-
{
928-
await StartSession(
913+
=> GetCommandResponseAsync(
929914
plugins: new[] { plugin },
930-
action: async (node, client, writer, reader, cancellationToken) => {
931-
await writer.WriteLineAsync($"config {plugin.Name}", cancellationToken);
932-
await writer.FlushAsync(cancellationToken);
933-
934-
var lines = new List<string>();
935-
936-
for (; ; ) {
937-
var line = await reader.ReadLineAsync(cancellationToken);
938-
939-
if (line is null)
940-
break;
941-
942-
lines.Add(line);
943-
944-
if (line == ".")
945-
break;
946-
}
947-
948-
assertResponseLines(lines);
949-
}
915+
getCommand: () => $"config {plugin.Name}",
916+
assertResponseLines: assertResponseLines
950917
);
951-
}
952918

953919
private static System.Collections.IEnumerable YieldTestCases_ProcessCommandAsync_ConfigCommand_GraphTotal()
954920
{
@@ -994,35 +960,15 @@ private static System.Collections.IEnumerable YieldTestCases_ProcessCommandAsync
994960
}
995961

996962
[TestCaseSource(nameof(YieldTestCases_ProcessCommandAsync_ConfigCommand_GraphTotal))]
997-
public async Task ProcessCommandAsync_ConfigCommand_GraphTotal(
963+
public Task ProcessCommandAsync_ConfigCommand_GraphTotal(
998964
IPlugin plugin,
999965
Action<IReadOnlyList<string>> assertResponseLines
1000966
)
1001-
{
1002-
await StartSession(
967+
=> GetCommandResponseAsync(
1003968
plugins: new[] { plugin },
1004-
action: async (node, client, writer, reader, cancellationToken) => {
1005-
await writer.WriteLineAsync($"config {plugin.Name}", cancellationToken);
1006-
await writer.FlushAsync(cancellationToken);
1007-
1008-
var lines = new List<string>();
1009-
1010-
for (; ; ) {
1011-
var line = await reader.ReadLineAsync(cancellationToken);
1012-
1013-
if (line is null)
1014-
break;
1015-
1016-
lines.Add(line);
1017-
1018-
if (line == ".")
1019-
break;
1020-
}
1021-
1022-
assertResponseLines(lines);
1023-
}
969+
getCommand: () => $"config {plugin.Name}",
970+
assertResponseLines: assertResponseLines
1024971
);
1025-
}
1026972

1027973
[TestCase(PluginFieldGraphStyle.Default, null, null)]
1028974
[TestCase(PluginFieldGraphStyle.Area, "AREA", null)]
@@ -1063,31 +1009,10 @@ public async Task ProcessCommandAsync_ConfigCommand_TranslateGraphStyle(
10631009
};
10641010

10651011
try {
1066-
await StartSession(
1012+
await GetCommandResponseAsync(
10671013
plugins: plugins,
1068-
action: async (node, client, writer, reader, cancellationToken) => {
1069-
await writer.WriteLineAsync("config plugin", cancellationToken);
1070-
await writer.FlushAsync(cancellationToken);
1071-
1072-
var lines = new List<string>();
1073-
1074-
try {
1075-
for (; ; ) {
1076-
var line = await reader.ReadLineAsync(cancellationToken);
1077-
1078-
if (line is null)
1079-
break;
1080-
1081-
lines.Add(line);
1082-
1083-
if (line == ".")
1084-
break;
1085-
}
1086-
}
1087-
catch (IOException ex) when (ex.InnerException is SocketException) {
1088-
// ignore
1089-
}
1090-
1014+
getCommand: () => "config plugin",
1015+
assertResponseLines: lines => {
10911016
if (expectedFieldDrawAttribute is null)
10921017
Assert.That(lines, Has.No.Member("field.draw"));
10931018
else
@@ -1162,31 +1087,10 @@ public async Task ProcessCommandAsync_ConfigCommand_WarningAndCriticalField(
11621087
),
11631088
};
11641089

1165-
await StartSession(
1090+
await GetCommandResponseAsync(
11661091
plugins: plugins,
1167-
action: async (node, client, writer, reader, cancellationToken) => {
1168-
await writer.WriteLineAsync("config plugin", cancellationToken);
1169-
await writer.FlushAsync(cancellationToken);
1170-
1171-
var lines = new List<string>();
1172-
1173-
try {
1174-
for (; ; ) {
1175-
var line = await reader.ReadLineAsync(cancellationToken);
1176-
1177-
if (line is null)
1178-
break;
1179-
1180-
lines.Add(line);
1181-
1182-
if (line == ".")
1183-
break;
1184-
}
1185-
}
1186-
catch (IOException ex) when (ex.InnerException is SocketException) {
1187-
// ignore
1188-
}
1189-
1092+
getCommand: () => "config plugin",
1093+
assertResponseLines: lines => {
11901094
if (expectedFieldWarningAttributeLine is null)
11911095
Assert.That(lines, Has.No.Member(expectedFieldWarningAttributeLine));
11921096
else
@@ -1240,40 +1144,21 @@ public async Task ProcessCommandAsync_ConfigCommand_NegativeField()
12401144
),
12411145
};
12421146

1243-
await StartSession(
1147+
await GetCommandResponseAsync(
12441148
plugins: plugins,
1245-
action: async (node, client, writer, reader, cancellationToken) => {
1246-
await writer.WriteLineAsync("config plugin", cancellationToken);
1247-
await writer.FlushAsync(cancellationToken);
1248-
1249-
var lines = new List<string>();
1250-
1251-
try {
1252-
for (; ;) {
1253-
var line = await reader.ReadLineAsync(cancellationToken);
1254-
1255-
if (line is null)
1256-
break;
1257-
1258-
lines.Add(line);
1259-
1260-
if (line == ".")
1261-
break;
1262-
}
1263-
}
1264-
catch (IOException ex) when (ex.InnerException is SocketException) {
1265-
// ignore
1266-
}
1267-
1149+
getCommand: () => "config plugin",
1150+
assertResponseLines: lines => {
12681151
var expectedAttrNegativeLine = $"{PositiveFieldName}.negative {NegativeFieldName}";
12691152
var expectedAttrGraphLine = $"{NegativeFieldName}.graph no";
12701153

12711154
Assert.That(lines, Has.Member(expectedAttrNegativeLine));
12721155
Assert.That(lines, Has.Member(expectedAttrGraphLine));
12731156

1157+
var ls = lines.ToList();
1158+
12741159
Assert.That(
1275-
lines.IndexOf(expectedAttrGraphLine),
1276-
Is.LessThan(lines.IndexOf(expectedAttrNegativeLine)),
1160+
ls.IndexOf(expectedAttrGraphLine),
1161+
Is.LessThan(ls.IndexOf(expectedAttrNegativeLine)),
12771162
"negative field's attributes must be listed first"
12781163
);
12791164
}

0 commit comments

Comments
 (0)