1- using System ;
2- using System . Collections . Generic ;
3- using System . Text ;
1+ using System . Collections . Generic ;
42using System . Threading ;
53using System . Threading . Tasks ;
64using Microsoft . Extensions . Configuration ;
7- using Microsoft . Extensions . Primitives ;
85using SqlStreamStore ;
96using Xunit ;
107
@@ -29,127 +26,52 @@ public async Task Can_build_config_source_with_instance_from_lambda()
2926 . Build ( ) ;
3027
3128
32- SqlStreamStoreInstance = new InMemoryStreamStore ( ) ;
33-
34- var repo = new ConfigRepository ( SqlStreamStoreInstance ) ;
35- await repo . WriteChanges ( new ModifiedConfigurationSettings (
36- ( "setting1" , "value1" ) ) , CancellationToken . None ) ;
29+ SqlStreamStoreInstance = await BuildSteamStoreWithSettings ( ( "setting1" , "value1" ) ) ;
3730
3831 Assert . Equal ( "value1" , config . GetValue < string > ( "setting1" ) ) ;
3932 }
4033
4134 [ Fact ]
4235 public async Task Can_build_config_source_with_connection_string ( )
4336 {
44- const string key = "Config.SqlStreamStore.ConnectionString" ;
37+ const string connectionStringKey = "Config.SqlStreamStore.ConnectionString" ;
38+ const string expectedConnectionString = "not really a conection string, but as good as it gets" ;
4539
4640 string usedConnectionString = null ;
4741
48- Func < string , IStreamStore > factory = ( s ) =>
42+ var instance = await BuildSteamStoreWithSettings ( ( "setting1" , "value1" ) ) ;
43+
44+ IStreamStore BuildStreamStore ( string s )
4945 {
5046 usedConnectionString = s ;
51- return new InMemoryStreamStore ( ) ;
52- } ;
47+ return instance ;
48+ }
49+
5350
5451 var config = new ConfigurationBuilder ( )
55- . AddInMemoryCollection ( new Dictionary < string , string > ( )
52+ . AddInMemoryCollection ( new Dictionary < string , string >
5653 {
57- { key , "sql server" }
54+ { connectionStringKey , expectedConnectionString }
5855 } )
59- . Add ( new StreamStoreConfigurationSource ( connectionStringKey : key , streamStoreFactory : factory ) )
56+ . Add ( new StreamStoreConfigurationSource ( connectionStringKey : connectionStringKey , streamStoreFactory : BuildStreamStore ) )
6057 . Build ( ) ;
6158
62- SqlStreamStoreInstance = new InMemoryStreamStore ( ) ;
63-
64- var repo = new ConfigRepository ( SqlStreamStoreInstance ) ;
65- await repo . WriteChanges ( new ModifiedConfigurationSettings (
66- ( "setting1" , "value1" ) ) , CancellationToken . None ) ;
59+ // Ensure the factory actually used the configured connection string key
60+ Assert . Equal ( expectedConnectionString , usedConnectionString ) ;
6761
62+ // Ensure setting 1was read from stream store
6863 Assert . Equal ( "value1" , config . GetValue < string > ( "setting1" ) ) ;
69-
70- }
71- }
72-
73- public class StreamStoreConfigurationSource : IConfigurationSource
74- {
75- private readonly string _connectionStringKey ;
76- private readonly Func < IConfigRepository > _getConfigRepository ;
77- private Func < string , IStreamStore > _streamStoreFactory ;
78-
79- public StreamStoreConfigurationSource ( string connectionStringKey , Func < string , IStreamStore > streamStoreFactory )
80- {
81- _connectionStringKey = connectionStringKey ;
82- _streamStoreFactory = streamStoreFactory ;
83- }
84-
85- public StreamStoreConfigurationSource ( Func < IStreamStore > getStreamStore ) :
86- this ( ( ) => new ConfigRepository ( getStreamStore ( ) ) )
87- {
88-
89- }
90-
91- public StreamStoreConfigurationSource ( Func < IConfigRepository > getConfigRepository )
92- {
93- _getConfigRepository = getConfigRepository ;
94- }
95-
96- public IConfigurationProvider Build ( IConfigurationBuilder builder )
97- {
98- var getConfigRepository = _getConfigRepository ;
99- if ( getConfigRepository == null )
100- {
101- getConfigRepository = ( ) =>
102- {
103- var connectionString = GetConnectionString ( builder ) ;
104- return new ConfigRepository ( _streamStoreFactory ( connectionString ) ) ;
105- } ;
106- }
107-
108- return new StreamStoreConfigurationProvider ( _getConfigRepository ) ;
10964 }
11065
111- }
112-
113- public class StreamStoreConfigurationProvider : IConfigurationProvider
114- {
115- private readonly Func < IConfigRepository > _getConfigRepository ;
116-
117- private IConfigurationSettings _settings ;
118-
119- public StreamStoreConfigurationProvider ( Func < IConfigRepository > getConfigRepository )
66+ private static async Task < InMemoryStreamStore > BuildSteamStoreWithSettings ( params ( string key , string value ) [ ] settings )
12067 {
121- _getConfigRepository = getConfigRepository ;
122- }
123-
124- public bool TryGet ( string key , out string value )
125- {
126- if ( _settings == null )
127- {
128- _settings = _getConfigRepository ( ) . GetLatest ( CancellationToken . None ) . GetAwaiter ( ) . GetResult ( ) ;
129- }
68+ var instance = new InMemoryStreamStore ( ) ;
13069
131- return _settings . TryGetValue ( key , out value ) ;
132- }
133-
134- public void Set ( string key , string value )
135- {
136- // Not going to write to SSS this way..
137- }
138-
139- public IChangeToken GetReloadToken ( )
140- {
141- return new ConfigurationReloadToken ( ) ;
142- }
143-
144- public void Load ( )
145- {
146-
147- //_settings = _getConfigRepository()?.GetLatest(CancellationToken.None).GetAwaiter().GetResult();
148- }
70+ var repo = new ConfigRepository ( instance ) ;
71+ await repo . WriteChanges ( new ModifiedConfigurationSettings (
72+ settings ) , CancellationToken . None ) ;
14973
150- public IEnumerable < string > GetChildKeys ( IEnumerable < string > earlierKeys , string parentPath )
151- {
152- throw new NotImplementedException ( ) ;
74+ return instance ;
15375 }
15476 }
15577}
0 commit comments