View Javadoc

1   /**
2    * Copyright 2005-2011 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.Ignore;
19  import org.junit.Test;
20  import org.kuali.rice.ken.bo.NotificationMessageDelivery;
21  import org.kuali.rice.ken.service.NotificationMessageDeliveryAutoRemovalService;
22  import org.kuali.rice.ken.service.ProcessingResult;
23  import org.kuali.rice.ken.test.KENTestCase;
24  import org.kuali.rice.ken.util.NotificationConstants;
25  import org.kuali.rice.test.BaselineTestCase;
26  
27  import java.util.Collection;
28  import java.util.HashMap;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.assertTrue;
32  
33  //import org.kuali.rice.core.jpa.criteria.Criteria;
34  
35  /**
36   * Tests NotificationMessageDeliveryAutoRemovalServiceImpl
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   */
39  // deadlocks are detected during clear database lifecycle (even when select for update is commented out...)
40  @Ignore
41  public class NotificationMessageDeliveryAutoRemovalServiceImplTest extends KENTestCase {
42  	// NOTE: this value is highly dependent on test data 
43  	private static final int EXPECTED_SUCCESSES = 6;
44  
45  	protected void assertProcessResults() {
46  		// one error should have occurred and the delivery should have been marked unlocked again
47  		Collection<NotificationMessageDelivery> lockedDeliveries = services.getNotificationMessegDeliveryDao().getLockedDeliveries(NotificationMessageDelivery.class, services.getGenericDao());
48  
49  		assertEquals(0, lockedDeliveries.size());
50  
51  		// should be 1 autoremoved delivery
52  		HashMap<String, String> queryCriteria = new HashMap<String, String>();
53  		queryCriteria.put(NotificationConstants.BO_PROPERTY_NAMES.MESSAGE_DELIVERY_STATUS, NotificationConstants.MESSAGE_DELIVERY_STATUS.AUTO_REMOVED);
54  		Collection<NotificationMessageDelivery> unprocessedDeliveries = services.getGenericDao().findMatching(NotificationMessageDelivery.class, queryCriteria);
55  		assertEquals(EXPECTED_SUCCESSES, unprocessedDeliveries.size());
56  	}
57  
58  	/**
59  	 * Test auto-removal message deliveries
60  	 */
61  	@Test
62  	public void testAutoRemovedNotificationMessageDeliveries() {
63  		NotificationMessageDeliveryAutoRemovalService nSvc = services.getNotificationMessageDeliveryAutoRemovalService();
64  
65  		services.getNotificationMessageDeliveryResolverService().resolveNotificationMessageDeliveries();
66  
67  		ProcessingResult result = nSvc.processAutoRemovalOfDeliveredNotificationMessageDeliveries();
68  
69  		assertEquals(0, result.getFailures().size());
70  		assertEquals(EXPECTED_SUCCESSES, result.getSuccesses().size());
71  
72  		assertProcessResults();
73  	}
74  
75  	/**
76  	 * Test concurrent auto-removal of message deliveries
77  	 */
78  	@Test
79  	public void testAutoRemovalConcurrency() throws InterruptedException {
80  		final NotificationMessageDeliveryAutoRemovalService nSvc = services.getNotificationMessageDeliveryAutoRemovalService();
81  
82  		services.getNotificationMessageDeliveryResolverService().resolveNotificationMessageDeliveries();
83  
84  		final ProcessingResult[] results = new ProcessingResult[2];
85  		Thread t1 = new Thread(new Runnable() {
86  			public void run() {
87  				results[0] = nSvc.processAutoRemovalOfDeliveredNotificationMessageDeliveries();
88  			}
89  		});
90  		Thread t2 = new Thread(new Runnable() {
91  			public void run() {
92  				results[1] = nSvc.processAutoRemovalOfDeliveredNotificationMessageDeliveries();
93  			}
94  		});
95  
96  		t1.start();
97  		t2.start();
98  
99  		t1.join();
100 		t2.join();
101 
102 		// assert that ONE of the autoremovers got all the items, and the other got NONE of the items
103 		LOG.info("Results of thread #1: " + results[0]);
104 		LOG.info("Results of thread #2: " + results[1]);
105 		assertTrue((results[0].getSuccesses().size() == EXPECTED_SUCCESSES && results[0].getFailures().size() == 0 && results[1].getSuccesses().size() == 0 && results[1].getFailures().size() == 0) ||
106 				(results[1].getSuccesses().size() == EXPECTED_SUCCESSES && results[1].getFailures().size() == 0 && results[0].getSuccesses().size() == 0 && results[0].getFailures().size() == 0));
107 
108 		assertProcessResults();
109 	}
110 }