Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d12b60d045b2268b6dcc6f6db53be0751902232c (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
/*
 * Copyright (c) 2014 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;

import org.eclipse.emf.cdo.CDOLock;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.tests.model1.SalesOrder;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;

import org.eclipse.net4j.util.io.IOUtil;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @author Eike Stepper
 */
public class LockingSequenceTest extends AbstractLockingTest
{
  private static final int USERS = 10;

  private static final int ALLOCATIONS = 30;

  private static final int RETRIES = 5;

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

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

  public void testSafeCounter() throws Exception
  {
    disableConsole();
    final SalesOrder sequence = getModel1Factory().createSalesOrder();

    final CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    CDOResource resource = transaction.createResource(getResourcePath("/res1"));
    resource.getContents().add(sequence);
    transaction.commit();

    CountDownLatch latch = new CountDownLatch(USERS);
    User[] users = new User[USERS];

    for (int userID = 0; userID < USERS; userID++)
    {
      users[userID] = new User(userID, latch, sequence);
    }

    for (int userID = 0; userID < USERS; userID++)
    {
      users[userID].start();
    }

    latch.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
    IOUtil.OUT().println("FINISHED");

    Exception exception = null;
    for (int userID = 0; userID < USERS; userID++)
    {
      Exception ex = users[userID].getException();
      if (ex != null)
      {
        exception = ex;
        ex.printStackTrace();
      }
    }

    if (exception != null)
    {
      throw exception;
    }

    IOUtil.OUT().println("SUCCESS");
  }

  /**
   * @author Eike Stepper
   */
  private static final class User extends Thread
  {
    private final CountDownLatch latch;

    private final CDOTransaction transaction;

    private final SalesOrder sequence;

    private Exception exception;

    public User(int userID, CountDownLatch latch, SalesOrder sequence)
    {
      super("User" + userID);
      this.latch = latch;
      transaction = CDOUtil.getCDOObject(sequence).cdoView().getSession().openTransaction();
      this.sequence = transaction.getObject(sequence);
    }

    public Exception getException()
    {
      return exception;
    }

    @Override
    public void run()
    {
      try
      {
        CDOLock lock = CDOUtil.getCDOObject(sequence).cdoWriteLock();
        for (int allocation = 0; allocation < ALLOCATIONS; allocation++)
        {
          for (int retry = 0; retry < RETRIES; retry++)
          {
            try
            {
              lock.lock(1000);
              break;
            }
            catch (TimeoutException ex)
            {
              if (retry == RETRIES)
              {
                exception = ex;
                return;
              }

              msg("Lock timed out. Trying again...");
            }
          }

          int id = sequence.getId() + 1;
          sequence.setId(id);
          msg("Allocated " + id);

          try
          {
            transaction.commit();
            yield();
          }
          catch (Exception ex)
          {
            exception = ex;
            return;
          }
        }
      }
      finally
      {
        transaction.close();
        latch.countDown();
      }
    }

    private void msg(String string)
    {
      IOUtil.OUT().println(getName() + ": " + string);
    }
  }
}

Back to the top