Skip to content

Commit

Permalink
Batch/Transaction need to override new ExecuteAsync API; fix #2167 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mgravell committed Jun 28, 2022
1 parent 488e107 commit b585710
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Pending

- URGENT Fix: [#2167](https://github.com/StackExchange/StackExchange.Redis/issues/2167), [#2176](https://github.com/StackExchange/StackExchange.Redis/issues/2176): fix error in batch/transaction handling that can result in out-of-order instructions ([#2177 by MarcGravell](https://github.com/StackExchange/StackExchange.Redis/pull/2177))
- Fix: [#2164](https://github.com/StackExchange/StackExchange.Redis/issues/2164): fix `LuaScript.Prepare` for scripts that don't have parameters ([#2166 by MarcGravell](https://github.com/StackExchange/StackExchange.Redis/pull/2166))

## 2.6.45
Expand Down
7 changes: 4 additions & 3 deletions src/StackExchange.Redis/RedisBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ public void Execute()
}
}
}

internal override Task<T?> ExecuteAsync<T>(Message? message, ResultProcessor<T>? processor, ServerEndPoint? server = null) where T : default
=> ExecuteAsync(message, processor!, default, server);
internal override Task<T> ExecuteAsync<T>(Message? message, ResultProcessor<T>? processor, T defaultValue, ServerEndPoint? server = null) where T : default
{
if (message == null) return CompletedTask<T>.Default(asyncState);
if (message == null) return CompletedTask<T>.FromDefault(defaultValue, asyncState);
multiplexer.CheckMessage(message);

// prepare the inner command as a task
Expand All @@ -83,7 +84,7 @@ public void Execute()

// store it
(pending ??= new List<Message>()).Add(message);
return task;
return task!;
}

internal override T ExecuteSync<T>(Message? message, ResultProcessor<T>? processor, ServerEndPoint? server = null, T? defaultValue = default) where T : default =>
Expand Down
6 changes: 4 additions & 2 deletions src/StackExchange.Redis/RedisTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ public Task<bool> ExecuteAsync(CommandFlags flags)
}

internal override Task<T?> ExecuteAsync<T>(Message? message, ResultProcessor<T>? processor, ServerEndPoint? server = null) where T : default
=> ExecuteAsync(message, processor!, default, server);
internal override Task<T> ExecuteAsync<T>(Message? message, ResultProcessor<T>? processor, T defaultValue, ServerEndPoint? server = null) where T : default
{
if (message == null) return CompletedTask<T>.Default(asyncState);
if (message == null) return CompletedTask<T>.FromDefault(defaultValue, asyncState);
multiplexer.CheckMessage(message);

multiplexer.Trace("Wrapping " + message.Command, "Transaction");
Expand Down Expand Up @@ -102,7 +104,7 @@ public Task<bool> ExecuteAsync(CommandFlags flags)
break;
}
}
return task;
return task!;
}

internal override T? ExecuteSync<T>(Message? message, ResultProcessor<T>? processor, ServerEndPoint? server = null, T? defaultValue = default) where T : default
Expand Down
48 changes: 48 additions & 0 deletions tests/StackExchange.Redis.Tests/Issues/Issue2176.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace StackExchange.Redis.Tests.Issues
{
public class Issue2176 : TestBase
{
public Issue2176(ITestOutputHelper output) : base(output) { }

[Fact]
public void Execute()
{
using var conn = Create();
var db = conn.GetDatabase();

var me = Me();
var key = me + ":1";
var key2 = me + ":2";
var keyIntersect = me + ":result";

db.KeyDelete(key);
db.KeyDelete(key2);
db.KeyDelete(keyIntersect);
db.SortedSetAdd(key, "a", 1345);

var tasks = new List<Task>();
var batch = db.CreateBatch();
tasks.Add(batch.SortedSetAddAsync(key2, "a", 4567));
tasks.Add(batch.SortedSetCombineAndStoreAsync(SetOperation.Intersect,
keyIntersect, new RedisKey[] { key, key2 }));
var rangeByRankTask = batch.SortedSetRangeByRankAsync(keyIntersect);
tasks.Add(rangeByRankTask);
batch.Execute();

Task.WhenAll(tasks.ToArray());

var rangeByRankSortedSetValues = rangeByRankTask.Result;

int size = rangeByRankSortedSetValues.Length;
Assert.Equal(1, size);
string firstRedisValue = rangeByRankSortedSetValues.FirstOrDefault().ToString();
Assert.Equal("a", firstRedisValue);
}
}
}

0 comments on commit b585710

Please sign in to comment.