1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.tradefed.cluster;
17 
18 import static org.junit.Assert.assertEquals;
19 
20 import org.json.JSONException;
21 import org.json.JSONObject;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.junit.runners.JUnit4;
26 
27 import java.io.IOException;
28 import java.util.LinkedList;
29 import java.util.List;
30 
31 /** Unit tests for {@link ClusterEventUploader}. */
32 @RunWith(JUnit4.class)
33 public class ClusterEventUploaderTest {
34 
35     private IClusterEventUploader<Event> mUploader;
36     private List<Event> mUploadedEvents = null;
37 
38     /** Event class used in tests. */
39     static class Event implements IClusterEvent {
40 
41         String mName = null;
42         boolean mUploadSuccess = true;
43 
Event(String name)44         public Event(String name) {
45             this(name, true);
46         }
47 
Event(String name, boolean uploadSuccess)48         public Event(String name, boolean uploadSuccess) {
49             mName = name;
50             mUploadSuccess = uploadSuccess;
51         }
52 
53         @Override
toJSON()54         public JSONObject toJSON() throws JSONException {
55             return null;
56         }
57     }
58 
59     @Before
setUp()60     public void setUp() throws Exception {
61         mUploadedEvents = new LinkedList<>();
62         mUploader =
63                 new ClusterEventUploader<Event>() {
64                     @Override
65                     protected void doUploadEvents(List<Event> events) throws IOException {
66                         for (Event e : events) {
67                             if (e.mUploadSuccess) {
68                                 mUploadedEvents.add(e);
69                             } else {
70                                 throw new IOException();
71                             }
72                         }
73                     }
74                 };
75     }
76 
77     @Test
testPostCommandEvent_simpleEvent()78     public void testPostCommandEvent_simpleEvent() {
79         // Create an event to post
80         final Event event = new Event("event");
81         mUploader.postEvent(event);
82         mUploader.flush();
83         assertEquals(1, mUploadedEvents.size());
84         assertEquals("event", mUploadedEvents.get(0).mName);
85     }
86 
87     /*
88      * If there are no events in the queue, do not post.
89      */
90     @Test
testPostCommandEvent_NoEvent()91     public void testPostCommandEvent_NoEvent() {
92         mUploader.flush();
93         assertEquals(0, mUploadedEvents.size());
94     }
95 
96     @Test
testPostCommandEvent_multipleEvents()97     public void testPostCommandEvent_multipleEvents() {
98         final Event event = new Event("event");
99         mUploader.postEvent(event);
100         mUploader.postEvent(event);
101         mUploader.postEvent(event);
102         mUploader.postEvent(event);
103         mUploader.flush();
104         assertEquals(4, mUploadedEvents.size());
105     }
106 
107     @Test
testPostCommandEvent_multipleBatches()108     public void testPostCommandEvent_multipleBatches() {
109         final Event event = new Event("event");
110         mUploader.setMaxBatchSize(2);
111         mUploader.postEvent(event);
112         mUploader.postEvent(event);
113         mUploader.postEvent(event);
114         mUploader.postEvent(event);
115         mUploader.flush();
116         assertEquals(4, mUploadedEvents.size());
117     }
118 
119     @Test
testPostCommandEvent_failed()120     public void testPostCommandEvent_failed() {
121         final Event event = new Event("event");
122         final Event failedEvent = new Event("failedEvent", false);
123         mUploader.postEvent(event);
124         mUploader.postEvent(failedEvent);
125         mUploader.flush();
126         assertEquals(1, mUploadedEvents.size());
127         assertEquals("event", mUploadedEvents.get(0).mName);
128         failedEvent.mUploadSuccess = true;
129         mUploader.flush();
130         assertEquals(2, mUploadedEvents.size());
131         assertEquals("failedEvent", mUploadedEvents.get(1).mName);
132     }
133 }
134