View Javadoc

1   /*
2    * SymmetricDS is an open source database synchronization solution.
3    *   
4    * Copyright (C) Chris Henson <chenson42@users.sourceforge.net>
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 3 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, see
18   * <http://www.gnu.org/licenses/>.
19   */
20  package org.jumpmind.symmetric.service.impl;
21  
22  import org.jumpmind.symmetric.common.Constants;
23  import org.jumpmind.symmetric.model.Node;
24  import org.jumpmind.symmetric.service.IClusterService;
25  import org.jumpmind.symmetric.service.LockAction;
26  import org.jumpmind.symmetric.test.AbstractDatabaseTest;
27  import org.junit.Test;
28  
29  public class ClusterServiceTest extends AbstractDatabaseTest {
30  
31      public ClusterServiceTest() throws Exception {
32          super();
33      }
34  
35      public ClusterServiceTest(String dbName) {
36          super(dbName);
37      }
38  
39      @Test
40      public void testLock() throws Exception {
41          final IClusterService service = (IClusterService) find(Constants.CLUSTER_SERVICE);
42          assertTrue(service.lock(LockAction.PURGE_INCOMING), "Could not lock for PURGE");
43          assertEquals(countActivePurgeLocks(), 1, "Could not find the lock in the database.");
44          assertFalse(service.lock(LockAction.PURGE_INCOMING), "Should not have been able to lock for PURGE");
45          service.unlock(LockAction.PURGE_INCOMING);
46          assertEquals(countActivePurgeLocks(), 0, "Could not find the lock in the database.");
47      }
48  
49      @Test
50      public void testOtherNodeLock() throws Exception {
51          final String ID_ONE = "00020";
52          final String ID_TWO = "00010";
53          final Node nodeOne = new Node();
54          nodeOne.setNodeId(ID_ONE);
55  
56          final Node nodeTwo = new Node();
57          nodeTwo.setNodeId(ID_TWO);
58  
59          final IClusterService service = (IClusterService) find(Constants.CLUSTER_SERVICE);
60          service.initLockTable(LockAction.OTHER, ID_ONE);
61          service.initLockTable(LockAction.OTHER, ID_TWO);
62          assertTrue(service.lock(LockAction.OTHER, nodeOne), "Could not lock for OTHER " + ID_ONE);
63          assertFalse(service.lock(LockAction.OTHER, nodeOne), "Should not have been able to lock for OTHER "
64                  + ID_ONE);
65          assertTrue(service.lock(LockAction.OTHER, nodeTwo), "Could not lock for OTHER " + ID_TWO);
66          service.unlock(LockAction.OTHER, nodeOne);
67          assertTrue(service.lock(LockAction.OTHER, nodeOne), "Could not lock for OTHER " + ID_ONE);
68      }
69  
70      private int countActivePurgeLocks() {
71          return getJdbcTemplate().queryForInt(
72                  "select count(*) from sym_lock where lock_id=? and lock_action=? and lock_time is not null",
73                  new Object[] { ClusterService.COMMON_LOCK_ID, LockAction.PURGE_INCOMING.name() });
74      }
75  }