1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 }