Skip to content

Commit 2dfbb60

Browse files
committed
try to add more replication stability
1 parent 3800073 commit 2dfbb60

File tree

1 file changed

+59
-37
lines changed

1 file changed

+59
-37
lines changed

tests/NRedisStack.Tests/Search/SearchTests.cs

Lines changed: 59 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ private void AddDocument(IDatabase db, string key, Dictionary<string, object> ob
4444
db.HashSet(key, hash);
4545
}
4646

47+
private void AssertDatabaseSize(IDatabase db, int expected)
48+
{
49+
// in part, this is to allow replication to catch up
50+
for (int i = 0; i < 10; i++)
51+
{
52+
Assert.Equal(expected, DatabaseSize(db));
53+
}
54+
}
55+
56+
private async Task AssertDatabaseSizeAsync(IDatabase db, int expected)
57+
{
58+
// in part, this is to allow replication to catch up
59+
for (int i = 0; i < 10; i++)
60+
{
61+
Assert.Equal(expected, await DatabaseSizeAsync(db));
62+
}
63+
}
64+
4765
[SkipIfRedisTheory(Is.Enterprise)]
4866
[MemberData(nameof(EndpointsFixture.Env.AllEnvironments), MemberType = typeof(EndpointsFixture.Env))]
4967
public void TestAggregationRequestVerbatim(string endpointId)
@@ -223,7 +241,7 @@ public void TestAggregationsLoad(string endpointId)
223241
ft.Create("idx", new(), sc);
224242

225243
AddDocument(db, new Document("doc1").Set("t1", "hello").Set("t2", "world"));
226-
Assert.Equal(1, DatabaseSize(db)); // in part, this is to allow replication to catch up
244+
AssertDatabaseSize(db, 1);
227245

228246
// load t1
229247
var req = new AggregationRequest("*").Load(new FieldName("t1"));
@@ -256,7 +274,7 @@ public async Task TestAggregationsLoadAsync(string endpointId)
256274
await ft.CreateAsync("idx", new(), sc);
257275

258276
AddDocument(db, new Document("doc1").Set("t1", "hello").Set("t2", "world"));
259-
Assert.Equal(1, await DatabaseSizeAsync(db)); // in part, this is to allow replication to catch up
277+
await AssertDatabaseSizeAsync(db, 1);
260278

261279
// load t1
262280
var req = new AggregationRequest("*").Load(new FieldName("t1"));
@@ -511,13 +529,17 @@ public void TestApplyAndFilterAggregations(string endpointId)
511529
AddDocument(db, new Document("data4").Set("name", "abc").Set("subj1", 30).Set("subj2", 20));
512530
AddDocument(db, new Document("data5").Set("name", "def").Set("subj1", 65).Set("subj2", 45));
513531
AddDocument(db, new Document("data6").Set("name", "ghi").Set("subj1", 70).Set("subj2", 70));
514-
Assert.Equal(6, DatabaseSize(db)); // in part, this is to allow replication to catch up
532+
AssertDatabaseSize(db, 6);
515533

516534
AggregationRequest r = new AggregationRequest().Apply("(@subj1+@subj2)/2", "attemptavg")
517535
.GroupBy("@name", Reducers.Avg("@attemptavg").As("avgscore"))
518536
.Filter("@avgscore>=50")
519537
.SortBy(10, SortedField.Asc("@name"));
520538

539+
// abc: 20+70 => 45, 30+20 => 25, filtered out
540+
// def: 60+40 => 50, 65+45 => 55, avg 52.5
541+
// ghi: 50+80 => 65, 70+70 => 70, avg 67.5
542+
521543
// actual search
522544
AggregationResult res = ft.Aggregate(index, r);
523545
Assert.Equal(2, res.TotalResults);
@@ -549,7 +571,7 @@ public void TestCreate(string endpointId)
549571
db.HashSet("pupil:4444", [new("first", "Pat"), new("last", "Shu"), new("age", "21")]);
550572
db.HashSet("student:5555", [new("first", "Joen"), new("last", "Ko"), new("age", "20")]);
551573
db.HashSet("teacher:6666", [new("first", "Pat"), new("last", "Rod"), new("age", "20")]);
552-
Assert.Equal(7, DatabaseSize(db)); // in part, this is to allow replication to catch up
574+
AssertDatabaseSize(db, 7);
553575

