Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8e2fa299b1b408b617ffad156813a672ff0494da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * Copyright (c) 2004 - 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:
 *    Ronald Krijgsheld - initial API and implementation
 */
package org.eclipse.emf.cdo.tests.bugzilla;

import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.tests.AbstractCDOTest;
import org.eclipse.emf.cdo.tests.config.IRepositoryConfig;
import org.eclipse.emf.cdo.tests.config.impl.SessionConfig;
import org.eclipse.emf.cdo.tests.model1.Category;
import org.eclipse.emf.cdo.tests.model1.Company;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;
import org.eclipse.emf.cdo.util.InvalidObjectException;
import org.eclipse.emf.cdo.util.ObjectNotFoundException;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author Ronald Krijgsheld
 */
public class Bugzilla_368223_Test extends AbstractCDOTest
{
  @Requires({ IRepositoryConfig.CAPABILITY_BRANCHING, "MEM" })
  public void testRules1() throws Throwable
  {
    CDOSession session = openSession();

    CDOTransaction transaction = session.openTransaction();
    transaction.createResource(getResourcePath("/test1"));
    transaction.commit();

    AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
    long start = System.currentTimeMillis();

    Creator creator = new Creator(exception);
    creator.start();

    Loader loader = new Loader(exception);
    loader.start();

    creator.join();
    loader.join();

    System.out.println(System.currentTimeMillis() - start);

    Throwable ex = exception.get();
    if (ex != null && !(ex instanceof Success))
    {
      throw exception.get();
    }
  }

  @Override
  protected void doSetUp() throws Exception
  {
    disableConsole();
    super.doSetUp();
  }

  @Override
  protected void doTearDown() throws Exception
  {
    disableConsole();
    super.doTearDown();
  }

  /**
   * @author Eike Stepper
   */
  private static final class Success extends Exception
  {
    private static final long serialVersionUID = 1L;
  }

  /**
   * @author Eike Stepper
   */
  private abstract class Actor extends Thread
  {
    private final AtomicReference<Throwable> exception;

    protected Actor(AtomicReference<Throwable> exception)
    {
      this.exception = exception;
    }

    @Override
    public final void run()
    {
      try
      {
        runSafe(exception);
      }
      catch (Throwable ex)
      {
        exception.compareAndSet(null, ex);
      }
    }

    protected abstract void runSafe(AtomicReference<Throwable> exception) throws Exception;
  }

  /**
   * @author Ronald Krijgsheld
   */
  private final class Creator extends Actor
  {
    public Creator(AtomicReference<Throwable> exception)
    {
      super(exception);
    }

    @Override
    protected void runSafe(AtomicReference<Throwable> exception) throws Exception
    {
      CDOSession session = openSession();
      CDOTransaction transaction = session.openTransaction();
      CDOResource resource = transaction.getResource(getResourcePath("/test1"));

      List<Company> listOfCompanies = new ArrayList<Company>();

      int loop = 10;
      while (exception.get() == null && --loop != 0)
      {
        // System.out.println(loop);
        synchronized (transaction)
        {
          for (int i = 0; i < 100; i++)
          {
            Company company = createCompanyWithCategories(resource);
            listOfCompanies.add(company);
          }

          transaction.commit();

          while (!listOfCompanies.isEmpty())
          {
            Company company = listOfCompanies.remove(0);

            EList<Category> categories = company.getCategories();
            while (!categories.isEmpty())
            {
              categories.remove(0);
              transaction.commit();
            }

            resource.getContents().remove(company);
            transaction.commit();
          }
        }
      }

      throw new Success();
    }

    private Company createCompanyWithCategories(CDOResource resource)
    {
      Company company = getModel1Factory().createCompany();
      EList<Category> categories = company.getCategories();

      for (int i = 0; i < 10; i++)
      {
        Category category = getModel1Factory().createCategory();
        categories.add(category);
      }

      resource.getContents().add(company);
      return company;
    }
  }

  /**
   * @author Ronald Krijgsheld
   */
  private final class Loader extends Actor
  {
    public Loader(AtomicReference<Throwable> exception)
    {
      super(exception);
    }

    @Override
    protected void runSafe(AtomicReference<Throwable> exception) throws Exception
    {
      getTestProperties().put(SessionConfig.PROP_TEST_FETCH_RULE_MANAGER, CDOUtil.createThreadLocalFetchRuleManager());
      CDOSession session = openSession();

      CDOTransaction transaction = session.openTransaction();
      transaction.options().setFeatureAnalyzer(CDOUtil.createModelBasedFeatureAnalyzer());

      CDOResource resource = transaction.getResource(getResourcePath("/test1"));

      while (exception.get() == null)
      {
        try
        {
          synchronized (transaction)
          {
            for (EObject object : resource.getContents())
            {
              Company company = (Company)object;
              EList<Category> categories = company.getCategories();
              if (categories.size() > 0)
              {
                Category category = categories.get(0);
                msg(category);
              }
            }
          }
        }
        catch (InvalidObjectException ex)
        {
          System.err.println(ex.getMessage());
          continue;
        }
        catch (ObjectNotFoundException ex)
        {
          System.err.println(ex.getMessage());
          continue;
        }
      }

      throw new Success();
    }
  }
}

Back to the top