@@ -5,10 +5,12 @@ namespace MySqlConnector.Tests.Metrics;
5
5
public class ConnectionsUsageTests : MetricsTestsBase
6
6
{
7
7
[ Theory ( Skip = MetricsSkip ) ]
8
- [ MemberData ( nameof ( GetPoolCreators ) ) ]
9
- public void PoolCreators ( IConnectionCreator connectionCreator )
8
+ [ InlineData ( "DataSource|true|" ) ]
9
+ [ InlineData ( "DataSource|true|metrics-test" ) ]
10
+ [ InlineData ( "Plain|true" ) ]
11
+ public void ConnectionsWithPoolsHaveMetrics ( string connectionCreatorSpec )
10
12
{
11
- connectionCreator . SetConnectionStringBuilder ( CreateConnectionStringBuilder ( ) ) ;
13
+ using var connectionCreator = CreateConnectionCreator ( connectionCreatorSpec , CreateConnectionStringBuilder ( ) ) ;
12
14
PoolName = connectionCreator . PoolName ;
13
15
14
16
// no connections at beginning of test
@@ -55,15 +57,44 @@ public void PoolCreators(IConnectionCreator connectionCreator)
55
57
AssertMeasurement ( "db.client.connections.usage|idle" , 2 ) ;
56
58
AssertMeasurement ( "db.client.connections.usage|used" , 0 ) ;
57
59
Assert . Equal ( 2 , Server . ActiveConnections ) ;
58
-
59
- connectionCreator . Dispose ( ) ;
60
60
}
61
61
62
- public static IEnumerable < object [ ] > GetPoolCreators ( )
62
+ [ Theory ( Skip = MetricsSkip ) ]
63
+ [ InlineData ( "DataSource|false|" ) ]
64
+ [ InlineData ( "DataSource|false|metrics-test" ) ]
65
+ [ InlineData ( "Plain|false" ) ]
66
+ public void ConnectionsWithoutPoolsHaveNoMetrics ( string connectionCreatorSpec )
63
67
{
64
- yield return new object [ ] { new DataSourceConnectionCreator ( "metrics-test" ) } ;
65
- yield return new object [ ] { new DataSourceConnectionCreator ( null ) } ;
66
- yield return new object [ ] { new PlainConnectionCreator ( ) } ;
68
+ using var connectionCreator = CreateConnectionCreator ( connectionCreatorSpec , CreateConnectionStringBuilder ( ) ) ;
69
+ PoolName = connectionCreator . PoolName ;
70
+
71
+ // no connections at beginning of test
72
+ AssertMeasurement ( "db.client.connections.usage" , 0 ) ;
73
+ AssertMeasurement ( "db.client.connections.usage|idle" , 0 ) ;
74
+ AssertMeasurement ( "db.client.connections.usage|used" , 0 ) ;
75
+ Assert . Equal ( 0 , Server . ActiveConnections ) ;
76
+
77
+ // opening a connection doesn't change connection counts
78
+ using ( var connection = connectionCreator . OpenConnection ( ) )
79
+ {
80
+ AssertMeasurement ( "db.client.connections.usage" , 0 ) ;
81
+ AssertMeasurement ( "db.client.connections.usage|idle" , 0 ) ;
82
+ AssertMeasurement ( "db.client.connections.usage|used" , 0 ) ;
83
+ Assert . Equal ( 1 , Server . ActiveConnections ) ;
84
+ }
85
+
86
+ // closing it doesn't create an idle connection but closes it immediately
87
+ AssertMeasurement ( "db.client.connections.usage" , 0 ) ;
88
+ AssertMeasurement ( "db.client.connections.usage|idle" , 0 ) ;
89
+ AssertMeasurement ( "db.client.connections.usage|used" , 0 ) ;
90
+
91
+ // disposing the connection sends a COM_QUIT packet and immediately returns; give the in-proc server a chance to process it
92
+ for ( var retry = 0 ; retry < 20 ; retry ++ )
93
+ {
94
+ if ( Server . ActiveConnections != 0 )
95
+ Thread . Sleep ( 1 ) ;
96
+ }
97
+ Assert . Equal ( 0 , Server . ActiveConnections ) ;
67
98
}
68
99
69
100
[ Fact ( Skip = MetricsSkip ) ]
@@ -135,4 +166,15 @@ public async Task PendingRequestForOpenFromPool()
135
166
136
167
AssertMeasurement ( "db.client.connections.pending_requests" , 0 ) ;
137
168
}
169
+
170
+ private IConnectionCreator CreateConnectionCreator ( string spec , MySqlConnectionStringBuilder connectionStringBuilder )
171
+ {
172
+ var parts = spec . Split ( '|' ) ;
173
+ return parts [ 0 ] switch
174
+ {
175
+ "DataSource" => new DataSourceConnectionCreator ( bool . Parse ( parts [ 1 ] ) , parts [ 2 ] == "" ? null : parts [ 2 ] , connectionStringBuilder ) ,
176
+ "Plain" => new PlainConnectionCreator ( bool . Parse ( parts [ 1 ] ) , connectionStringBuilder ) ,
177
+ _ => throw new NotSupportedException ( ) ,
178
+ } ;
179
+ }
138
180
}
0 commit comments