View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.vm.functional;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.module.client.MuleClient;
11  import org.mule.tck.junit4.FunctionalTestCase;
12  
13  import java.util.HashSet;
14  import java.util.Set;
15  
16  import org.junit.Test;
17  
18  import static org.junit.Assert.assertNotNull;
19  import static org.junit.Assert.assertNull;
20  import static org.junit.Assert.assertTrue;
21  
22  public class PersistentBoundedQueueTestCase extends FunctionalTestCase
23  {
24      // add some sizeable delat, as queue store ordering won't be guaranteed
25      private static final int SLEEP = 100;
26  
27      @Override
28      protected String getConfigResources()
29      {
30          return "vm/persistent-bounded-vm-queue-test.xml";
31      }
32  
33      @Test
34      public void testBoundedQueue() throws Exception
35      {
36          MuleClient client = new MuleClient(muleContext);
37          client.send("vm://in", "Test1", null);
38          Thread.sleep(SLEEP);
39          client.send("vm://in", "Test2", null);
40          Thread.sleep(SLEEP);
41          client.send("vm://in", "Test3", null);
42          Thread.sleep(SLEEP);
43  
44          // wait enough for queue offer to timeout
45          Thread.sleep(muleContext.getConfiguration().getDefaultQueueTimeout());
46  
47          // poll the 'out' queue 3 times, the first 2 times we must have a result. The 3rd message
48          // must have been discarded as the queue was bounded.
49          Set<String> results = new HashSet<String>();
50          pollOutQueue(client, results);
51          pollOutQueue(client, results);
52          assertTrue(results.contains("Test1"));
53          assertTrue(results.contains("Test2"));
54  
55          Thread.sleep(SLEEP);
56          MuleMessage result = client.request("vm://out", RECEIVE_TIMEOUT);
57          if (result != null)
58          {
59              System.out.println("result = " + result.getPayloadAsString());
60          }
61          assertNull(result);
62      }
63  
64      private void pollOutQueue(MuleClient client, Set<String> results) throws Exception
65      {
66          MuleMessage result = client.request("vm://out", RECEIVE_TIMEOUT);
67          assertNotNull(result);
68          results.add(result.getPayloadAsString());
69      }
70  }