1
1
using StackExchange . Redis ;
2
+ using System . Text . Json ;
2
3
3
4
namespace NRedisStack . Tests ;
4
5
6
+ public class EndpointConfig
7
+ {
8
+ public List < string > ? endpoints { get ; set ; }
9
+
10
+ public bool tls { get ; set ; }
11
+
12
+ public string ? password { get ; set ; }
13
+
14
+ public int ? bdb_id { get ; set ; }
15
+
16
+ public object ? raw_endpoints { get ; set ; }
17
+
18
+ public ConnectionMultiplexer CreateConnection ( ConfigurationOptions configurationOptions )
19
+ {
20
+ configurationOptions . EndPoints . Clear ( ) ;
21
+
22
+ foreach ( var endpoint in endpoints ! )
23
+ {
24
+ configurationOptions . EndPoints . Add ( endpoint ) ;
25
+ }
26
+
27
+ if ( password != null )
28
+ {
29
+ configurationOptions . Password = password ;
30
+ }
31
+
32
+ // TODO(imalinovskiy): Add support for TLS
33
+ // TODO(imalinovskiy): Add support for Discovery/Sentinel API
34
+
35
+ return ConnectionMultiplexer . Connect ( configurationOptions ) ;
36
+ }
37
+ }
38
+
39
+
5
40
public class RedisFixture : IDisposable
6
41
{
7
42
// Set the environment variable to specify your own alternate host and port:
8
43
private readonly string redisStandalone = Environment . GetEnvironmentVariable ( "REDIS" ) ?? "localhost:6379" ;
9
44
private readonly string ? redisCluster = Environment . GetEnvironmentVariable ( "REDIS_CLUSTER" ) ;
10
45
private readonly string ? numRedisClusterNodesEnv = Environment . GetEnvironmentVariable ( "NUM_REDIS_CLUSTER_NODES" ) ;
46
+
47
+ private readonly string defaultEndpointId = Environment . GetEnvironmentVariable ( "REDIS_DEFAULT_ENDPOINT_ID" ) ?? "standalone" ;
48
+ private readonly string ? redisEndpointsPath = Environment . GetEnvironmentVariable ( "REDIS_ENDPOINTS_CONFIG_PATH" ) ;
49
+ private Dictionary < string , EndpointConfig > redisEndpoints = new ( ) ;
50
+
51
+
11
52
public bool isEnterprise = Environment . GetEnvironmentVariable ( "IS_ENTERPRISE" ) == "true" ;
12
53
public bool isOSSCluster ;
13
54
@@ -18,7 +59,42 @@ public RedisFixture()
18
59
AsyncTimeout = 10000 ,
19
60
SyncTimeout = 10000
20
61
} ;
21
- Redis = Connect ( clusterConfig , out isOSSCluster ) ;
62
+
63
+ if ( redisEndpointsPath != null && File . Exists ( redisEndpointsPath ) )
64
+ {
65
+ string json = File . ReadAllText ( redisEndpointsPath ) ;
66
+ var parsedEndpoints = JsonSerializer . Deserialize < Dictionary < string , EndpointConfig > > ( json ) ;
67
+
68
+ redisEndpoints = parsedEndpoints ?? throw new Exception ( "Failed to parse the Redis endpoints configuration." ) ;
69
+ }
70
+ else
71
+ {
72
+ redisEndpoints . Add ( "standalone" ,
73
+ new EndpointConfig { endpoints = new List < string > { redisStandalone } } ) ;
74
+
75
+ if ( redisCluster != null )
76
+ {
77
+ string [ ] parts = redisCluster ! . Split ( ':' ) ;
78
+ string host = parts [ 0 ] ;
79
+ int startPort = int . Parse ( parts [ 1 ] ) ;
80
+
81
+ var endpoints = new List < string > ( ) ;
82
+ int numRedisClusterNodes = int . Parse ( numRedisClusterNodesEnv ! ) ;
83
+ for ( int i = 0 ; i < numRedisClusterNodes ; i ++ )
84
+ {
85
+ endpoints . Add ( $ "{ host } :{ startPort + i } ") ;
86
+ }
87
+
88
+ redisEndpoints . Add ( "cluster" ,
89
+ new EndpointConfig { endpoints = endpoints } ) ;
90
+
91
+ // Set the default endpoint to the cluster to keep the tests consistent
92
+ defaultEndpointId = "cluster" ;
93
+ isOSSCluster = true ;
94
+ }
95
+ }
96
+
97
+ Redis = GetConnectionById ( clusterConfig , defaultEndpointId ) ;
22
98
}
23
99
24
100
public void Dispose ( )
@@ -28,39 +104,18 @@ public void Dispose()
28
104
29
105
public ConnectionMultiplexer Redis { get ; }
30
106
31
- public ConnectionMultiplexer CustomRedis ( ConfigurationOptions configurationOptions , out bool isOssCluster )
107
+ public ConnectionMultiplexer GetConnectionById ( ConfigurationOptions configurationOptions , string id )
32
108
{
33
- return Connect ( configurationOptions , out isOssCluster ) ;
34
- }
35
-
36
- private ConnectionMultiplexer Connect ( ConfigurationOptions configurationOptions , out bool isOssCluster )
37
- {
38
- // Redis Cluster
39
- if ( redisCluster != null && numRedisClusterNodesEnv != null )
109
+ if ( ! redisEndpoints . ContainsKey ( id ) )
40
110
{
41
- // Split to host and port
42
- string [ ] parts = redisCluster ! . Split ( ':' ) ;
43
- string host = parts [ 0 ] ;
44
- int startPort = int . Parse ( parts [ 1 ] ) ;
45
-
46
- var endpoints = new EndPointCollection ( ) ; // TODO: check if needed
47
-
48
- configurationOptions . EndPoints . Clear ( ) ;
49
- int numRedisClusterNodes = int . Parse ( numRedisClusterNodesEnv ! ) ;
50
- for ( int i = 0 ; i < numRedisClusterNodes ; i ++ )
51
- {
52
- configurationOptions . EndPoints . Add ( host , startPort + i ) ;
53
- }
54
-
55
- isOssCluster = true ;
56
- return ConnectionMultiplexer . Connect ( configurationOptions ) ;
111
+ throw new Exception ( $ "The connection with id '{ id } ' is not configured.") ;
57
112
}
58
113
59
- // Redis Standalone
60
- configurationOptions . EndPoints . Clear ( ) ;
61
- configurationOptions . EndPoints . Add ( $ "{ redisStandalone } ") ;
114
+ return redisEndpoints [ id ] . CreateConnection ( configurationOptions ) ;
115
+ }
62
116
63
- isOssCluster = false ;
64
- return ConnectionMultiplexer . Connect ( configurationOptions ) ;
117
+ public bool IsTargetConnectionExist ( string id )
118
+ {
119
+ return redisEndpoints . ContainsKey ( id ) ;
65
120
}
66
121
}
0 commit comments