]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - tests/org.simantics.db.tests/src/org/simantics/db/tests/client/RefreshTest5.java
Added missing org.simantics.db.{tests,testing} plug-ins.
[simantics/platform.git] / tests / org.simantics.db.tests / src / org / simantics / db / tests / client / RefreshTest5.java
diff --git a/tests/org.simantics.db.tests/src/org/simantics/db/tests/client/RefreshTest5.java b/tests/org.simantics.db.tests/src/org/simantics/db/tests/client/RefreshTest5.java
new file mode 100644 (file)
index 0000000..0ee7a4e
--- /dev/null
@@ -0,0 +1,116 @@
+package org.simantics.db.tests.client;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.junit.Test;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.testing.base.TestCommonNoVirtual;
+import org.simantics.db.testing.common.Client;
+import org.simantics.db.testing.common.ClientFactory;
+import org.simantics.db.tests.common.ClientOperations;
+import org.simantics.db.tests.common.Configuration;
+
+public class RefreshTest5 extends TestCommonNoVirtual {
+    private static final int COUNT = Configuration.get().refreshLoopCount;
+    private static final int ADD_COUNT = Configuration.get().refreshAddCount;
+    boolean stop;
+    AtomicInteger size = null;
+    AtomicInteger validate = null;
+
+    @Test
+    public void testRefresh5() throws DatabaseException {
+        if (noVirtual())
+            return;
+        for (int i=0; i<COUNT; ++i) {
+            refreshTestAdd();
+        }
+    }
+    void refreshTestAdd() throws DatabaseException {
+        AtomicInteger size = new AtomicInteger(0);
+        AtomicInteger validate = new AtomicInteger(0);
+//        ServerAddress serverAddress = getSessionContext().getAddress();
+        Client client1 = ClientFactory.create(getRandomString());
+        Client client2 = ClientFactory.create(getRandomString());
+        RefreshValidateThread t1 = null;
+        RefreshValidateThread t2 = null;
+        stop = false;
+        try {
+            String name1 = ClientOperations.createOrderedSet(client1, size.get());
+            ClientOperations.validateOrderedSet(client1, name1, size.get());
+            ClientOperations.validateOrderedSet(client2, name1, size.get());
+            t1 = new RefreshValidateThread(client1, name1, size, validate);
+            t1.start();
+            t2 = new RefreshValidateThread(client1, name1, size, validate);
+            t2.start();
+            for (int i=0; i<ADD_COUNT; ++i) {
+                ClientOperations.adddElement(client1, name1, 1);
+                int oldSize = size.incrementAndGet();
+                while (validate.get() < oldSize*2 &&
+                        null == t1.exception &&
+                        null == t2.exception) {
+                    if (null != t1.exception)
+                        throw t1.exception;
+                    else if (null != t2.exception)
+                        throw t1.exception;
+                    try {
+                        Thread.sleep(10); // milliseconds
+                    } catch (InterruptedException e) {
+                        e.printStackTrace();
+                    }
+                }
+            }
+        } catch (Throwable t) {
+            t.printStackTrace();
+        } finally {
+            stop = true;
+            try {
+                t1.join();
+                t2.join();
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+            if (null != client1)
+                client1.close();
+            if (null != client2)
+                client2.close();
+            if (null != t1 && null != t1.exception)
+                throw t1.exception;
+            if (null != t2 && null != t2.exception)
+                throw t2.exception;
+        }
+    }
+    class RefreshValidateThread extends Thread {
+        private Client client;
+        private String name;
+        private int oldSize = 0;
+        private AtomicInteger size;
+        private AtomicInteger validate;
+        DatabaseException exception = null;
+        public RefreshValidateThread(Client client, String name, AtomicInteger size, AtomicInteger validate) {
+            super("RefreshValidateThread");
+            this.client = client;
+            this.name = name;
+            this.size = size;
+            this.validate = validate;
+        }
+        @Override
+        public void run() {
+            while (!stop) {
+                while (!stop && size.get() <= oldSize && size.get() <= ADD_COUNT)
+                    try {
+                        Thread.sleep(10); // milliseconds
+                    } catch (InterruptedException e) {
+                        e.printStackTrace();
+                    }
+                oldSize = size.get();
+                try {
+                    ClientOperations.validateOrderedSet(client, name, oldSize);
+                } catch (DatabaseException e) {
+                    exception = e;
+                    return;
+                }
+                validate.incrementAndGet();
+            }
+        }
+    }
+}