Skip to content

Commit 0d379af

Browse files
Leverage NUnit Assert.Throws (#1416)
* Assert throws * Fix whitespace * small fix * small changes * fix --------- Co-authored-by: Tal Zaccai <[email protected]>
1 parent 3361253 commit 0d379af

13 files changed

+158
-462
lines changed

test/Garnet.test.cluster/ClusterConfigTests.cs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
3-
using System;
43
using System.Collections.Generic;
54
using System.Linq;
65
using System.Net;
@@ -10,6 +9,7 @@
109
using Microsoft.Extensions.Logging;
1110
using NUnit.Framework;
1211
using NUnit.Framework.Legacy;
12+
using StackExchange.Redis;
1313

1414
namespace Garnet.test.cluster
1515
{
@@ -79,21 +79,16 @@ public void ClusterForgetAfterNodeRestartTest()
7979
var nodesResult = context.clusterTestUtils.ClusterNodes(0);
8080
Assert.That(nodesResult.Nodes.Count == nbInstances);
8181

82-
try
83-
{
84-
var server = context.clusterTestUtils.GetServer(context.endpoints[0].ToIPEndPoint());
85-
var args = new List<object>() {
82+
var server = context.clusterTestUtils.GetServer(context.endpoints[0].ToIPEndPoint());
83+
var args = new List<object>() {
8684
"forget",
8785
Encoding.ASCII.GetBytes("1ip23j89123no"),
8886
Encoding.ASCII.GetBytes("0")
8987
};
90-
var result = (string)server.Execute("cluster", args);
91-
Assert.Fail("Cluster forget call shouldn't have succeeded for an invalid node id.");
92-
}
93-
catch (Exception ex)
94-
{
95-
Assert.That(ex.Message == "ERR I don't know about node 1ip23j89123no.");
96-
}
88+
var ex = Assert.Throws<RedisServerException>(() => server.Execute("cluster", args),
89+
"Cluster forget call shouldn't have succeeded for an invalid node id.");
90+
91+
Assert.That(ex.Message, Is.EqualTo("ERR I don't know about node 1ip23j89123no."));
9792

9893
nodesResult = context.clusterTestUtils.ClusterNodes(0);
9994
Assert.That(nodesResult.Nodes.Count == nbInstances, "No node should've been removed from the cluster after an invalid id was passed.");

test/Garnet.test.cluster/RedirectTests/ClusterSlotVerificationTests.cs

Lines changed: 45 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -283,33 +283,19 @@ public void ClusterCLUSTERDOWNTest()
283283

284284
void SERedisClusterDown(BaseCommand command)
285285
{
286-
try
287-
{
288-
AssertSlotsNotAssigned(requestNodeIndex);
289-
_ = context.clusterTestUtils.GetServer(requestNodeIndex).Execute(command.Command, command.GetSingleSlotRequest());
290-
}
291-
catch (Exception ex)
292-
{
293-
ClassicAssert.AreEqual("CLUSTERDOWN Hash slot not served", ex.Message, command.Command);
294-
return;
295-
}
296-
Assert.Fail($"Should not reach here. Command: {command.Command} \n{ClusterState()}");
286+
AssertSlotsNotAssigned(requestNodeIndex);
287+
var ex = Assert.Throws<RedisServerException>(() => context.clusterTestUtils.GetServer(requestNodeIndex).Execute(command.Command, command.GetSingleSlotRequest()),
288+
$"Expected exception was not thrown. Command: {command.Command} \n{ClusterState()}");
289+
ClassicAssert.AreEqual("CLUSTERDOWN Hash slot not served", ex.Message, command.Command);
297290
}
298291

299292
void GarnetClientSessionClusterDown(BaseCommand command)
300293
{
301294
var client = context.clusterTestUtils.GetGarnetClientSession(requestNodeIndex);
302-
try
303-
{
304-
AssertSlotsNotAssigned(requestNodeIndex);
305-
_ = client.ExecuteAsync(command.GetSingleSlotRequestWithCommand).GetAwaiter().GetResult();
306-
}
307-
catch (Exception ex)
308-
{
309-
ClassicAssert.AreEqual("CLUSTERDOWN Hash slot not served", ex.Message, command.Command);
310-
return;
311-
}
312-
Assert.Fail($"Should not reach here. Command: {command.Command} \n{ClusterState()}");
295+
AssertSlotsNotAssigned(requestNodeIndex);
296+
var ex = Assert.Throws<Exception>(() => client.ExecuteAsync(command.GetSingleSlotRequestWithCommand).GetAwaiter().GetResult(),
297+
$"Expected exception was not thrown. Command: {command.Command} \n{ClusterState()}");
298+
ClassicAssert.AreEqual("CLUSTERDOWN Hash slot not served", ex.Message, command.Command);
313299
}
314300
}
315301
}
@@ -391,33 +377,19 @@ void SERedisCrossslotTest(BaseCommand command)
391377
{
392378
if (!command.IsArrayCommand)
393379
return;
394-
try
395-
{
396-
_ = context.clusterTestUtils.GetServer(requestNodeIndex).Execute(command.Command, command.GetCrossSlotRequest());
397-
}
398-
catch (Exception ex)
399-
{
400-
ClassicAssert.AreEqual("CROSSSLOT Keys in request do not hash to the same slot", ex.Message, command.Command);
401-
return;
402-
}
403-
Assert.Fail($"Should not reach here. Command: {command.Command} \n{ClusterState()}");
380+
var ex = Assert.Throws<RedisServerException>(() => context.clusterTestUtils.GetServer(requestNodeIndex).Execute(command.Command, command.GetCrossSlotRequest()),
381+
$"Expected exception was not thrown. Command: {command.Command} \n{ClusterState()}");
382+
ClassicAssert.AreEqual("CROSSSLOT Keys in request do not hash to the same slot", ex.Message, command.Command);
404383
}
405384

406385
void GarnetClientSessionCrossslotTest(BaseCommand command)
407386
{
408387
if (!command.IsArrayCommand)
409388
return;
410389
var client = context.clusterTestUtils.GetGarnetClientSession(requestNodeIndex);
411-
try
412-
{
413-
client.ExecuteAsync(command.GetCrossslotRequestWithCommand).GetAwaiter().GetResult();
414-
}
415-
catch (Exception ex)
416-
{
417-
ClassicAssert.AreEqual("CROSSSLOT Keys in request do not hash to the same slot", ex.Message, command.Command);
418-
return;
419-
}
420-
Assert.Fail($"Should not reach here. Command: {command.Command} \n{ClusterState()}");
390+
var ex = Assert.Throws<Exception>(() => client.ExecuteAsync(command.GetCrossslotRequestWithCommand).GetAwaiter().GetResult(),
391+
$"Expected exception was not thrown. Command: {command.Command} \n{ClusterState()}");
392+
ClassicAssert.AreEqual("CROSSSLOT Keys in request do not hash to the same slot", ex.Message, command.Command);
421393
}
422394
}
423395
}
@@ -442,40 +414,26 @@ public void ClusterMOVEDTest()
442414

