KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > pojo > TestingUtil


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.cache.pojo;
9
10 import org.jboss.cache.CacheImpl;
11
12 import java.util.Vector JavaDoc;
13
14 /**
15  * Utilities for unit testing JBossCache.
16  *
17  * @author <a HREF="mailto://brian.stansberry@jboss.com">Brian Stansberry</a>
18  * @version $Revision: 1.4 $
19  */

20 public class TestingUtil
21 {
22
23    /**
24     * @param caches caches which must all have consistent views
25     * @param timeout max number of ms to loop
26     * @throws RuntimeException if <code>timeout</code> ms have elapse without
27     * all caches having the same number of members.
28     */

29    public static void blockUntilViewsReceived(PojoCache[] caches, long timeout)
30    {
31       long failTime = System.currentTimeMillis() + timeout;
32
33       while (System.currentTimeMillis() < failTime)
34       {
35          org.jboss.cache.pojo.TestingUtil.sleepThread(100);
36          if (org.jboss.cache.pojo.TestingUtil.areCacheViewsComplete(caches))
37             return;
38       }
39
40       throw new RuntimeException JavaDoc("timed out before caches had complete views");
41    }
42
43    /**
44     */

45    public static void blockUntilViewReceived(PojoCache cache, int groupSize, long timeout)
46    {
47       long failTime = System.currentTimeMillis() + timeout;
48
49       CacheImpl tcache = (CacheImpl) cache.getCache();
50       while (System.currentTimeMillis() < failTime)
51       {
52          org.jboss.cache.pojo.TestingUtil.sleepThread(100);
53          if (org.jboss.cache.pojo.TestingUtil.isCacheViewComplete(tcache, groupSize))
54             return;
55       }
56
57       throw new RuntimeException JavaDoc("timed out before caches had complete views");
58    }
59
60    /**
61     * Checks each cache to see if the number of elements in the array
62     * returned by {@link org.jboss.cache.CacheImpl#getMembers()} matches the size of
63     * the <code>caches</code> parameter.
64     *
65     * @param caches caches that should form a View
66     * @return <code>true</code> if all caches have
67     * <code>caches.length</code> members; false otherwise
68     * @throws IllegalStateException if any of the caches have MORE view
69     * members than caches.length
70     */

71    public static boolean areCacheViewsComplete(PojoCache[] caches)
72    {
73       int memberCount = caches.length;
74
75       for (int i = 0; i < memberCount; i++)
76       {
77          CacheImpl cache = (CacheImpl) caches[i].getCache();
78          return org.jboss.cache.pojo.TestingUtil.isCacheViewComplete(cache, memberCount);
79       }
80
81       return true;
82    }
83
84    /**
85     * FIXME Comment this
86     *
87     * @param cache
88     * @param memberCount
89     */

90    public static boolean isCacheViewComplete(CacheImpl cache, int memberCount)
91    {
92       Vector JavaDoc members = cache.getMembers();
93       if (members == null || memberCount > members.size())
94       {
95          return false;
96       }
97       else if (memberCount < members.size())
98       {
99          // This is an exceptional condition
100
StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Cache at address ");
101          sb.append(cache.getLocalAddress());
102          sb.append(" had ");
103          sb.append(members.size());
104          sb.append(" members; expecting ");
105          sb.append(memberCount);
106          sb.append(". Members were (");
107          for (int j = 0; j < members.size(); j++)
108          {
109             if (j > 0)
110                sb.append(", ");
111             sb.append(members.elementAt(j));
112          }
113          sb.append(')');
114
115          throw new IllegalStateException JavaDoc(sb.toString());
116       }
117
118       return true;
119    }
120
121
122    /**
123     * Puts the current thread to sleep for the desired number of ms, suppressing
124     * any exceptions.
125     *
126     * @param sleeptime number of ms to sleep
127     */

128    public static void sleepThread(long sleeptime)
129    {
130       try
131       {
132          Thread.sleep(sleeptime);
133       }
134       catch (InterruptedException JavaDoc ie)
135       {
136       }
137    }
138 }
139
Popular Tags