Merge pull request #260 from pcc/topmost
[mono.git] / mcs / class / Mono.Data.Tds / Test / ConnLifetime.cs
1 //
2 // ConnLifetime.cs
3 //
4 // Authors:
5 //      James Lewis <james.lewis@7digital.com>
6 //      Andres G. Aragoneses <andres@7digital.com>
7 //
8 // Copyright (C) 2012 7digital Media Ltd (http://www.7digital.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 using System;
31
32 using NUnit.Framework;
33
34 using Mono.Data.Tds.Protocol;
35
36
37 namespace Mono.Data.Tds.Tests
38 {
39
40         [TestFixture]
41         public class ConnLifetime
42         {
43                 [Test]
44                 public void LifeTimeIsTakenInAccount ()
45                 {
46                         var SMALLEST_LIFETIME_TO_TEST = 1;
47                         var WAIT_TO_MAKE_LIFETIME_PASSED = 2;
48
49                         TdsConnectionPoolManager sqlConnectionPools = new FakeConnectionPoolManager ();
50                         TdsConnectionInfo info = new TdsConnectionInfo ("dummy", 0, 0, 0,
51                                                                        1 /*minpoolsize*/,
52                                                                        1 /*maxpoolsize*/,
53                                                                        SMALLEST_LIFETIME_TO_TEST/*lifetime*/);
54
55                         TdsConnectionPool pool = sqlConnectionPools.GetConnectionPool ("test",info);
56                         Mono.Data.Tds.Protocol.Tds tds, tds2 = null;
57
58                         tds = pool.GetConnection();
59
60                         System.Threading.Thread.Sleep (TimeSpan.FromSeconds (WAIT_TO_MAKE_LIFETIME_PASSED));
61                         pool.ReleaseConnection (tds);
62
63                         tds2 = pool.GetConnection ();
64
65
66                         Assert.IsFalse (object.ReferenceEquals (tds, tds2));
67                         pool.ReleaseConnection(tds2);
68                 }
69
70                 class FakeConnectionPoolManager : TdsConnectionPoolManager {
71
72                         internal FakeConnectionPoolManager () : base (Mono.Data.Tds.Protocol.TdsVersion.tds90)
73                         {
74                         }
75
76                         public override Mono.Data.Tds.Protocol.Tds CreateConnection (TdsConnectionInfo info)
77                         {
78                                 return new FakeTds (info.LifeTime);
79                         }
80                 }
81
82                 class FakeTds : Mono.Data.Tds.Protocol.Tds {
83                         internal FakeTds(int lifetime) : base (null, 0, 0, 0, lifetime, Mono.Data.Tds.Protocol.TdsVersion.tds90){
84                         }
85
86                         public override bool Connect (TdsConnectionParameters connectionParameters)
87                         {
88                                 throw new NotImplementedException ();
89                         }
90
91                         protected override void ProcessColumnInfo ()
92                         {
93                                 throw new NotImplementedException ();
94                         }
95
96                         protected override void InitComm (int port, int timeout)
97                         {
98                                 //do nothing, not relevant for the test
99                         }
100
101                         public override bool IsConnected {
102                                 get { return true; }
103                                 set { }
104                         }
105
106                         public override void Disconnect ()
107                         {
108                                 // do nothing, not relevant for the test
109                         }
110                 }
111         }
112 }
113