3 // NUnit Test Cases for Mono.Data.Tds.Protocol.TdsConnectionPool
6 // Robert Wilkens <robwilkens@gmail.com>
8 // Copyright (C) 2012 Robert Wilkens (http://www.robssoftwareprojects.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 namespace MonoTests.Mono.Data.Tds
33 using NUnit.Framework;
34 using global::Mono.Data.Tds.Protocol;
37 using System.Net.Sockets;
41 public class TdsConnectionPoolTest
43 const string SERVER="localhost";
45 public void CheckNullException()
48 TcpListener Listener = null;
50 //set up dummy sql listener, if there is a real sql server on this
51 //machine at that port, in theory this part will fail, but that's ok
52 //becuase something will be listening on the port and that's all we
53 //require at this point: a listener on port 1433...
56 IPAddress hostIP =Dns.GetHostEntry("localhost").AddressList[0];
57 IPEndPoint ep = new IPEndPoint(hostIP, 1433);
58 Listener = new TcpListener (ep);
64 //try to connect twice, in earlier failure would get null exception
65 //on 2nd call to pool.GetConnection();
66 //Most of this code ripped from sqlConnection.Open()
68 TdsConnectionPool pool;
70 TdsConnectionPoolManager sqlConnectionPools =
71 new TdsConnectionPoolManager(TdsVersion.tds80);
72 TdsConnectionInfo info=
73 new TdsConnectionInfo(SERVER/*dummyhost*/,1433/*port*/,
74 8192/*pktsize*/,15/*timeout*/,0/*minpoolsize*/,
75 100/*maxpoolsize*/, 0/*lifetime*/);
76 pool=sqlConnectionPools.GetConnectionPool("test",info);
79 //this first one succeeded regardless as long as something answered
80 //the phone on port 1433 of localhost
81 tds=pool.GetConnection();
83 pool.ReleaseConnection(tds);
86 // 2nd time thru: This will fail with nullreferenceexception
87 // at pool.GetConnection() unless the patch by Rob Wilkens which
88 // adds "result=null;" before retry in pool.getConnection() source
90 //First let's pretend we're calling this test fresh, as if we
91 //call sqlConnection.Open() again :
93 info=new TdsConnectionInfo(SERVER/*dummyhost*/,1433/*port*/,
94 8192/*pktsize*/,15/*timeout*/,0/*minpoolsize*/,
95 100/*maxpoolsize*/, 0/*lifetime*/);
97 pool=sqlConnectionPools.GetConnectionPool("test",info);
99 //Then: Test for failure (will raise uncaught exception which
100 //causes failure of test if bug is not fixed
101 tds=pool.GetConnection();
103 pool.ReleaseConnection(tds);