554576
var noFilters = ft.Search(index, new());
555577
Assert.Equal(4, noFilters.TotalResults);
@@ -580,7 +602,7 @@ public async Task TestCreateAsync(string endpointId)
580602
db.HashSet("pupil:4444", [new("first", "Pat"), new("last", "Shu"), new("age", "21")]);
581603
db.HashSet("student:5555", [new("first", "Joen"), new("last", "Ko"), new("age", "20")]);
582604
db.HashSet("teacher:6666", [new("first", "Pat"), new("last", "Rod"), new("age", "20")]);
583-
Assert.Equal(7, await DatabaseSizeAsync(db)); // in part, this is to allow replication to catch up
605+
await AssertDatabaseSizeAsync(db, 7);
584606

585607
var noFilters = ft.Search(index, new());
586608
Assert.Equal(4, noFilters.TotalResults);
@@ -606,7 +628,7 @@ public void CreateNoParams(string endpointId)
606628
db.HashSet("student:3333", [new("first", "El"), new("last", "Mark"), new("age", 17)]);
607629
db.HashSet("pupil:4444", [new("first", "Pat"), new("last", "Shu"), new("age", 21)]);
608630
db.HashSet("student:5555", [new("first", "Joen"), new("last", "Ko"), new("age", 20)]);
609-
Assert.Equal(4, DatabaseSize(db)); // in part, this is to allow replication to catch up
631+
AssertDatabaseSize(db, 4);
610632

611633
SearchResult noFilters = ft.Search(index, new());
612634
Assert.Equal(4, noFilters.TotalResults);
@@ -635,7 +657,7 @@ public async Task CreateNoParamsAsync(string endpointId)
635657
db.HashSet("student:3333", [new("first", "El"), new("last", "Mark"), new("age", 17)]);
636658
db.HashSet("pupil:4444", [new("first", "Pat"), new("last", "Shu"), new("age", 21)]);
637659
db.HashSet("student:5555", [new("first", "Joen"), new("last", "Ko"), new("age", 20)]);
638-
Assert.Equal(4, await DatabaseSizeAsync(db)); // in part, this is to allow replication to catch up
660+
await AssertDatabaseSizeAsync(db, 4);
639661

640662
SearchResult noFilters = ft.Search(index, new());
641663
Assert.Equal(4, noFilters.TotalResults);
@@ -668,7 +690,7 @@ public void CreateWithFieldNames(string endpointId)
668690
db.HashSet("pupil:4444", [new("first", "Pat"), new("last", "Shu"), new("age", "21")]);
669691
db.HashSet("student:5555", [new("first", "Joen"), new("last", "Ko"), new("age", "20")]);
670692
db.HashSet("teacher:6666", [new("first", "Pat"), new("last", "Rod"), new("age", "20")]);
671-
Assert.Equal(7, DatabaseSize(db)); // in part, this is to allow replication to catch up
693+
AssertDatabaseSize(db, 7);
672694

673695
SearchResult noFilters = ft.Search(index, new());
674696
Assert.Equal(5, noFilters.TotalResults);
@@ -750,13 +772,12 @@ public void AlterAdd(string endpointId)
750772

751773
var fields = new HashEntry("title", "hello world");
752774
//fields.("title", "hello world");
753-
Assert.Equal(0, DatabaseSize(db, out int replicas));
754-
Log($"Replicas: {replicas}");
775+
AssertDatabaseSize(db, 0);
755776
for (int i = 0; i < 100; i++)
756777
{
757778
db.HashSet($"doc{i}", fields.Name, fields.Value);
758779
}
759-
Assert.Equal(100, DatabaseSize(db));
780+
AssertDatabaseSize(db, 100);
760781
var info = ft.Info(index);
761782
Assert.Equal(index, info.IndexName);
762783
if (endpointId == EndpointsFixture.Env.Cluster)
@@ -782,7 +803,7 @@ public void AlterAdd(string endpointId)
782803
SearchResult res2 = ft.Search(index, new("@tags:{tagA}"));
783804
Assert.Equal(100, res2.TotalResults);
784805

785-
Assert.Equal(100, DatabaseSize(db));
806+
AssertDatabaseSize(db, 100);
786807

787808
info = ft.Info(index);
788809
Assert.Equal(index, info.IndexName);
@@ -867,7 +888,7 @@ public async Task AlterAddAsync(string endpointId)
867888
SearchResult res2 = ft.Search(index, new("@tags:{tagA}"));
868889
Assert.Equal(100, res2.TotalResults);
869890

870-
Assert.Equal(100, await DatabaseSizeAsync(db));
891+
await AssertDatabaseSizeAsync(db, 100);
871892

