001: package test;
002:
003: import dalma.Fiber;
004: import dalma.endpoints.email.EmailEndPoint;
005: import dalma.endpoints.email.MimeMessageEx;
006: import dalma.endpoints.email.NewMailHandler;
007: import dalma.test.WorkflowTestProgram;
008: import junit.framework.Assert;
009: import junit.textui.TestRunner;
010:
011: import javax.mail.Message;
012: import javax.mail.internet.InternetAddress;
013: import javax.mail.internet.MimeMessage;
014: import java.io.Serializable;
015: import java.util.ArrayList;
016: import java.util.List;
017:
018: /**
019: * Tests {@link Fiber}s.
020: *
021: * @author Kohsuke Kawaguchi
022: */
023: public class FiberTest extends WorkflowTestProgram {
024:
025: public FiberTest(String name) {
026: super (name);
027: }
028:
029: public static void main(String[] args) {
030: TestRunner.run(FiberTest.class);
031: }
032:
033: EmailEndPoint ep1;
034: EmailEndPoint ep2;
035:
036: protected void setupEndPoints() throws Exception {
037: // passive side --- just send reply e-mails
038: ep1 = (EmailEndPoint) engine.addEndPoint("email1",
039: getProperty("email.endpoint1"));
040: ep1.setNewMailHandler(new NewMailHandler() {
041: int counter;
042:
043: public void onNewMail(MimeMessage mail) throws Exception {
044: MimeMessage m = (MimeMessage) mail.reply(false);
045: m.setText(String.valueOf(counter++));
046: ep1.send(m);
047: }
048: });
049:
050: // active side
051: ep2 = (EmailEndPoint) engine.addEndPoint("email2",
052: getProperty("email.endpoint2"));
053: }
054:
055: public void test() throws Throwable {
056: createConversation(Alice.class, ep2, ep1.getAddress());
057: engine.waitForCompletion();
058: }
059:
060: /**
061: * Use multiple fibers.
062: */
063: static final class Alice implements Runnable, Serializable {
064: private final EmailEndPoint ep;
065: private final InternetAddress adrs;
066:
067: public Alice(EmailEndPoint ep, InternetAddress adrs) {
068: this .ep = ep;
069: this .adrs = adrs;
070: }
071:
072: final class ReqRsp implements Runnable, Serializable {
073: int value;
074:
075: final String id;
076:
077: public ReqRsp(String id) {
078: this .id = id;
079: }
080:
081: public void run() {
082: try {
083: MimeMessage msg = new MimeMessageEx(ep.getSession());
084: msg.setRecipient(Message.RecipientType.TO, adrs);
085: msg.setSubject("new conv");
086: msg.setText("abc");
087: log("sending a message");
088: msg = ep.waitForReply(msg);
089: log("got a reply");
090: value = Integer.parseInt(msg.getContent()
091: .toString().trim());
092: } catch (Exception e) {
093: e.printStackTrace();
094: Assert.fail();
095: }
096: }
097:
098: private void log(String msg) {
099: System.out.println(id + ": " + msg);
100: }
101: }
102:
103: public void run() {
104: try {
105: List<Fiber<ReqRsp>> fibers = new ArrayList<Fiber<ReqRsp>>();
106:
107: System.out.println("A: initiating fibers");
108: for (int i = 0; i < 10; i++) {
109: Fiber<ReqRsp> f = Fiber.create(new ReqRsp("Alice"
110: + i));
111: fibers.add(f);
112: f.start();
113: }
114:
115: System.out.println("A: waiting for fibers to complete");
116:
117: for (Fiber<ReqRsp> fiber : fibers.toArray(new Fiber[0])) {
118: fiber.join();
119: }
120:
121: int value = 0;
122:
123: for (Fiber<ReqRsp> fiber : fibers) {
124: value += fiber.getRunnable().value;
125: }
126:
127: Assert.assertEquals(45 /* 1/2*N*(N-1) with N=10 */,
128: value);
129:
130: System.out.println("A: exiting");
131: } catch (InterruptedException e) {
132: e.printStackTrace();
133: Assert.fail();
134: }
135: }
136: }
137: }
|