-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Supporting CLIENT identification via SETINFO (#180)
* deprecated redisGraph * supporting client SetInfo command * add tests * check somthing * check2 * Async * try ConnectionMultiplexer.Connect("localhost"); * new format * fix core tests * try change to IDatabase instead of IDatabaseAsync * add bool _setinfo to Auxiliary * comment CoreTests * fix Aux * change test * fix tests * not using pipeline * move _setInfo = false; * delete unused key * if redis version less than 7.1.242 dont sent SETINFO * add sleep * no sleep * try setinfo to true in each test * reset info defaults in each test * skip cluster + send commands in pipeline * delete comments * add null test * compare and of strings in null test * fixes
- Loading branch information
1 parent
da200b6
commit aa6f6a4
Showing
9 changed files
with
311 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using NRedisStack.RedisStackCommands; | ||
using NRedisStack.Core.Literals; | ||
using NRedisStack.Core; | ||
|
||
namespace NRedisStack | ||
{ | ||
|
||
public static class CoreCommandBuilder | ||
{ | ||
public static SerializedCommand ClientSetInfo(SetInfoAttr attr, string value) | ||
{ | ||
string attrValue = attr switch | ||
{ | ||
SetInfoAttr.LibraryName => CoreArgs.lib_name, | ||
SetInfoAttr.LibraryVersion => CoreArgs.lib_ver, | ||
_ => throw new System.NotImplementedException(), | ||
}; | ||
|
||
return new SerializedCommand(RedisCoreCommands.CLIENT, RedisCoreCommands.SETINFO, attrValue, value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using NRedisStack.Core; | ||
using StackExchange.Redis; | ||
namespace NRedisStack | ||
{ | ||
|
||
public static class CoreCommands | ||
{ | ||
/// <summary> | ||
/// Sets information specific to the client or connection. | ||
/// </summary> | ||
/// <param name="attr">which attribute to set</param> | ||
/// <param name="value">the attribute value</param> | ||
/// <returns><see langword="true"/> if the attribute name was successfully set, Error otherwise.</returns> | ||
/// <remarks><seealso href="https://redis.io/commands/client-setinfo/"/></remarks> | ||
public static bool ClientSetInfo(this IDatabase db, SetInfoAttr attr, string value) | ||
{ | ||
return db.Execute(CoreCommandBuilder.ClientSetInfo(attr, value)).OKtoBoolean(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using NRedisStack.Core; | ||
using StackExchange.Redis; | ||
namespace NRedisStack | ||
{ | ||
|
||
public static class CoreCommandsAsync //: ICoreCommandsAsync | ||
{ | ||
/// <summary> | ||
/// Sets information specific to the client or connection. | ||
/// </summary> | ||
/// <param name="attr">which attribute to set</param> | ||
/// <param name="value">the attribute value</param> | ||
/// <returns><see langword="true"/> if the attribute name was successfully set, Error otherwise.</returns> | ||
/// <remarks><seealso href="https://redis.io/commands/client-setinfo/"/></remarks> | ||
public static async Task<bool> ClientSetInfoAsync(this IDatabaseAsync db, SetInfoAttr attr, string value) | ||
{ | ||
return (await db.ExecuteAsync(CoreCommandBuilder.ClientSetInfo(attr, value))).OKtoBoolean(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace NRedisStack.Core; | ||
public enum SetInfoAttr | ||
{ | ||
/// <summary> | ||
/// meant to hold the name of the client library that's in use. | ||
/// </summary> | ||
LibraryName, | ||
/// <summary> | ||
/// meant to hold the client library's version. | ||
/// </summary> | ||
LibraryVersion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace NRedisStack.Core.Literals | ||
{ | ||
internal class CoreArgs | ||
{ | ||
public const string lib_name = "LIB-NAME"; | ||
public const string lib_ver = "LIB-VER"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace NRedisStack.Core.Literals | ||
{ | ||
/// <summary> | ||
/// Redis Core command literals | ||
/// </summary> | ||
internal class RedisCoreCommands | ||
{ | ||
public const string CLIENT = "CLIENT"; | ||
public const string SETINFO = "SETINFO"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
using Xunit; | ||
using NRedisStack.Core; | ||
using NRedisStack; | ||
using static NRedisStack.Auxiliary; | ||
using StackExchange.Redis; | ||
using System.Xml.Linq; | ||
using System.Reflection; | ||
using NRedisStack.RedisStackCommands; | ||
|
||
|
||
namespace NRedisStack.Tests.Core; | ||
|
||
public class CoreTests : AbstractNRedisStackTest, IDisposable | ||
{ | ||
public CoreTests(RedisFixture redisFixture) : base(redisFixture) { } | ||
|
||
|
||
[SkipIfRedis(Is.OSSCluster, Comparison.LessThan, "7.1.242")] | ||
public void TestSimpleSetInfo() | ||
{ | ||
var redis = ConnectionMultiplexer.Connect("localhost"); | ||
var db = redis.GetDatabase(); | ||
db.Execute("FLUSHALL"); | ||
|
||
db.ClientSetInfo(SetInfoAttr.LibraryName, "TestLibraryName"); | ||
db.ClientSetInfo(SetInfoAttr.LibraryVersion, "1.2.3"); | ||
|
||
var info = db.Execute("CLIENT", "INFO").ToString(); | ||
Assert.EndsWith($"lib-name=TestLibraryName lib-ver=1.2.3\n", info); | ||
} | ||
|
||
[SkipIfRedis(Is.OSSCluster, Comparison.LessThan, "7.1.242")] | ||
public async Task TestSimpleSetInfoAsync() | ||
{ | ||
var redis = ConnectionMultiplexer.Connect("localhost"); | ||
var db = redis.GetDatabase(); | ||
db.Execute("FLUSHALL"); | ||
|
||
await db.ClientSetInfoAsync(SetInfoAttr.LibraryName, "TestLibraryName"); | ||
await db.ClientSetInfoAsync(SetInfoAttr.LibraryVersion, "1.2.3"); | ||
|
||
var info = db.Execute("CLIENT", "INFO").ToString(); | ||
Assert.EndsWith($"lib-name=TestLibraryName lib-ver=1.2.3\n", info); | ||
} | ||
|
||
[SkipIfRedis(Is.OSSCluster, Comparison.LessThan, "7.1.242")] | ||
public void TestSetInfoDefaultValue() | ||
{ | ||
ResetInfoDefaults(); // demonstrate first connection | ||
var redis = ConnectionMultiplexer.Connect("localhost"); | ||
var db = redis.GetDatabase(); | ||
db.Execute("FLUSHALL"); | ||
|
||
db.Execute(new SerializedCommand("PING")); // only the extension method of Execute (which is used for all the commands of Redis Stack) will set the library name and version. | ||
|
||
var info = db.Execute("CLIENT", "INFO").ToString(); | ||
Assert.EndsWith($"lib-name=NRedisStack;.NET-{Environment.Version} lib-ver={GetNRedisStackVersion()}\n", info); | ||
} | ||
|
||
[SkipIfRedis(Is.OSSCluster, Comparison.LessThan, "7.1.242")] | ||
public async Task TestSetInfoDefaultValueAsync() | ||
{ | ||
ResetInfoDefaults(); // demonstrate first connection | ||
var redis = ConnectionMultiplexer.Connect("localhost"); | ||
var db = redis.GetDatabase(); | ||
db.Execute("FLUSHALL"); | ||
|
||
await db.ExecuteAsync(new SerializedCommand("PING")); // only the extension method of Execute (which is used for all the commands of Redis Stack) will set the library name and version. | ||
|
||
var info = (await db.ExecuteAsync("CLIENT", "INFO")).ToString(); | ||
Assert.EndsWith($"lib-name=NRedisStack;.NET-{Environment.Version} lib-ver={GetNRedisStackVersion()}\n", info); | ||
} | ||
|
||
[SkipIfRedis(Is.OSSCluster, Comparison.LessThan, "7.1.242")] | ||
public void TestSetInfoWithValue() | ||
{ | ||
ResetInfoDefaults(); // demonstrate first connection | ||
var redis = ConnectionMultiplexer.Connect("localhost"); | ||
var db = redis.GetDatabase("MyLibraryName;v1.0.0"); | ||
db.Execute("FLUSHALL"); | ||
|
||
db.Execute(new SerializedCommand("PING")); // only the extension method of Execute (which is used for all the commands of Redis Stack) will set the library name and version. | ||
|
||
var info = db.Execute("CLIENT", "INFO").ToString(); | ||
Assert.EndsWith($"NRedisStack(MyLibraryName;v1.0.0);.NET-{Environment.Version}) lib-ver={GetNRedisStackVersion()}\n", info); | ||
} | ||
|
||
[SkipIfRedis(Is.OSSCluster, Comparison.LessThan, "7.1.242")] | ||
public async Task TestSetInfoWithValueAsync() | ||
{ | ||
ResetInfoDefaults(); // demonstrate first connection | ||
var redis = ConnectionMultiplexer.Connect("localhost"); | ||
var db = redis.GetDatabase("MyLibraryName;v1.0.0"); | ||
db.Execute("FLUSHALL"); | ||
|
||
await db.ExecuteAsync(new SerializedCommand("PING")); // only the extension method of Execute (which is used for all the commands of Redis Stack) will set the library name and version. | ||
|
||
var info = (await db.ExecuteAsync("CLIENT", "INFO")).ToString(); | ||
Assert.EndsWith($"NRedisStack(MyLibraryName;v1.0.0);.NET-{Environment.Version}) lib-ver={GetNRedisStackVersion()}\n", info); | ||
} | ||
|
||
[SkipIfRedis(Is.OSSCluster, Comparison.LessThan, "7.1.242")] | ||
public void TestSetInfoNull() | ||
{ | ||
ResetInfoDefaults(); // demonstrate first connection | ||
var redis = ConnectionMultiplexer.Connect("localhost"); | ||
var db = redis.GetDatabase(null); | ||
|
||
db.Execute("FLUSHALL"); | ||
var infoBefore = db.Execute("CLIENT", "INFO").ToString(); | ||
db.Execute(new SerializedCommand("PING")); // only the extension method of Execute (which is used for all the commands of Redis Stack) will set the library name and version. | ||
|
||
var infoAfter = db.Execute("CLIENT", "INFO").ToString(); | ||
// Find the indices of "lib-name=" in the strings | ||
int infoAfterLibNameIndex = infoAfter!.IndexOf("lib-name="); | ||
int infoBeforeLibNameIndex = infoBefore!.IndexOf("lib-name="); | ||
|
||
// Extract the sub-strings starting from "lib-name=" | ||
string infoAfterLibNameToEnd = infoAfter.Substring(infoAfterLibNameIndex); | ||
string infoBeforeLibNameToEnd = infoBefore.Substring(infoBeforeLibNameIndex); | ||
|
||
// Assert that the extracted sub-strings are equal | ||
Assert.Equal(infoAfterLibNameToEnd, infoBeforeLibNameToEnd); | ||
} | ||
|
||
[SkipIfRedis(Is.OSSCluster, Comparison.LessThan, "7.1.242")] | ||
public async Task TestSetInfoNullAsync() | ||
{ | ||
ResetInfoDefaults(); // demonstrate first connection | ||
var redis = ConnectionMultiplexer.Connect("localhost"); | ||
var db = redis.GetDatabase(null); | ||
|
||
db.Execute("FLUSHALL"); | ||
var infoBefore = (await db.ExecuteAsync("CLIENT", "INFO")).ToString(); | ||
await db.ExecuteAsync(new SerializedCommand("PING")); // only the extension method of Execute (which is used for all the commands of Redis Stack) will set the library name and version. | ||
|
||
var infoAfter = (await db.ExecuteAsync("CLIENT", "INFO")).ToString(); | ||
// Find the indices of "lib-name=" in the strings | ||
int infoAfterLibNameIndex = infoAfter!.IndexOf("lib-name="); | ||
int infoBeforeLibNameIndex = infoBefore!.IndexOf("lib-name="); | ||
|
||
// Extract the sub-strings starting from "lib-name=" | ||
string infoAfterLibNameToEnd = infoAfter.Substring(infoAfterLibNameIndex); | ||
string infoBeforeLibNameToEnd = infoBefore.Substring(infoBeforeLibNameIndex); | ||
|
||
// Assert that the extracted sub-strings are equal | ||
Assert.Equal(infoAfterLibNameToEnd, infoBeforeLibNameToEnd); | ||
} | ||
} |