Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 88a8d78

Browse files
committed
Merge branch 'master' of ssh://github.com/ServiceStack/ServiceStack.Redis
2 parents 16f0059 + 83197ea commit 88a8d78

File tree

5 files changed

+91
-1
lines changed

5 files changed

+91
-1
lines changed

src/ServiceStack.Redis/Commands.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,5 +180,8 @@ public static class Commands
180180
public readonly static byte[] Px = "PX".ToUtf8Bytes();
181181
public readonly static byte[] Nx = "NX".ToUtf8Bytes();
182182
public readonly static byte[] Xx = "XX".ToUtf8Bytes();
183+
184+
// Sentinel commands
185+
public readonly static byte[] Sentinel = "SENTINEL".ToUtf8Bytes();
183186
}
184187
}

src/ServiceStack.Redis/RedisNativeClient.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,24 @@ public byte[] BRPopLPush(string fromListId, string toListId, int timeOutSecs)
12091209
return result.Length == 0 ? null : result[1];
12101210
}
12111211

1212+
public object[] Sentinel(string command, string master = null)
1213+
{
1214+
if (command == null)
1215+
throw new ArgumentNullException("command");
1216+
1217+
var args = new List<byte[]>()
1218+
{
1219+
Commands.Sentinel,
1220+
command.ToUtf8Bytes()
1221+
};
1222+
1223+
if (master != null)
1224+
{
1225+
args.Add(master.ToUtf8Bytes());
1226+
}
1227+
return SendExpectDeeplyNestedMultiData(args.ToArray());
1228+
}
1229+
12121230
#endregion
12131231

12141232

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using NUnit.Framework;
6+
7+
namespace ServiceStack.Redis.Tests
8+
{
9+
[TestFixture, Category("Integration")]
10+
public class RedisSentinelTests
11+
: RedisClientTestsBase
12+
{
13+
protected RedisClient RedisSentinel;
14+
15+
16+
public override void OnBeforeEachTest()
17+
{
18+
base.OnBeforeEachTest();
19+
20+
RedisSentinel = new RedisClient(TestConfig.SingleHost, TestConfig.RedisSentinelPort);
21+
}
22+
23+
24+
public override void TearDown()
25+
{
26+
base.TearDown();
27+
28+
RedisSentinel.Dispose();
29+
}
30+
31+
32+
[Test]
33+
public void Can_Ping_Sentinel()
34+
{
35+
Assert.True(RedisSentinel.Ping());
36+
}
37+
38+
[Test]
39+
public void Can_Get_Sentinel_Masters()
40+
{
41+
object[] masters = RedisSentinel.Sentinel("masters");
42+
43+
Assert.AreEqual(masters.Count(), TestConfig.MasterHosts.Count());
44+
}
45+
46+
[Test]
47+
public void Can_Get_Sentinel_Slaves()
48+
{
49+
object[] slaves = RedisSentinel.Sentinel("slaves", TestConfig.MasterName);
50+
51+
Assert.AreEqual(slaves.Count(), TestConfig.SlaveHosts.Count());
52+
}
53+
54+
[Test]
55+
public void Can_Get_Master_Addr()
56+
{
57+
object[] addr = RedisSentinel.Sentinel("get-master-addr-by-name", TestConfig.MasterName);
58+
59+
string host = Encoding.UTF8.GetString((byte[])addr[0]);
60+
string port = Encoding.UTF8.GetString((byte[])addr[1]);
61+
62+
Assert.AreEqual(host, "127.0.0.1"); // IP of localhost
63+
Assert.AreEqual(port, TestConfig.RedisPort.ToString());
64+
}
65+
}
66+
}

tests/ServiceStack.Redis.Tests/ServiceStack.Redis.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@
232232
<Compile Include="Properties\AssemblyInfo.cs" />
233233
<Compile Include="RedisClientTestsBase.cs" />
234234
<Compile Include="RedisPubSubTests.cs" />
235+
<Compile Include="RedisSentinelTests.cs" />
235236
<Compile Include="RedisTransactionCommonTests.cs" />
236237
<Compile Include="RedisTransactionTests.cs" />
237238
<Compile Include="RedisUtilTests.cs" />

tests/ServiceStack.Redis.Tests/TestConfig.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ static TestConfig()
1212

1313
public const bool IgnoreLongTests = true;
1414

15-
public const string SingleHost = "localhost";
15+
public const string SingleHost = "localhost";
16+
public const string MasterName = "mymaster";
1617
public static readonly string[] MasterHosts = new[] { "localhost" };
1718
public static readonly string[] SlaveHosts = new[] { "localhost" };
1819

1920
public const int RedisPort = 6379;
21+
public const int RedisSentinelPort = 26379;
2022

2123
public static string SingleHostConnectionString
2224
{

0 commit comments

Comments
 (0)