443415
void SERedisMOVEDTest(BaseCommand command)
444416
{
445-
try
446-
{
447-
context.clusterTestUtils.GetServer(requestNodeIndex).Execute(command.Command, command.GetSingleSlotRequest(), CommandFlags.NoRedirect);
448-
}
449-
catch (Exception ex)
450-
{
451-
ClassicAssert.IsTrue(ex.Message.StartsWith("Key has MOVED"), command.Command);
452-
var tokens = ex.Message.Split(' ');
453-
ClassicAssert.IsTrue(tokens.Length > 10 && tokens[2].Equals("MOVED"), command.Command);
454-
455-
var _address = tokens[5].Split(':')[0];
456-
var _port = int.Parse(tokens[5].Split(':')[1]);
457-
var _slot = int.Parse(tokens[8]);
458-
ClassicAssert.AreEqual(address, _address, command.Command);
459-
ClassicAssert.AreEqual(port, _port, command.Command);
460-
ClassicAssert.AreEqual(command.GetSlot, _slot, command.Command);
461-
return;
462-
}
463-
Assert.Fail($"Should not reach here. Command: {command.Command} \n{ClusterState()}");
417+
var ex = Assert.Throws<RedisServerException>(() => context.clusterTestUtils.GetServer(requestNodeIndex).Execute(command.Command, command.GetSingleSlotRequest(), CommandFlags.NoRedirect),
418+
$"Expected exception was not thrown. Command: {command.Command} \n{ClusterState()}");
419+
ClassicAssert.IsTrue(ex.Message.StartsWith("Key has MOVED"), command.Command);
420+
var tokens = ex.Message.Split(' ');
421+
ClassicAssert.IsTrue(tokens.Length > 10 && tokens[2].Equals("MOVED"), command.Command);
422+
423+
var _address = tokens[5].Split(':')[0];
424+
var _port = int.Parse(tokens[5].Split(':')[1]);
425+
var _slot = int.Parse(tokens[8]);
426+
ClassicAssert.AreEqual(address, _address, command.Command);
427+
ClassicAssert.AreEqual(port, _port, command.Command);
428+
ClassicAssert.AreEqual(command.GetSlot, _slot, command.Command);
464429
}
465430

466431
void GarnetClientSessionMOVEDTest(BaseCommand command)
467432
{
468433
var client = context.clusterTestUtils.GetGarnetClientSession(requestNodeIndex);
469-
try
470-
{
471-
client.ExecuteAsync(command.GetSingleSlotRequestWithCommand).GetAwaiter().GetResult();
472-
}
473-
catch (Exception ex)
474-
{
475-
ClassicAssert.AreEqual($"MOVED {command.GetSlot} {address}:{port}", ex.Message, command.Command);
476-
return;
477-
}
478-
Assert.Fail($"Should not reach here. Command: {command.Command} \n{ClusterState()}");
434+
var ex = Assert.Throws<Exception>(() => client.ExecuteAsync(command.GetSingleSlotRequestWithCommand).GetAwaiter().GetResult(),
435+
$"Expected exception was not thrown. Command: {command.Command} \n{ClusterState()}");
436+
ClassicAssert.AreEqual($"MOVED {command.GetSlot} {address}:{port}", ex.Message, command.Command);
479437
}
480438
}
481439
}
@@ -518,40 +476,25 @@ public void ClusterASKTest()
518476

