@@ -36,6 +36,7 @@ import com.amplifyframework.hub.HubEvent
36
36
import com.amplifyframework.testutils.HubAccumulator
37
37
import com.amplifyframework.testutils.Resources
38
38
import com.amplifyframework.testutils.Sleep
39
+ import com.amplifyframework.testutils.junitcategories.StressTests
39
40
import com.amplifyframework.testutils.sync.SynchronousAuth
40
41
import java.util.UUID
41
42
import java.util.concurrent.TimeUnit
@@ -45,8 +46,80 @@ import org.junit.Assert
45
46
import org.junit.Before
46
47
import org.junit.BeforeClass
47
48
import org.junit.Test
49
+ import org.junit.experimental.categories.Category
48
50
51
+ @Category(StressTests ::class )
49
52
class PinpointAnalyticsStressTest {
53
+
54
+ companion object {
55
+ private const val CREDENTIALS_RESOURCE_NAME = " credentials"
56
+ private const val CONFIGURATION_NAME = " amplifyconfiguration"
57
+ private const val COGNITO_CONFIGURATION_TIMEOUT = 5 * 1000L
58
+ private const val PINPOINT_ROUNDTRIP_TIMEOUT = 1 * 1000L
59
+ private const val FLUSH_TIMEOUT = 1 * 500L
60
+ private const val RECORD_INSERTION_TIMEOUT = 1 * 1000L
61
+ private const val UNIQUE_ID_KEY = " UniqueId"
62
+ private const val PREFERENCES_AND_FILE_MANAGER_SUFFIX = " 515d6767-01b7-49e5-8273-c8d11b0f331d"
63
+ private lateinit var synchronousAuth: SynchronousAuth
64
+ private lateinit var preferences: SharedPreferences
65
+ private lateinit var appId: String
66
+ private lateinit var uniqueId: String
67
+ private lateinit var pinpointClient: PinpointClient
68
+
69
+ @BeforeClass
70
+ @JvmStatic
71
+ fun setupBefore () {
72
+ val context = ApplicationProvider .getApplicationContext<Context >()
73
+ @RawRes val resourceId = Resources .getRawResourceId(context, CONFIGURATION_NAME )
74
+ appId = readAppIdFromResource(context, resourceId)
75
+ preferences = context.getSharedPreferences(
76
+ " ${appId}$PREFERENCES_AND_FILE_MANAGER_SUFFIX " ,
77
+ Context .MODE_PRIVATE
78
+ )
79
+ setUniqueId()
80
+ Amplify .Auth .addPlugin(AWSCognitoAuthPlugin () as AuthPlugin <* >)
81
+ Amplify .addPlugin(AWSPinpointAnalyticsPlugin ())
82
+ Amplify .configure(context)
83
+ Sleep .milliseconds(COGNITO_CONFIGURATION_TIMEOUT )
84
+ synchronousAuth = SynchronousAuth .delegatingTo(Amplify .Auth )
85
+ }
86
+
87
+ private fun setUniqueId () {
88
+ uniqueId = UUID .randomUUID().toString()
89
+ preferences.edit().putString(UNIQUE_ID_KEY , uniqueId).commit()
90
+ }
91
+
92
+ private fun readCredentialsFromResource (context : Context , @RawRes resourceId : Int ): Pair <String , String >? {
93
+ val resource = Resources .readAsJson(context, resourceId)
94
+ var userCredentials: Pair <String , String >? = null
95
+ return try {
96
+ val credentials = resource.getJSONArray(" credentials" )
97
+ for (index in 0 until credentials.length()) {
98
+ val credential = credentials.getJSONObject(index)
99
+ val username = credential.getString(" username" )
100
+ val password = credential.getString(" password" )
101
+ userCredentials = Pair (username, password)
102
+ }
103
+ userCredentials
104
+ } catch (jsonReadingFailure: JSONException ) {
105
+ throw RuntimeException (jsonReadingFailure)
106
+ }
107
+ }
108
+
109
+ private fun readAppIdFromResource (context : Context , @RawRes resourceId : Int ): String {
110
+ val resource = Resources .readAsJson(context, resourceId)
111
+ return try {
112
+ val analyticsJson = resource.getJSONObject(" analytics" )
113
+ val pluginsJson = analyticsJson.getJSONObject(" plugins" )
114
+ val pluginJson = pluginsJson.getJSONObject(" awsPinpointAnalyticsPlugin" )
115
+ val pinpointJson = pluginJson.getJSONObject(" pinpointAnalytics" )
116
+ pinpointJson.getString(" appId" )
117
+ } catch (jsonReadingFailure: JSONException ) {
118
+ throw RuntimeException (jsonReadingFailure)
119
+ }
120
+ }
121
+ }
122
+
50
123
@Before
51
124
fun flushEvents () {
52
125
val context = ApplicationProvider .getApplicationContext<Context >()
@@ -68,16 +141,16 @@ class PinpointAnalyticsStressTest {
68
141
}
69
142
70
143
/* *
71
- * Calls Analytics.recordEvent on an event with 5 attributes 100 times
144
+ * Calls Analytics.recordEvent on an event with 5 attributes 50 times
72
145
*/
73
146
@Test
74
147
fun testMultipleRecordEvent () {
75
148
var eventName: String
76
149
val hubAccumulator =
77
150
HubAccumulator .create(HubChannel .ANALYTICS , AnalyticsChannelEventName .FLUSH_EVENTS , 2 ).start()
78
151
79
- for (i in 1 .. 50 ) {
80
- eventName = " Amplify-event$i "
152
+ repeat( 50 ) {
153
+ eventName = " Amplify-event" + UUID .randomUUID().toString()
81
154
val event = AnalyticsEvent .builder()
82
155
.name(eventName)
83
156
.addProperty(" AnalyticsStringProperty" , " Pancakes" )
@@ -104,8 +177,8 @@ class PinpointAnalyticsStressTest {
104
177
val hubAccumulator =
105
178
HubAccumulator .create(HubChannel .ANALYTICS , AnalyticsChannelEventName .FLUSH_EVENTS , 2 ).start()
106
179
107
- for (i in 1 .. 50 ) {
108
- eventName = " Amplify-event$i "
180
+ repeat( 50 ) {
181
+ eventName = " Amplify-event" + UUID .randomUUID().toString()
109
182
val event = AnalyticsEvent .builder()
110
183
.name(eventName)
111
184
.addProperty(" AnalyticsStringProperty1" , " Pancakes" )
@@ -167,7 +240,7 @@ class PinpointAnalyticsStressTest {
167
240
val analyticsHubEventAccumulator =
168
241
HubAccumulator .create(HubChannel .ANALYTICS , AnalyticsChannelEventName .FLUSH_EVENTS , 50 )
169
242
.start()
170
- val eventName = " Amplify-event"
243
+ val eventName = " Amplify-event" + UUID .randomUUID().toString()
171
244
val event = AnalyticsEvent .builder()
172
245
.name(eventName)
173
246
.addProperty(" AnalyticsStringProperty" , " Pancakes" )
@@ -186,7 +259,7 @@ class PinpointAnalyticsStressTest {
186
259
}
187
260
188
261
/* *
189
- * calls Analytics.recordEvent, then calls Analytics.flushEvent; 50 times
262
+ * calls Analytics.recordEvent, then calls Analytics.flushEvent; 30 times
190
263
*/
191
264
@Test
192
265
fun testFlushEvent_AfterRecordEvent () {
@@ -195,8 +268,8 @@ class PinpointAnalyticsStressTest {
195
268
HubAccumulator .create(HubChannel .ANALYTICS , AnalyticsChannelEventName .FLUSH_EVENTS , 35 )
196
269
.start()
197
270
198
- for (i in 1 .. 30 ) {
199
- eventName = " Amplify-event$i "
271
+ repeat( 30 ) {
272
+ eventName = " Amplify-event" + UUID .randomUUID().toString()
200
273
val event = AnalyticsEvent .builder()
201
274
.name(eventName)
202
275
.addProperty(" AnalyticsStringProperty" , " Pancakes" )
@@ -425,72 +498,4 @@ class PinpointAnalyticsStressTest {
425
498
}
426
499
return result
427
500
}
428
-
429
- companion object {
430
- private const val CREDENTIALS_RESOURCE_NAME = " credentials"
431
- private const val CONFIGURATION_NAME = " amplifyconfiguration"
432
- private const val COGNITO_CONFIGURATION_TIMEOUT = 5 * 1000L
433
- private const val PINPOINT_ROUNDTRIP_TIMEOUT = 1 * 1000L
434
- private const val FLUSH_TIMEOUT = 1 * 500L
435
- private const val RECORD_INSERTION_TIMEOUT = 1 * 1000L
436
- private const val UNIQUE_ID_KEY = " UniqueId"
437
- private const val PREFERENCES_AND_FILE_MANAGER_SUFFIX = " 515d6767-01b7-49e5-8273-c8d11b0f331d"
438
- private lateinit var synchronousAuth: SynchronousAuth
439
- private lateinit var preferences: SharedPreferences
440
- private lateinit var appId: String
441
- private lateinit var uniqueId: String
442
- private lateinit var pinpointClient: PinpointClient
443
- @BeforeClass
444
- @JvmStatic
445
- fun setupBefore () {
446
- val context = ApplicationProvider .getApplicationContext<Context >()
447
- @RawRes val resourceId = Resources .getRawResourceId(context, CONFIGURATION_NAME )
448
- appId = readAppIdFromResource(context, resourceId)
449
- preferences = context.getSharedPreferences(
450
- " ${appId}$PREFERENCES_AND_FILE_MANAGER_SUFFIX " ,
451
- Context .MODE_PRIVATE
452
- )
453
- setUniqueId()
454
- Amplify .Auth .addPlugin(AWSCognitoAuthPlugin () as AuthPlugin <* >)
455
- Amplify .addPlugin(AWSPinpointAnalyticsPlugin ())
456
- Amplify .configure(context)
457
- Sleep .milliseconds(COGNITO_CONFIGURATION_TIMEOUT )
458
- synchronousAuth = SynchronousAuth .delegatingTo(Amplify .Auth )
459
- }
460
-
461
- private fun setUniqueId () {
462
- uniqueId = UUID .randomUUID().toString()
463
- preferences.edit().putString(UNIQUE_ID_KEY , uniqueId).commit()
464
- }
465
-
466
- private fun readCredentialsFromResource (context : Context , @RawRes resourceId : Int ): Pair <String , String >? {
467
- val resource = Resources .readAsJson(context, resourceId)
468
- var userCredentials: Pair <String , String >? = null
469
- return try {
470
- val credentials = resource.getJSONArray(" credentials" )
471
- for (index in 0 until credentials.length()) {
472
- val credential = credentials.getJSONObject(index)
473
- val username = credential.getString(" username" )
474
- val password = credential.getString(" password" )
475
- userCredentials = Pair (username, password)
476
- }
477
- userCredentials
478
- } catch (jsonReadingFailure: JSONException ) {
479
- throw RuntimeException (jsonReadingFailure)
480
- }
481
- }
482
-
483
- private fun readAppIdFromResource (context : Context , @RawRes resourceId : Int ): String {
484
- val resource = Resources .readAsJson(context, resourceId)
485
- return try {
486
- val analyticsJson = resource.getJSONObject(" analytics" )
487
- val pluginsJson = analyticsJson.getJSONObject(" plugins" )
488
- val pluginJson = pluginsJson.getJSONObject(" awsPinpointAnalyticsPlugin" )
489
- val pinpointJson = pluginJson.getJSONObject(" pinpointAnalytics" )
490
- pinpointJson.getString(" appId" )
491
- } catch (jsonReadingFailure: JSONException ) {
492
- throw RuntimeException (jsonReadingFailure)
493
- }
494
- }
495
- }
496
501
}
0 commit comments