@Override public synchronized V in (Object key) { Object obj; while ((obj = inp (key)) == null) { try { this.wait (); } catch (InterruptedException e) { } } return (V) obj; }
@Override public synchronized V in (Object key, long timeout) { Object obj; long now = System.currentTimeMillis(); long end = now + timeout; while ((obj = inp (key)) == null && (now = System.currentTimeMillis()) < end) { try { this.wait (end - now); } catch (InterruptedException e) { } } return (V) obj; }
@Override public synchronized void removeListener (Object key, SpaceListener listener) { if (sl != null) { sl.inp (new ObjectTemplate (key, listener)); } } public boolean isEmpty() {
public synchronized void removeListener (Object key, SpaceListener listener) { if (sl != null) { sl.inp (new ObjectTemplate (key, listener)); } } public void notifyListeners (final Object key, final Object value) {
@After public void tearDown() { Set keySet = new HashSet(sp.getKeySet()); for (Object key : keySet) { sp.inp(key); } sp.gc(); sp = null; }
@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 testInp2() throws Throwable { TSpace tSpace = new TSpace(); Object result = tSpace.inp(null); assertNull("result", result); assertEquals("tSpace.entries.size()", 0, tSpace.entries.size()); }
@Test public void testInp() throws Throwable { TSpace tSpace = new TSpace(); MD5Template key = new MD5Template("", ""); Object result = tSpace.inp(key); assertNull("result", result); assertEquals("tSpace.entries.size()", 0, tSpace.entries.size()); }
@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")); }
@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 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 testNullEntry() { try { sp.out("testNull", null); fail("NullPointerException should have been called"); } catch (NullPointerException e) { assertNull("Verify null entry (rdp)", sp.rdp("testNull")); assertNull("Verify null entry (inp)", sp.inp("testNull")); } }
@Test public void testMD5Template() throws Exception { final String KEY = "TestMD5Template_Key"; sp.out(KEY, "123"); sp.out(KEY, "456"); sp.out(KEY, "789"); Template tmpl = new MD5Template(KEY, "456"); assertEquals(sp.rdp(KEY), "123"); assertEquals(sp.rdp(tmpl), "456"); assertEquals(sp.rdp(KEY), "123"); assertEquals(sp.inp(tmpl), "456"); assertNull(sp.rdp(tmpl)); assertNull(sp.inp(tmpl)); assertEquals(sp.rdp(KEY), "123"); assertEquals(sp.inp(KEY), "123"); assertEquals(sp.rdp(KEY), "789"); assertEquals(sp.inp(KEY), "789"); assertNull(sp.rdp(KEY)); assertNull(sp.inp(KEY)); }
@Test public void testTemplate() throws Exception { final String KEY = "TestTemplate_Key"; sp.out(KEY, "123"); sp.out(KEY, "456"); sp.out(KEY, "789"); Template tmpl = new ObjectTemplate(KEY, "456"); assertEquals(sp.rdp(KEY), "123"); assertEquals(sp.rdp(tmpl), "456"); assertEquals(sp.rdp(KEY), "123"); assertEquals(sp.inp(tmpl), "456"); assertNull(sp.rdp(tmpl)); assertNull(sp.inp(tmpl)); assertEquals(sp.rdp(KEY), "123"); assertEquals(sp.inp(KEY), "123"); assertEquals(sp.rdp(KEY), "789"); assertEquals(sp.inp(KEY), "789"); assertNull(sp.rdp(KEY)); assertNull(sp.inp(KEY)); }
@Test public void testPut() { sp.out("PUT", "ONE"); sp.out("PUT", "TWO"); sp.put("PUT", "ZERO"); assertEquals("ZERO", sp.rdp("PUT")); assertEquals("ZERO", sp.inp("PUT")); assertNull(sp.rdp("PUT")); }