519477
void SERedisASKTest(BaseCommand command)
520478
{
521-
RedisResult result = default;
522-
try
523-
{
524-
result = context.clusterTestUtils.GetServer(requestNodeIndex).Execute(command.Command, command.GetSingleSlotRequest(), CommandFlags.NoRedirect);
525-
}
526-
catch (Exception ex)
527-
{
528-
var tokens = ex.Message.Split(' ');
529-
ClassicAssert.IsTrue(tokens.Length > 10 && tokens[0].Equals("Endpoint"), command.Command + " => " + ex.Message);
530-
531-
var _address = tokens[1].Split(':')[0];
532-
var _port = int.Parse(tokens[1].Split(':')[1]);
533-
var _slot = int.Parse(tokens[4]);
534-
ClassicAssert.AreEqual(address, _address, command.Command);
535-
ClassicAssert.AreEqual(port, _port, command.Command);
536-
ClassicAssert.AreEqual(command.GetSlot, _slot, command.Command);
537-
return;
538-
}
539-
Assert.Fail($"Should not reach here. Command: {command.Command} \n{ClusterState()}");
479+
var ex = Assert.Throws<RedisConnectionException>(() => context.clusterTestUtils.GetServer(requestNodeIndex).Execute(command.Command, command.GetSingleSlotRequest(), CommandFlags.NoRedirect),
480+
$"Expected exception was not thrown. Command: {command.Command} \n{ClusterState()}");
481+
var tokens = ex.Message.Split(' ');
482+
ClassicAssert.IsTrue(tokens.Length > 10 && tokens[0].Equals("Endpoint"), command.Command + " => " + ex.Message);
483+
484+
var _address = tokens[1].Split(':')[0];
485+
var _port = int.Parse(tokens[1].Split(':')[1]);
486+
var _slot = int.Parse(tokens[4]);
487+
ClassicAssert.AreEqual(address, _address, command.Command);
488+
ClassicAssert.AreEqual(port, _port, command.Command);
489+
ClassicAssert.AreEqual(command.GetSlot, _slot, command.Command);
540490
}
541491

542492
void GarnetClientSessionASKTest(BaseCommand command)
543493
{
544494
var client = context.clusterTestUtils.GetGarnetClientSession(requestNodeIndex);
545-
try
546-
{
547-
_ = client.ExecuteAsync(command.GetSingleSlotRequestWithCommand).GetAwaiter().GetResult();
548-
}
549-
catch (Exception ex)
550-
{
551-
ClassicAssert.AreEqual($"ASK {command.GetSlot} {address}:{port}", ex.Message, command.Command);
552-
return;
553-
}
554-
Assert.Fail($"Should not reach here. Command: {command.Command} \n{ClusterState()}");
495+
var ex = Assert.Throws<Exception>(() => client.ExecuteAsync(command.GetSingleSlotRequestWithCommand).GetAwaiter().GetResult(),
496+
$"Expected exception was not thrown. Command: {command.Command} \n{ClusterState()}");
497+
ClassicAssert.AreEqual($"ASK {command.GetSlot} {address}:{port}", ex.Message, command.Command);
555498
}
556499
}
557500
}
@@ -591,12 +534,9 @@ void SERedisTRYAGAINTest(BaseCommand command)
591534
ConfigureSlotForMigration();
592535
try
593536
{
594-
_ = context.clusterTestUtils.GetServer(requestNodeIndex).Execute(command.Command, command.GetSingleSlotRequest(), CommandFlags.NoRedirect);
595-
}
596-
catch (Exception ex)
597-
{
537+
var ex = Assert.Throws<RedisServerException>(() => context.clusterTestUtils.GetServer(requestNodeIndex).Execute(command.Command, command.GetSingleSlotRequest(), CommandFlags.NoRedirect),
538+
$"Expected exception was not thrown. Command: {command.Command} \n{ClusterState()}");
598539
ClassicAssert.AreEqual("TRYAGAIN Multiple keys request during rehashing of slot", ex.Message, command.Command, $"\n{ClusterState()}");
599-
return;
600540
}
601541
finally
602542
{
@@ -611,8 +551,6 @@ void SERedisTRYAGAINTest(BaseCommand command)
611551
Assert.Fail($"Failed executing cleanup. Command: {command.Command} \n{ClusterState()}");
612552
}
613553
}
614-
615-
Assert.Fail($"Should not reach here. Command: {command.Command} \n{ClusterState()}");
616554
}
617555
}
618556
}

0 commit comments

Comments
 (0)