FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
NRedisStack/tests/NRedisStack.Tests/EndpointsFixture.cs at master · redis/NRedisStack · GitHub
redis
NRedisStack
Repository navigation
Code
Issues
Pull requests
4
(4)
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
NRedisStack
/
tests
/
NRedisStack.Tests
/
EndpointsFixture.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
174 lines (141 loc) · 5.94 KB
Breadcrumbs
NRedisStack
/
tests
/
NRedisStack.Tests
/
EndpointsFixture.cs
Copy path
File metadata and controls
174 lines (141 loc) · 5.94 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using
System
.
Collections
.
Concurrent
;
using
System
.
Diagnostics
;
using
StackExchange
.
Redis
;
using
System
.
Text
.
Json
;
using
Xunit
;
namespace
NRedisStack
.
Tests
;
public
class
EndpointConfig
{
public
List
<
string
>
?
endpoints
{
get
;
set
;
}
public
bool
tls
{
get
;
set
;
}
public
string
?
password
{
get
;
set
;
}
public
int
?
bdb_id
{
get
;
set
;
}
public
object
?
raw_endpoints
{
get
;
set
;
}
public
ConnectionMultiplexer
CreateConnection
(
ConfigurationOptions
configurationOptions
)
{
configurationOptions
.
EndPoints
.
Clear
(
)
;
foreach
(
var
endpoint
in
endpoints
!
)
{
configurationOptions
.
EndPoints
.
Add
(
endpoint
)
;
}
if
(
password
!=
null
)
{
configurationOptions
.
Password
=
password
;
}
// TODO(imalinovskiy): Add support for TLS
// TODO(imalinovskiy): Add support for Discovery/Sentinel API
return
ConnectionMultiplexer
.
Connect
(
configurationOptions
)
;
}
}
public
class
EndpointsFixture
:
IDisposable
{
public
static
class
Env
{
public
const
string
Standalone
=
"standalone"
;
public
const
string
StandaloneEntraId
=
"standalone-entraid"
;
public
const
string
Cluster
=
"cluster"
;
public
static
IEnumerable
<
object
[
]
>
AllEnvironments
(
)
{
yield
return
[
Standalone
]
;
yield
return
[
Cluster
]
;
// TODO(imalinovskiy): Add support for Sentinel
}
public
static
IEnumerable
<
object
[
]
>
StandaloneOnly
(
)
{
yield
return
[
Standalone
]
;
}
}
private
readonly
string
?
redisEndpointsPath
=
Environment
.
GetEnvironmentVariable
(
"REDIS_ENDPOINTS_CONFIG_PATH"
)
??
"endpoints.json"
;
private
readonly
Dictionary
<
string
,
EndpointConfig
>
redisEndpoints
=
new
(
)
;
public
static
readonly
bool
IsEnterprise
=
Environment
.
GetEnvironmentVariable
(
"IS_ENTERPRISE"
)
==
"true"
;
public
static
Version
RedisVersion
=
new
(
Environment
.
GetEnvironmentVariable
(
"REDIS_VERSION"
)
??
"0.0.0"
)
;
public
static
bool
IsAtLeast
(
Version
version
)
=>
IsAtLeast
(
version
.
Major
,
version
.
Minor
,
version
.
Build
,
version
.
Revision
)
;
public
static
bool
IsAtLeast
(
int
major
,
int
minor
=
0
,
int
build
=
0
,
int
revision
=
0
)
{
var
version
=
RedisVersion
;
// note: watch out for negative numbers (means "undefined") from parsing n-part strings
var
test
=
Math
.
Max
(
version
.
Major
,
0
)
;
if
(
test
>
major
)
return
true
;
if
(
test
<
major
)
return
false
;
// if here, we're a match on major; test minor
test
=
Math
.
Max
(
version
.
Minor
,
0
)
;
// interpret "9" as "9.0"
if
(
test
>
minor
)
return
true
;
if
(
test
<
minor
)
return
false
;
// if here, we're a match on minor; test build
test
=
Math
.
Max
(
version
.
Build
,
0
)
;
// interpret "9.0" as "9.0.0"
if
(
test
>
build
)
return
true
;
if
(
test
<
build
)
return
false
;
// if here, we're a match on build; test revision
test
=
Math
.
Max
(
version
.
Revision
,
0
)
;
// interpret "9.0.0" as "9.0.0.0"
return
test
>=
revision
;
}
public
EndpointsFixture
(
)
{
if
(
redisEndpointsPath
!=
null
&&
File
.
Exists
(
redisEndpointsPath
)
)
{
string
json
=
File
.
ReadAllText
(
redisEndpointsPath
)
;
var
parsedEndpoints
=
JsonSerializer
.
Deserialize
<
Dictionary
<
string
,
EndpointConfig
>
>
(
json
)
;
redisEndpoints
=
parsedEndpoints
??
throw
new
(
"Failed to parse the Redis endpoints configuration."
)
;
}
else
{
throw
new
FileNotFoundException
(
"The Redis endpoints configuration file is not found."
)
;
}
}
public
void
Dispose
(
)
{
foreach
(
var
connection
in
shared
.
Values
)
{
connection
.
Dispose
(
)
;
}
shared
.
Clear
(
)
;
}
public
ConnectionMultiplexer
GetConnectionById
(
ConfigurationOptions
configurationOptions
,
string
id
,
bool
shareConnection
)
{
Assert
.
SkipUnless
(
redisEndpoints
.
ContainsKey
(
id
)
,
$
"The connection with id '
{
id
}
' is not configured."
)
;
var
protocol
=
TestContext
.
Current
.
GetRunProtocol
(
)
;
if
(
!
(
shareConnection
&&
shared
.
TryGetValue
(
(
id
,
protocol
)
,
out
var
connection
)
)
)
{
var
options
=
configurationOptions
.
Clone
(
)
;
// isolate before we start applying the protocol
options
.
Protocol
=
protocol
.
IsResp3
(
)
?
RedisProtocol
.
Resp3
:
RedisProtocol
.
Resp2
;
options
.
AllowAdmin
=
true
;
// from 3.0, Execute[Async] does a better job of recognizing admin commands
options
.
HighIntegrity
=
protocol
.
IsHighIntegrity
(
)
;
options
.
ConnectTimeout
=
2000
;
if
(
shareConnection
)
options
.
AbortOnConnectFail
=
false
;
connection
=
redisEndpoints
[
id
]
.
CreateConnection
(
options
)
;
if
(
shareConnection
)
{
if
(
shared
.
Count
is
0
)
{
try
{
// infer version from the first shared connection
var
server
=
connection
.
GetServer
(
(
RedisKey
)
"any key"
)
;
server
.
Ping
(
)
;
RedisVersion
=
server
.
Version
;
}
catch
(
Exception
ex
)
{
Debug
.
WriteLine
(
ex
.
Message
)
;
}
}
var
key
=
(
id
,
protocol
)
;
if
(
!
shared
.
TryAdd
(
key
,
connection
)
&&
shared
.
TryGetValue
(
key
,
out
var
existing
)
)
{
// prefer the existing one
connection
.
Dispose
(
)
;
connection
=
existing
;
}
}
}
Assert
.
SkipUnless
(
connection
.
IsConnected
,
$
"The connection with id '
{
id
}
' is not connected."
)
;
return
connection
;
}
// allow tests to share connections
ConcurrentDictionary
<
(
string
,
RunProtocol
)
,
ConnectionMultiplexer
>
shared
=
new
(
)
;
public
bool
IsTargetConnectionExist
(
string
id
)
{
return
redisEndpoints
.
ContainsKey
(
id
)
;
}
}
Back
|
FazBrowse Home
|
New Git URL