15
15
package com .google .firebase .heartbeatinfo ;
16
16
17
17
import static com .google .common .truth .Truth .assertThat ;
18
+ import static com .google .firebase .heartbeatinfo .TaskWaiter .await ;
18
19
import static java .nio .charset .StandardCharsets .UTF_8 ;
19
20
import static org .mockito .ArgumentMatchers .anyLong ;
20
21
import static org .mockito .ArgumentMatchers .anyString ;
26
27
import android .content .Context ;
27
28
import android .content .SharedPreferences ;
28
29
import androidx .test .core .app .ApplicationProvider ;
29
- import androidx .test .runner .AndroidJUnit4 ;
30
+ import androidx .test .ext . junit . runners .AndroidJUnit4 ;
30
31
import com .google .firebase .platforminfo .UserAgentPublisher ;
31
32
import java .io .ByteArrayOutputStream ;
32
33
import java .io .IOException ;
35
36
import java .util .Collections ;
36
37
import java .util .HashSet ;
37
38
import java .util .Set ;
38
- import java .util .concurrent .ExecutionException ;
39
- import java .util .concurrent .ExecutorService ;
40
- import java .util .concurrent .LinkedBlockingQueue ;
41
- import java .util .concurrent .ThreadPoolExecutor ;
42
- import java .util .concurrent .TimeUnit ;
39
+ import java .util .concurrent .Executor ;
40
+ import java .util .concurrent .Executors ;
41
+ import java .util .concurrent .TimeoutException ;
43
42
import java .util .zip .GZIPOutputStream ;
44
- import org .json .JSONException ;
45
43
import org .junit .Before ;
46
44
import org .junit .Test ;
47
45
import org .junit .runner .RunWith ;
48
46
import org .robolectric .annotation .Config ;
49
47
50
48
@ RunWith (AndroidJUnit4 .class )
51
49
public class DefaultHeartBeatControllerTest {
52
- private ExecutorService executor ;
53
- private TestOnCompleteListener <Void > storeOnCompleteListener ;
54
- private TestOnCompleteListener <String > getOnCompleteListener ;
55
- private final String DEFAULT_USER_AGENT = "agent1" ;
56
- private HeartBeatInfoStorage storage = mock (HeartBeatInfoStorage .class );
57
- private UserAgentPublisher publisher = mock (UserAgentPublisher .class );
58
- private static Context applicationContext = ApplicationProvider .getApplicationContext ();
50
+ private static final String DEFAULT_USER_AGENT = "agent1" ;
51
+ private final Executor executor = Executors .newSingleThreadExecutor ();
52
+
53
+ private final HeartBeatInfoStorage storage = mock (HeartBeatInfoStorage .class );
54
+ private final UserAgentPublisher publisher = mock (UserAgentPublisher .class );
55
+ private final Context applicationContext = ApplicationProvider .getApplicationContext ();
59
56
private final Set <HeartBeatConsumer > logSources =
60
57
new HashSet <HeartBeatConsumer >() {
61
58
{
@@ -66,22 +63,18 @@ public class DefaultHeartBeatControllerTest {
66
63
67
64
@ Before
68
65
public void setUp () {
69
- executor = new ThreadPoolExecutor (0 , 1 , 30L , TimeUnit .SECONDS , new LinkedBlockingQueue <>());
70
66
when (publisher .getUserAgent ()).thenReturn (DEFAULT_USER_AGENT );
71
- storeOnCompleteListener = new TestOnCompleteListener <>();
72
- getOnCompleteListener = new TestOnCompleteListener <>();
73
67
heartBeatController =
74
68
new DefaultHeartBeatController (
75
69
() -> storage , logSources , executor , () -> publisher , applicationContext );
76
70
}
77
71
78
72
@ Test
79
- public void whenNoSource_dontStoreHeartBeat () throws ExecutionException , InterruptedException {
73
+ public void whenNoSource_dontStoreHeartBeat () throws InterruptedException , TimeoutException {
80
74
DefaultHeartBeatController controller =
81
75
new DefaultHeartBeatController (
82
76
() -> storage , new HashSet <>(), executor , () -> publisher , applicationContext );
83
- controller .registerHeartBeat ().addOnCompleteListener (executor , storeOnCompleteListener );
84
- storeOnCompleteListener .await ();
77
+ await (controller .registerHeartBeat ());
85
78
verify (storage , times (0 )).storeHeartBeat (anyLong (), anyString ());
86
79
}
87
80
@@ -99,31 +92,24 @@ public void getHeartBeatCode_noHeartBeat() {
99
92
100
93
@ Config (sdk = 29 )
101
94
@ Test
102
- public void generateHeartBeat_oneHeartBeat ()
103
- throws ExecutionException , InterruptedException , JSONException , IOException {
95
+ public void generateHeartBeat_oneHeartBeat () throws InterruptedException , TimeoutException {
104
96
ArrayList <HeartBeatResult > returnResults = new ArrayList <>();
105
97
returnResults .add (
106
- HeartBeatResult .create (
107
- "test-agent" , new ArrayList <String >(Collections .singleton ("2015-02-03" ))));
98
+ HeartBeatResult .create ("test-agent" , Collections .singletonList ("2015-02-03" )));
108
99
when (storage .getAllHeartBeats ()).thenReturn (returnResults );
109
- heartBeatController
110
- .registerHeartBeat ()
111
- .addOnCompleteListener (executor , storeOnCompleteListener );
112
- storeOnCompleteListener .await ();
100
+ await (heartBeatController .registerHeartBeat ());
113
101
verify (storage , times (1 )).storeHeartBeat (anyLong (), anyString ());
114
- heartBeatController
115
- .getHeartBeatsHeader ()
116
- .addOnCompleteListener (executor , getOnCompleteListener );
117
102
String str =
118
103
"{\" heartbeats\" :[{\" agent\" :\" test-agent\" ,\" dates\" :[\" 2015-02-03\" ]}],\" version\" :\" 2\" }" ;
119
104
String expected = compress (str );
120
- assertThat (getOnCompleteListener .await ().replace ("\n " , "" )).isEqualTo (expected );
105
+ assertThat (await (heartBeatController .getHeartBeatsHeader ()).replace ("\n " , "" ))
106
+ .isEqualTo (expected );
121
107
}
122
108
123
109
@ Config (sdk = 29 )
124
110
@ Test
125
111
public void firstNewThenOld_synchronizedCorrectly ()
126
- throws ExecutionException , InterruptedException {
112
+ throws InterruptedException , TimeoutException {
127
113
Context context = ApplicationProvider .getApplicationContext ();
128
114
SharedPreferences heartBeatSharedPreferences =
129
115
context .getSharedPreferences ("testHeartBeat" , Context .MODE_PRIVATE );
@@ -136,10 +122,8 @@ public void firstNewThenOld_synchronizedCorrectly()
136
122
Base64 .getUrlEncoder ()
137
123
.withoutPadding ()
138
124
.encodeToString ("{\" heartbeats\" :[],\" version\" :\" 2\" }" .getBytes ());
139
- controller .registerHeartBeat ().addOnCompleteListener (executor , storeOnCompleteListener );
140
- storeOnCompleteListener .await ();
141
- controller .getHeartBeatsHeader ().addOnCompleteListener (executor , getOnCompleteListener );
142
- String output = getOnCompleteListener .await ();
125
+ await (controller .registerHeartBeat ());
126
+ String output = await (controller .getHeartBeatsHeader ());
143
127
assertThat (output .replace ("\n " , "" )).isNotEqualTo (emptyString );
144
128
int heartBeatCode = controller .getHeartBeatCode ("test" ).getCode ();
145
129
assertThat (heartBeatCode ).isEqualTo (0 );
@@ -148,7 +132,7 @@ public void firstNewThenOld_synchronizedCorrectly()
148
132
@ Config (sdk = 29 )
149
133
@ Test
150
134
public void firstOldThenNew_synchronizedCorrectly ()
151
- throws ExecutionException , InterruptedException , IOException {
135
+ throws InterruptedException , TimeoutException {
152
136
Context context = ApplicationProvider .getApplicationContext ();
153
137
SharedPreferences heartBeatSharedPreferences =
154
138
context .getSharedPreferences ("testHeartBeat" , Context .MODE_PRIVATE );
@@ -158,46 +142,36 @@ public void firstOldThenNew_synchronizedCorrectly()
158
142
new DefaultHeartBeatController (
159
143
() -> heartBeatInfoStorage , logSources , executor , () -> publisher , context );
160
144
String emptyString = compress ("{\" heartbeats\" :[],\" version\" :\" 2\" }" );
161
- controller .registerHeartBeat ().addOnCompleteListener (executor , storeOnCompleteListener );
162
- storeOnCompleteListener .await ();
145
+ await (controller .registerHeartBeat ());
163
146
int heartBeatCode = controller .getHeartBeatCode ("test" ).getCode ();
164
147
assertThat (heartBeatCode ).isEqualTo (2 );
165
- controller .getHeartBeatsHeader ().addOnCompleteListener (executor , getOnCompleteListener );
166
- String output = getOnCompleteListener .await ();
148
+ String output = await (controller .getHeartBeatsHeader ());
167
149
assertThat (output .replace ("\n " , "" )).isEqualTo (emptyString );
168
- controller .registerHeartBeat ().addOnCompleteListener (executor , storeOnCompleteListener );
169
- storeOnCompleteListener .await ();
170
- controller .getHeartBeatsHeader ().addOnCompleteListener (executor , getOnCompleteListener );
171
- output = getOnCompleteListener .await ();
150
+
151
+ await (controller .registerHeartBeat ());
152
+ await (controller .getHeartBeatsHeader ());
172
153
assertThat (output .replace ("\n " , "" )).isEqualTo (emptyString );
173
154
}
174
155
175
156
@ Config (sdk = 29 )
176
157
@ Test
177
158
public void generateHeartBeat_twoHeartBeatsSameUserAgent ()
178
- throws ExecutionException , InterruptedException , JSONException , IOException {
159
+ throws InterruptedException , TimeoutException {
179
160
ArrayList <HeartBeatResult > returnResults = new ArrayList <>();
180
161
ArrayList <String > dateList = new ArrayList <>();
181
162
dateList .add ("2015-03-02" );
182
163
dateList .add ("2015-03-01" );
183
164
returnResults .add (HeartBeatResult .create ("test-agent" , dateList ));
184
165
when (storage .getAllHeartBeats ()).thenReturn (returnResults );
185
- heartBeatController
186
- .registerHeartBeat ()
187
- .addOnCompleteListener (executor , storeOnCompleteListener );
188
- storeOnCompleteListener .await ();
189
- heartBeatController
190
- .registerHeartBeat ()
191
- .addOnCompleteListener (executor , storeOnCompleteListener );
192
- storeOnCompleteListener .await ();
166
+ await (heartBeatController .registerHeartBeat ());
167
+ await (heartBeatController .registerHeartBeat ());
193
168
verify (storage , times (2 )).storeHeartBeat (anyLong (), anyString ());
194
- heartBeatController
195
- .getHeartBeatsHeader ()
196
- .addOnCompleteListener (executor , getOnCompleteListener );
169
+
197
170
String str =
198
171
"{\" heartbeats\" :[{\" agent\" :\" test-agent\" ,\" dates\" :[\" 2015-03-02\" ,\" 2015-03-01\" ]}],\" version\" :\" 2\" }" ;
199
172
String expected = compress (str );
200
- assertThat (getOnCompleteListener .await ().replace ("\n " , "" )).isEqualTo (expected );
173
+ assertThat (await (heartBeatController .getHeartBeatsHeader ()).replace ("\n " , "" ))
174
+ .isEqualTo (expected );
201
175
}
202
176
203
177
private static String base64Encode (byte [] input ) {
@@ -218,38 +192,28 @@ private static byte[] gzip(String input) {
218
192
}
219
193
}
220
194
221
- private String compress (String str ) throws IOException {
195
+ private String compress (String str ) {
222
196
return base64Encode (gzip (str ));
223
197
}
224
198
225
199
@ Config (sdk = 29 )
226
200
@ Test
227
201
public void generateHeartBeat_twoHeartBeatstwoUserAgents ()
228
- throws ExecutionException , InterruptedException , JSONException , IOException {
202
+ throws InterruptedException , TimeoutException {
229
203
ArrayList <HeartBeatResult > returnResults = new ArrayList <>();
230
204
returnResults .add (
231
- HeartBeatResult .create (
232
- "test-agent" , new ArrayList <String >(Collections .singleton ("2015-03-02" ))));
205
+ HeartBeatResult .create ("test-agent" , Collections .singletonList ("2015-03-02" )));
233
206
returnResults .add (
234
- HeartBeatResult .create (
235
- "test-agent-1" , new ArrayList <String >(Collections .singleton ("2015-03-03" ))));
207
+ HeartBeatResult .create ("test-agent-1" , Collections .singletonList ("2015-03-03" )));
236
208
when (storage .getAllHeartBeats ()).thenReturn (returnResults );
237
- heartBeatController
238
- .registerHeartBeat ()
239
- .addOnCompleteListener (executor , storeOnCompleteListener );
240
- storeOnCompleteListener .await ();
241
- heartBeatController
242
- .registerHeartBeat ()
243
- .addOnCompleteListener (executor , storeOnCompleteListener );
244
- storeOnCompleteListener .await ();
245
- Thread .sleep (1000 );
209
+ await (heartBeatController .registerHeartBeat ());
210
+ await (heartBeatController .registerHeartBeat ());
211
+
246
212
verify (storage , times (2 )).storeHeartBeat (anyLong (), anyString ());
247
- heartBeatController
248
- .getHeartBeatsHeader ()
249
- .addOnCompleteListener (executor , getOnCompleteListener );
250
213
String str =
251
214
"{\" heartbeats\" :[{\" agent\" :\" test-agent\" ,\" dates\" :[\" 2015-03-02\" ]},{\" agent\" :\" test-agent-1\" ,\" dates\" :[\" 2015-03-03\" ]}],\" version\" :\" 2\" }" ;
252
215
String expected = compress (str );
253
- assertThat (getOnCompleteListener .await ().replace ("\n " , "" )).isEqualTo (expected );
216
+ assertThat (await (heartBeatController .getHeartBeatsHeader ()).replace ("\n " , "" ))
217
+ .isEqualTo (expected );
254
218
}
255
219
}
0 commit comments