[mini] Fix test compiling when running !MOBILE
[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         //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...
52
53         try{
54                 IPAddress hostIP =Dns.GetHostEntry("localhost").AddressList[0];
55         IPEndPoint ep = new IPEndPoint(hostIP, 1433);
56         TcpListener Listener = new TcpListener (ep);
57         Listener.Start ();
58         } catch (Exception){
59                 //ignore
60         }
61
62         //try to connect twice, in earlier failure would get null exception
63         //on 2nd call to pool.GetConnection();
64         //Most of this code ripped from sqlConnection.Open()
65
66         TdsConnectionPool pool;
67         
68         TdsConnectionPoolManager sqlConnectionPools = 
69                 new TdsConnectionPoolManager(TdsVersion.tds80); 
70         TdsConnectionInfo info=
71                 new TdsConnectionInfo(SERVER/*dummyhost*/,1433/*port*/,
72                 8192/*pktsize*/,15/*timeout*/,0/*minpoolsize*/,
73                 100/*maxpoolsize*/, 0/*lifetime*/);
74         pool=sqlConnectionPools.GetConnectionPool("test",info);
75         Tds tds=null;
76
77         //this first one succeeded regardless as long as something answered
78         //the phone on port 1433 of localhost
79         tds=pool.GetConnection();
80
81         pool.ReleaseConnection(tds);
82
83
84         // 2nd time thru: This will fail with nullreferenceexception 
85         // at pool.GetConnection() unless the patch by Rob Wilkens which 
86         // adds "result=null;" before retry in pool.getConnection() source
87
88         //First let's pretend we're calling this test fresh, as if we
89         //call sqlConnection.Open() again :
90
91         info=new TdsConnectionInfo(SERVER/*dummyhost*/,1433/*port*/,
92                 8192/*pktsize*/,15/*timeout*/,0/*minpoolsize*/,
93                 100/*maxpoolsize*/, 0/*lifetime*/);
94
95         pool=sqlConnectionPools.GetConnectionPool("test",info);
96
97         //Then: Test for failure (will raise uncaught exception which
98         //causes failure of test if bug is not fixed
99         tds=pool.GetConnection();
100
101         pool.ReleaseConnection(tds);
102
103         Listener.Stop ();
104         //exit
105     }
106   }
107 }
108