Repository navigation
Expand file tree
/
Copy pathAbstractNRedisStackTest.cs
More file actions
152 lines (125 loc) · 5.28 KB
/
Copy pathAbstractNRedisStackTest.cs
File metadata and controls
152 lines (125 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using NRedisStack.DataTypes;
using System.Runtime.CompilerServices;
using StackExchange.Redis;
using Xunit;
namespace NRedisStack.Tests;
public abstract class AbstractNRedisStackTest : IClassFixture<EndpointsFixture>, IAsyncLifetime
{
private protected EndpointsFixture EndpointsFixture { get; }
private readonly ITestOutputHelper? log;
protected void Log(string message, bool demand = true)
{
if (log is null)
{
if (demand) throw new InvalidOperationException("Log is not initialized");
}
else
{
log.WriteLine(message);
}
}
protected readonly ConfigurationOptions DefaultConnectionConfig = new()
{
AsyncTimeout = 10000,
SyncTimeout = 10000,
AllowAdmin = true,
};
protected internal AbstractNRedisStackTest(EndpointsFixture endpointsFixture, ITestOutputHelper? log = null)
{
this.EndpointsFixture = endpointsFixture;
this.log = log;
}
protected void AssertVersion(IDatabase db, [CallerMemberName] string testName = "", string? overrideVersion = null)
{
Version? version = null;
if (overrideVersion is not null)
{
// ignore the attribs; apply "at least the override"
version ??= db.Multiplexer.GetServer((RedisKey)"any key").Version;
Log($"Validating with detected server version: {version}", demand: false);
var skip = new SkipIfRedisCore(Comparison.LessThan, overrideVersion).GetSkip(version);
Assert.SkipWhen(skip is not null, skip ?? "");
return;
}
// this is used to reapply "Skip" logic after auto-discovery of the server version is possible
var attributes = GetType().GetMethod(testName)?.GetCustomAttributes(true) ?? [];
foreach (var attribute in attributes)
{
SkipIfRedisCore? core = attribute switch
{
SkipIfRedisFactAttribute fact => fact.Core,
SkipIfRedisTheoryAttribute theory => theory.Core,
_ => null,
};
if (core is { } defined)
{
// get the actual redis version and use that to recheck
version ??= db.Multiplexer.GetServer((RedisKey)"any key").Version;
Log($"Validating with detected server version: {version}", demand: false);
var skip = defined.GetSkip(version);
Assert.SkipWhen(skip is not null, skip ?? "");
}
}
}
protected ConnectionMultiplexer GetConnection(string endpointId = EndpointsFixture.Env.Standalone, bool shareConnection = true) => EndpointsFixture.GetConnectionById(this.DefaultConnectionConfig, endpointId, shareConnection);
protected ConnectionMultiplexer GetConnection(ConfigurationOptions configurationOptions, string endpointId = EndpointsFixture.Env.Standalone) => EndpointsFixture.GetConnectionById(configurationOptions, endpointId, false);
protected IDatabase GetDatabase(string endpointId = EndpointsFixture.Env.Standalone, bool shareConnection = true)
{
var redis = GetConnection(endpointId, shareConnection);
IDatabase db = redis.GetDatabase();
return db;
}
protected IDatabase GetCleanDatabase(string endpointId = EndpointsFixture.Env.Standalone, bool shareConnection = true)
{
var redis = GetConnection(endpointId, shareConnection);
if (endpointId == EndpointsFixture.Env.Cluster)
{
foreach (var endPoint in redis.GetEndPoints())
{
var server = redis.GetServer(endPoint);
if (server.IsReplica || !server.IsConnected) continue;
server.Execute("FLUSHALL");
}
}
IDatabase db = redis.GetDatabase();
db.Execute("FLUSHALL");
return db;
}
protected void SkipIfTargetConnectionDoesNotExist(string id)
{
Assert.SkipUnless(EndpointsFixture.IsTargetConnectionExist(id), $"The connection with id '{id}' is not configured.");
}
private List<string> keyNames = [];
protected internal string CreateKeyName([CallerMemberName] string memberName = "") => CreateKeyNames(1, memberName)[0];
protected internal string[] CreateKeyNames(int count, [CallerMemberName] string memberName = "")
{
if (count < 1) throw new ArgumentOutOfRangeException(nameof(count), "Must be greater than zero.");
var newKeys = new string[count];
for (var i = 0; i < count; i++)
{
newKeys[i] = $"{GetType().Name}:{memberName}:{i}";
}
keyNames.AddRange(newKeys);
return newKeys;
}
protected internal static List<TimeSeriesTuple> ReverseData(List<TimeSeriesTuple> data)
{
var tuples = new List<TimeSeriesTuple>(data.Count);
for (var i = data.Count - 1; i >= 0; i--)
{
tuples.Add(data[i]);
}
return tuples;
}
public ValueTask InitializeAsync() => default;
public void Dispose()
{
//Redis.GetDatabase().ExecuteBroadcast("FLUSHALL");
}
public ValueTask DisposeAsync() => default;
/*{
var redis = Redis.GetDatabase();
await redis.KeyDeleteAsync(keyNames.Select(i => (RedisKey)i).ToArray());
await redis.ExecuteBroadcastAsync("FLUSHALL");
}*/
}