1 21 package net.sf.hajdbc.balancer; 22 23 import java.sql.SQLException ; 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 37 @Test 38 public class TestLoadBalancer extends AbstractTestBalancer 39 { 40 43 protected Balancer createBalancer() 44 { 45 return new LoadBalancer(); 46 } 47 48 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 Thread [] database2Threads = new Thread [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 Thread 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 112 { 113 private Balancer balancer; 114 private Operation operation; 115 private Database database; 116 117 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 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 e) 148 { 149 assert false : e; 150 } 151 } 152 153 156 public synchronized void start() 157 { 158 super.start(); 159 160 this.pause(); 161 } 162 163 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 e) 180 { 181 assert false : e; 182 } 183 } 184 } 185 186 private class MockOperation implements Operation<Void , Void > 187 { 188 191 public Void execute(Database database, Void object) 192 { 193 try 195 { 196 Thread.sleep(10000); 197 } 198 catch (InterruptedException e) 199 { 200 } 202 203 return null; 204 } 205 } 206 } 207 | Popular Tags |