@@ -4,13 +4,12 @@ namespace MySqlConnector.Tests.Metrics;
4
4
5
5
public class ConnectionsUsageTests : MetricsTestsBase
6
6
{
7
- [ Fact ( Skip = MetricsSkip ) ]
8
- public void NamedDataSource ( )
7
+ [ Theory ( Skip = MetricsSkip ) ]
8
+ [ MemberData ( nameof ( GetPoolCreators ) ) ]
9
+ public void PoolCreators ( IConnectionCreator connectionCreator )
9
10
{
10
- PoolName = "metrics-test" ;
11
- using var dataSource = new MySqlDataSourceBuilder ( CreateConnectionStringBuilder ( ) . ConnectionString )
12
- . UseName ( PoolName )
13
- . Build ( ) ;
11
+ connectionCreator . SetConnectionStringBuilder ( CreateConnectionStringBuilder ( ) ) ;
12
+ PoolName = connectionCreator . PoolName ;
14
13
15
14
// no connections at beginning of test
16
15
AssertMeasurement ( "db.client.connections.usage" , 0 ) ;
@@ -19,7 +18,7 @@ public void NamedDataSource()
19
18
Assert . Equal ( 0 , Server . ActiveConnections ) ;
20
19
21
20
// opening a connection creates a 'used' connection
22
- using ( var connection = dataSource . OpenConnection ( ) )
21
+ using ( var connection = connectionCreator . OpenConnection ( ) )
23
22
{
24
23
AssertMeasurement ( "db.client.connections.usage" , 1 ) ;
25
24
AssertMeasurement ( "db.client.connections.usage|idle" , 0 ) ;
@@ -34,7 +33,7 @@ public void NamedDataSource()
34
33
Assert . Equal ( 1 , Server . ActiveConnections ) ;
35
34
36
35
// reopening the connection transitions it back to 'used'
37
- using ( var connection = dataSource . OpenConnection ( ) )
36
+ using ( var connection = connectionCreator . OpenConnection ( ) )
38
37
{
39
38
AssertMeasurement ( "db.client.connections.usage" , 1 ) ;
40
39
AssertMeasurement ( "db.client.connections.usage|idle" , 0 ) ;
@@ -43,8 +42,8 @@ public void NamedDataSource()
43
42
Assert . Equal ( 1 , Server . ActiveConnections ) ;
44
43
45
44
// opening a second connection creates a net new 'used' connection
46
- using ( var connection = dataSource . OpenConnection ( ) )
47
- using ( var connection2 = dataSource . OpenConnection ( ) )
45
+ using ( var connection = connectionCreator . OpenConnection ( ) )
46
+ using ( var connection2 = connectionCreator . OpenConnection ( ) )
48
47
{
49
48
AssertMeasurement ( "db.client.connections.usage" , 2 ) ;
50
49
AssertMeasurement ( "db.client.connections.usage|idle" , 0 ) ;
@@ -56,6 +55,15 @@ public void NamedDataSource()
56
55
AssertMeasurement ( "db.client.connections.usage|idle" , 2 ) ;
57
56
AssertMeasurement ( "db.client.connections.usage|used" , 0 ) ;
58
57
Assert . Equal ( 2 , Server . ActiveConnections ) ;
58
+
59
+ connectionCreator . Dispose ( ) ;
60
+ }
61
+
62
+ public static IEnumerable < object [ ] > GetPoolCreators ( )
63
+ {
64
+ yield return new object [ ] { new DataSourceConnectionCreator ( "metrics-test" ) } ;
65
+ yield return new object [ ] { new DataSourceConnectionCreator ( null ) } ;
66
+ yield return new object [ ] { new PlainConnectionCreator ( ) } ;
59
67
}
60
68
61
69
[ Fact ( Skip = MetricsSkip ) ]
@@ -91,121 +99,6 @@ public void NamedDataSourceWithMinPoolSize()
91
99
Assert . Equal ( 3 , Server . ActiveConnections ) ;
92
100
}
93
101
94
- [ Fact ( Skip = MetricsSkip ) ]
95
- public void UnnamedDataSource ( )
96
- {
97
- var csb = CreateConnectionStringBuilder ( ) ;
98
-
99
- // NOTE: pool "name" is connection string (without password)
100
- PoolName = csb . GetConnectionString ( includePassword : false ) ;
101
-
102
- using var dataSource = new MySqlDataSourceBuilder ( csb . ConnectionString )
103
- . Build ( ) ;
104
-
105
- // no connections at beginning of test
106
- AssertMeasurement ( "db.client.connections.usage" , 0 ) ;
107
- AssertMeasurement ( "db.client.connections.usage|idle" , 0 ) ;
108
- AssertMeasurement ( "db.client.connections.usage|used" , 0 ) ;
109
- Assert . Equal ( 0 , Server . ActiveConnections ) ;
110
-
111
- // opening a connection creates a 'used' connection
112
- using ( var connection = dataSource . OpenConnection ( ) )
113
- {
114
- AssertMeasurement ( "db.client.connections.usage" , 1 ) ;
115
- AssertMeasurement ( "db.client.connections.usage|idle" , 0 ) ;
116
- AssertMeasurement ( "db.client.connections.usage|used" , 1 ) ;
117
- Assert . Equal ( 1 , Server . ActiveConnections ) ;
118
- }
119
-
120
- // closing it creates an 'idle' connection
121
- AssertMeasurement ( "db.client.connections.usage" , 1 ) ;
122
- AssertMeasurement ( "db.client.connections.usage|idle" , 1 ) ;
123
- AssertMeasurement ( "db.client.connections.usage|used" , 0 ) ;
124
- Assert . Equal ( 1 , Server . ActiveConnections ) ;
125
-
126
- // reopening the connection transitions it back to 'used'
127
- using ( var connection = dataSource . OpenConnection ( ) )
128
- {
129
- AssertMeasurement ( "db.client.connections.usage" , 1 ) ;
130
- AssertMeasurement ( "db.client.connections.usage|idle" , 0 ) ;
131
- AssertMeasurement ( "db.client.connections.usage|used" , 1 ) ;
132
- }
133
- Assert . Equal ( 1 , Server . ActiveConnections ) ;
134
-
135
- // opening a second connection creates a net new 'used' connection
136
- using ( var connection = dataSource . OpenConnection ( ) )
137
- using ( var connection2 = dataSource . OpenConnection ( ) )
138
- {
139
- AssertMeasurement ( "db.client.connections.usage" , 2 ) ;
140
- AssertMeasurement ( "db.client.connections.usage|idle" , 0 ) ;
141
- AssertMeasurement ( "db.client.connections.usage|used" , 2 ) ;
142
- Assert . Equal ( 2 , Server . ActiveConnections ) ;
143
- }
144
-
145
- AssertMeasurement ( "db.client.connections.usage" , 2 ) ;
146
- AssertMeasurement ( "db.client.connections.usage|idle" , 2 ) ;
147
- AssertMeasurement ( "db.client.connections.usage|used" , 0 ) ;
148
- Assert . Equal ( 2 , Server . ActiveConnections ) ;
149
- }
150
-
151
- [ Fact ( Skip = MetricsSkip ) ]
152
- public void NoDataSource ( )
153
- {
154
- var csb = CreateConnectionStringBuilder ( ) ;
155
-
156
- // NOTE: pool "name" is connection string (without password)
157
- PoolName = csb . GetConnectionString ( includePassword : false ) ;
158
-
159
- // no connections at beginning of test
160
- AssertMeasurement ( "db.client.connections.usage" , 0 ) ;
161
- AssertMeasurement ( "db.client.connections.usage|idle" , 0 ) ;
162
- AssertMeasurement ( "db.client.connections.usage|used" , 0 ) ;
163
- Assert . Equal ( 0 , Server . ActiveConnections ) ;
164
-
165
- // opening a connection creates a 'used' connection
166
- using ( var connection = new MySqlConnection ( csb . ConnectionString ) )
167
- {
168
- connection . Open ( ) ;
169
- AssertMeasurement ( "db.client.connections.usage" , 1 ) ;
170
- AssertMeasurement ( "db.client.connections.usage|idle" , 0 ) ;
171
- AssertMeasurement ( "db.client.connections.usage|used" , 1 ) ;
172
- Assert . Equal ( 1 , Server . ActiveConnections ) ;
173
- }
174
-
175
- // closing it creates an 'idle' connection
176
- AssertMeasurement ( "db.client.connections.usage" , 1 ) ;
177
- AssertMeasurement ( "db.client.connections.usage|idle" , 1 ) ;
178
- AssertMeasurement ( "db.client.connections.usage|used" , 0 ) ;
179
- Assert . Equal ( 1 , Server . ActiveConnections ) ;
180
-
181
- // reopening the connection transitions it back to 'used'
182
- using ( var connection = new MySqlConnection ( csb . ConnectionString ) )
183
- {
184
- connection . Open ( ) ;
185
- AssertMeasurement ( "db.client.connections.usage" , 1 ) ;
186
- AssertMeasurement ( "db.client.connections.usage|idle" , 0 ) ;
187
- AssertMeasurement ( "db.client.connections.usage|used" , 1 ) ;
188
- }
189
- Assert . Equal ( 1 , Server . ActiveConnections ) ;
190
-
191
- // opening a second connection creates a net new 'used' connection
192
- using ( var connection = new MySqlConnection ( csb . ConnectionString ) )
193
- using ( var connection2 = new MySqlConnection ( csb . ConnectionString ) )
194
- {
195
- connection . Open ( ) ;
196
- connection2 . Open ( ) ;
197
- AssertMeasurement ( "db.client.connections.usage" , 2 ) ;
198
- AssertMeasurement ( "db.client.connections.usage|idle" , 0 ) ;
199
- AssertMeasurement ( "db.client.connections.usage|used" , 2 ) ;
200
- Assert . Equal ( 2 , Server . ActiveConnections ) ;
201
- }
202
-
203
- AssertMeasurement ( "db.client.connections.usage" , 2 ) ;
204
- AssertMeasurement ( "db.client.connections.usage|idle" , 2 ) ;
205
- AssertMeasurement ( "db.client.connections.usage|used" , 0 ) ;
206
- Assert . Equal ( 2 , Server . ActiveConnections ) ;
207
- }
208
-
209
102
[ Fact ( Skip = MetricsSkip ) ]
210
103
public async Task PendingRequestForCreation ( )
211
104
{
0 commit comments