99import com .microsoft .applicationinsights .internal .schemav2 .Envelope ;
1010import com .microsoft .applicationinsights .smoketest .JsonHelper ;
1111
12+ import javax .annotation .concurrent .GuardedBy ;
1213import javax .servlet .ServletConfig ;
1314import javax .servlet .ServletException ;
1415import javax .servlet .http .HttpServlet ;
1819import java .io .InputStreamReader ;
1920import java .io .StringWriter ;
2021import java .util .ArrayList ;
22+ import java .util .Collection ;
2123import java .util .List ;
2224import java .util .Queue ;
25+ import java .util .concurrent .Callable ;
2326import java .util .concurrent .ConcurrentLinkedDeque ;
27+ import java .util .concurrent .ExecutionException ;
28+ import java .util .concurrent .ExecutorService ;
29+ import java .util .concurrent .Executors ;
30+ import java .util .concurrent .Future ;
31+ import java .util .concurrent .TimeUnit ;
32+ import java .util .concurrent .TimeoutException ;
2433import java .util .zip .GZIPInputStream ;
2534
2635public class MockedAppInsightsIngestionServlet extends HttpServlet {
@@ -31,12 +40,18 @@ public class MockedAppInsightsIngestionServlet extends HttpServlet {
3140
3241 private final String appid = "DUMMYAPPID" ;
3342
34- private Queue <Envelope > telemetryReceived ;
35- private ListMultimap <String , Envelope > type2envelope ;
43+
44+ private final Queue <Envelope > telemetryReceived ;
45+ @ GuardedBy ("multimapLock" )
46+ private final ListMultimap <String , Envelope > type2envelope ;
3647 private List <Predicate <Envelope >> filters ;
3748
49+ private final Object multimapLock = new Object ();
50+
3851 private MockedIngestionServletConfig config ;
3952
53+ private final ExecutorService itemExecutor = Executors .newSingleThreadExecutor ();
54+
4055 public static final String LOG_PAYLOADS_PARAMETER_KEY = "logPayloads" ;
4156 public static final String RETAIN_PAYLOADS_PARAMETER_KEY = "retainPayloads" ;
4257
@@ -96,7 +111,9 @@ public MockedIngestionServletConfig getIngestionConfig() {
96111 public void resetData () {
97112 logit ("Clearing telemetry accumulator..." );
98113 telemetryReceived .clear ();
99- type2envelope .clear ();
114+ synchronized (multimapLock ) {
115+ type2envelope .clear ();
116+ }
100117 }
101118
102119 public boolean hasData () {
@@ -113,7 +130,33 @@ public Envelope nextItem() {
113130
114131 public List <Envelope > getItemsByType (String type ) {
115132 Preconditions .checkNotNull (type , "type" );
116- return type2envelope .get (type );
133+ synchronized (multimapLock ) {
134+ return type2envelope .get (type );
135+ }
136+ }
137+
138+ public List <Envelope > waitForItems (final Predicate <Envelope > condition , final int numItems , int timeout , TimeUnit timeUnit ) throws InterruptedException , ExecutionException , TimeoutException {
139+ final Future <List <Envelope >> future = itemExecutor .submit (new Callable <List <Envelope >>() {
140+ @ Override
141+ public List <Envelope > call () throws Exception {
142+ List <Envelope > targetCollection = new ArrayList <>(numItems );
143+ while (targetCollection .size () < numItems ) {
144+ targetCollection .clear ();
145+ final Collection <Envelope > currentValues ;
146+ synchronized (multimapLock ) {
147+ currentValues = new ArrayList <>(type2envelope .values ());
148+ }
149+ for (Envelope val : currentValues ) {
150+ if (condition .apply (val )) {
151+ targetCollection .add (val );
152+ }
153+ }
154+ TimeUnit .MILLISECONDS .sleep (75 );
155+ }
156+ return targetCollection ;
157+ }
158+ });
159+ return future .get (timeout , timeUnit );
117160 }
118161
119162 @ Override
@@ -157,8 +200,10 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
157200 String baseType = envelope .getData ().getBaseType ();
158201 if (filtersAllowItem (envelope )) {
159202 logit ("Adding telemetry item: " +baseType );
160- type2envelope .put (baseType , envelope );
161203 telemetryReceived .offer (envelope );
204+ synchronized (multimapLock ) {
205+ type2envelope .put (baseType , envelope );
206+ }
162207 } else {
163208 logit ("Rejected telemetry item by filter: " +baseType );
164209 }
0 commit comments