View Javadoc

1   /*
2    * Copyright 2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.ken.services.impl;
17  
18  import org.junit.Test;
19  import org.kuali.rice.ken.bo.NotificationMessageDelivery;
20  import org.kuali.rice.ken.service.NotificationMessageDeliveryAutoRemovalService;
21  import org.kuali.rice.ken.service.ProcessingResult;
22  import org.kuali.rice.ken.test.KENTestCase;
23  import org.kuali.rice.ken.util.NotificationConstants;
24  
25  import java.util.Collection;
26  import java.util.HashMap;
27  
28  import static org.junit.Assert.assertEquals;
29  import static org.junit.Assert.assertTrue;
30  
31  //import org.kuali.rice.core.jpa.criteria.Criteria;
32  
33  /**
34   * Tests NotificationMessageDeliveryAutoRemovalServiceImpl
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   */
37  // deadlocks are detected during clear database lifecycle (even when select for update is commented out...)
38  public class NotificationMessageDeliveryAutoRemovalServiceImplTest extends KENTestCase {
39  	// NOTE: this value is highly dependent on test data 
40  	private static final int EXPECTED_SUCCESSES = 6;
41  
42  	protected void assertProcessResults() {
43  		// one error should have occurred and the delivery should have been marked unlocked again
44  		Collection<NotificationMessageDelivery> lockedDeliveries = services.getNotificationMessegDeliveryDao().getLockedDeliveries(NotificationMessageDelivery.class, services.getGenericDao());
45  
46  		assertEquals(0, lockedDeliveries.size());
47  
48  		// should be 1 autoremoved delivery
49  		HashMap<String, String> queryCriteria = new HashMap<String, String>();
50  		queryCriteria.put(NotificationConstants.BO_PROPERTY_NAMES.MESSAGE_DELIVERY_STATUS, NotificationConstants.MESSAGE_DELIVERY_STATUS.AUTO_REMOVED);
51  		Collection<NotificationMessageDelivery> unprocessedDeliveries = services.getGenericDao().findMatching(NotificationMessageDelivery.class, queryCriteria);
52  		assertEquals(EXPECTED_SUCCESSES, unprocessedDeliveries.size());
53  	}
54  
55  	/**
56  	 * Test auto-removal message deliveries
57  	 */
58  	@Test
59  	public void testAutoRemovedNotificationMessageDeliveries() {
60  		NotificationMessageDeliveryAutoRemovalService nSvc = services.getNotificationMessageDeliveryAutoRemovalService();
61  
62  		services.getNotificationMessageDeliveryResolverService().resolveNotificationMessageDeliveries();
63  
64  		ProcessingResult result = nSvc.processAutoRemovalOfDeliveredNotificationMessageDeliveries();
65  
66  		assertEquals(0, result.getFailures().size());
67  		assertEquals(EXPECTED_SUCCESSES, result.getSuccesses().size());
68  
69  		assertProcessResults();
70  	}
71  
72  	/**
73  	 * Test concurrent auto-removal of message deliveries
74  	 */
75  	@Test
76  	public void testAutoRemovalConcurrency() throws InterruptedException {
77  		final NotificationMessageDeliveryAutoRemovalService nSvc = services.getNotificationMessageDeliveryAutoRemovalService();
78  
79  		services.getNotificationMessageDeliveryResolverService().resolveNotificationMessageDeliveries();
80  
81  		final ProcessingResult[] results = new ProcessingResult[2];
82  		Thread t1 = new Thread(new Runnable() {
83  			public void run() {
84  				results[0] = nSvc.processAutoRemovalOfDeliveredNotificationMessageDeliveries();
85  			}
86  		});
87  		Thread t2 = new Thread(new Runnable() {
88  			public void run() {
89  				results[1] = nSvc.processAutoRemovalOfDeliveredNotificationMessageDeliveries();
90  			}
91  		});
92  
93  		t1.start();
94  		t2.start();
95  
96  		t1.join();
97  		t2.join();
98  
99  		// assert that ONE of the autoremovers got all the items, and the other got NONE of the items
100 		LOG.info("Results of thread #1: " + results[0]);
101 		LOG.info("Results of thread #2: " + results[1]);
102 		assertTrue((results[0].getSuccesses().size() == EXPECTED_SUCCESSES && results[0].getFailures().size() == 0 && results[1].getSuccesses().size() == 0 && results[1].getFailures().size() == 0) ||
103 				(results[1].getSuccesses().size() == EXPECTED_SUCCESSES && results[1].getFailures().size() == 0 && results[0].getSuccesses().size() == 0 && results[0].getFailures().size() == 0));
104 
105 		assertProcessResults();
106 	}
107 }