Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2018-05-27 05:30:50 -0400
committerEike Stepper2018-05-27 05:30:50 -0400
commitad54bdbd490bf43fa1cdabf4b4a0e1c6934deb8c (patch)
tree668436b90b9590b2d687ddeca1e2ba303c970068 /plugins/org.eclipse.net4j.tests/src
parentbaa83a0936ece0e0edbd705fdf41b9b06b5555db (diff)
downloadcdo-ad54bdbd490bf43fa1cdabf4b4a0e1c6934deb8c.tar.gz
cdo-ad54bdbd490bf43fa1cdabf4b4a0e1c6934deb8c.tar.xz
cdo-ad54bdbd490bf43fa1cdabf4b4a0e1c6934deb8c.zip
[535161] Implement a buffer tracer
https://bugs.eclipse.org/bugs/show_bug.cgi?id=535161
Diffstat (limited to 'plugins/org.eclipse.net4j.tests/src')
-rw-r--r--plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/tests/AllTests.java4
-rw-r--r--plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/ReferenceValueMapTest.java43
-rw-r--r--plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/RollingLogTest.java142
-rw-r--r--plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/TestListener.java102
-rw-r--r--plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/TestListener2.java249
-rw-r--r--plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/Timer.java75
6 files changed, 570 insertions, 45 deletions
diff --git a/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/tests/AllTests.java b/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/tests/AllTests.java
index 91b2ddba06..af00e7c164 100644
--- a/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/tests/AllTests.java
+++ b/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/tests/AllTests.java
@@ -17,7 +17,7 @@ import org.eclipse.net4j.util.tests.ExecutorWorkSerializerTest;
import org.eclipse.net4j.util.tests.ExpectedIOTest;
import org.eclipse.net4j.util.tests.ExtendedIOTest;
import org.eclipse.net4j.util.tests.MultiMapTest;
-import org.eclipse.net4j.util.tests.ReferenceValueMapTest;
+import org.eclipse.net4j.util.tests.RollingLogTest;
import org.eclipse.net4j.util.tests.SecurityTest;
import org.eclipse.net4j.util.tests.SortedFileMapTest;
import org.eclipse.net4j.util.tests.StringCompressorTest;
@@ -39,7 +39,6 @@ public class AllTests
suite.addTestSuite(MultiMapTest.class);
suite.addTestSuite(SortedFileMapTest.class);
suite.addTestSuite(SynchronizingCorrelatorTest.class);
- suite.addTestSuite(ReferenceValueMapTest.class);
suite.addTestSuite(BufferPoolTest.class);
suite.addTestSuite(ExtendedIOTest.class);
suite.addTestSuite(StringCompressorTest.class);
@@ -54,6 +53,7 @@ public class AllTests
suite.addTestSuite(SecurityTest.class);
suite.addTestSuite(ExecutorWorkSerializerTest.class);
suite.addTestSuite(ExpectedIOTest.class);
+ suite.addTestSuite(RollingLogTest.class);
// Bugzillas
suite.addTestSuite(Bugzilla_241463_Test.JVM.class);
diff --git a/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/ReferenceValueMapTest.java b/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/ReferenceValueMapTest.java
deleted file mode 100644
index e8c2101ece..0000000000
--- a/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/ReferenceValueMapTest.java
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * Copyright (c) 2007-2009, 2011, 2012, 2015 Eike Stepper (Berlin, Germany) and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Eike Stepper - initial API and implementation
- */
-package org.eclipse.net4j.util.tests;
-
-import org.eclipse.net4j.util.ref.ReferenceValueMap;
-
-/**
- * @author Eike Stepper
- */
-public class ReferenceValueMapTest extends AbstractOMTest
-{
- public void testSameKey() throws Exception
- {
- ReferenceValueMap<String, Object> map = new ReferenceValueMap.Weak<String, Object>();
- for (int i = 0; i < 10; i++)
- {
- map.put("SIMON", new Object()); //$NON-NLS-1$
- System.gc();
- map.put("SIMON", new Object()); //$NON-NLS-1$
- assertEquals(true, map.size() >= 0);
- }
- }
-
- public void testDifferentKey() throws Exception
- {
- ReferenceValueMap<String, Object> map = new ReferenceValueMap.Weak<String, Object>();
- for (int i = 0; i < 10; i++)
- {
- map.put("SIMON", new Object()); //$NON-NLS-1$
- System.gc();
- map.put("SIMON2", new Object()); //$NON-NLS-1$
- assertEquals(true, map.size() >= 1);
- }
- }
-}
diff --git a/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/RollingLogTest.java b/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/RollingLogTest.java
new file mode 100644
index 0000000000..60aa59acc8
--- /dev/null
+++ b/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/RollingLogTest.java
@@ -0,0 +1,142 @@
+/*
+ * Copyright (c) 2010-2012 Eike Stepper (Berlin, Germany) and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ * Caspar De Groot - maintenance
+ */
+package org.eclipse.net4j.util.tests;
+
+import org.eclipse.net4j.util.io.TMPUtil;
+import org.eclipse.net4j.util.om.log.RollingLog;
+
+import java.io.File;
+
+/**
+ * @author Eike Stepper
+ * @since 3.1
+ */
+public class RollingLogTest extends AbstractOMTest
+{
+ private static final String MESSAGE = "Hello World";
+
+ public void testAppend() throws Exception
+ {
+ TestListener2 propertiesListener = new TestListener2(RollingLog.PropertiesEvent.class);
+ TestListener2 splitListener = new TestListener2(RollingLog.SplitEvent.class);
+
+ RollingLog log = new RollingLog(TMPUtil.getTempName("rollinglog").getAbsolutePath(), 1000, false);
+ log.setWriteBulk(false);
+ log.setWriteInterval(0);
+ log.addListener(propertiesListener);
+ log.addListener(splitListener);
+ log.activate();
+
+ write(log, 3000);
+ log.commit();
+
+ assertEquals(9, log.getFileNumber());
+ assertEquals(9, splitListener.getEvents().size());
+
+ log.deactivate();
+ assertEquals(1, propertiesListener.getEvents().size());
+
+ propertiesListener = new TestListener2(RollingLog.PropertiesEvent.class);
+ splitListener = new TestListener2(RollingLog.SplitEvent.class);
+
+ log = new RollingLog(log.getLogFile(), 1000, true);
+ log.setWriteBulk(false);
+ log.setWriteInterval(0);
+ log.addListener(propertiesListener);
+ log.addListener(splitListener);
+ log.activate();
+
+ write(log, 3000);
+ log.commit();
+
+ assertEquals(19, log.getFileNumber());
+ assertEquals(10, splitListener.getEvents().size());
+
+ log.deactivate();
+ assertEquals(2, propertiesListener.getEvents().size());
+ }
+
+ public void testOverwrite() throws Exception
+ {
+ TestListener2 propertiesListener = new TestListener2(RollingLog.PropertiesEvent.class);
+ TestListener2 splitListener = new TestListener2(RollingLog.SplitEvent.class);
+
+ RollingLog log = new RollingLog(TMPUtil.getTempName("rollinglog").getAbsolutePath(), 1000, false);
+ log.setWriteBulk(false);
+ log.setWriteInterval(0);
+ log.addListener(propertiesListener);
+ log.addListener(splitListener);
+ log.activate();
+
+ write(log, 3000);
+ log.commit();
+
+ assertEquals(9, log.getFileNumber());
+ assertEquals(9, splitListener.getEvents().size());
+
+ log.deactivate();
+ assertEquals(1, propertiesListener.getEvents().size());
+
+ propertiesListener = new TestListener2(RollingLog.PropertiesEvent.class);
+ splitListener = new TestListener2(RollingLog.SplitEvent.class);
+
+ log = new RollingLog(log.getLogFile(), 1000, false);
+ log.setWriteBulk(false);
+ log.setWriteInterval(0);
+ log.addListener(propertiesListener);
+ log.addListener(splitListener);
+ log.activate();
+
+ write(log, 3000);
+ log.commit();
+
+ assertEquals(9, log.getFileNumber());
+ assertEquals(9, splitListener.getEvents().size());
+
+ log.deactivate();
+ assertEquals(1, propertiesListener.getEvents().size());
+ }
+
+ public void testRecover() throws Exception
+ {
+ RollingLog log = new RollingLog(TMPUtil.getTempName("rollinglog").getAbsolutePath(), 1000, false);
+ log.setWriteBulk(false);
+ log.setWriteInterval(0);
+ log.activate();
+
+ write(log, 3000);
+ log.commit();
+ log.deactivate();
+
+ assertTrue(new File(log.getLogFile() + ".properties").delete());
+ TestListener2 recoveryListener = new TestListener2(RollingLog.RecoveryEvent.class);
+ TestListener2 propertiesListener = new TestListener2(RollingLog.PropertiesEvent.class);
+
+ log = new RollingLog(log.getLogFile(), 1000, true);
+ log.setWriteBulk(false);
+ log.setWriteInterval(0);
+ log.addListener(recoveryListener);
+ log.addListener(propertiesListener);
+ log.activate();
+ assertEquals(19, recoveryListener.getEvents().size());
+ log.deactivate();
+ assertEquals(2, propertiesListener.getEvents().size());
+ }
+
+ private static void write(RollingLog log, int size)
+ {
+ for (int i = 0; i < size; i += MESSAGE.length())
+ {
+ log.log(MESSAGE);
+ }
+ }
+}
diff --git a/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/TestListener.java b/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/TestListener.java
new file mode 100644
index 0000000000..b208f3e34a
--- /dev/null
+++ b/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/TestListener.java
@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2011-2013, 2016 Eike Stepper (Berlin, Germany) and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ */
+package org.eclipse.net4j.util.tests;
+
+import org.eclipse.net4j.util.event.IEvent;
+import org.eclipse.net4j.util.event.IListener;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.AssertionFailedError;
+
+/**
+ * @author Eike Stepper
+ */
+public class TestListener implements IListener
+{
+ private List<IEvent> events = new ArrayList<IEvent>();
+
+ private int nextAssertion;
+
+ public TestListener()
+ {
+ }
+
+ public <T extends IEvent> void assertEvent(final Class<?> eventType, final EventAssertion<T> assertion) throws Exception
+ {
+ new AbstractOMTest.PollingTimeOuter()
+ {
+ @SuppressWarnings("unchecked")
+ @Override
+ protected boolean successful()
+ {
+ synchronized (events)
+ {
+ for (int i = nextAssertion; i < events.size(); i++)
+ {
+ IEvent event = events.get(i);
+ System.out.println(event);
+ ++nextAssertion;
+
+ if (eventType.isAssignableFrom(event.getClass()))
+ {
+ try
+ {
+ assertion.execute((T)event);
+ return true;
+ }
+ catch (AssertionFailedError ignore)
+ {
+ // This is not the expected event. Either it'll come later or we'll time out below.
+ }
+ }
+ }
+ }
+
+ return false;
+ }
+ }.assertNoTimeOut();
+ }
+
+ public IEvent[] getEvents()
+ {
+ synchronized (events)
+ {
+ return events.toArray(new IEvent[events.size()]);
+ }
+ }
+
+ public void clearEvents()
+ {
+ synchronized (events)
+ {
+ events.clear();
+ nextAssertion = 0;
+ }
+ }
+
+ public void notifyEvent(IEvent event)
+ {
+ synchronized (events)
+ {
+ events.add(event);
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ public interface EventAssertion<T extends IEvent>
+ {
+ public void execute(T event);
+ }
+}
diff --git a/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/TestListener2.java b/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/TestListener2.java
new file mode 100644
index 0000000000..f05ecfb9c9
--- /dev/null
+++ b/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/TestListener2.java
@@ -0,0 +1,249 @@
+/*
+ * Copyright (c) 2011-2013, 2016 Eike Stepper (Berlin, Germany) and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ */
+package org.eclipse.net4j.util.tests;
+
+import org.eclipse.net4j.util.WrappedException;
+import org.eclipse.net4j.util.event.IEvent;
+import org.eclipse.net4j.util.event.IListener;
+
+import org.junit.Assert;
+
+import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+/**
+ * @author Caspar De Groot
+ */
+public class TestListener2 implements IListener
+{
+ public static final int NO_TIME_STAMP = 0;
+
+ private static final Set<Class<? extends IEvent>> NO_EVENT_CLASSES = Collections.emptySet();
+
+ private static final long DEFAULT_TIMEOUT = 3000; // 3 seconds
+
+ private final Map<IEvent, Long> events = new LinkedHashMap<IEvent, Long>();
+
+ private final Collection<Class<? extends IEvent>> eventClasses;
+
+ private long timeout;
+
+ private String name;
+
+ public TestListener2(Collection<Class<? extends IEvent>> eventClasses)
+ {
+ this(eventClasses, null);
+ }
+
+ public TestListener2(Class<? extends IEvent> eventClass)
+ {
+ this(singleton(eventClass));
+ }
+
+ public TestListener2(Collection<Class<? extends IEvent>> eventClasses, String name)
+ {
+ this.eventClasses = eventClasses != null ? eventClasses : NO_EVENT_CLASSES;
+ this.name = name;
+ timeout = DEFAULT_TIMEOUT;
+ }
+
+ public TestListener2(Class<? extends IEvent> eventClass, String name)
+ {
+ this(singleton(eventClass), name);
+ }
+
+ public boolean isApplicable(IEvent event)
+ {
+ if (eventClasses.isEmpty())
+ {
+ return true;
+ }
+
+ Class<? extends IEvent> theClass = event.getClass();
+ for (Class<? extends IEvent> eventClass : eventClasses)
+ {
+ if (eventClass.isAssignableFrom(theClass))
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ public void notifyEvent(IEvent event)
+ {
+ if (isApplicable(event))
+ {
+ long timeStamp = System.currentTimeMillis();
+ if (timeStamp == NO_TIME_STAMP)
+ {
+ throw new IllegalStateException("Regular time stamp is equal to NO_TIME_STAMP");
+ }
+
+ synchronized (this)
+ {
+ events.put(event, timeStamp);
+ notify();
+ }
+ }
+ }
+
+ public List<IEvent> getEvents()
+ {
+ synchronized (this)
+ {
+ return new ArrayList<IEvent>(events.keySet());
+ }
+ }
+
+ public long getTimeStamp(IEvent event)
+ {
+ Long timeStamp;
+ synchronized (this)
+ {
+ timeStamp = events.get(event);
+ }
+
+ if (timeStamp == null)
+ {
+ return NO_TIME_STAMP;
+ }
+
+ return timeStamp;
+ }
+
+ public void setTimeout(long timeout)
+ {
+ this.timeout = timeout;
+ }
+
+ public synchronized void waitFor(int n, long timeout)
+ {
+ long t = 0;
+
+ synchronized (this)
+ {
+ while (events.size() < n)
+ {
+ if (timeout <= 0)
+ {
+ Assert.fail("Timed out");
+ }
+
+ try
+ {
+ t = System.currentTimeMillis();
+ wait(timeout);
+ }
+ catch (InterruptedException ex)
+ {
+ throw WrappedException.wrap(ex);
+ }
+
+ timeout -= System.currentTimeMillis() - t;
+ }
+ }
+ }
+
+ public void waitFor(int i)
+ {
+ waitFor(i, timeout);
+ }
+
+ public void clearEvents()
+ {
+ synchronized (this)
+ {
+ events.clear();
+ }
+ }
+
+ public String formatEvents(String prefix, String suffix)
+ {
+ StringBuilder builder = new StringBuilder();
+
+ synchronized (this)
+ {
+ for (Entry<IEvent, Long> entry : events.entrySet())
+ {
+ builder.append(prefix + entry.getValue() + ": " + entry.getKey() + suffix);
+ }
+ }
+
+ return builder.toString();
+ }
+
+ public void dump(PrintStream out)
+ {
+ out.println(this);
+ out.print(formatEvents(" ", "\n"));
+ }
+
+ @Override
+ public String toString()
+ {
+ StringBuilder builder = new StringBuilder(TestListener2.class.getSimpleName());
+ builder.append('[');
+
+ if (name != null)
+ {
+ builder.append("name=\"");
+ builder.append(name);
+ builder.append('\"');
+ }
+
+ if (!eventClasses.isEmpty())
+ {
+ if (name != null)
+ {
+ builder.append(", ");
+ }
+
+ builder.append("eventClasses=[");
+ boolean first = true;
+
+ for (Class<? extends IEvent> eventClass : eventClasses)
+ {
+ if (first)
+ {
+ first = false;
+ }
+ else
+ {
+ builder.append(", ");
+ }
+
+ builder.append(eventClass.getSimpleName());
+ }
+
+ builder.append(']');
+ }
+
+ builder.append(']');
+ return builder.toString();
+ }
+
+ private static Set<Class<? extends IEvent>> singleton(Class<? extends IEvent> eventClass)
+ {
+ Set<Class<? extends IEvent>> singleton = new HashSet<Class<? extends IEvent>>();
+ singleton.add(eventClass);
+ return singleton;
+ }
+}
diff --git a/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/Timer.java b/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/Timer.java
new file mode 100644
index 0000000000..7f4ec2b8f1
--- /dev/null
+++ b/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/Timer.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2012 Eike Stepper (Berlin, Germany) and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ */
+package org.eclipse.net4j.util.tests;
+
+import java.util.concurrent.Callable;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @author Eike Stepper
+ */
+public class Timer
+{
+ private long sum;
+
+ private long start;
+
+ public Timer()
+ {
+ start();
+ }
+
+ public void start()
+ {
+ start = System.nanoTime();
+ }
+
+ public void stop()
+ {
+ if (start > 0)
+ {
+ long nanos = System.nanoTime() - start;
+ sum += nanos;
+ start = 0;
+ }
+ }
+
+ public void done()
+ {
+ done(TimeUnit.NANOSECONDS);
+ }
+
+ public void done(TimeUnit timeUnit)
+ {
+ stop();
+ long converted = timeUnit.convert(sum, TimeUnit.NANOSECONDS);
+ System.out.println(converted);
+ }
+
+ public static <T> T execute(Callable<T> callable) throws Exception
+ {
+ return execute(TimeUnit.NANOSECONDS, callable);
+ }
+
+ public static <T> T execute(TimeUnit timeUnit, Callable<T> callable) throws Exception
+ {
+ Timer timer = new Timer();
+
+ try
+ {
+ return callable.call();
+ }
+ finally
+ {
+ timer.done(timeUnit);
+ }
+ }
+}

Back to the top