From f5514666669d64a92a5e15432dd2a869f505f0dc Mon Sep 17 00:00:00 2001 From: atakavci Date: Sat, 28 Sep 2024 10:37:35 +0300 Subject: [PATCH] deprecate T&F --- src/NRedisStack/Gears/GearsCommandBuilder.cs | 5 + src/NRedisStack/Gears/GearsCommands.cs | 7 +- src/NRedisStack/Gears/GearsCommandsAsync.cs | 5 + tests/NRedisStack.Tests/Gears/GearsTests.cs | 210 ------------------- 4 files changed, 16 insertions(+), 211 deletions(-) delete mode 100644 tests/NRedisStack.Tests/Gears/GearsTests.cs diff --git a/src/NRedisStack/Gears/GearsCommandBuilder.cs b/src/NRedisStack/Gears/GearsCommandBuilder.cs index 5af745ba..8dec9026 100644 --- a/src/NRedisStack/Gears/GearsCommandBuilder.cs +++ b/src/NRedisStack/Gears/GearsCommandBuilder.cs @@ -3,8 +3,10 @@ namespace NRedisStack { + [Obsolete] public static class GearsCommandBuilder { + [Obsolete] public static SerializedCommand TFunctionLoad(string libraryCode, bool replace = false, string? config = null) { var args = new List() { GearsArgs.LOAD }; @@ -23,11 +25,13 @@ public static SerializedCommand TFunctionLoad(string libraryCode, bool replace = return new SerializedCommand(RG.TFUNCTION, args); } + [Obsolete] public static SerializedCommand TFunctionDelete(string libraryName) { return new SerializedCommand(RG.TFUNCTION, GearsArgs.DELETE, libraryName); } + [Obsolete] public static SerializedCommand TFunctionList(bool withCode = false, int verbose = 0, string? libraryName = null) { var args = new List() { GearsArgs.LIST }; @@ -55,6 +59,7 @@ public static SerializedCommand TFunctionList(bool withCode = false, int verbose return new SerializedCommand(RG.TFUNCTION, args); } + [Obsolete] public static SerializedCommand TFCall(string libraryName, string functionName, string[]? keys = null, string[]? args = null, bool async = false) { string command = async ? RG.TFCALLASYNC : RG.TFCALL; diff --git a/src/NRedisStack/Gears/GearsCommands.cs b/src/NRedisStack/Gears/GearsCommands.cs index b82a1eb2..4787fa86 100644 --- a/src/NRedisStack/Gears/GearsCommands.cs +++ b/src/NRedisStack/Gears/GearsCommands.cs @@ -1,7 +1,7 @@ using StackExchange.Redis; namespace NRedisStack { - + [Obsolete] public static class GearsCommands //: GearsCommandsAsync, IGearsCommands { @@ -17,6 +17,7 @@ public static class GearsCommands //: GearsCommandsAsync, IGearsCommands /// an optional argument, instructs RedisGears to replace the function if its already exists. /// if everything was done correctly, Error otherwise. /// //TODO: check this link when it's available + [Obsolete] public static bool TFunctionLoad(this IDatabase db, string libraryCode, bool replace = false, string? config = null) { return db.Execute(GearsCommandBuilder.TFunctionLoad(libraryCode, replace, config)).OKtoBoolean(); @@ -28,6 +29,7 @@ public static bool TFunctionLoad(this IDatabase db, string libraryCode, bool rep /// the name of the library to delete. /// if the library was deleted successfully, Error otherwise. /// //TODO: check this link when it's available + [Obsolete] public static bool TFunctionDelete(this IDatabase db, string libraryName) { return db.Execute(GearsCommandBuilder.TFunctionDelete(libraryName)).OKtoBoolean(); @@ -42,6 +44,7 @@ public static bool TFunctionDelete(this IDatabase db, string libraryName) /// multiple times to show multiple libraries in a single command) /// Information about the requested libraries. /// //TODO: check this link when it's available + [Obsolete] public static Dictionary[] TFunctionList(this IDatabase db, bool withCode = false, int verbose = 0, string? libraryName = null) { return db.Execute(GearsCommandBuilder.TFunctionList(withCode, verbose, libraryName)).ToDictionarys(); @@ -56,6 +59,7 @@ public static Dictionary[] TFunctionList(this IDatabase db, /// Additional argument to pass to the function. /// The return value from the sync & async function on error in case of failure. /// + [Obsolete] public static RedisResult TFCall_(this IDatabase db, string libraryName, string functionName, string[]? keys = null, string[]? args = null) { return db.Execute(GearsCommandBuilder.TFCall(libraryName, functionName, keys, args, async: false)); @@ -70,6 +74,7 @@ public static RedisResult TFCall_(this IDatabase db, string libraryName, string /// Additional argument to pass to the function. /// The return value from the sync & async function on error in case of failure. /// + [Obsolete] public static RedisResult TFCallAsync_(this IDatabase db, string libraryName, string functionName, string[]? keys = null, string[]? args = null) { return db.Execute(GearsCommandBuilder.TFCall(libraryName, functionName, keys, args, async: true)); diff --git a/src/NRedisStack/Gears/GearsCommandsAsync.cs b/src/NRedisStack/Gears/GearsCommandsAsync.cs index 076d51bc..3963dfca 100644 --- a/src/NRedisStack/Gears/GearsCommandsAsync.cs +++ b/src/NRedisStack/Gears/GearsCommandsAsync.cs @@ -16,6 +16,7 @@ public static class GearsCommandsAsync //: IGearsCommandsAsync /// an optional argument, instructs RedisGears to replace the function if its already exists. /// if everything was done correctly, Error otherwise. /// //TODO: add link to the command when it's available + [Obsolete] public static async Task TFunctionLoadAsync(this IDatabase db, string libraryCode, string? config = null, bool replace = false) { return (await db.ExecuteAsync(GearsCommandBuilder.TFunctionLoad(libraryCode, replace, config))).OKtoBoolean(); @@ -27,6 +28,7 @@ public static async Task TFunctionLoadAsync(this IDatabase db, string libr /// the name of the library to delete. /// if the library was deleted successfully, Error otherwise. /// //TODO: add link to the command when it's available + [Obsolete] public static async Task TFunctionDeleteAsync(this IDatabase db, string libraryName) { return (await db.ExecuteAsync(GearsCommandBuilder.TFunctionDelete(libraryName))).OKtoBoolean(); @@ -41,6 +43,7 @@ public static async Task TFunctionDeleteAsync(this IDatabase db, string li /// multiple times to show multiple libraries in a single command) /// Information about the requested libraries. /// //TODO: add link to the command when it's available + [Obsolete] public static async Task[]> TFunctionListAsync(this IDatabase db, bool withCode = false, int verbose = 0, string? libraryName = null) { return (await db.ExecuteAsync(GearsCommandBuilder.TFunctionList(withCode, verbose, libraryName))).ToDictionarys(); @@ -55,6 +58,7 @@ public static async Task[]> TFunctionListAsync(t /// Additional argument to pass to the function. /// The return value from the sync & async function on error in case of failure. /// + [Obsolete] public async static Task TFCall_Async(this IDatabase db, string libraryName, string functionName, string[]? keys = null, string[]? args = null) { return await db.ExecuteAsync(GearsCommandBuilder.TFCall(libraryName, functionName, keys, args, async: false)); @@ -69,6 +73,7 @@ public async static Task TFCall_Async(this IDatabase db, string lib /// Additional argument to pass to the function. /// The return value from the sync & async function on error in case of failure. /// + [Obsolete] public async static Task TFCallAsync_Async(this IDatabase db, string libraryName, string functionName, string[]? keys = null, string[]? args = null) { return await db.ExecuteAsync(GearsCommandBuilder.TFCall(libraryName, functionName, keys, args, async: true)); diff --git a/tests/NRedisStack.Tests/Gears/GearsTests.cs b/tests/NRedisStack.Tests/Gears/GearsTests.cs deleted file mode 100644 index 02a7c474..00000000 --- a/tests/NRedisStack.Tests/Gears/GearsTests.cs +++ /dev/null @@ -1,210 +0,0 @@ -using Xunit; -using StackExchange.Redis; - -namespace NRedisStack.Tests.Gears; - -public class GearsTests : AbstractNRedisStackTest, IDisposable -{ - // private readonly string key = "GEARS_TESTS"; - public GearsTests(RedisFixture redisFixture) : base(redisFixture) { } - - [SkipIfRedis(Is.Enterprise, Comparison.LessThan, "7.1.242")] - public void TestTFunctionLoadDelete() - { - IDatabase db = redisFixture.Redis.GetDatabase(); - if (!redisFixture.isEnterprise) - db.ExecuteBroadcast("REDISGEARS_2.REFRESHCLUSTER"); - db.Execute("FLUSHALL"); - Assert.True(db.TFunctionLoad(GenerateLibCode("lib"))); - Assert.True(db.TFunctionDelete("lib")); - } - - - [SkipIfRedis(Is.Enterprise, Comparison.LessThan, "7.1.242")] - public async Task TestTFunctionLoadDeleteAsync() - { - IDatabase db = redisFixture.Redis.GetDatabase(); - if (!redisFixture.isEnterprise) - db.ExecuteBroadcast("REDISGEARS_2.REFRESHCLUSTER"); - db.Execute("FLUSHALL"); - TryDeleteLib(db, "lib", "lib1", "lib2", "lib3"); - - Assert.True(await db.TFunctionLoadAsync(GenerateLibCode("lib"))); - Assert.True(await db.TFunctionDeleteAsync("lib")); - } - - [SkipIfRedis(Is.Enterprise, Comparison.LessThan, "7.1.242")] - public void TestTFunctionList() - { - IDatabase db = redisFixture.Redis.GetDatabase(); - if (!redisFixture.isEnterprise) - db.ExecuteBroadcast("REDISGEARS_2.REFRESHCLUSTER"); - db.Execute("FLUSHALL"); - TryDeleteLib(db, "lib", "lib1", "lib2", "lib3"); - - Assert.True(db.TFunctionLoad(GenerateLibCode("lib1"))); - Assert.True(db.TFunctionLoad(GenerateLibCode("lib2"))); - Assert.True(db.TFunctionLoad(GenerateLibCode("lib3"))); - - // test error throwing: - Assert.Throws(() => db.TFunctionList(verbose: 8)); - var functions = db.TFunctionList(verbose: 1); - Assert.Equal(3, functions.Length); - - HashSet expectedNames = new HashSet { "lib1", "lib2", "lib3" }; - HashSet actualNames = new HashSet{ - functions[0]["name"].ToString()!, - functions[1]["name"].ToString()!, - functions[2]["name"].ToString()! - }; - - Assert.Equal(expectedNames, actualNames); - - - Assert.True(db.TFunctionDelete("lib1")); - Assert.True(db.TFunctionDelete("lib2")); - Assert.True(db.TFunctionDelete("lib3")); - } - - [SkipIfRedis(Is.Enterprise, Comparison.LessThan, "7.1.242")] - public async Task TestTFunctionListAsync() - { - IDatabase db = redisFixture.Redis.GetDatabase(); - if (!redisFixture.isEnterprise) - db.ExecuteBroadcast("REDISGEARS_2.REFRESHCLUSTER"); - db.Execute("FLUSHALL"); - TryDeleteLib(db, "lib", "lib1", "lib2", "lib3"); - - Assert.True(await db.TFunctionLoadAsync(GenerateLibCode("lib1"))); - Assert.True(await db.TFunctionLoadAsync(GenerateLibCode("lib2"))); - Assert.True(await db.TFunctionLoadAsync(GenerateLibCode("lib3"))); - - var functions = await db.TFunctionListAsync(verbose: 1); - Assert.Equal(3, functions.Length); - - HashSet expectedNames = new HashSet { "lib1", "lib2", "lib3" }; - HashSet actualNames = new HashSet{ - functions[0]["name"].ToString()!, - functions[1]["name"].ToString()!, - functions[2]["name"].ToString()! - }; - - Assert.Equal(expectedNames, actualNames); - - - Assert.True(await db.TFunctionDeleteAsync("lib1")); - Assert.True(await db.TFunctionDeleteAsync("lib2")); - Assert.True(await db.TFunctionDeleteAsync("lib3")); - } - - [SkipIfRedis(Is.Enterprise, Comparison.LessThan, "7.1.242")] - public void TestTFCall() - { - IDatabase db = redisFixture.Redis.GetDatabase(); - if (!redisFixture.isEnterprise) - db.ExecuteBroadcast("REDISGEARS_2.REFRESHCLUSTER"); - db.Execute("FLUSHALL"); - TryDeleteLib(db, "lib", "lib1", "lib2", "lib3"); - - Assert.True(db.TFunctionLoad(GenerateLibCode("lib"))); - Assert.Equal("bar", db.TFCall_("lib", "foo").ToString()); - Assert.Equal("bar", db.TFCallAsync_("lib", "foo").ToString()); - - Assert.True(db.TFunctionDelete("lib")); - } - - [SkipIfRedis(Is.Enterprise, Comparison.LessThan, "7.1.242")] - public async Task TestTFCallAsync() - { - IDatabase db = redisFixture.Redis.GetDatabase(); - if (!redisFixture.isEnterprise) - db.ExecuteBroadcast("REDISGEARS_2.REFRESHCLUSTER"); - db.Execute("FLUSHALL"); - TryDeleteLib(db, "lib", "lib1", "lib2", "lib3"); - - Assert.True(await db.TFunctionLoadAsync(GenerateLibCode("lib"))); - Assert.Equal("bar", (await db.TFCall_Async("lib", "foo")).ToString()); - Assert.Equal("bar", (await db.TFCallAsync_Async("lib", "foo")).ToString()); - - Assert.True(await db.TFunctionDeleteAsync("lib")); - } - - [SkipIfRedis(Is.Enterprise, Comparison.LessThan, "7.1.242")] - public void TestGearsCommandBuilder() - { - // TFunctionLoad: - var buildCommand = GearsCommandBuilder - .TFunctionLoad(GenerateLibCode("lib"), - true, "config"); - var expected = new List - { - "LOAD", - "REPLACE", - "CONFIG", - "config", - GenerateLibCode("lib") - }; - Assert.Equal("TFUNCTION", buildCommand.Command); - Assert.Equal(expected, buildCommand.Args); - - // TFunctionDelete: - buildCommand = GearsCommandBuilder.TFunctionDelete("lib"); - expected = new List - { - "DELETE", - "lib" - }; - Assert.Equal("TFUNCTION", buildCommand.Command); - Assert.Equal(expected, buildCommand.Args); - - // TFunctionList: - buildCommand = GearsCommandBuilder.TFunctionList(true, 2, "lib"); - expected = new List - { - "LIST", - "WITHCODE", - "vv", - "LIBRARY", - "lib", - }; - Assert.Equal("TFUNCTION", buildCommand.Command); - Assert.Equal(expected, buildCommand.Args); - - // TFCall: - var buildSync = GearsCommandBuilder.TFCall("libName", "funcName", new string[] { "key1", "key2" }, new string[] { "arg1", "arg2" }, false); - var buildAsync = GearsCommandBuilder.TFCall("libName", "funcName", new string[] { "key1", "key2" }, new string[] { "arg1", "arg2" }, true); - - expected = new List - { - "libName.funcName", - 2, - "key1", - "key2", - "arg1", - "arg2" - }; - - Assert.Equal("TFCALL", buildSync.Command); - Assert.Equal(expected, buildSync.Args); - - Assert.Equal("TFCALLASYNC", buildAsync.Command); - Assert.Equal(expected, buildAsync.Args); - } - - private static void TryDeleteLib(IDatabase db, params string[] libNames) - { - foreach (var libName in libNames) - { - try - { - db.ExecuteBroadcast(GearsCommandBuilder.TFunctionDelete(libName)); - } - catch (RedisServerException) { } // ignore - } - } - - private static string GenerateLibCode(string libName) - { - return $"#!js api_version=1.0 name={libName}\n redis.registerFunction('foo', ()=>{{return 'bar'}})"; - } -}