Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6c741957b1c056f28165ab29fad16061a221184a (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
/**
 * Copyright (c) 2004 - 2011 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:
 *    Caspar De Groot - initial API and implementation
 */
package org.eclipse.emf.cdo.tests.offline;

import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.common.lock.CDOLockChangeInfo.Operation;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.internal.server.syncing.OfflineClone;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.session.CDOSessionInvalidationEvent;
import org.eclipse.emf.cdo.session.CDOSessionLocksChangedEvent;
import org.eclipse.emf.cdo.spi.server.InternalRepository;
import org.eclipse.emf.cdo.tests.AbstractSyncingTest;
import org.eclipse.emf.cdo.tests.model1.Company;
import org.eclipse.emf.cdo.tests.util.TestListener2;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;
import org.eclipse.emf.cdo.util.CommitException;
import org.eclipse.emf.cdo.view.CDOView;
import org.eclipse.emf.cdo.view.CDOViewLocksChangedEvent;

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

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

/**
 * @author Caspar De Groot
 */
public class OfflineLockingTest extends AbstractSyncingTest
{
  public void testLockAndUnlockThrough() throws Exception
  {
    assertEquals(true, getRepository("repo1") instanceof OfflineClone);

    CDOSession masterSession = openSession("master");
    CDOSession cloneSession = openSession("repo1");
    waitForOnline(cloneSession.getRepositoryInfo());

    CDOTransaction cloneTx = cloneSession.openTransaction();
    cloneTx.enableDurableLocking(true);

    CDOResource res = cloneTx.createResource(getResourcePath("test"));
    Company company = getModel1Factory().createCompany();
    res.getContents().add(company);
    cloneTx.commit();
    CDOObject cdoCompany = CDOUtil.getCDOObject(company);

    CDOView masterView = masterSession.openView();
    masterView.options().setLockNotificationEnabled(true);
    TestListener2 masterViewListener = new TestListener2(CDOViewLocksChangedEvent.class);
    masterView.addListener(masterViewListener);
    CDOObject cdoCompanyOnMaster = masterView.getObject(cdoCompany.cdoID());

    cdoCompany.cdoWriteLock().lock();
    masterViewListener.waitFor(1);
    assertEquals(true, cdoCompanyOnMaster.cdoWriteLock().isLockedByOthers());

    cdoCompany.cdoWriteLock().unlock();
    masterViewListener.waitFor(2);
    assertEquals(false, cdoCompanyOnMaster.cdoWriteLock().isLockedByOthers());

    cloneSession.close();
    masterSession.close();
  }

  public void testCloneLocks_arrivalInOtherClone() throws Exception
  {
    // Create a 2nd clone repository
    assertEquals(true, getRepository("repo1") instanceof OfflineClone);
    assertEquals(true, getRepository("repo2") instanceof OfflineClone);

    CDOSession clone1Session = openSession("repo1");
    TestListener2 session1lockListener = new TestListener2(CDOSessionLocksChangedEvent.class, "session1lockListener");
    clone1Session.addListener(session1lockListener);
    waitForOnline(clone1Session.getRepositoryInfo());

    CDOSession clone2Session = openSession("repo2");
    TestListener2 session2invalidationListener = new TestListener2(CDOSessionInvalidationEvent.class,
        "session2invalidationListener");
    clone2Session.addListener(session2invalidationListener);
    TestListener2 session2lockListener = new TestListener2(CDOSessionLocksChangedEvent.class, "session2lockListener");
    clone2Session.addListener(session2lockListener);
    waitForOnline(clone2Session.getRepositoryInfo());

    CDOTransaction clone1Tx = openTransaction(clone1Session);

    CDOResource resourceInSession1 = clone1Tx.createResource(getResourcePath("test"));
    Company companyA = getModel1Factory().createCompany();
    Company companyB = getModel1Factory().createCompany();
    Company companyC = getModel1Factory().createCompany();
    resourceInSession1.getContents().add(companyA);
    resourceInSession1.getContents().add(companyB);
    resourceInSession1.getContents().add(companyC);
    clone1Tx.commit();

    CDOObject companyA_session1 = CDOUtil.getCDOObject(companyA);
    CDOObject companyB_session1 = CDOUtil.getCDOObject(companyB);
    CDOObject companyC_session1 = CDOUtil.getCDOObject(companyC);

    session2invalidationListener.setTimeout(Integer.MAX_VALUE);
    session2invalidationListener.waitFor(1); // Wait for the commit notification

    CDOTransaction clone2Tx = openTransaction(clone2Session);

    CDOResource resourceInSession2 = clone2Tx.getResource(getResourcePath("test"));
    CDOObject companyA_session2 = CDOUtil.getCDOObject(resourceInSession2.getContents().get(0));
    CDOObject companyB_session2 = CDOUtil.getCDOObject(resourceInSession2.getContents().get(1));
    CDOObject companyC_session2 = CDOUtil.getCDOObject(resourceInSession2.getContents().get(2));

    CDOSessionLocksChangedEvent e;

    // Verify that thusfar we haven't received any locking events in session 1
    assertEquals(0, session1lockListener.getEvents().size());

    // Perform the lock in session 2, connected to clone 2
    companyA_session2.cdoWriteLock().lock();

    // Wait for the lock notification in session 1, which is connected to clone 1
    session1lockListener.waitFor(1);

    e = (CDOSessionLocksChangedEvent)session1lockListener.getEvents().get(0);
    assertSame(LockType.WRITE, e.getLockType());
    assertSame(Operation.LOCK, e.getOperation());
    assertEquals(true, companyA_session1.cdoWriteLock().isLockedByOthers());

    // Perform the unlock in session 2, connected to clone 2
    companyA_session2.cdoWriteLock().unlock();

    // Wait for the lock notification in session 1, which is connected to clone 1
    session1lockListener.waitFor(2);

    e = (CDOSessionLocksChangedEvent)session1lockListener.getEvents().get(1);
    assertSame(LockType.WRITE, e.getLockType());
    assertSame(Operation.UNLOCK, e.getOperation());
    assertEquals(false, companyA_session1.cdoWriteLock().isLockedByOthers());

    // Now vice versa . . .

    session2lockListener.getEvents().clear();

    // Perform the lock in session 1, connected to clone 1
    companyA_session1.cdoWriteLock().lock();

    // Wait for the lock notification in session 2, which is connected to clone 2
    session2lockListener.waitFor(1);

    e = (CDOSessionLocksChangedEvent)session2lockListener.getEvents().get(0);
    assertSame(LockType.WRITE, e.getLockType());
    assertSame(Operation.LOCK, e.getOperation());
    assertEquals(true, companyA_session2.cdoWriteLock().isLockedByOthers());

    // Perform the unlock in session 1, connected to clone 1
    companyA_session1.cdoWriteLock().unlock();

    // Wait for the lock notification in session 1, which is connected to clone 1
    session2lockListener.waitFor(2);

    e = (CDOSessionLocksChangedEvent)session2lockListener.getEvents().get(1);
    assertSame(LockType.WRITE, e.getLockType());
    assertSame(Operation.UNLOCK, e.getOperation());
    assertEquals(false, companyA_session2.cdoWriteLock().isLockedByOthers());

    // Now try an unlock-all . . .

    session1lockListener.getEvents().clear();

    companyA_session2.cdoReadLock().lock();
    companyB_session2.cdoWriteLock().lock();
    companyC_session2.cdoWriteOption().lock();

    session1lockListener.waitFor(3);

    assertEquals(true, companyA_session1.cdoReadLock().isLockedByOthers());
    assertEquals(true, companyB_session1.cdoWriteLock().isLockedByOthers());
    assertEquals(true, companyC_session1.cdoWriteOption().isLockedByOthers());

    clone2Tx.unlockObjects();

    session1lockListener.waitFor(4);

    assertEquals(false, companyA_session1.cdoReadLock().isLockedByOthers());
    assertEquals(false, companyA_session1.cdoWriteLock().isLockedByOthers());
    assertEquals(false, companyA_session1.cdoWriteOption().isLockedByOthers());

    clone1Session.close();
    clone2Session.close();
  }

  public void testCloneLocks_replicationToOtherClone() throws CommitException
  {
    InternalRepository repo1 = getRepository("repo1");
    assertEquals(true, repo1 instanceof OfflineClone);
    InternalRepository repo2 = getRepository("repo2");
    assertEquals(true, repo2 instanceof OfflineClone);

    OfflineClone clone2 = (OfflineClone)repo2;

    waitForOnline(getRepository("repo1"));
    waitForOnline(getRepository("repo2"));

    CDOSession clone1session = openSession("repo1");

    // Store 3 objects in repo1
    CDOTransaction tx1_sess1 = openTransaction(clone1session);
    CDOResource resource_tx1_sess1 = tx1_sess1.createResource(getResourcePath("test"));
    Company companyA = getModel1Factory().createCompany();
    Company companyB = getModel1Factory().createCompany();
    Company companyC = getModel1Factory().createCompany();
    resource_tx1_sess1.getContents().add(companyA);
    resource_tx1_sess1.getContents().add(companyB);
    resource_tx1_sess1.getContents().add(companyC);
    tx1_sess1.commit();

    {
      // Verify that they're visible in repo2
      CDOSession clone2session = openSession("repo2");
      CDOTransaction tx1_sess2 = openTransaction(clone2session);
      CDOResource resource_tx1_sess2 = tx1_sess2.getResource(getResourcePath("test"));
      assertEquals(3, resource_tx1_sess2.getContents().size());
      tx1_sess2.close();
      clone2session.close();
    }

    clone2.goOffline();
    waitForOffline(clone2);

    // Lock the objects in repo1. Since repo1 is ONLINE, this will also lock them
    // in the master.
    CDOUtil.getCDOObject(companyA).cdoReadLock().lock();
    CDOUtil.getCDOObject(companyB).cdoWriteLock().lock();
    CDOUtil.getCDOObject(companyC).cdoWriteOption().lock();

    clone2.goOnline();
    waitForOnline(clone2);

    {
      // Verify that the locks are visible in repo2
      CDOSession clone2session = openSession("repo2");
      CDOTransaction tx1_sess2 = openTransaction(clone2session);
      CDOResource resource_tx1_sess2 = tx1_sess2.getResource(getResourcePath("test"));
      EList<EObject> contents = resource_tx1_sess2.getContents();
      CDOObject companyA_in_sess2 = CDOUtil.getCDOObject(contents.get(0));
      CDOObject companyB_in_sess2 = CDOUtil.getCDOObject(contents.get(1));
      CDOObject companyC_in_sess2 = CDOUtil.getCDOObject(contents.get(2));

      assertEquals(true, companyA_in_sess2.cdoReadLock().isLockedByOthers());
      assertEquals(true, companyB_in_sess2.cdoWriteLock().isLockedByOthers());
      assertEquals(true, companyC_in_sess2.cdoWriteOption().isLockedByOthers());

      tx1_sess2.close();
      clone2session.close();
    }

    clone2.goOffline();
    waitForOffline(clone2);

    // Unlock the objects in repo1. Since repo1 is ONLINE, this will also lock them
    // in the master.
    CDOUtil.getCDOObject(companyA).cdoReadLock().unlock();
    CDOUtil.getCDOObject(companyB).cdoWriteLock().unlock();
    CDOUtil.getCDOObject(companyC).cdoWriteOption().unlock();

    clone2.goOnline();
    waitForOnline(clone2);

    {
      // Verify in repo2
      CDOSession clone2session = openSession("repo2");
      CDOTransaction tx1_sess2 = openTransaction(clone2session);
      CDOResource resource_tx1_sess2 = tx1_sess2.getResource(getResourcePath("test"));
      EList<EObject> contents = resource_tx1_sess2.getContents();
      CDOObject companyA_in_sess2 = CDOUtil.getCDOObject(contents.get(0));
      CDOObject companyB_in_sess2 = CDOUtil.getCDOObject(contents.get(1));
      CDOObject companyC_in_sess2 = CDOUtil.getCDOObject(contents.get(2));

      assertEquals(false, companyA_in_sess2.cdoReadLock().isLockedByOthers());
      assertEquals(false, companyB_in_sess2.cdoWriteLock().isLockedByOthers());
      assertEquals(false, companyC_in_sess2.cdoWriteOption().isLockedByOthers());

      tx1_sess2.close();
      clone2session.close();
    }

    clone1session.close();
  }

  private static CDOTransaction openTransaction(CDOSession session)
  {
    CDOTransaction tx = session.openTransaction();
    tx.options().setLockNotificationEnabled(true);
    tx.enableDurableLocking(true);
    return tx;
  }
}

Back to the top