private TSpace getSL() { synchronized (this) { if (sl == null) sl = new TSpace(); } return sl; }
@Override public synchronized V in (Object key) { Object obj; while ((obj = inp (key)) == null) { try { this.wait (); } catch (InterruptedException e) { } } return (V) obj; }
private void gc (int generation) { Set<K> exps; synchronized (this) { exps = expirables[generation]; expirables[generation] = new HashSet<K>(); } for (K k : exps) { if (rdp(k) != null) { synchronized (this) { expirables[generation].add(k); } } Thread.yield (); } if (sl != null) { synchronized (this) { if (sl != null && sl.isEmpty()) sl = null; } } }
@Test public void testSimpleOut() { sp.out("testSimpleOut_Key", "ABC"); sp.out("testSimpleOut_Key", "XYZ"); assertEquals("ABC", sp.rdp("testSimpleOut_Key")); assertEquals("ABC", sp.inp("testSimpleOut_Key")); assertEquals("XYZ", sp.rdp("testSimpleOut_Key")); assertEquals("XYZ", sp.inp("testSimpleOut_Key")); assertNull(sp.rdp("Test")); assertNull(sp.inp("Test")); }
@Test public void testOutRdpInpRdp() throws Exception { Object o = Boolean.TRUE; String k = "testOutRdpInpRdp_Key"; sp.out(k, o); assertTrue(o.equals(sp.rdp(k))); assertTrue(o.equals(sp.rd(k))); assertTrue(o.equals(sp.rd(k, 1000))); assertTrue(o.equals(sp.inp(k))); assertNull(sp.rdp(k)); assertNull(sp.rd(k, 100)); }
@Test public void testPush() { sp.push("PUSH", "ONE"); sp.push("PUSH", "TWO"); sp.push("PUSH", "THREE"); sp.out("PUSH", "FOUR"); assertEquals("THREE", sp.rdp("PUSH")); assertEquals("THREE", sp.inp("PUSH")); assertEquals("TWO", sp.inp("PUSH")); assertEquals("ONE", sp.inp("PUSH")); assertEquals("FOUR", sp.inp("PUSH")); assertNull(sp.rdp("PUSH")); }
@SuppressWarnings("unchecked") @Test public void testInp1() throws Throwable { TSpace tSpace = new TSpace(); tSpace.out("", "testString"); tSpace.out(Integer.valueOf(0), Integer.valueOf(-1)); tSpace.out(Integer.valueOf(31), Integer.valueOf(-32), 100L); tSpace.inp(new MD5Template("", (Object) null)); tSpace.out("", "", 1000L); String result = (String) tSpace.inp(""); assertEquals("tSpace.entries.get(\"\").size()", 1, ((List) tSpace.entries.get("")).size()); assertFalse("tSpace.entries.get(\"\").get(0) had \"testString\" removed", "testString".equals(((List) tSpace.entries.get("")).get(0))); assertEquals("result", "testString", result); assertEquals("tSpace.entries.size()", 3, tSpace.entries.size()); }
@Test public void testGC() throws Exception { sp.out("testGC_Key", "ABC", 50); sp.out("testGC_Key", "XYZ", 50); assertEquals("ABC", sp.rdp("testGC_Key")); try { Thread.sleep(60); } catch (InterruptedException e) { } assertEquals("testGC_Key", sp.getKeysAsString()); sp.gc(); assertEquals("", sp.getKeysAsString()); sp.gc(); }
@Test public void testDump() throws Throwable { TSpace tSpace = new TSpace(); tSpace.out("", "testString"); tSpace.out(Integer.valueOf(0), Integer.valueOf(-1)); tSpace.out(Integer.valueOf(31), ""); PrintStream p = new PrintStream(new ByteArrayOutputStream(), true); tSpace.dump(p, "testTSpaceIndent"); assertEquals("tSpace.entries.size()", 3, tSpace.entries.size()); }
@Test public void testNotify() { sp.addListener("TestDelayNotify_Key", this, 500); sp.out("TestNotify_Key", "ABCCBA"); assertNull(notifiedValue); sp.addListener("TestNotify_Key", this); sp.out("TestNotify_Key", "ABCCBA"); assertEquals(notifiedValue, "ABCCBA"); sp.out("TestNotify_Key", "012345"); assertEquals(notifiedValue, "012345"); assertEquals(sp.inp("TestNotify_Key"), "ABCCBA"); assertEquals(sp.inp("TestNotify_Key"), "ABCCBA"); assertEquals(sp.inp("TestNotify_Key"), "012345"); sp.out("TestDelayNotify_Key", "OLD"); assertEquals(notifiedValue, "OLD"); try { Thread.sleep(600); } catch (InterruptedException e) { } sp.out("TestDelayNotify_Key", "NEW"); assertEquals(notifiedValue, "OLD"); // still OLD assertEquals(sp.inp("TestDelayNotify_Key"), "OLD"); assertEquals(sp.inp("TestDelayNotify_Key"), "NEW"); }
@Test public void testSingleKeyLoad() throws Exception { String s = "The quick brown fox jumped over the lazy dog"; String k = "testSingleKeyLoad_SingleKey"; Profiler prof = new Profiler(); for (int i = 0; i < COUNT; i++) { sp.out(k, s, 60000); } // prof.dump (System.err, "SingleKeyLoad out >"); prof = new Profiler(); for (int i = 0; i < COUNT; i++) { assertTrue(s.equals(sp.in(k))); } // prof.dump (System.err, "SingleKeyLoad in >"); assertNull(sp.rdp(k)); }
@Test public void testExist() { sp.out("KEYA", Boolean.TRUE); sp.out("KEYB", Boolean.TRUE); assertTrue("existAny ([KEYA])", sp.existAny(new String[] { "KEYA" })); assertTrue("existAny ([KEYB])", sp.existAny(new String[] { "KEYB" })); assertTrue("existAny ([KEYA,KEYB])", sp.existAny(new String[] { "KEYA", "KEYB" })); assertFalse("existAny ([KEYC,KEYD])", sp.existAny(new String[] { "KEYC", "KEYD" })); }