View Javadoc

1   /*
2    * $Id: RedeliveryPolicyTestCase.java 22735 2011-08-25 16:02:35Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.test.routing;
11  
12  import static org.junit.Assert.assertEquals;
13  
14  import org.mule.api.MuleException;
15  import org.mule.api.MuleMessage;
16  import org.mule.module.client.MuleClient;
17  import org.mule.tck.junit4.FunctionalTestCase;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.junit.Test;
23  
24  public class RedeliveryPolicyTestCase extends FunctionalTestCase
25  {
26      @Override
27      protected String getConfigResources()
28      {
29          return "redelivery-policy-test.xml";
30      }
31  
32      @Test
33      public void testSuccess() throws Exception
34      {
35          for (int i = 0; i < 100; i++)
36          {
37              MuleClient client = new MuleClient(muleContext);
38              client.send("vm://success", "hello", null);
39          }
40          checkNumberOfMessages("vm://dead-letter-queue", 0, 1000);
41      }
42  
43      @Test
44      public void testLimitedFailures() throws Exception
45      {
46          int success = 0;
47          int failure = 0;
48          for (int i = 0; i < 100; i++)
49          {
50              MuleClient client = new MuleClient(muleContext);
51              MuleMessage msg = client.send("vm://limitedFailures", "hello", null);
52              if (msg.getExceptionPayload() != null)
53              {
54                  failure++;
55              }
56              else if (msg.getPayload().equals("hello"))
57              {
58                  success++;
59              }
60          }
61          checkNumberOfMessages("vm://dead-letter-queue", 0, 1000);
62          assertEquals(25, success);
63          assertEquals(75, failure);
64      }
65  
66      @Test
67      public void testManyRealFailures() throws Exception
68      {
69          int success = 0;
70          int failure = 0;
71          for (int i = 0; i < 12; i++)
72          {
73              for (int j = 0; j < 10; j++)
74              {
75  
76                  MuleClient client = new MuleClient(muleContext);
77                  String payload = "hello" + j;
78                  MuleMessage msg = client.send("vm://manyRealFailures", payload, null);
79                  if (msg.getExceptionPayload() != null)
80                  {
81                      failure++;
82                  }
83                  else if (msg.getPayload().equals(payload))
84                  {
85                      success++;
86                  }
87              }
88          }
89          checkNumberOfMessages("vm://dead-letter-queue", 20, 1000);
90          assertEquals(10, success);
91          assertEquals(90, failure);
92      }
93  
94      protected void checkNumberOfMessages(String url, int size, long timeout) throws MuleException
95      {
96          int count = 0;
97          MuleClient client = new MuleClient(muleContext);
98          while (client.request(url, timeout) != null)
99          {
100             count++;
101         }
102         assertEquals(size, count);
103     }
104 
105 
106     public static class FailThree
107     {
108         static int count = 0;
109 
110         public String process(String msg)
111         {
112             if (count++ % 4 != 0)
113             {
114                 throw new RuntimeException();
115             }
116 
117             return msg;
118         }
119     }
120 
121     public static class FailThreeOrSeven
122     {
123         static Map<String, Integer> counts = new HashMap<String, Integer>();
124 
125         public String process(String msg)
126         {
127             Integer icount = counts.get(msg);
128             int count = (icount == null) ? 0 : icount;
129             counts.put(msg, count + 1);
130             if (count % 12 != 3 && count++ % 12 != 11)
131             {
132                 throw new RuntimeException();
133             }
134 
135             return msg;
136         }
137     }
138 }