872893
info = await ft.InfoAsync(index);
873894
Assert.Equal(index, info.IndexName);
@@ -1192,7 +1213,7 @@ public async Task TestCursor(string endpointId)
11921213
AddDocument(db, new Document("data1").Set("name", "abc").Set("count", 10));
11931214
AddDocument(db, new Document("data2").Set("name", "def").Set("count", 5));
11941215
AddDocument(db, new Document("data3").Set("name", "def").Set("count", 25));
1195-
Assert.Equal(3, DatabaseSize(db)); // in part, this is to allow replication to catch up
1216+
AssertDatabaseSize(db, 3);
11961217

11971218
AggregationRequest r = new AggregationRequest()
11981219
.GroupBy("@name", Reducers.Sum("@count").As("sum"))
@@ -1247,7 +1268,7 @@ public void TestCursorEnumerable(string endpointId)
12471268
AddDocument(db, new Document("data1").Set("name", "abc").Set("count", 10));
12481269
AddDocument(db, new Document("data2").Set("name", "def").Set("count", 5));
12491270
AddDocument(db, new Document("data3").Set("name", "def").Set("count", 25));
1250-
Assert.Equal(3, DatabaseSize(db)); // in part, this is to allow replication to catch up
1271+
AssertDatabaseSize(db, 3);
12511272

12521273
AggregationRequest r = new AggregationRequest()
12531274
.GroupBy("@name", Reducers.Sum("@count").As("sum"))
@@ -1285,7 +1306,7 @@ public async Task TestCursorAsync(string endpointId)
12851306
AddDocument(db, new Document("data1").Set("name", "abc").Set("count", 10));
12861307
AddDocument(db, new Document("data2").Set("name", "def").Set("count", 5));
12871308
AddDocument(db, new Document("data3").Set("name", "def").Set("count", 25));
1288-
Assert.Equal(3, await DatabaseSizeAsync(db)); // in part, this is to allow replication to catch up
1309+
await AssertDatabaseSizeAsync(db, 3);
12891310

12901311
AggregationRequest r = new AggregationRequest()
12911312
.GroupBy("@name", Reducers.Sum("@count").As("sum"))
@@ -1340,7 +1361,7 @@ public async Task TestCursorEnumerableAsync(string endpointId)
13401361
AddDocument(db, new Document("data1").Set("name", "abc").Set("count", 10));
13411362
AddDocument(db, new Document("data2").Set("name", "def").Set("count", 5));
13421363
AddDocument(db, new Document("data3").Set("name", "def").Set("count", 25));
1343-
Assert.Equal(3, await DatabaseSizeAsync(db)); // in part, this is to allow replication to catch up
1364+
await AssertDatabaseSizeAsync(db, 3);
13441365

13451366
AggregationRequest r = new AggregationRequest()
13461367
.GroupBy("@name", Reducers.Sum("@count").As("sum"))
@@ -1498,15 +1519,15 @@ public void TestDictionary(string endpointId)
14981519
var ft = db.FT();
14991520

15001521
Assert.Equal(3L, ft.DictAdd("dict", "bar", "foo", "hello world"));
1501-
Assert.Equal(0, DatabaseSize(db)); // in part, this is to allow replication to catch up
1522+
AssertDatabaseSize(db, 0);
15021523
var dumResult = ft.DictDump("dict");
15031524
int i = 0;
15041525
Assert.Equal("bar", dumResult[i++].ToString());
15051526
Assert.Equal("foo", dumResult[i++].ToString());
15061527
Assert.Equal("hello world", dumResult[i].ToString());
15071528

15081529
Assert.Equal(3L, ft.DictDel("dict", "foo", "bar", "hello world"));
1509-
Assert.Equal(0, DatabaseSize(db)); // in part, this is to allow replication to catch up
1530+
AssertDatabaseSize(db, 0);
15101531
Assert.Empty(ft.DictDump("dict"));
15111532
}
15121533

@@ -1541,7 +1562,7 @@ public void TestDropIndex(string endpointId)
15411562
Assert.Contains("no such index", ex.Message, StringComparison.OrdinalIgnoreCase);
15421563
}
15431564

1544-
Assert.Equal(100, DatabaseSize(db));
1565+
AssertDatabaseSize(db, 100);
15451566
}
15461567

15471568
private int DatabaseSize(IDatabase db) => DatabaseSize(db, out _);
@@ -1610,7 +1631,7 @@ public async Task TestDropIndexAsync(string endpointId)
16101631
Assert.Contains("no such index", ex.Message, StringComparison.OrdinalIgnoreCase);
16111632
}
16121633

1613-
Assert.Equal(100, DatabaseSize(db));
1634+
AssertDatabaseSize(db, 100);
16141635
}
16151636

16161637
[SkippableTheory]
@@ -1636,7 +1657,7 @@ public void dropIndexDD(string endpointId)
16361657

