INT-1040, added ResrWriteLock usage to the toString() and hashCode() methods, added Test

This commit is contained in:
Oleg Zhurakousky
2010-03-17 23:59:08 +00:00
parent 82da6be6e2
commit 43fa9015c1
2 changed files with 89 additions and 3 deletions

View File

@@ -30,8 +30,10 @@ import org.springframework.integration.support.ComponentMetadata;
* Iterable list of {@link MessageHistoryEvent} instances.
*
* @author Mark Fisher
* @author Oleg Zhurakousky
* @since 2.0
*/
@SuppressWarnings("serial")
public class MessageHistory implements Iterable<MessageHistoryEvent>, Serializable {
private final List<MessageHistoryEvent> events = new ArrayList<MessageHistoryEvent>();
@@ -76,14 +78,26 @@ public class MessageHistory implements Iterable<MessageHistoryEvent>, Serializab
}
public int hashCode() {
return 17 * this.events.hashCode();
try {
this.lock.readLock().lock();
return 17 * this.events.hashCode();
}
finally {
this.lock.readLock().unlock();
}
}
/**
* Returns a String representation of the history event list.
*/
public String toString() {
return this.events.toString();
try {
this.lock.readLock().lock();
return this.events.toString();
}
finally {
this.lock.readLock().unlock();
}
}
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.history;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.springframework.integration.support.ComponentMetadata;
/**
* @author Oleg Zhurakousky
* @since 2.0
*/
public class MessageHistoryTests {
private long times = 1000;
private ExecutorService executor = Executors.newCachedThreadPool();
@Test
public void testConcurrentModificationsOnObjectMethods() throws Exception{
final MessageHistory history = new MessageHistory();
executor.execute(new Runnable() {
public void run() {
for (int i = 0; i < times; i++) {
ComponentMetadata event = new ComponentMetadata();
event.setAttribute("foo", "foo");
event.setAttribute("bar", "bar");
event.setComponentName("MessageHistoryTests");
history.addEvent(event);
}
}
});
executor.execute(new Runnable() {
public void run() {
for (int i = 0; i < times; i++) {
history.toString();
}
}
});
executor.execute(new Runnable() {
public void run() {
for (int i = 0; i < times; i++) {
history.hashCode();
}
}
});
executor.execute(new Runnable() {
public void run() {
for (int i = 0; i < times; i++) {
history.equals(new Object());
}
}
});
executor.shutdown();
executor.awaitTermination(3, TimeUnit.SECONDS);
}
}