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.management;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.management.stats.ApplicationStatistics;
11  import org.mule.management.stats.FlowConstructStatistics;
12  import org.mule.management.stats.ServiceStatistics;
13  import org.mule.module.client.MuleClient;
14  import org.mule.tck.junit4.FunctionalTestCase;
15  
16  import java.util.Iterator;
17  
18  import org.junit.Test;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.assertTrue;
23  
24  public class JmxStatisticsAsyncTestCase extends FunctionalTestCase
25  {
26      
27      @Override
28      protected String getConfigResources()
29      {
30          return "jmx-statistics-test.xml";
31      }
32  
33      @Override
34      protected void doSetUp() throws Exception
35      {
36          super.doSetUp();
37                  
38          MuleClient muleClient = new MuleClient(muleContext);
39          muleClient.dispatch("vm://in", "Hello world", null);
40          MuleMessage response = muleClient.request("vm://out", RECEIVE_TIMEOUT * 2);
41          assertNotNull(response);
42          assertEquals("data", response.getPayloadAsString());
43          muleClient.dispatch("vm://inflow", "Flow data", null);
44          response = muleClient.request("vm://outflow", RECEIVE_TIMEOUT * 2);
45          muleClient.dispatch("vm://inflow", "Flow data", null);
46          response = muleClient.request("vm://outflow", RECEIVE_TIMEOUT * 2);
47          assertNotNull(response);
48          assertEquals("Flow data", response.getPayloadAsString());
49      }
50  
51      @Override
52      protected void doTearDown() throws Exception
53      {
54          getServiceStatistics().clear();
55          super.doTearDown();
56      }
57  
58      @Test
59      public void testCorrectAverageQueueSize() throws Exception
60      {
61          ServiceStatistics stats = getServiceStatistics();
62          assertEquals(1, stats.getAverageQueueSize());
63      }
64  
65      @Test
66      public void testCorrectAsynchEventsReceived() throws Exception
67      {
68          ServiceStatistics stats = getServiceStatistics();
69          assertEquals(1, stats.getAsyncEventsReceived());
70          FlowConstructStatistics fstats = getFlowConstructStatistics();
71          assertEquals(2, fstats.getAsyncEventsReceived());
72          ApplicationStatistics astats = getApplicationStatistics();
73          assertEquals(3, astats.getAsyncEventsReceived());
74      }
75  
76      @Test
77      public void testCorrectMaxQueueSize() throws Exception
78      {
79          ServiceStatistics stats = getServiceStatistics();
80          assertEquals(1, stats.getMaxQueueSize());
81      }
82  
83      @Test
84      public void testCorrectAsynchEventsSent() throws Exception
85      {
86          ServiceStatistics stats = getServiceStatistics();
87          assertEquals(1, stats.getAsyncEventsSent());
88      }
89  
90      @Test
91      public void testCorrectTotalEventsSent() throws Exception
92      {
93          ServiceStatistics stats = getServiceStatistics();
94          assertEquals(1, stats.getTotalEventsSent());
95      }
96  
97      @Test
98      public void testCorrectTotalEventsReceived() throws Exception
99      {
100         ServiceStatistics stats = getServiceStatistics();
101         assertEquals(1, stats.getTotalEventsReceived());
102         FlowConstructStatistics fstats = getFlowConstructStatistics();
103         assertEquals(2, fstats.getTotalEventsReceived());
104         ApplicationStatistics astats = getApplicationStatistics();
105         assertEquals(3, astats.getTotalEventsReceived());
106     }
107 
108     private ServiceStatistics getServiceStatistics()
109     {
110         Iterator<FlowConstructStatistics> iterator = muleContext.getStatistics().getServiceStatistics().iterator();
111         FlowConstructStatistics stat1;
112         do
113         {
114             assertTrue(iterator.hasNext());
115             stat1 = iterator.next();
116         }
117         while (!(stat1 instanceof ServiceStatistics));
118         return (ServiceStatistics)stat1;
119 
120     }
121 
122     private FlowConstructStatistics getFlowConstructStatistics()
123     {
124         Iterator<FlowConstructStatistics> iterator = muleContext.getStatistics().getServiceStatistics().iterator();
125         FlowConstructStatistics stat1;
126         do
127         {
128             assertTrue(iterator.hasNext());
129             stat1 = iterator.next();
130         }
131         while (stat1.getClass() != FlowConstructStatistics.class);
132         return stat1;
133     }
134 
135     private ApplicationStatistics getApplicationStatistics()
136     {
137         Iterator<FlowConstructStatistics> iterator = muleContext.getStatistics().getServiceStatistics().iterator();
138         FlowConstructStatistics stat1;
139         do
140         {
141             assertTrue(iterator.hasNext());
142             stat1 = iterator.next();
143         }
144         while (!(stat1 instanceof ApplicationStatistics));
145         return (ApplicationStatistics)stat1;
146     }
147 
148 }