]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 package org.simantics.db.tests.client;
2
3 import java.util.concurrent.atomic.AtomicInteger;
4
5 import org.junit.Test;
6 import org.simantics.db.exception.DatabaseException;
7 import org.simantics.db.testing.base.TestCommonNoVirtual;
8 import org.simantics.db.testing.common.Client;
9 import org.simantics.db.testing.common.ClientFactory;
10 import org.simantics.db.tests.common.ClientOperations;
11 import org.simantics.db.tests.common.Configuration;
12
13 public class RefreshTest5 extends TestCommonNoVirtual {
14     private static final int COUNT = Configuration.get().refreshLoopCount;
15     private static final int ADD_COUNT = Configuration.get().refreshAddCount;
16     boolean stop;
17     AtomicInteger size = null;
18     AtomicInteger validate = null;
19
20     @Test
21     public void testRefresh5() throws DatabaseException {
22         if (noVirtual())
23             return;
24         for (int i=0; i<COUNT; ++i) {
25             refreshTestAdd();
26         }
27     }
28     void refreshTestAdd() throws DatabaseException {
29         AtomicInteger size = new AtomicInteger(0);
30         AtomicInteger validate = new AtomicInteger(0);
31 //        ServerAddress serverAddress = getSessionContext().getAddress();
32         Client client1 = ClientFactory.create(getRandomString());
33         Client client2 = ClientFactory.create(getRandomString());
34         RefreshValidateThread t1 = null;
35         RefreshValidateThread t2 = null;
36         stop = false;
37         try {
38             String name1 = ClientOperations.createOrderedSet(client1, size.get());
39             ClientOperations.validateOrderedSet(client1, name1, size.get());
40             ClientOperations.validateOrderedSet(client2, name1, size.get());
41             t1 = new RefreshValidateThread(client1, name1, size, validate);
42             t1.start();
43             t2 = new RefreshValidateThread(client1, name1, size, validate);
44             t2.start();
45             for (int i=0; i<ADD_COUNT; ++i) {
46                 ClientOperations.adddElement(client1, name1, 1);
47                 int oldSize = size.incrementAndGet();
48                 while (validate.get() < oldSize*2 &&
49                         null == t1.exception &&
50                         null == t2.exception) {
51                     if (null != t1.exception)
52                         throw t1.exception;
53                     else if (null != t2.exception)
54                         throw t1.exception;
55                     try {
56                         Thread.sleep(10); // milliseconds
57                     } catch (InterruptedException e) {
58                         e.printStackTrace();
59                     }
60                 }
61             }
62         } catch (Throwable t) {
63             t.printStackTrace();
64         } finally {
65             stop = true;
66             try {
67                 t1.join();
68                 t2.join();
69             } catch (InterruptedException e) {
70                 e.printStackTrace();
71             }
72             if (null != client1)
73                 client1.close();
74             if (null != client2)
75                 client2.close();
76             if (null != t1 && null != t1.exception)
77                 throw t1.exception;
78             if (null != t2 && null != t2.exception)
79                 throw t2.exception;
80         }
81     }
82     class RefreshValidateThread extends Thread {
83         private Client client;
84         private String name;
85         private int oldSize = 0;
86         private AtomicInteger size;
87         private AtomicInteger validate;
88         DatabaseException exception = null;
89         public RefreshValidateThread(Client client, String name, AtomicInteger size, AtomicInteger validate) {
90             super("RefreshValidateThread");
91             this.client = client;
92             this.name = name;
93             this.size = size;
94             this.validate = validate;
95         }
96         @Override
97         public void run() {
98             while (!stop) {
99                 while (!stop && size.get() <= oldSize && size.get() <= ADD_COUNT)
100                     try {
101                         Thread.sleep(10); // milliseconds
102                     } catch (InterruptedException e) {
103                         e.printStackTrace();
104                     }
105                 oldSize = size.get();
106                 try {
107                     ClientOperations.validateOrderedSet(client, name, oldSize);
108                 } catch (DatabaseException e) {
109                     exception = e;
110                     return;
111                 }
112                 validate.incrementAndGet();
113             }
114         }
115     }
116 }