Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 95c1a9ba1463f663b021f16b653ad5f55cf11398 (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
/*
 * Copyright (c) 2004-2013 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.emf.cdo.tests.bugzilla;

import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.common.lock.CDOLockState;
import org.eclipse.emf.cdo.common.lock.IDurableLockingManager.LockGrade;
import org.eclipse.emf.cdo.server.StoreThreadLocal;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.spi.server.InternalSession;
import org.eclipse.emf.cdo.tests.AbstractLockingTest;
import org.eclipse.emf.cdo.tests.model1.Category;
import org.eclipse.emf.cdo.tests.model1.Company;
import org.eclipse.emf.cdo.util.CDOUtil;

import org.eclipse.net4j.util.concurrent.IRWLockManager.LockType;

import org.eclipse.emf.spi.cdo.InternalCDOTransaction;

import org.eclipse.core.runtime.NullProgressMonitor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;

/**
 * Test ensuring that locks are correctly released on deleted objects.
 * @author Alex Lagarde
 */
public class Bugzilla_423699_Test extends AbstractLockingTest
{

  private CDOSession session;

  private InternalCDOTransaction tx;

  private CDOObject container;

  private CDOObject child;

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

    // Open a session and a transaction
    session = openSession();
    tx = (InternalCDOTransaction)session.openTransaction();

    // Create semantic model
    Company company = getModel1Factory().createCompany();
    Category category = getModel1Factory().createCategory();
    company.getCategories().add(category);
    tx.getOrCreateResource(getResourcePath("testResource")).getContents().add(company);
    tx.commit();

    container = CDOUtil.getCDOObject(company);
    child = CDOUtil.getCDOObject(category);
  }

  @Override
  protected void doTearDown() throws Exception
  {
    tx.close();
    tx = null;

    session.close();
    session = null;
    super.doTearDown();
  }

  public void testUnlockDeletedElementsWithDurableLockingAndAutoReleaseLocks() throws Exception
  {
    doTestUnlockDeletedElements(true, true);
  }

  public void testUnlockDeletedElementsWithDurableLockingAndNoAutoReleaseLocks() throws Exception
  {
    doTestUnlockDeletedElements(true, false);
  }

  public void testUnlockDeletedElementsWithNoDurableLockingAndAutoReleaseLocks() throws Exception
  {
    doTestUnlockDeletedElements(false, true);
  }

  public void testUnlockDeletedElementsWithNoDurableLockingAndNoAutoReleaseLocks() throws Exception
  {
    doTestUnlockDeletedElements(false, false);
  }

  /**
   * Ensures that locks are correctly released on deleted objects.
   * @param durableLocking indicates if durableLocking should be activated or not
   * @param autoReleaseLocksEnabled indicates if locks should be release on commit or not
   * @throws Exception if issue occurs while locking or committing elements
   */
  private void doTestUnlockDeletedElements(boolean durableLocking, boolean autoReleaseLocksEnabled) throws Exception
  {
    // Step 1: update transaction options
    tx.options().setAutoReleaseLocksEnabled(autoReleaseLocksEnabled);
    if (durableLocking)
    {
      tx.enableDurableLocking();
    }
    CDOID containerID = container.cdoID();
    CDOID childID = child.cdoID();

    // Step 2: lock the root and its child
    Collection<CDOObject> objectsToLock = new LinkedHashSet<CDOObject>();
    objectsToLock.add(container);
    objectsToLock.add(child);
    tx.lockObjects(objectsToLock, LockType.WRITE, 10000);
    assertIsLocked(durableLocking, true, containerID);
    assertIsLocked(durableLocking, true, childID);

    // Step 3: delete child from its container
    ((Company)CDOUtil.getEObject(container)).getCategories().clear();

    // Step 4: commit
    tx.commit(new NullProgressMonitor());
    // Lock should be deleted on detached object
    assertIsLocked(durableLocking, false, childID);
    // Lock should be deleted only if lock autorelease is enabled
    assertIsLocked(durableLocking, !autoReleaseLocksEnabled, containerID);

    // Step 5 (optional): reopen transaction & session with the same durable locking ID
    if (durableLocking)
    {
      String durableLockingID = tx.getDurableLockingID();
      tx.close();
      session.close();
      session = openSession();
      tx = (InternalCDOTransaction)session.openTransaction(durableLockingID);
      tx.options().setAutoReleaseLocksEnabled(autoReleaseLocksEnabled);
      // Lock states should not have changed
      assertIsLocked(durableLocking, false, childID);
      assertIsLocked(durableLocking, !autoReleaseLocksEnabled, containerID);
    }

    // Step 6: unlock all elements
    tx.unlockObjects();
    assertIsLocked(durableLocking, false, containerID);
    assertIsLocked(durableLocking, false, childID);
  }

  /**
   * Ensures that the given element is locked or not (according to the given parameter), durably or not (according to the given parameter).
   * @param durably indicates if we expect a durable lock or not
   * @param shouldBeLocked indicates if elements should be locked or not
   * @param elementID {@link CDOID} of the element ot test
   */
  private void assertIsLocked(boolean durably, boolean shouldBeLocked, CDOID elementID)
  {
    // Step 1: check durable lock
    if (durably)
    {
      LockGrade durableLock = null;
      try
      {
        InternalSession session = getRepository().getSessionManager().getSession(tx.getSessionID());
        StoreThreadLocal.setSession(session);
        // Do your call
        durableLock = getRepository().getLockingManager().getLockArea(tx.getDurableLockingID()).getLocks()
            .get(elementID);
      }
      finally
      {
        StoreThreadLocal.release();
      }
      assertEquals(elementID + " has not the expected durable lock status", shouldBeLocked, durableLock != null);
    }

    // Step 2: check lock
    ArrayList<CDOID> elementIDs = new ArrayList<CDOID>();
    elementIDs.add(elementID);
    CDOLockState cdoLockState = tx.getLockStates(elementIDs)[0];
    assertEquals(elementID + " has wrong lock status", shouldBeLocked, cdoLockState.getWriteLockOwner() != null);
  }
}

Back to the top