16371658
RedisResult[] keys = (RedisResult[])db.Execute("KEYS", "*")!;
16381659
Assert.Empty(keys);
1639-
Assert.Equal(0, DatabaseSize(db));
1660+
AssertDatabaseSize(db, 0);
16401661
}
16411662

16421663
[SkippableTheory]
@@ -1662,7 +1683,7 @@ public async Task dropIndexDDAsync(string endpointId)
16621683

16631684
RedisResult[] keys = (RedisResult[])db.Execute("KEYS", "*")!;
16641685
Assert.Empty(keys);
1665-
Assert.Equal(0, DatabaseSize(db));
1686+
AssertDatabaseSize(db, 0);
16661687
}
16671688

16681689
[SkippableTheory]
@@ -1673,15 +1694,15 @@ public async Task TestDictionaryAsync(string endpointId)
16731694
var ft = db.FT();
16741695

16751696
Assert.Equal(3L, await ft.DictAddAsync("dict", "bar", "foo", "hello world"));
1676-
Assert.Equal(0, await DatabaseSizeAsync(db)); // in part, this is to allow replication to catch up
1697+
await AssertDatabaseSizeAsync(db, 0);
16771698
var dumResult = await ft.DictDumpAsync("dict");
16781699
int i = 0;
16791700
Assert.Equal("bar", dumResult[i++].ToString());
16801701
Assert.Equal("foo", dumResult[i++].ToString());
16811702
Assert.Equal("hello world", dumResult[i].ToString());
16821703

16831704
Assert.Equal(3L, await ft.DictDelAsync("dict", "foo", "bar", "hello world"));
1684-
Assert.Equal(0, await DatabaseSizeAsync(db)); // in part, this is to allow replication to catch up
1705+
await AssertDatabaseSizeAsync(db, 0);
16851706
Assert.Empty((await ft.DictDumpAsync("dict")));
16861707
}
16871708

@@ -2382,7 +2403,7 @@ public void TestLimit(string endpointId)
23822403
Document doc2 = new("doc2", new() { { "t1", "b" }, { "t2", "a" } });
23832404
AddDocument(db, doc1);
23842405
AddDocument(db, doc2);
2385-
Assert.Equal(2, DatabaseSize(db)); // in part, this is to allow replication to catch up
2406+
AssertDatabaseSize(db, 2);
23862407

23872408
var req = new AggregationRequest("*").SortBy("@t1").Limit(1);
23882409
var res = ft.Aggregate("idx", req);
@@ -2403,7 +2424,7 @@ public async Task TestLimitAsync(string endpointId)
24032424
Document doc2 = new("doc2", new() { { "t1", "b" }, { "t2", "a" } });
24042425
AddDocument(db, doc1);
24052426
AddDocument(db, doc2);
2406-
Assert.Equal(2, await DatabaseSizeAsync(db)); // in part, this is to allow replication to catch up
2427+
await AssertDatabaseSizeAsync(db, 2);
24072428

24082429
var req = new AggregationRequest("*").SortBy("@t1").Limit(1, 1);
24092430
var res = await ft.AggregateAsync("idx", req);
@@ -2488,7 +2509,7 @@ public void VectorSimilaritySearch(string endpointId)
24882509
float[] vec = [2, 2, 2, 2];
24892510
byte[] queryVec = MemoryMarshal.Cast<float, byte>(vec).ToArray();
24902511

2491-
Assert.Equal(4, DatabaseSize(db)); // in part, this is to allow replication to catch up
2512+
AssertDatabaseSize(db, 4);
24922513
var query = new Query("*=>[KNN 3 @vector $query_vec]")
24932514
.AddParam("query_vec", queryVec)
24942515
.SetSortBy("__vector_score")
@@ -2526,7 +2547,7 @@ public void QueryingVectorFields(string endpointId)
25262547
db.HashSet("b", "v", "aaaabaaa");
25272548
db.HashSet("c", "v", "aaaaabaa");
25282549

2529-
Assert.Equal(3, DatabaseSize(db)); // in part, this is to allow replication to catch up
2550+
AssertDatabaseSize(db, 3);
25302551
var q = new Query("*=>[KNN 2 @v $vec]").ReturnFields("__v_score").Dialect(2);
25312552
var res = ft.Search("idx", q.AddParam("vec", "aaaaaaaa"));
25322553
Assert.Equal(2, res.TotalResults);
@@ -2567,7 +2588,7 @@ public void TestQueryAddParam_DefaultDialect(string endpointId)
25672588
db.HashSet("2", "numval", 2);
25682589
db.HashSet("3", "numval", 3);
25692590

