Fix bug 4786 test.
[mono.git] / mcs / class / Mono.Data.Tds / Test / bug-4786.cs
1 //
2 // bug-4786.cs- 
3 //      NUnit Test Cases for Mono.Data.Tds.Protocol.TdsConnectionPool
4 //
5 // Author:
6 //      Robert Wilkens <robwilkens@gmail.com>
7 //
8 // Copyright (C) 2012 Robert Wilkens (http://www.robssoftwareprojects.com)
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 namespace MonoTests.Mono.Data.Tds
31 {
32         
33   using NUnit.Framework;
34   using global::Mono.Data.Tds.Protocol;
35   using System;
36   using System.Net;
37   using System.Net.Sockets;
38
39
40   [TestFixture]
41   public class TdsConnectionPoolTest
42   {
43     const string SERVER="localhost";
44     [Test]
45     public void CheckNullException() 
46     {
47
48     TcpListener Listener = null;
49
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...
54
55         try{
56                 IPAddress hostIP =Dns.GetHostEntry("localhost").AddressList[0];
57         IPEndPoint ep = new IPEndPoint(hostIP, 1433);
58         Listener = new TcpListener (ep);
59         Listener.Start ();
60         } catch (Exception){
61                 //ignore
62         }
63
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()
67
68         TdsConnectionPool pool;
69         
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);
77         Tds tds=null;
78
79         //this first one succeeded regardless as long as something answered
80         //the phone on port 1433 of localhost
81         tds=pool.GetConnection();
82
83         pool.ReleaseConnection(tds);
84
85
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
89
90         //First let's pretend we're calling this test fresh, as if we
91         //call sqlConnection.Open() again :
92
93         info=new TdsConnectionInfo(SERVER/*dummyhost*/,1433/*port*/,
94                 8192/*pktsize*/,15/*timeout*/,0/*minpoolsize*/,
95                 100/*maxpoolsize*/, 0/*lifetime*/);
96
97         pool=sqlConnectionPools.GetConnectionPool("test",info);
98
99         //Then: Test for failure (will raise uncaught exception which
100         //causes failure of test if bug is not fixed
101         tds=pool.GetConnection();
102
103         pool.ReleaseConnection(tds);
104
105         Listener.Stop ();
106         //exit
107     }
108   }
109 }
110