From 873e21c0a36c2d8262f69c09322ac2a5ca999187 Mon Sep 17 00:00:00 2001 From: Adam Chelminski Date: Thu, 28 Feb 2019 15:04:22 -0500 Subject: [PATCH 01/15] add stress testing sample app --- android/examples/stress-tests/.gitignore | 1 + android/examples/stress-tests/build.gradle | 28 +++ .../examples/stress-tests/proguard-rules.pro | 21 +++ .../stresstests/ExampleInstrumentedTest.java | 26 +++ .../stress-tests/src/main/AndroidManifest.xml | 21 +++ .../examples/stresstests/MainActivity.java | 127 +++++++++++++ .../drawable-v24/ic_launcher_foreground.xml | 34 ++++ .../res/drawable/ic_launcher_background.xml | 170 ++++++++++++++++++ .../src/main/res/layout/activity_main.xml | 61 +++++++ .../res/mipmap-anydpi-v26/ic_launcher.xml | 5 + .../mipmap-anydpi-v26/ic_launcher_round.xml | 5 + .../src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 2963 bytes .../res/mipmap-hdpi/ic_launcher_round.png | Bin 0 -> 4905 bytes .../src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2060 bytes .../res/mipmap-mdpi/ic_launcher_round.png | Bin 0 -> 2783 bytes .../src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4490 bytes .../res/mipmap-xhdpi/ic_launcher_round.png | Bin 0 -> 6895 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 6387 bytes .../res/mipmap-xxhdpi/ic_launcher_round.png | Bin 0 -> 10413 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 9128 bytes .../res/mipmap-xxxhdpi/ic_launcher_round.png | Bin 0 -> 15132 bytes .../src/main/res/values/colors.xml | 6 + .../src/main/res/values/strings.xml | 3 + .../src/main/res/values/styles.xml | 11 ++ .../examples/stresstests/ExampleUnitTest.java | 17 ++ settings.gradle | 2 + 26 files changed, 538 insertions(+) create mode 100644 android/examples/stress-tests/.gitignore create mode 100644 android/examples/stress-tests/build.gradle create mode 100644 android/examples/stress-tests/proguard-rules.pro create mode 100644 android/examples/stress-tests/src/androidTest/java/com/mongodb/stitch/android/examples/stresstests/ExampleInstrumentedTest.java create mode 100644 android/examples/stress-tests/src/main/AndroidManifest.xml create mode 100644 android/examples/stress-tests/src/main/java/com/mongodb/stitch/android/examples/stresstests/MainActivity.java create mode 100644 android/examples/stress-tests/src/main/res/drawable-v24/ic_launcher_foreground.xml create mode 100644 android/examples/stress-tests/src/main/res/drawable/ic_launcher_background.xml create mode 100644 android/examples/stress-tests/src/main/res/layout/activity_main.xml create mode 100644 android/examples/stress-tests/src/main/res/mipmap-anydpi-v26/ic_launcher.xml create mode 100644 android/examples/stress-tests/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml create mode 100644 android/examples/stress-tests/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 android/examples/stress-tests/src/main/res/mipmap-hdpi/ic_launcher_round.png create mode 100644 android/examples/stress-tests/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 android/examples/stress-tests/src/main/res/mipmap-mdpi/ic_launcher_round.png create mode 100644 android/examples/stress-tests/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 android/examples/stress-tests/src/main/res/mipmap-xhdpi/ic_launcher_round.png create mode 100644 android/examples/stress-tests/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 android/examples/stress-tests/src/main/res/mipmap-xxhdpi/ic_launcher_round.png create mode 100644 android/examples/stress-tests/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 android/examples/stress-tests/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png create mode 100644 android/examples/stress-tests/src/main/res/values/colors.xml create mode 100644 android/examples/stress-tests/src/main/res/values/strings.xml create mode 100644 android/examples/stress-tests/src/main/res/values/styles.xml create mode 100644 android/examples/stress-tests/src/test/java/com/mongodb/stitch/android/examples/stresstests/ExampleUnitTest.java diff --git a/android/examples/stress-tests/.gitignore b/android/examples/stress-tests/.gitignore new file mode 100644 index 000000000..796b96d1c --- /dev/null +++ b/android/examples/stress-tests/.gitignore @@ -0,0 +1 @@ +/build diff --git a/android/examples/stress-tests/build.gradle b/android/examples/stress-tests/build.gradle new file mode 100644 index 000000000..103114391 --- /dev/null +++ b/android/examples/stress-tests/build.gradle @@ -0,0 +1,28 @@ +apply plugin: 'com.android.application' +apply plugin: 'digital.wup.android-maven-publish' +apply plugin: 'jacoco-android' + +buildscript { + dependencies { + classpath 'com.android.tools.build:gradle:3.3.0' + classpath 'digital.wup:android-maven-publish:3.3.0' + classpath 'com.dicedmelon.gradle:jacoco-android:0.1.2' + } +} + +android { + compileSdkVersion target_api + defaultConfig { + minSdkVersion min_api + targetSdkVersion target_api + } +} + +dependencies { + implementation project(':android:stitch-android-sdk') + implementation project(':android:android-services:stitch-android-services-mongodb-remote') + implementation "com.android.support:support-v4:${support_library_version}" + implementation "com.android.support:appcompat-v7:${support_library_version}" + implementation "com.android.support:recyclerview-v7:${support_library_version}" + implementation "com.android.support.constraint:constraint-layout:1.1.0" +} diff --git a/android/examples/stress-tests/proguard-rules.pro b/android/examples/stress-tests/proguard-rules.pro new file mode 100644 index 000000000..f1b424510 --- /dev/null +++ b/android/examples/stress-tests/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/android/examples/stress-tests/src/androidTest/java/com/mongodb/stitch/android/examples/stresstests/ExampleInstrumentedTest.java b/android/examples/stress-tests/src/androidTest/java/com/mongodb/stitch/android/examples/stresstests/ExampleInstrumentedTest.java new file mode 100644 index 000000000..8837b6300 --- /dev/null +++ b/android/examples/stress-tests/src/androidTest/java/com/mongodb/stitch/android/examples/stresstests/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.mongodb.stitch.android.examples.stresstests; + +import android.content.Context; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getTargetContext(); + + assertEquals("com.mongodb.stitch.android.examples.stresstests", appContext.getPackageName()); + } +} diff --git a/android/examples/stress-tests/src/main/AndroidManifest.xml b/android/examples/stress-tests/src/main/AndroidManifest.xml new file mode 100644 index 000000000..8c006d7cd --- /dev/null +++ b/android/examples/stress-tests/src/main/AndroidManifest.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/android/examples/stress-tests/src/main/java/com/mongodb/stitch/android/examples/stresstests/MainActivity.java b/android/examples/stress-tests/src/main/java/com/mongodb/stitch/android/examples/stresstests/MainActivity.java new file mode 100644 index 000000000..55808cd85 --- /dev/null +++ b/android/examples/stress-tests/src/main/java/com/mongodb/stitch/android/examples/stresstests/MainActivity.java @@ -0,0 +1,127 @@ +package com.mongodb.stitch.android.examples.stresstests; + +import android.support.annotation.NonNull; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +import com.google.android.gms.tasks.OnCompleteListener; +import com.google.android.gms.tasks.OnSuccessListener; +import com.google.android.gms.tasks.Task; +import com.mongodb.stitch.android.core.Stitch; +import com.mongodb.stitch.android.core.StitchAppClient; +import com.mongodb.stitch.android.core.auth.StitchUser; +import com.mongodb.stitch.android.services.mongodb.remote.RemoteMongoClient; +import com.mongodb.stitch.android.services.mongodb.remote.RemoteMongoCollection; +import com.mongodb.stitch.android.services.mongodb.remote.Sync; +import com.mongodb.stitch.core.auth.providers.anonymous.AnonymousCredential; +import com.mongodb.stitch.core.services.mongodb.remote.sync.ChangeEventListener; +import com.mongodb.stitch.core.services.mongodb.remote.sync.DefaultSyncConflictResolvers; +import com.mongodb.stitch.core.services.mongodb.remote.sync.ErrorListener; +import com.mongodb.stitch.core.services.mongodb.remote.sync.SyncInsertOneResult; +import com.mongodb.stitch.core.services.mongodb.remote.sync.internal.ChangeEvent; + +import org.bson.BsonValue; +import org.bson.Document; +import org.bson.types.ObjectId; + +import java.util.Locale; +import java.util.Random; + +public class MainActivity extends AppCompatActivity { + + private Button button1; + private Button button2; + private TextView label; + + private StitchAppClient stitchAppClient; + private RemoteMongoCollection coll; + private Sync syncedColl; + + private String TAG = MainActivity.class.getName(); + + private void initializeSync() { + stitchAppClient.getAuth().loginWithCredential(new AnonymousCredential()).addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(StitchUser stitchUser) { + coll = stitchAppClient + .getServiceClient(RemoteMongoClient.factory, "mongodb-atlas") + .getDatabase("stress") + .getCollection("tests"); + + syncedColl = coll.sync(); + syncedColl.configure( + DefaultSyncConflictResolvers.remoteWins(), + new ChangeEventListener() { + @Override + public void onEvent(BsonValue documentId, ChangeEvent event) { + Log.i(TAG, String.format("Got event for doc %s: %s", documentId, event)); + } + }, + new ErrorListener() { + @Override + public void onError(BsonValue documentId, Exception error) { + Log.e(TAG, String.format("Got sync error for doc %s: %s", documentId, error)); + } + } + ); + updateLabels(); + } + }); + + } + + private void updateLabels() { + label.setText(String.format( + Locale.US, + "# of synced docs: %d", + syncedColl.getSyncedIds().size() + )); + } + + private void syncNewDocument() { + final StitchUser user = stitchAppClient.getAuth().getUser(); + if (user == null) { + return; + } + final Document docToInsert = new Document() + .append("_id", new ObjectId()) + .append("owner_id", stitchAppClient.getAuth().getUser().getId()) + .append("message", String.format(Locale.US, "%d", (new Random()).nextInt())); + + syncedColl.insertOne(docToInsert).addOnCompleteListener(new OnCompleteListener() { + @Override + public void onComplete(@NonNull Task task) { + updateLabels(); + } + }); + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + if (!Stitch.hasAppClient("stitch-tests-js-sdk-jntlj")) { + Stitch.initializeDefaultAppClient("stitch-tests-js-sdk-jntlj"); + } + + stitchAppClient = Stitch.getDefaultAppClient(); + initializeSync(); + + button1 = findViewById(R.id.button); + button2 = findViewById(R.id.button2); + + label = findViewById(R.id.textView); + + button1.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + syncNewDocument(); + } + }); + } +} diff --git a/android/examples/stress-tests/src/main/res/drawable-v24/ic_launcher_foreground.xml b/android/examples/stress-tests/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 000000000..1f6bb2906 --- /dev/null +++ b/android/examples/stress-tests/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + diff --git a/android/examples/stress-tests/src/main/res/drawable/ic_launcher_background.xml b/android/examples/stress-tests/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 000000000..0d025f9bf --- /dev/null +++ b/android/examples/stress-tests/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/android/examples/stress-tests/src/main/res/layout/activity_main.xml b/android/examples/stress-tests/src/main/res/layout/activity_main.xml new file mode 100644 index 000000000..fbfd91762 --- /dev/null +++ b/android/examples/stress-tests/src/main/res/layout/activity_main.xml @@ -0,0 +1,61 @@ + + + + + +