Skip to content

Commit 917e189

Browse files
authored
Backport of: Fix failing docuement.load in query results with an expired doc #357 (#410)
Fix failing docuement.load in query results with an expired doc (#357) * fix failing docuement load in query results with an expired doc * IsCompletedSuccessfully" is *not* available in all test envs * set test case for non cluster env
1 parent 73abd4f commit 917e189

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/NRedisStack/Search/Document.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public static Document Load(string id, double score, byte[]? payload, RedisValue
3131
{
3232
Document ret = new Document(id, score, payload);
3333
if (fields == null) return ret;
34+
if (fields.Length == 1 && fields[0].IsNull)
35+
{
36+
return ret;
37+
}
3438
for (int i = 0; i < fields.Length; i += 2)
3539
{
3640
string fieldName = fields[i]!;

tests/NRedisStack.Tests/Search/SearchTests.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212

1313
namespace NRedisStack.Tests.Search;
14-
1514
public class SearchTests : AbstractNRedisStackTest, IDisposable
1615
{
1716
// private readonly string key = "SEARCH_TESTS";
@@ -3313,4 +3312,53 @@ public void TestNumericLogicalOperatorsInDialect4()
33133312
Assert.Equal(1, ft.Search(index, new Query("@version:[123 123] | @id:[456 7890]")).TotalResults);
33143313
Assert.Equal(1, ft.Search(index, new Query("@version==123 @id==456").Dialect(4)).TotalResults);
33153314
}
3315+
3316+
[Fact]
3317+
public void TestDocumentLoad_Issue352()
3318+
{
3319+
Document d = Document.Load("1", 0.5, null, new RedisValue[] { RedisValue.Null });
3320+
Assert.Empty(d.GetProperties().ToList());
3321+
}
3322+
3323+
[SkipIfRedis(Is.OSSCluster)]
3324+
public void TestDocumentLoadWithDB_Issue352()
3325+
{
3326+
IDatabase db = redisFixture.Redis.GetDatabase();
3327+
db.Execute("FLUSHALL");
3328+
var ft = db.FT();
3329+
3330+
Schema sc = new Schema().AddTextField("first", 1.0).AddTextField("last", 1.0).AddNumericField("age");
3331+
Assert.True(ft.Create(index, FTCreateParams.CreateParams(), sc));
3332+
3333+
Document droppedDocument = null;
3334+
int numberOfAttempts = 0;
3335+
do
3336+
{
3337+
db.HashSet("student:1111", new HashEntry[] { new("first", "Joe"), new("last", "Dod"), new("age", 18) });
3338+
3339+
Assert.True(db.KeyExpire("student:1111", TimeSpan.FromMilliseconds(500)));
3340+
3341+
Boolean cancelled = false;
3342+
Task searchTask = Task.Run(() =>
3343+
{
3344+
for (int i = 0; i < 100000; i++)
3345+
{
3346+
SearchResult result = ft.Search(index, new Query());
3347+
List<Document> docs = result.Documents;
3348+
if (docs.Count == 0 || cancelled)
3349+
{
3350+
break;
3351+
}
3352+
else if (docs[0].GetProperties().ToList().Count == 0)
3353+
{
3354+
droppedDocument = docs[0];
3355+
}
3356+
}
3357+
});
3358+
Task.WhenAny(searchTask, Task.Delay(1000)).GetAwaiter().GetResult();
3359+
Assert.True(searchTask.IsCompleted);
3360+
Assert.Null(searchTask.Exception);
3361+
cancelled = true;
3362+
} while (droppedDocument == null && numberOfAttempts++ < 3);
3363+
}
33163364
}

0 commit comments

Comments
 (0)