01: /*
02: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tcclient.cache;
05:
06: import java.util.Collection;
07: import java.util.HashSet;
08: import java.util.Set;
09:
10: import junit.framework.TestCase;
11:
12: public class GlobalKeySetTest extends TestCase {
13:
14: private GlobalKeySet gks;
15:
16: public void setUp() {
17: gks = new GlobalKeySet();
18: }
19:
20: public void testTypicalGlobalEvictionCycle() {
21: // Node 1 is global evictor and starts global eviction
22: assertFalse(gks.inGlobalEviction());
23: gks.globalEvictionStart(new Object[] { "k1" });
24:
25: // Node 2 does a local eviction
26: assertTrue(gks.inGlobalEviction());
27: gks.addLocalKeySet(new Object[] { "k1", "k2" });
28:
29: // Node 1 does another local eviction during global cycle
30: assertTrue(gks.inGlobalEviction());
31:
32: // Node 3 does a local eviction
33: assertTrue(gks.inGlobalEviction());
34: // note keys are not mutually exclusive and can be managed on multiple nodes like "k2" here
35: gks.addLocalKeySet(new Object[] { "k2", "k3", "k4" });
36:
37: // Node 1 checks for end of global eviction and it's finally time
38: assertTrue(gks.inGlobalEviction());
39: Collection remoteKeys = gks.globalEvictionEnd();
40: assertEquals(toSet(new String[] { "k1", "k2", "k3", "k4" }),
41: new HashSet(remoteKeys));
42: }
43:
44: public void testAllNodesDieDuringGlobalEviction() {
45: // Node 1 is global evictor and starts global eviction
46: assertFalse(gks.inGlobalEviction());
47: gks.globalEvictionStart(new Object[] { "k1" });
48:
49: // Node 2 does a local eviction
50: assertTrue(gks.inGlobalEviction());
51: gks.addLocalKeySet(new Object[] { "k1", "k2" });
52:
53: // Node 2 comes back and will become the new global evictor
54:
55: // Node 2 hits global eviction frequency (its still set as running from before)
56: assertTrue(gks.inGlobalEviction());
57: Collection remoteKeys = gks.globalEvictionEnd();
58: assertEquals(toSet(new String[] { "k1", "k2" }), new HashSet(
59: remoteKeys));
60:
61: // Here the orphaned keys from nodes that had not yet reported will
62: // be detected and handled. Keys from nodes that had reported will be
63: // seen (possibly incorrectly as orphans). This is going to cause
64: // faulting, but this is an unusual (and presumably rare case).
65: }
66:
67: private Set toSet(String[] values) {
68: Set s = new HashSet();
69: if (values != null) {
70: for (int i = 0; i < values.length; i++) {
71: s.add(values[i]);
72: }
73: }
74: return s;
75: }
76:
77: }
|