View Javadoc

1   /*
2    * $Id: PersistentBoundedQueueTestCase.java 22597 2011-08-05 20:40:24Z 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  
11  package org.mule.transport.vm.functional;
12  
13  import static org.junit.Assert.assertNotNull;
14  import static org.junit.Assert.assertNull;
15  import static org.junit.Assert.assertTrue;
16  
17  import java.util.Arrays;
18  import java.util.Collection;
19  import java.util.HashSet;
20  import java.util.Set;
21  
22  import org.junit.Test;
23  import org.junit.runners.Parameterized.Parameters;
24  import org.mule.api.MuleMessage;
25  import org.mule.module.client.MuleClient;
26  import org.mule.tck.AbstractServiceAndFlowTestCase;
27  
28  public class PersistentBoundedQueueTestCase extends AbstractServiceAndFlowTestCase
29  {
30  
31      // add some sizeable delat, as queue store ordering won't be guaranteed
32      private static final int SLEEP = 100;
33  
34      public PersistentBoundedQueueTestCase(ConfigVariant variant, String configResources)
35      {
36          super(variant, configResources);
37      }
38  
39      @Parameters
40      public static Collection<Object[]> parameters()
41      {
42          return Arrays.asList(new Object[][]{
43              {ConfigVariant.SERVICE, "vm/persistent-bounded-vm-queue-test-service.xml"},
44              {ConfigVariant.FLOW, "vm/persistent-bounded-vm-queue-test-flow.xml"}});
45      }
46  
47      @Test
48      public void testBoundedQueue() throws Exception
49      {
50          MuleClient client = new MuleClient(muleContext);
51          client.dispatch("vm://in", "Test1", null);
52          Thread.sleep(SLEEP);
53          client.dispatch("vm://in", "Test2", null);
54          Thread.sleep(SLEEP);
55          client.dispatch("vm://in", "Test3", null);
56          Thread.sleep(SLEEP);
57  
58          // wait enough for queue offer to timeout
59          Thread.sleep(muleContext.getConfiguration().getDefaultQueueTimeout());
60  
61          // poll the 'out' queue 3 times, the first 2 times we must have a result. The
62          // 3rd message
63          // must have been discarded as the queue was bounded.
64          Set<String> results = new HashSet<String>();
65          pollOutQueue(client, results);
66          pollOutQueue(client, results);
67          assertTrue(results.contains("Test1"));
68          assertTrue(results.contains("Test2"));
69  
70          Thread.sleep(SLEEP);
71          MuleMessage result = client.request("vm://out", RECEIVE_TIMEOUT);
72          if (result != null)
73          {
74              System.out.println("result = " + result.getPayloadAsString());
75          }
76          assertNull(result);
77      }
78  
79      private void pollOutQueue(MuleClient client, Set<String> results) throws Exception
80      {
81          MuleMessage result = client.request("vm://out", RECEIVE_TIMEOUT);
82          assertNotNull(result);
83          results.add(result.getPayloadAsString());
84      }
85  }