Skip to main content
summaryrefslogtreecommitdiffstats
blob: b43e805e49cbf678875bf727b9052ad0dd70a793 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/**
 * Copyright (c) 2004 - 2010 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:
 *    Martin Fluegge - initial API and implementation
 */
package org.eclipse.emf.cdo.tests.bugzilla;

import org.eclipse.emf.cdo.internal.server.Repository;
import org.eclipse.emf.cdo.internal.server.TransactionCommitContext;
import org.eclipse.emf.cdo.internal.server.TransactionCommitContext.ContainmentCycleDetectedException;
import org.eclipse.emf.cdo.net4j.CDOSession;
import org.eclipse.emf.cdo.server.IRepository;
import org.eclipse.emf.cdo.spi.server.InternalCommitContext;
import org.eclipse.emf.cdo.spi.server.InternalRepository;
import org.eclipse.emf.cdo.spi.server.InternalTransaction;
import org.eclipse.emf.cdo.tests.AbstractCDOTest;
import org.eclipse.emf.cdo.tests.config.impl.RepositoryConfig;
import org.eclipse.emf.cdo.tests.model3.NodeA;
import org.eclipse.emf.cdo.tests.model3.NodeB;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CommitException;

import org.eclipse.net4j.util.WrappedException;
import org.eclipse.net4j.util.lifecycle.LifecycleUtil;

import org.eclipse.emf.ecore.resource.Resource;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @author Martin Fluegge
 * @since 4.0
 */
public class Bugzilla_316444_Test extends AbstractCDOTest
{
  private static final String REPOSITORY_NAME = "repo1";

  private static final String RESOURCE_PATH = "/my/resource";

  public int idSessionA;

  public int idSessionB;

  private int idInitSession;

  private Object monitor = new Object();

  private CountDownLatch latch = new CountDownLatch(2);

  private boolean finishedSessionA = false;

  private List<Exception> exceptions = new ArrayList<Exception>();

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

  private void createRepository()
  {
    Repository repository = new Repository.Default()
    {
      @Override
      public InternalCommitContext createCommitContext(InternalTransaction transaction)
      {
        return new TransactionCommitContext(transaction)
        {
          @Override
          protected void lockObjects() throws InterruptedException
          {
            int sessionID = getTransaction().getSession().getSessionID();
            if (sessionID == idSessionB)
            {
              synchronized (monitor)
              {
                // Only wait if Session A has not passed the lockObjects
                if (!finishedSessionA)
                {
                  msg("Session B is waiting for Session A");
                  monitor.wait(DEFAULT_TIMEOUT);
                  msg("Session B stopped waiting");
                }
                else
                {
                  msg("Session B - no need to wait. A has already passed lockObjects()");
                }
              }
            }

            msg("Passing lockObjects() " + getTransaction().getSession());

            try
            {
              super.lockObjects();
            }
            catch (Exception e)
            {
              latch.countDown();
              throw new RuntimeException(e);
            }

            msg("Passed lockObjects() " + getTransaction().getSession());

            if (sessionID == idSessionA)
            {
              synchronized (monitor)
              {
                finishedSessionA = true;
                monitor.notifyAll();
                msg("Session A notified others not to wait anymore.");
              }
            }

            // Do nothing for inital session. Otherwise the test will block too early
            if (sessionID != idInitSession)
            {
              latch.countDown();

              try
              {
                assertEquals(true, latch.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS));
              }
              catch (InterruptedException ex)
              {
                throw WrappedException.wrap(ex);
              }
            }
          }
        };
      }
    };

    Map<String, String> props = getRepositoryProperties();
    ((InternalRepository)repository).setProperties(props);

    repository.setName(REPOSITORY_NAME);

