KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > hajdbc > balancer > TestLoadBalancer


1 /*
2  * HA-JDBC: High-Availability JDBC
3  * Copyright (c) 2004-2006 Paul Ferraro
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by the
7  * Free Software Foundation; either version 2.1 of the License, or (at your
8  * option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: ferraro@users.sourceforge.net
20  */

21 package net.sf.hajdbc.balancer;
22
23 import java.sql.SQLException JavaDoc;
24
25 import net.sf.hajdbc.Balancer;
26 import net.sf.hajdbc.Database;
27 import net.sf.hajdbc.MockDatabase;
28 import net.sf.hajdbc.Operation;
29
30 import org.testng.annotations.Test;
31
32 /**
33  * @author Paul Ferraro
34  * @version $Revision: 1022 $
35  * @since 1.0
36  */

37 @Test
38 public class TestLoadBalancer extends AbstractTestBalancer
39 {
40     /**
41      * @see net.sf.hajdbc.balancer.AbstractTestBalancer#createBalancer()
42      */

43     protected Balancer createBalancer()
44     {
45         return new LoadBalancer();
46     }
47
48     /**
49      * @see net.sf.hajdbc.balancer.AbstractTestBalancer#testNext(net.sf.hajdbc.Balancer)
50      */

51     protected void testNext(Balancer balancer)
52     {
53         Database database0 = new MockDatabase("0", 0);
54         Database database1 = new MockDatabase("1", 1);
55         Database database2 = new MockDatabase("2", 2);
56
57         balancer.add(database0);
58         
59         Database next = balancer.next();
60         
61         assert database0.equals(next) : next;
62
63         balancer.add(database2);
64
65         next = balancer.next();
66
67         assert database2.equals(next) : next;
68         
69         balancer.add(database1);
70
71         next = balancer.next();
72
73         assert database2.equals(next) : next;
74         
75         // Add enough load to database2 to shift relative effective load
76
Thread JavaDoc[] database2Threads = new Thread JavaDoc[2];
77         for (int i = 0; i < 2; ++i)
78         {
79             database2Threads[i] = new OperationThread(balancer, new MockOperation(), database2);
80             database2Threads[i].start();
81         }
82         
83         next = balancer.next();
84         
85         assert database1.equals(next) : next;
86         
87         // Add enough load to database1 to shift relative effective load
88
Thread JavaDoc database1Thread = new OperationThread(balancer, new MockOperation(), database1);
89         database1Thread.start();
90
91         next = balancer.next();
92         
93         assert database2.equals(next) : next;
94
95         database1Thread.interrupt();
96
97         next = balancer.next();
98
99         assert database1.equals(next) : next;
100         
101         for (int i = 0; i < 2; ++i)
102         {
103             database2Threads[i].interrupt();
104         }
105         
106         next = balancer.next();
107         
108         assert database2.equals(next) : next;
109     }
110     
111     private class OperationThread extends Thread JavaDoc
112     {
113         private Balancer balancer;
114         private Operation operation;
115         private Database database;
116         
117         /**
118          * Constructs a new OperationThread.
119          * @param balancer
120          * @param operation
121          * @param database
122          */

123         public OperationThread(Balancer balancer, Operation operation, Database database)
124         {
125             super();
126             
127             this.balancer = balancer;
128             this.operation = operation;
129             this.database = database;
130             
131             this.setDaemon(true);
132         }
133         
134         /**
135          * @see java.lang.Runnable#run()
136          */

137         public void run()
138         {
139             this.balancer.beforeOperation(this.database);
140             
141             try
142             {
143                 this.operation.execute(this.database, null);
144                 
145                 this.balancer.afterOperation(this.database);
146             }
147             catch (SQLException JavaDoc e)
148             {
149                 assert false : e;
150             }
151         }
152
153         /**
154          * @see java.lang.Thread#start()
155          */

156         public synchronized void start()
157         {
158             super.start();
159             
160             this.pause();
161         }
162         
163         /**
164          * @see java.lang.Thread#interrupt()
165          */

166         public void interrupt()
167         {
168             super.interrupt();
169             
170             this.pause();
171         }
172
173         private void pause()
174         {
175             try
176             {
177                 Thread.sleep(10);
178             }
179             catch (InterruptedException JavaDoc e)
180             {
181                 assert false : e;
182             }
183         }
184     }
185     
186     private class MockOperation implements Operation<Void JavaDoc, Void JavaDoc>
187     {
188         /**
189          * @see net.sf.hajdbc.Operation#execute(net.sf.hajdbc.Database, java.lang.Object)
190          */

191         public Void JavaDoc execute(Database database, Void JavaDoc object)
192         {
193             // Simulate a long operation
194
try
195             {
196                 Thread.sleep(10000);
197             }
198             catch (InterruptedException JavaDoc e)
199             {
200                 // Ignore
201
}
202             
203             return null;
204         }
205     }
206 }
207
Popular Tags