2570-
Assert.Equal(3, DatabaseSize(db)); // in part, this is to allow replication to catch up
2591+
AssertDatabaseSize(db, 3);
25712592
Query query = new Query("@numval:[$min $max]").AddParam("min", 1).AddParam("max", 2);
25722593
var res = ft.Search("idx", query);
25732594
Assert.Equal(2, res.TotalResults);
@@ -2587,7 +2608,7 @@ public async Task TestQueryAddParam_DefaultDialectAsync(string endpointId)
25872608
db.HashSet("2", "numval", 2);
25882609
db.HashSet("3", "numval", 3);
25892610

2590-
Assert.Equal(3, await DatabaseSizeAsync(db)); // in part, this is to allow replication to catch up
2611+
await AssertDatabaseSizeAsync(db, 3);
25912612
Query query = new Query("@numval:[$min $max]").AddParam("min", 1).AddParam("max", 2);
25922613
var res = await ft.SearchAsync("idx", query);
25932614
Assert.Equal(2, res.TotalResults);
@@ -2607,7 +2628,7 @@ public void TestQueryParamsWithParams_DefaultDialect(string endpointId)
26072628
db.HashSet("2", "numval", 2);
26082629
db.HashSet("3", "numval", 3);
26092630

2610-
Assert.Equal(3, DatabaseSize(db)); // in part, this is to allow replication to catch up
2631+
AssertDatabaseSize(db, 3);
26112632
Query query = new Query("@numval:[$min $max]").AddParam("min", 1).AddParam("max", 2);
26122633
var res = ft.Search("idx", query);
26132634
Assert.Equal(2, res.TotalResults);
@@ -2635,7 +2656,7 @@ public void TestBasicSpellCheck(string endpointId)
26352656
db.HashSet("doc1", [new("name", "name2"), new("body", "body2")]);
26362657
db.HashSet("doc1", [new("name", "name2"), new("body", "name2")]);
26372658

2638-
Assert.Equal(1, DatabaseSize(db)); // in part, this is to allow replication to catch up
2659+
AssertDatabaseSize(db, 1);
26392660
var reply = ft.SpellCheck(index, "name");
26402661
Assert.Single(reply.Keys);
26412662
Assert.Equal("name", reply.Keys.First());
@@ -2656,7 +2677,7 @@ public async Task TestBasicSpellCheckAsync(string endpointId)
26562677
db.HashSet("doc1", [new("name", "name2"), new("body", "body2")]);
26572678
db.HashSet("doc1", [new("name", "name2"), new("body", "name2")]);
26582679

2659-
Assert.Equal(1, await DatabaseSizeAsync(db)); // in part, this is to allow replication to catch up
2680+
await AssertDatabaseSizeAsync(db, 1);
26602681
var reply = await ft.SpellCheckAsync(index, "name");
26612682
Assert.Single(reply.Keys);
26622683
Assert.Equal("name", reply.Keys.First());
@@ -2681,7 +2702,8 @@ public void TestCrossTermDictionary(string endpointId)
26812702
}
26822703
};
26832704

2684-
Assert.Equal(0, DatabaseSize(db)); // in part, this is to allow replication to catch up
2705+
AssertDatabaseSize(db, 0);
2706+
26852707
Assert.Equal(expected, ft.SpellCheck(index,
26862708
"Tooni toque kerfuffle",
26872709
new FTSpellCheckParams()
@@ -2706,7 +2728,7 @@ public async Task TestCrossTermDictionaryAsync(string endpointId)
27062728
}
27072729
};
27082730

2709-
Assert.Equal(0, DatabaseSize(db)); // in part, this is to allow replication to catch up
2731+
AssertDatabaseSize(db, 0);
27102732
Assert.Equal(expected, await ft.SpellCheckAsync(index,
27112733
"Tooni toque kerfuffle",
27122734
new FTSpellCheckParams()
@@ -2772,7 +2794,7 @@ public async Task TestQueryParamsWithParams_DefaultDialectAsync(string endpointI
27722794
db.HashSet("2", "numval", 2);
27732795
db.HashSet("3", "numval", 3);
27742796

2775-
Assert.Equal(3, DatabaseSize(db)); // in part, this is to allow replication to catch up
2797+
AssertDatabaseSize(db, 3);
27762798
Query query = new Query("@numval:[$min $max]").AddParam("min", 1).AddParam("max", 2);
27772799
var res = await ft.SearchAsync("idx", query);
27782800
Assert.Equal(2, res.TotalResults);

0 commit comments

Comments
 (0)