    Map<String, Object> map = getTestProperties();
    map.put(RepositoryConfig.PROP_TEST_REPOSITORY, repository);
  }

  @Override
  public synchronized Map<String, Object> getTestProperties()
  {
    Map<String, Object> map = super.getTestProperties();
    map.put(IRepository.Props.ENSURE_REFERENTIAL_INTEGRITY, "true");
    return map;
  }

  public void testLockParentWithEAttributeChange() throws Exception
  {
    String resourcePath = RESOURCE_PATH + "1";
    {
      CDOSession session = (CDOSession)openSession(REPOSITORY_NAME);
      idInitSession = session.getSessionID();
      CDOTransaction transaction = session.openTransaction();

      Resource resource = transaction.createResource(resourcePath);

      // -------------- create graph begin ------------------------
      NodeB root = createSimpleNode("root");

      NodeB A = createSimpleNode("A");
      NodeB B = createSimpleNode("B");
      NodeB C = createSimpleNode("C");
      NodeB D = createSimpleNode("D");
      NodeB E = createSimpleNode("E");

      root.getChildren().add(A);
      root.getChildren().add(D);

      A.getChildren().add(B);
      B.getChildren().add(C);

      D.getChildren().add(E);

      resource.getContents().add(root);
      transaction.commit();

      // -------- check for consistency -----------

      checkInitialGraph(root, A, B, C, D, E);

      transaction.close();
      session.close();
    }

    // restartRepository();

    {
      CDOSession session = (CDOSession)openSession(REPOSITORY_NAME);
      idInitSession = session.getSessionID();

      // ----- start threads -----
      ThreadX threadX = new ThreadX(resourcePath);
      ThreadA threadA = new ThreadA(resourcePath);

      threadA.start();
      sleepIfNeeded();
      threadX.start();

      threadX.join(DEFAULT_TIMEOUT);
      threadA.join(DEFAULT_TIMEOUT);

      if (exceptions.size() > 0)
      {
        throw exceptions.get(0);
      }
    }
  }

  public void testMovingSubtree() throws Exception
  {
    exceptions.clear();

    {
      CDOSession session = (CDOSession)openSession(REPOSITORY_NAME);
      idInitSession = session.getSessionID();
      CDOTransaction transaction = session.openTransaction();

      Resource resource = transaction.createResource(RESOURCE_PATH);

      // -------------- create graph begin ------------------------
      NodeB root = createSimpleNode("root");

      NodeB A = createSimpleNode("A");
      NodeB B = createSimpleNode("B");
      NodeB C = createSimpleNode("C");
      NodeB D = createSimpleNode("D");
      NodeB E = createSimpleNode("E");

      root.getChildren().add(A);
      root.getChildren().add(D);

      A.getChildren().add(B);
      B.getChildren().add(C);

      D.getChildren().add(E);

      resource.getContents().add(root);
      transaction.commit();

      // -------- check for consistency -----------

      checkInitialGraph(root, A, B, C, D, E);

      transaction.close();
      session.close();
    }

    // if (!(isConfig(MEM) || isConfig(MEM_AUDITS) || isConfig(MEM_BRANCHES) || isConfig(MEM_OFFLINE)))
    // {
    // // do not restart the repository on MEM store
    // restartRepository();
    // }

    {
      // Just an additional check to make sure that the graph is stored correctly even after repository restart
      CDOSession session = (CDOSession)openSession(REPOSITORY_NAME);
      idInitSession = session.getSessionID();
      CDOTransaction transaction = session.openTransaction();
      Resource resource = transaction.getResource(RESOURCE_PATH, true);

      NodeB root = (NodeB)resource.getContents().get(0);
      assertEquals("root", root.getName());

      NodeB A = getElementFromGraphNodeB(root, "A");
      NodeB B = getElementFromGraphNodeB(root, "B");
      NodeB C = getElementFromGraphNodeB(root, "C");
      NodeB D = getElementFromGraphNodeB(root, "D");
      NodeB E = getElementFromGraphNodeB(root, "E");

      assertNotNull(A);
      assertNotNull(B);
      assertNotNull(C);
      assertNotNull(D);
      assertNotNull(E);

      checkInitialGraph(root, A, B, C, D, E);
    }

    {
      CDOSession session = (CDOSession)openSession(REPOSITORY_NAME);
      idInitSession = session.getSessionID();

      // ----- start threads -----
      ThreadA threadA = new ThreadA(RESOURCE_PATH);
      ThreadB threadB = new ThreadB(RESOURCE_PATH);

      threadA.start();
      sleepIfNeeded();
      threadB.start();

      threadA.join(DEFAULT_TIMEOUT);
      threadB.join(DEFAULT_TIMEOUT);

      if (exceptions.size() > 0)
      {
        Exception exception = exceptions.get(0);
        if (exception instanceof ThreadBShouldHaveThrownAnExceptionException)
        {
          fail(exception.getMessage());
        }
        else
        {
          throw exception;
        }
      }

      session.close();
      msg("finished");
    }
  }

  private void sleepIfNeeded()
  {
    if (isConfig(LEGACY))
    {
      // sleep in legacy while Bug 318816 is not solved
      sleep(1000);
    }
  }

  private void checkInitialGraph(NodeB root, NodeB A, NodeB B, NodeB C, NodeB D, NodeB E)
  {
    assertEquals("A", A.getName());
    assertEquals("B", B.getName());
    assertEquals("C", C.getName());
    assertEquals("D", D.getName());
    assertEquals("E", E.getName());

    assertEquals(root, A.getParent());
    assertEquals(root, A.eContainer());

    assertEquals(root, D.getParent());
    assertEquals(root, D.eContainer());

    assertEquals(A, B.getParent());
    assertEquals(A, B.eContainer());

    assertEquals(B, C.getParent());
    assertEquals(B, C.eContainer());

    assertEquals(D, E.getParent());
    assertEquals(D, E.eContainer());
  }

  private abstract class AbstactTestThread extends Thread
  {
    protected final String resourcePath;

    public AbstactTestThread(String resourcePath)
    {
      this.resourcePath = resourcePath;
    }
  }

  /**
   * @author Martin Fluegge
   */
  private class ThreadA extends AbstactTestThread
  {
    private CDOSession session;

    public ThreadA(String resourcePath)
    {
      super(resourcePath);
      msg("Starting Thread A");
      session = (CDOSession)openSession(REPOSITORY_NAME);
      idSessionA = session.getSessionID();
    }

    @Override
    public void run()
    {
      try
      {
        msg("Started Thread A " + session);
        CDOTransaction transaction = session.openTransaction();
        Resource resource = transaction.getResource(resourcePath, true);

        NodeB root = (NodeB)resource.getContents().get(0);
        assertEquals("root", root.getName());

        NodeB B = getElementFromGraphNodeB(root, "B");
        NodeB E = getElementFromGraphNodeB(root, "E");

        assertEquals("B", B.getName());
        assertEquals("E", E.getName());

        E.getChildren().add(B);

        try
        {
          transaction.commit();
        }
        catch (CommitException ex)
        {
          exceptions.add(ex);
        }

        session.close();
        msg("Finished Thread A " + session);
      }
      catch (Exception e)
      {
        exceptions.add(e);
      }
    }
  }

  /**
   * @author Martin Fluegge
   */
  private class ThreadB extends AbstactTestThread
  {
    private CDOSession session;

    public ThreadB(String resourcePath)
    {
      super(resourcePath);
      msg("Starting Thread B");
      session = (CDOSession)openSession(REPOSITORY_NAME);
      idSessionB = session.getSessionID();
    }

    @Override
    public void run()
    {
      try
      {
        msg("Started Thread B " + session);
        CDOTransaction transaction = session.openTransaction();

        Resource resource = transaction.getResource(resourcePath, true);

        NodeB root = (NodeB)resource.getContents().get(0);
        assertEquals("root", root.getName());
        NodeB C = getElementFromGraphNodeB(root, "C");
        NodeB D = getElementFromGraphNodeB(root, "D");

        assertEquals("C", C.getName());
        assertEquals("D", D.getName());

        C.getChildren().add(D);

        try
        {
          transaction.commit();
        }
        catch (CommitException ex)
        {
          try
          {
            Exception ex1 = (Exception)ex.getCause();
            Exception ex2 = (Exception)ex1.getCause();
            Exception ex3 = (Exception)ex2.getCause();

            if (ex3 == null || !(ex3 instanceof ContainmentCycleDetectedException))
            {
              throw new RuntimeException(ex1);
            }

            msg("Finished (Passed) Thread B " + session);
            return;
          }
          catch (Exception exx)
          {
            throw new RuntimeException(exx);
          }
        }

        exceptions.add(new ThreadBShouldHaveThrownAnExceptionException("Thread B should have thrown an exception"));
        session.close();
      }
      catch (Exception e)
      {
        exceptions.add(e);
        msg("Finished Thread B " + session);
      }
    }
  }

  /**
   * @author Martin Fluegge
   */
  private class ThreadX extends AbstactTestThread
  {
    private CDOSession session;

    public ThreadX(String resourcePath)
    {
      super(resourcePath);
      msg("Starting Thread X");
      session = (CDOSession)openSession(REPOSITORY_NAME);
      idSessionB = session.getSessionID();
    }

    @Override
    public void run()
    {
      try
      {
        msg("Started Thread X" + session);
        CDOTransaction transaction = session.openTransaction();
        Resource resource = transaction.getResource(resourcePath, true);

        NodeB root = (NodeB)resource.getContents().get(0);
        assertEquals("root", root.getName());

        NodeB D = getElementFromGraphNodeB(root, "D");

        assertEquals("D", D.getName());
        D.setName("DD");

        try
        {
          transaction.commit();
        }
        catch (CommitException ex)
        {
          exceptions.add(ex);
        }

        session.close();
        msg("Finished Thread X");
      }
      catch (Exception e)
      {
        exceptions.add(e);
      }
    }
  }

  private NodeB createSimpleNode(String name)
  {
    NodeB y = getModel3Factory().createNodeB();
    y.setName(name);
    return y;
  }

  private NodeB getElementFromGraphNodeB(NodeB node, String name)
  {
    if (node.getName().equals(name))
    {
      return node;
    }

    for (NodeB child : node.getChildren())
    {
      NodeB elementFromGraph = getElementFromGraphNodeB(child, name);
      if (elementFromGraph != null)
      {
        return elementFromGraph;
      }
    }

    return null;
  }

  @SuppressWarnings("unused")
  private NodeA getElementFromGraphNodeA(NodeA node, String name)
  {
    if (node.getName().equals(name))
    {
      return node;
    }

    for (NodeA child : node.getChildren())
    {
      NodeA elementFromGraph = getElementFromGraphNodeA(child, name);
      if (elementFromGraph != null)
      {
        return elementFromGraph;
      }
    }

    return null;
  }

  @SuppressWarnings("unused")
  private void restartRepository()
  {
    LifecycleUtil.deactivate(getRepository());
    createRepository();
    getRepository();
  }

  /**
   * @author Martin Fluegge
   */
  private static class ThreadBShouldHaveThrownAnExceptionException extends Exception
  {
    private static final long serialVersionUID = 1L;

    public ThreadBShouldHaveThrownAnExceptionException(String s)
    {
      super(s);
    }
  }
}

Back to the top