KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > email > test > EmailMultiThreadedPerformanceTest


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.email.test;
19
20
21 import org.sape.carbon.core.component.Lookup;
22 import org.sape.carbon.services.email.MailDataObject;
23 import org.sape.carbon.services.email.MailFailureException;
24 import org.sape.carbon.services.email.MailService;
25 import org.sape.carbon.services.email.util.MailContentTypeEnum;
26
27 import junit.framework.Test;
28 import junit.framework.TestSuite;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 /**
34  * <p>Test harness for testing of Email Service in multi-threaded
35  * environment. </p>
36  *
37  * <p>Copyright 2002 Sapient</p>
38  * @stereotype test harness
39  * @since carbon 1.0
40  * @author $Author: dvoet $ $Date: 2003/05/05 21:21:29 $
41  * @version $Revision: 1.5 $
42  *
43  */

44 public class EmailMultiThreadedPerformanceTest extends EmailServiceTest {
45
46     /**
47      * Provides a handle to Apache-commons logger
48      */

49     private Log log = LogFactory.getLog(this.getClass());
50
51     public static final int NO_OF_THREADS = 3;
52
53     public static final int NO_OF_MAILS_PER_THREAD = 10;
54
55     public static boolean testFailed = false;
56
57     /**
58      * <p> This test tests the email component in multi threaded environment.
59      * </p>
60      */

61     public void testMultiThreaded() {
62
63         if (log.isInfoEnabled()) {
64             log.info("Starting multi threaded test.");
65         }
66
67         Thread JavaDoc emailDeliveryThread = new Thread JavaDoc() {
68             public void run() {
69
70                 MailService mail = (MailService) Lookup.getInstance().
71                     fetchComponent("/email/test/testEmail");
72
73                 String JavaDoc message = "Simple email by thread # " +
74                     Thread.currentThread().getName();
75
76
77                 long startTime, endTime;
78
79                 startTime = System.currentTimeMillis();
80
81                 for (int i=0; i<EmailMultiThreadedPerformanceTest.NO_OF_MAILS_PER_THREAD;
82                     i++) {
83                     try {
84
85                         MailDataObject mailDataObject = new MailDataObject();
86                         mailDataObject.addTo(TO_EMAIL, TO_PERSONAL);
87                         mailDataObject.setFrom(FROM_EMAIL, FROM_PERSONAL);
88                         mailDataObject.setSubject(SUBJECT);
89                         mailDataObject.setBody(message,
90                             MailContentTypeEnum.PLAIN_TEXT);
91
92                         mail.sendMail(mailDataObject);
93                     } catch(MailFailureException mfe) {
94                         EmailMultiThreadedPerformanceTest.testFailed = true;
95                         fail("Failed to send email to single recipient. ");
96                     }
97                  }
98
99                 endTime = System.currentTimeMillis();
100
101                 long fullTime = endTime - startTime;
102
103                 if (log.isInfoEnabled()) {
104                     log.info("Time taken to send "
105                         + EmailMultiThreadedPerformanceTest.NO_OF_MAILS_PER_THREAD
106                         + " mails by thread # "
107                         + Thread.currentThread().getName() + " is : "
108                         + fullTime);
109                 }
110             }
111         };
112
113         Thread JavaDoc[] deliveryThreads =
114             new Thread JavaDoc[EmailMultiThreadedPerformanceTest.NO_OF_THREADS];
115
116         // Start the threads
117
for(int i = 0; i < EmailMultiThreadedPerformanceTest.NO_OF_THREADS; i++) {
118             deliveryThreads[i] = new Thread JavaDoc(emailDeliveryThread);
119             deliveryThreads[i].setName("Email Delivery Thread # "+i);
120             deliveryThreads[i].start();
121         }
122
123         for(int i = 0; i < EmailMultiThreadedPerformanceTest.NO_OF_THREADS; i++) {
124             try {
125                 deliveryThreads[i].join();
126             } catch(InterruptedException JavaDoc ie) {
127                 // Eat exception
128
}
129         }
130
131         if(EmailMultiThreadedPerformanceTest.testFailed) {
132             fail("Multi threaded test failed.");
133         }
134     }
135
136
137     public EmailMultiThreadedPerformanceTest(String JavaDoc name) {
138         super(name);
139     }
140
141
142     /**
143      * Adds all tests into the master suite.
144      */

145     public static Test suite() {
146         TestSuite masterSuite = new TestSuite();
147         masterSuite.addTest(new EmailMultiThreadedPerformanceTest("testMultiThreaded"));
148         return masterSuite;
149     }
150 }
151
Popular Tags