Skip to content

Commit e8f4a02

Browse files
committed
cleaned the tests & put things in order...
1 parent f3d9efc commit e8f4a02

File tree

7 files changed

+106
-155
lines changed

7 files changed

+106
-155
lines changed

src/main/java/com/antkorwin/xsync/XMutex.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@
33
import lombok.EqualsAndHashCode;
44
import lombok.Getter;
55

6-
import java.util.Objects;
7-
86
/**
9-
* Created by Korovin Anatolii on 14.06.2018.
7+
* Created on 14.06.2018.
108
*
11-
* @author Korovin Anatolii
12-
* @version 1.0
9+
* @author Korovin Anatoliy
1310
*/
1411
@EqualsAndHashCode
1512
@Getter
@@ -24,5 +21,4 @@ public XMutex(KeyT key) {
2421
public static <KeyT> XMutex<KeyT> of(KeyT key) {
2522
return new XMutex<>(key);
2623
}
27-
2824
}

src/main/java/com/antkorwin/xsync/XMutexFactory.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@
77
import java.util.WeakHashMap;
88

99
/**
10-
* Created by Korovin Anatolii on 14.06.2018.
10+
* Created on 14.06.2018.
1111
*
12-
* @author Korovin Anatolii
13-
* @version 1.0
12+
* @author Korovin Anatoliy
1413
*/
1514
public class XMutexFactory<KeyT> {
1615

1716
public final Map<XMutex<KeyT>, WeakReference<XMutex<KeyT>>> weakHashMap =
1817
Collections.synchronizedMap(new WeakHashMap<XMutex<KeyT>, WeakReference<XMutex<KeyT>>>());
1918

20-
19+
/**
20+
* Creates and returns a mutex by the key.
21+
* If the mutex for this key already exists in the weak-map, then returns the same reference of the mutex.
22+
*/
2123
public XMutex<KeyT> getMutex(KeyT key) {
2224
synchronized (weakHashMap) {
2325
validateKey(key);
@@ -48,6 +50,9 @@ private XMutex<KeyT> saveNewReference(KeyT key) {
4850
return mutex;
4951
}
5052

53+
/**
54+
* @return count of mutexes in this factory.
55+
*/
5156
public long size() {
5257
return weakHashMap.size();
5358
}

src/main/java/com/antkorwin/xsync/XSync.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
package com.antkorwin.xsync;
22

33
/**
4-
* Created by Korovin Anatolii on 18.06.2018.
4+
* Created on 18.06.2018.
55
*
6-
* @author Korovin Anatolii
7-
* @version 1.0
6+
* @author Korovin Anatoliy
87
*/
98
public class XSync<KeyT> {
109

1110
private XMutexFactory<KeyT> mutexFactory = new XMutexFactory<>();
1211

12+
/**
13+
* Executes a runnable in a synchronization block on a mutex,
14+
* which created from the mutexKey value.
15+
*
16+
* @param mutexKey key for the synchronization locks
17+
* @param runnable function that we need to run
18+
*/
1319
public void execute(KeyT mutexKey, Runnable runnable) {
1420
XMutex<KeyT> mutex = mutexFactory.getMutex(mutexKey);
1521
synchronized (mutex) {

src/main/resources/application.properties

Whitespace-only changes.

src/test/java/com/antkorwin/xsync/XMutexFactoryTest.java

Lines changed: 64 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
package com.antkorwin.xsync;
22

33
import org.assertj.core.api.Assertions;
4-
import org.hamcrest.Matchers;
54
import org.junit.Test;
65

7-
import java.util.Collections;
8-
import java.util.List;
9-
import java.util.Set;
10-
import java.util.UUID;
6+
import java.util.*;
117
import java.util.concurrent.ConcurrentHashMap;
128
import java.util.concurrent.TimeUnit;
139
import java.util.stream.Collectors;
1410
import java.util.stream.IntStream;
1511

1612
import static org.awaitility.Awaitility.await;
13+
import static org.hamcrest.Matchers.equalTo;
1714

1815
/**
19-
* Created by Korovin Anatolii on 17.06.2018.
16+
* Created on 17.06.2018.
2017
*
21-
* @author Korovin Anatolii
22-
* @version 1.0
18+
* @author Korovin Anatoliy
2319
*/
2420
public class XMutexFactoryTest {
2521

2622

23+
private static final String ID_STRING = "c117c526-606e-41b6-8197-1a6ba779f69b";
24+
private static final int NUMBER_OF_MUTEXES = 1000;
25+
private static final int NUMBER_OF_ITERATIONS = NUMBER_OF_MUTEXES * 100;
26+
2727
@Test
28-
public void testFirst() {
28+
public void testGetSameMutexFromTwoDifferentInstanceOfEqualsKeys() {
2929
// Arrange
3030
XMutexFactory<UUID> mutexFactory = new XMutexFactory<>();
31-
UUID firstId = UUID.fromString("c117c526-606e-41b6-8197-1a6ba779f69b");
32-
UUID secondId = UUID.fromString("c117c526-606e-41b6-8197-1a6ba779f69b");
31+
UUID firstId = UUID.fromString(ID_STRING);
32+
UUID secondId = UUID.fromString(ID_STRING);
3333
// Check precondition
3434
Assertions.assertThat(firstId != secondId).isTrue();
3535
Assertions.assertThat(firstId).isEqualTo(secondId);
@@ -46,55 +46,55 @@ public void testFirst() {
4646
}
4747

4848
@Test
49-
public void testSize() {
49+
public void testWithRunGCAfterReleaseFirstMutex() throws InterruptedException {
5050
// Arrange
5151
XMutexFactory<UUID> mutexFactory = new XMutexFactory<>();
52-
UUID firstId = UUID.fromString("c117c526-606e-41b6-8197-1a6ba779f69b");
53-
UUID secondId = UUID.fromString("c117c526-606e-41b6-8197-1a6ba779f69b");
54-
UUID thirdId = UUID.randomUUID();
52+
UUID firstId = UUID.fromString(ID_STRING);
53+
UUID secondId = UUID.fromString(ID_STRING);
5554

5655
// Act
5756
XMutex<UUID> firstMutex = mutexFactory.getMutex(firstId);
57+
int fisrtHashCode = System.identityHashCode(firstMutex);
58+
firstMutex = null;
59+
System.gc();
60+
await().atMost(5, TimeUnit.SECONDS)
61+
.until(mutexFactory::size, equalTo(0L));
62+
// Now, the weak-map of mutex factory is empty,
63+
// because all of the mutex references released
64+
Assertions.assertThat(mutexFactory.size()).isEqualTo(0);
65+
5866
XMutex<UUID> secondMutex = mutexFactory.getMutex(secondId);
59-
XMutex<UUID> thirdMutex = mutexFactory.getMutex(thirdId);
67+
int secondHashCode = System.identityHashCode(secondMutex);
6068

6169
// Asserts
62-
Assertions.assertThat(mutexFactory.size()).isEqualTo(2);
63-
Assertions.assertThat(firstMutex).isEqualTo(secondMutex);
64-
Assertions.assertThat(firstMutex).isNotEqualTo(thirdMutex);
70+
Assertions.assertThat(mutexFactory.size()).isEqualTo(1L);
71+
Assertions.assertThat(fisrtHashCode).isNotEqualTo(secondHashCode);
6572
}
6673

6774
@Test
68-
public void testWithGC() throws InterruptedException {
75+
public void testSizeOfMutexFactoryMap() {
6976
// Arrange
7077
XMutexFactory<UUID> mutexFactory = new XMutexFactory<>();
71-
UUID firstId = UUID.fromString("c117c526-606e-41b6-8197-1a6ba779f69b");
72-
UUID secondId = UUID.fromString("c117c526-606e-41b6-8197-1a6ba779f69b");
78+
UUID firstId = UUID.fromString(ID_STRING);
79+
UUID secondId = UUID.fromString(ID_STRING);
80+
UUID thirdId = UUID.randomUUID();
7381

7482
// Act
7583
XMutex<UUID> firstMutex = mutexFactory.getMutex(firstId);
76-
int fisrtHashCode = System.identityHashCode(firstMutex);
77-
firstMutex = null;
78-
System.gc();
79-
await().atMost(5, TimeUnit.SECONDS)
80-
.until(mutexFactory::size, Matchers.equalTo(0L));
81-
8284
XMutex<UUID> secondMutex = mutexFactory.getMutex(secondId);
83-
int secondHashCode = System.identityHashCode(secondMutex);
85+
XMutex<UUID> thirdMutex = mutexFactory.getMutex(thirdId);
8486

8587
// Asserts
86-
System.out.println(fisrtHashCode + " " + secondHashCode);
87-
Assertions.assertThat(mutexFactory.size()).isEqualTo(1L);
88-
Assertions.assertThat(fisrtHashCode).isNotEqualTo(secondHashCode);
88+
Assertions.assertThat(mutexFactory.size()).isEqualTo(2);
8989
}
9090

9191
@Test
92-
public void testHashCode() {
92+
public void testEqualityOfReturnedMutexesBySystemIdentityHashCode() {
9393
// Arrange
9494
XMutexFactory<UUID> mutexFactory = new XMutexFactory<>();
95-
UUID firstId = UUID.fromString("c117c526-606e-41b6-8197-1a6ba779f69b");
96-
UUID secondId = UUID.fromString("c117c526-606e-41b6-8197-1a6ba779f69b");
97-
UUID thirdId = UUID.fromString("c117c526-606e-41b6-8197-1a6ba779f69b");
95+
UUID firstId = UUID.fromString(ID_STRING);
96+
UUID secondId = UUID.fromString(ID_STRING);
97+
UUID thirdId = UUID.fromString(ID_STRING);
9898

9999
// Act
100100
XMutex<UUID> firstMutex = mutexFactory.getMutex(firstId);
@@ -114,70 +114,62 @@ public void testHashCode() {
114114
public void testALotOfHashCodes() {
115115
// Arrange
116116
XMutexFactory<UUID> mutexFactory = new XMutexFactory<>();
117-
UUID id = UUID.fromString("c117c526-606e-41b6-8197-1a6ba779f69b");
117+
Set<Integer> setOfHash = createConcurrentSet();
118+
List<XMutex<UUID>> resultReferences = new ArrayList<>();
118119

119120
// Act
120-
Set<Integer> hashs = Collections.newSetFromMap(new ConcurrentHashMap<Integer, Boolean>());
121-
for (int i = 0; i < 1000; i++) {
122-
XMutex<UUID> mutex = mutexFactory.getMutex(id);
123-
hashs.add(System.identityHashCode(mutex));
121+
for (int i = 0; i < NUMBER_OF_ITERATIONS; i++) {
122+
XMutex<UUID> mutex = mutexFactory.getMutex(UUID.fromString(ID_STRING));
123+
setOfHash.add(System.identityHashCode(mutex));
124+
// and now, we save reference in the list,
125+
// because the GC can delete a unused references:
126+
resultReferences.add(mutex);
124127
}
125128

126-
Assertions.assertThat(hashs.size()).isEqualTo(1);
129+
// Assertions
130+
Assertions.assertThat(setOfHash.size()).isEqualTo(1);
131+
Assertions.assertThat(resultReferences).hasSize(NUMBER_OF_ITERATIONS);
127132
}
128133

129-
@Test(timeout = 11000)
130-
public void testConcurrency() throws InterruptedException {
134+
private <TypeT> Set<TypeT> createConcurrentSet() {
135+
return Collections.newSetFromMap(new ConcurrentHashMap<TypeT, Boolean>());
136+
}
137+
138+
@Test(timeout = 15000)
139+
public void testConcurrency() {
131140
// Arrange
132141
XMutexFactory<UUID> mutexFactory = new XMutexFactory<>();
133142

134-
int endExclusive = 1000;
135-
136-
List<UUID> ids = IntStream.range(0, endExclusive)
143+
List<UUID> ids = IntStream.range(0, NUMBER_OF_MUTEXES)
137144
.boxed()
138145
.map(i -> UUID.randomUUID())
139146
.collect(Collectors.toList());
140-
System.out.println("idsSize = " + ids.size());
141147

142-
Set<XMutex<UUID>> results = Collections
143-
.newSetFromMap(new ConcurrentHashMap<XMutex<UUID>, Boolean>());
148+
Set<XMutex<UUID>> results = createConcurrentSet();
149+
Set<Integer> setOfHash = createConcurrentSet();
144150

145151
// Act
146-
// ExecutorService executorService = Executors.newFixedThreadPool(10);
147-
// IntStream.range(0, 10000)
148-
// .boxed()
149-
// .forEach(i -> {
150-
// executorService.submit(() -> {
151-
// UUID uuid = ids.get(i % endExclusive);
152-
// XMutex<UUID> mutex = mutexFactory.getMutex(uuid);
153-
// Assertions.assertThat(mutex).isNotNull();
154-
// results.add(mutex);
155-
// });
156-
// });
157-
158-
IntStream.range(0, 10000)
152+
IntStream.range(0, NUMBER_OF_ITERATIONS)
159153
.boxed()
160154
.parallel()
161155
.forEach(i -> {
162-
UUID uuid = ids.get(i % endExclusive);
156+
UUID uuid = ids.get(i % NUMBER_OF_MUTEXES);
163157
XMutex<UUID> mutex = mutexFactory.getMutex(uuid);
164-
Assertions.assertThat(mutex).isNotNull();
165158
results.add(mutex);
159+
setOfHash.add(System.identityHashCode(mutex));
166160
});
167161

168162
await().atMost(10, TimeUnit.SECONDS)
169-
.until(results::size, Matchers.equalTo(endExclusive));
170-
171-
System.out.println("res= " + results.size());
172-
System.out.println("mf= " + mutexFactory.size());
163+
.until(results::size, equalTo(NUMBER_OF_MUTEXES));
173164

174165
// Asserts
175-
Assertions.assertThat(results).hasSize(endExclusive);
176-
Assertions.assertThat(mutexFactory.size()).isEqualTo(endExclusive);
166+
Assertions.assertThat(results).hasSize(NUMBER_OF_MUTEXES);
167+
Assertions.assertThat(setOfHash).hasSize(NUMBER_OF_MUTEXES);
168+
Assertions.assertThat(mutexFactory.size()).isEqualTo(NUMBER_OF_MUTEXES);
177169
}
178170

179171
@Test
180-
public void testWithNullKey() {
172+
public void testExceptionThrownWhenTryToGetMutexWithNullKey() {
181173
// Arrange
182174
XMutexFactory<UUID> mutexFactory = new XMutexFactory<>();
183175
// Act

src/test/java/com/antkorwin/xsync/XMutexTest.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,33 @@
77
import java.util.WeakHashMap;
88

99
/**
10-
* Created by Korovin Anatolii on 17.06.2018.
10+
* Created on 17.06.2018.
1111
*
12-
* @author Korovin Anatolii
13-
* @version 1.0
12+
* @author Korovin Anatoliy
1413
*/
1514
public class XMutexTest {
1615

16+
private final String FIRST_KEY = new String("111");
17+
private final String SECOND_KEY = new String("111");
1718

1819
@Test
1920
public void testMutexEquals() {
2021
// Arrange
21-
String key1 = new String("111");
22-
String key2 = new String("111");
23-
XMutex<String> mutex1 = new XMutex<>(key1);
24-
XMutex<String> mutex2 = new XMutex<>(key2);
22+
XMutex<String> mutex1 = new XMutex<>(FIRST_KEY);
23+
XMutex<String> mutex2 = new XMutex<>(SECOND_KEY);
24+
2525
// Act & Assert
26-
Assertions.assertThat(key1 != key2).isTrue();
26+
Assertions.assertThat(FIRST_KEY != SECOND_KEY).isTrue();
2727
Assertions.assertThat(mutex1).isEqualTo(mutex2);
2828
}
2929

3030
@Test
31-
public void testEqualsAndMap() {
31+
public void testWeakMapWithTwoEqualMutexes() {
3232
// Arrange
33+
XMutex<String> mutex1 = new XMutex<>(FIRST_KEY);
34+
XMutex<String> mutex2 = new XMutex<>(SECOND_KEY);
35+
3336
WeakHashMap<XMutex<String>, WeakReference<XMutex<String>>> map = new WeakHashMap<>();
34-
String key1 = new String("111");
35-
String key2 = new String("111");
36-
XMutex<String> mutex1 = new XMutex<>(key1);
37-
XMutex<String> mutex2 = new XMutex<>(key2);
3837

3938
// Act
4039
map.put(mutex1, new WeakReference<>(mutex1));

0 commit comments

Comments
 (0)