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.
33 using NUnit.Framework;
34 using Mono.Data.Tds.Protocol;
37 using System.Net.Sockets;
41 public class TdsConnectionPoolTest
43 const string SERVER="localhost";
45 public void CheckNullException()
48 //set up dummy sql listener, if there is a real sql server on this
49 //machine at that port, in theory this part will fail, but that's ok
50 //becuase something will be listening on the port and that's all we
51 //require at this point: a listener on port 1433...
54 Socket Listener = new Socket(AddressFamily.InterNetwork,
57 IPAddress hostIP =Dns.GetHostEntry("localhost").AddressList[0];
58 IPEndPoint ep = new IPEndPoint(hostIP, 1433);
65 //try to connect twice, in earlier failure would get null exception
66 //on 2nd call to pool.GetConnection();
67 //Most of this code ripped from sqlConnection.Open()
69 TdsConnectionPool pool;
71 TdsConnectionPoolManager sqlConnectionPools =
72 new TdsConnectionPoolManager(TdsVersion.tds80);
73 TdsConnectionInfo info=
74 new TdsConnectionInfo(SERVER/*dummyhost*/,1433/*port*/,
75 8192/*pktsize*/,15/*timeout*/,0/*minpoolsize*/,
77 pool=sqlConnectionPools.GetConnectionPool("test",info);
80 //this first one succeeded regardless as long as something answered
81 //the phone on port 1433 of localhost
82 tds=pool.GetConnection();
84 pool.ReleaseConnection(tds);
87 // 2nd time thru: This will fail with nullreferenceexception
88 // at pool.GetConnection() unless the patch by Rob Wilkens which
89 // adds "result=null;" before retry in pool.getConnection() source
91 //First let's pretend we're calling this test fresh, as if we
92 //call sqlConnection.Open() again :
94 info=new TdsConnectionInfo(SERVER/*dummyhost*/,1433/*port*/,
95 8192/*pktsize*/,15/*timeout*/,0/*minpoolsize*/,
98 pool=sqlConnectionPools.GetConnectionPool("test",info);
100 //Then: Test for failure (will raise uncaught exception which
101 //causes failure of test if bug is not fixed
102 tds=pool.GetConnection();
104 pool.ReleaseConnection(tds);