View Javadoc
1   /**
2    * Copyright 2005-2014 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.core.api.criteria.QueryByCriteria;
21  import org.kuali.rice.ken.bo.NotificationMessageDelivery;
22  import org.kuali.rice.ken.service.NotificationMessageDeliveryAutoRemovalService;
23  import org.kuali.rice.ken.service.ProcessingResult;
24  import org.kuali.rice.ken.test.KENTestCase;
25  import org.kuali.rice.ken.util.NotificationConstants;
26  import org.kuali.rice.krad.service.KRADServiceLocator;
27  
28  import java.util.Collection;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.assertTrue;
32  
33  import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
34  import static org.kuali.rice.core.api.criteria.PredicateFactory.isNotNull;
35  
36  /**
37   * Tests NotificationMessageDeliveryAutoRemovalServiceImpl
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   */
40  @Ignore // deadlocks are detected during clear database lifecycle (even when select for update is commented out...)
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 = getLockedDeliveries();
48  
49  		assertEquals(0, lockedDeliveries.size());
50  
51  		// should be 1 autoremoved delivery
52          QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
53          criteria.setPredicates(equal(NotificationConstants.BO_PROPERTY_NAMES.MESSAGE_DELIVERY_STATUS,
54                  NotificationConstants.MESSAGE_DELIVERY_STATUS.AUTO_REMOVED));
55  		Collection<NotificationMessageDelivery> unprocessedDeliveries =
56                  KRADServiceLocator.getDataObjectService().findMatching(NotificationMessageDelivery.class,
57                  criteria.build()).getResults();
58  
59          assertEquals(EXPECTED_SUCCESSES, unprocessedDeliveries.size());
60  	}
61  
62  	/**
63  	 * Test auto-removal message deliveries
64  	 */
65  	@Test
66  	public void testAutoRemovedNotificationMessageDeliveries() {
67  		NotificationMessageDeliveryAutoRemovalService nSvc = services.getNotificationMessageDeliveryAutoRemovalService();
68  
69  		services.getNotificationMessageDeliveryResolverService().resolveNotificationMessageDeliveries();
70  
71  		ProcessingResult result = nSvc.processAutoRemovalOfDeliveredNotificationMessageDeliveries();
72  
73  		assertEquals(0, result.getFailures().size());
74  		assertEquals(EXPECTED_SUCCESSES, result.getSuccesses().size());
75  
76  		assertProcessResults();
77  	}
78  
79  	/**
80  	 * Test concurrent auto-removal of message deliveries
81  	 */
82  	@Test
83  	public void testAutoRemovalConcurrency() throws InterruptedException {
84  		final NotificationMessageDeliveryAutoRemovalService nSvc = services.getNotificationMessageDeliveryAutoRemovalService();
85  
86  		services.getNotificationMessageDeliveryResolverService().resolveNotificationMessageDeliveries();
87  
88  		final ProcessingResult[] results = new ProcessingResult[2];
89  		Thread t1 = new Thread(new Runnable() {
90  			public void run() {
91  				results[0] = nSvc.processAutoRemovalOfDeliveredNotificationMessageDeliveries();
92  			}
93  		});
94  		Thread t2 = new Thread(new Runnable() {
95  			public void run() {
96  				results[1] = nSvc.processAutoRemovalOfDeliveredNotificationMessageDeliveries();
97  			}
98  		});
99  
100 		t1.start();
101 		t2.start();
102 
103 		t1.join();
104 		t2.join();
105 
106 		// assert that ONE of the autoremovers got all the items, and the other got NONE of the items
107 		LOG.info("Results of thread #1: " + results[0]);
108 		LOG.info("Results of thread #2: " + results[1]);
109 		assertTrue((results[0].getSuccesses().size() == EXPECTED_SUCCESSES && results[0].getFailures().size() == 0 &&
110                 results[1].getSuccesses().size() == 0 && results[1].getFailures().size() == 0) ||
111 				(results[1].getSuccesses().size() == EXPECTED_SUCCESSES && results[1].getFailures().size() == 0 &&
112                 results[0].getSuccesses().size() == 0 && results[0].getFailures().size() == 0));
113 
114 		assertProcessResults();
115 
116     }
117 
118     /**
119      * Gets all locked deliveries for the given entity.
120      * @return null
121      */
122     protected Collection<NotificationMessageDelivery> getLockedDeliveries() {
123         QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
124         criteria.setPredicates(isNotNull(NotificationConstants.BO_PROPERTY_NAMES.LOCKED_DATE));
125         Collection<NotificationMessageDelivery> lockedDeliveries = KRADServiceLocator.getDataObjectService().findMatching(
126                 NotificationMessageDelivery.class, criteria.build()).getResults();
127 
128         return null;
129     }
130 }