1
1
package com .deploygate .sdk ;
2
2
3
3
import android .os .Bundle ;
4
+ import android .os .DeadObjectException ;
4
5
import android .os .RemoteException ;
6
+ import android .os .TransactionTooLargeException ;
5
7
6
8
import androidx .test .ext .junit .runners .AndroidJUnit4 ;
7
9
15
17
import org .mockito .Mock ;
16
18
import org .mockito .Mockito ;
17
19
import org .mockito .MockitoAnnotations ;
20
+ import org .mockito .invocation .InvocationOnMock ;
21
+ import org .mockito .stubbing .Answer ;
18
22
import org .mockito .verification .VerificationMode ;
19
23
import org .robolectric .Shadows ;
20
24
import org .robolectric .annotation .LooperMode ;
21
25
26
+ import java .util .ArrayList ;
27
+ import java .util .List ;
28
+
29
+ import static com .deploygate .sdk .mockito .BundleMatcher .eq ;
30
+ import static org .mockito .ArgumentMatchers .eq ;
31
+ import static org .mockito .Mockito .doAnswer ;
32
+ import static org .mockito .Mockito .doNothing ;
33
+ import static org .mockito .Mockito .doThrow ;
22
34
import static org .robolectric .annotation .LooperMode .Mode .PAUSED ;
23
35
24
36
@ RunWith (AndroidJUnit4 .class )
@@ -57,13 +69,15 @@ public void check_buffer_size_works_with_drop_by_oldest() throws RemoteException
57
69
Shadows .shadowOf (customLogTransmitter .getLooper ()).idle ();
58
70
59
71
for (int i = 0 ; i < 5 ; i ++) {
60
- Mockito .verify (service , Mockito .never ()).sendEvent (Mockito .eq (PACKAGE_NAME ), Mockito .eq (DeployGateEvent .ACTION_SEND_CUSTOM_LOG ), BundleMatcher .eq (createLogExtra ("type" , String .valueOf (i ))));
72
+ CustomLog log = new CustomLog ("type" , String .valueOf (i ));
73
+ Mockito .verify (service , Mockito .never ()).sendEvent (eq (PACKAGE_NAME ), eq (DeployGateEvent .ACTION_SEND_CUSTOM_LOG ), eq (log .toExtras ()));
61
74
}
62
75
63
76
VerificationMode once = Mockito .times (1 );
64
77
65
78
for (int i = 5 ; i < 10 ; i ++) {
66
- Mockito .verify (service , once ).sendEvent (Mockito .eq (PACKAGE_NAME ), Mockito .eq (DeployGateEvent .ACTION_SEND_CUSTOM_LOG ), BundleMatcher .eq (createLogExtra ("type" , String .valueOf (i ))));
79
+ CustomLog log = new CustomLog ("type" , String .valueOf (i ));
80
+ Mockito .verify (service , once ).sendEvent (eq (PACKAGE_NAME ), eq (DeployGateEvent .ACTION_SEND_CUSTOM_LOG ), eq (log .toExtras ()));
67
81
}
68
82
}
69
83
@@ -85,16 +99,40 @@ public void check_buffer_size_works_with_preserve_buffer() throws RemoteExceptio
85
99
VerificationMode once = Mockito .times (1 );
86
100
87
101
for (int i = 0 ; i < 5 ; i ++) {
88
- Mockito .verify (service , once ).sendEvent (Mockito .eq (PACKAGE_NAME ), Mockito .eq (DeployGateEvent .ACTION_SEND_CUSTOM_LOG ), BundleMatcher .eq (createLogExtra ("type" , String .valueOf (i ))));
102
+ CustomLog log = new CustomLog ("type" , String .valueOf (i ));
103
+ Mockito .verify (service , once ).sendEvent (eq (PACKAGE_NAME ), eq (DeployGateEvent .ACTION_SEND_CUSTOM_LOG ), eq (log .toExtras ()));
89
104
}
90
105
91
106
for (int i = 5 ; i < 10 ; i ++) {
92
- Mockito .verify (service , Mockito .never ()).sendEvent (Mockito .eq (PACKAGE_NAME ), Mockito .eq (DeployGateEvent .ACTION_SEND_CUSTOM_LOG ), BundleMatcher .eq (createLogExtra ("type" , String .valueOf (i ))));
107
+ CustomLog log = new CustomLog ("type" , String .valueOf (i ));
108
+ Mockito .verify (service , Mockito .never ()).sendEvent (eq (PACKAGE_NAME ), eq (DeployGateEvent .ACTION_SEND_CUSTOM_LOG ), eq (log .toExtras ()));
93
109
}
94
110
}
95
111
96
112
@ Test (timeout = 3000L )
97
- public void transmit_works_regardless_of_service () {
113
+ public void transmit_works_regardless_of_service () throws RemoteException {
114
+ customLogTransmitter .connect (service );
115
+
116
+ CustomLog noIssue = new CustomLog ("type" , "noIssue" );
117
+ CustomLog successAfterRetries = new CustomLog ("type" , "successAfterRetries" );
118
+ CustomLog retryExceeded = new CustomLog ("type" , "retryExceeded" );
119
+
120
+ doNothing ().when (service ).sendEvent (eq (PACKAGE_NAME ), eq (DeployGateEvent .ACTION_SEND_CUSTOM_LOG ), eq (noIssue .toExtras ()));
121
+ doThrow (TransactionTooLargeException .class ).doThrow (DeadObjectException .class ).doNothing ().when (service ).sendEvent (eq (PACKAGE_NAME ), eq (DeployGateEvent .ACTION_SEND_CUSTOM_LOG ), eq (successAfterRetries .toExtras ()));
122
+ doThrow (RemoteException .class ).when (service ).sendEvent (eq (PACKAGE_NAME ), eq (DeployGateEvent .ACTION_SEND_CUSTOM_LOG ), eq (retryExceeded .toExtras ()));
123
+
124
+ Truth .assertThat (customLogTransmitter .sendLog (noIssue )).isEqualTo (CustomLogTransmitter .SEND_LOG_RESULT_SUCCESS );
125
+ Truth .assertThat (customLogTransmitter .sendLog (successAfterRetries )).isEqualTo (CustomLogTransmitter .SEND_LOG_RESULT_FAILURE_RETRIABLE );
126
+ Truth .assertThat (customLogTransmitter .sendLog (successAfterRetries )).isEqualTo (CustomLogTransmitter .SEND_LOG_RESULT_FAILURE_RETRIABLE );
127
+ Truth .assertThat (customLogTransmitter .sendLog (successAfterRetries )).isEqualTo (CustomLogTransmitter .SEND_LOG_RESULT_SUCCESS );
128
+ Truth .assertThat (customLogTransmitter .sendLog (retryExceeded )).isEqualTo (CustomLogTransmitter .SEND_LOG_RESULT_FAILURE_RETRIABLE );
129
+ Truth .assertThat (customLogTransmitter .sendLog (retryExceeded )).isEqualTo (CustomLogTransmitter .SEND_LOG_RESULT_FAILURE_RETRIABLE );
130
+ Truth .assertThat (customLogTransmitter .sendLog (retryExceeded )).isEqualTo (CustomLogTransmitter .SEND_LOG_RESULT_FAILURE_RETRIABLE );
131
+ Truth .assertThat (customLogTransmitter .sendLog (retryExceeded )).isEqualTo (CustomLogTransmitter .SEND_LOG_RESULT_FAILURE_RETRY_EXCEEDED );
132
+ }
133
+
134
+ @ Test (timeout = 3000L )
135
+ public void retry_barrier_can_prevent_holding_logs_that_always_fail () {
98
136
CustomLogConfiguration configuration = new CustomLogConfiguration .Builder ().setBufferSize (8 ).build ();
99
137
CustomLogTransmitter customLogTransmitter = new CustomLogTransmitter (PACKAGE_NAME , configuration );
100
138
@@ -107,13 +145,36 @@ public void transmit_works_regardless_of_service() {
107
145
Truth .assertThat (customLogTransmitter .getPendingCount ()).isEqualTo (8 );
108
146
}
109
147
110
- private static Bundle createLogExtra (
111
- String type ,
112
- String body
113
- ) {
114
- Bundle bundle = new Bundle ();
115
- bundle .putString ("logType" , type );
116
- bundle .putString ("log" , body );
117
- return bundle ;
148
+ @ Test (timeout = 3000L )
149
+ public void transmit_works_as_expected_with_retry_barrier () throws RemoteException {
150
+ List <Bundle > extras = new ArrayList <>();
151
+ Answer capture = new Answer () {
152
+ @ Override
153
+ public Object answer (InvocationOnMock invocation ) throws Throwable {
154
+ Bundle extra = invocation .getArgument (2 );
155
+ extras .add (extra );
156
+ return null ;
157
+ }
158
+ };
159
+
160
+ CustomLog noIssue = new CustomLog ("type" , "noIssue" );
161
+ CustomLog successAfterRetries = new CustomLog ("type" , "successAfterRetries" );
162
+ CustomLog retryExceeded = new CustomLog ("type" , "retryExceeded" );
163
+
164
+ doAnswer (capture ).when (service ).sendEvent (eq (PACKAGE_NAME ), eq (DeployGateEvent .ACTION_SEND_CUSTOM_LOG ), eq (noIssue .toExtras ()));
165
+ doThrow (TransactionTooLargeException .class ).doThrow (DeadObjectException .class ).doAnswer (capture ).when (service ).sendEvent (eq (PACKAGE_NAME ), eq (DeployGateEvent .ACTION_SEND_CUSTOM_LOG ), eq (successAfterRetries .toExtras ()));
166
+ doThrow (RemoteException .class ).when (service ).sendEvent (eq (PACKAGE_NAME ), eq (DeployGateEvent .ACTION_SEND_CUSTOM_LOG ), eq (retryExceeded .toExtras ()));
167
+
168
+ customLogTransmitter .connect (service );
169
+
170
+ customLogTransmitter .transmit (successAfterRetries .type , successAfterRetries .body );
171
+ customLogTransmitter .transmit (noIssue .type , noIssue .body );
172
+ customLogTransmitter .transmit (retryExceeded .type , retryExceeded .body );
173
+
174
+ Shadows .shadowOf (customLogTransmitter .getLooper ()).idle ();
175
+
176
+ Truth .assertThat (extras ).hasSize (2 );
177
+ Truth .assertThat (extras .get (0 ).getString (DeployGateEvent .EXTRA_LOG )).isEqualTo (successAfterRetries .body );
178
+ Truth .assertThat (extras .get (1 ).getString (DeployGateEvent .EXTRA_LOG )).isEqualTo (noIssue .body );
118
179
}
119
180
}
0 commit comments