Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force tracker timeout #523

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions exampleapp/src/main/java/org/matomo/demo/DemoApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class DemoApp : MatomoApplication() {

// Track this app install, this will only trigger once per app version.
// i.e. "http://org.matomo.demo:1/185DECB5CFE28FDB2F45887022D668B4"
tracker.dispatchTimeout = 1
TrackHelper.track().download().identifier(Extra.ApkChecksum(this)).with(tracker)
// Alternative:
// i.e. "http://org.matomo.demo:1/com.android.vending"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ class DefaultDispatcher(
private val eventCache: EventCache,
private val connectivity: Connectivity,
private val packetFactory: PacketFactory,
private val packetSender: PacketSender
private val packetSender: PacketSender,
callback: ((Exception) -> Unit)?// = null
) : Dispatcher {

private val threadControl = Any()
private val sleepToken = Semaphore(0)

Expand Down Expand Up @@ -203,12 +205,13 @@ class DefaultDispatcher(
Timber.tag(TAG).d("Drained %s events.", drainedEvents.size)
for (packet in packetFactory.buildPackets(drainedEvents)) {
var success: Boolean

var resultException: Exception? = null
if (mDryRunTarget != null) {
Timber.tag(TAG).d("DryRun, stored HttpRequest, now %d.", mDryRunTarget!!.size)
success = mDryRunTarget!!.add(packet)
} else {
success = packetSender.send(packet)
resultException = packetSender.send(packet)
success = resultException == null
}

if (success) {
Expand All @@ -219,6 +222,7 @@ class DefaultDispatcher(
// memory or disk
Timber.tag(TAG).d("Failure while trying to send packet")
retryCounter++
callback?.let { resultException?.let { exception -> it(exception) } }
break
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ open class DefaultDispatcherFactory : DispatcherFactory {
EventCache(EventDiskCache(tracker)),
Connectivity(tracker.matomo.context),
PacketFactory(tracker.apiUrl),
DefaultPacketSender()
DefaultPacketSender(), {}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DefaultPacketSender : PacketSender {
private var mTimeout = Dispatcher.DEFAULT_CONNECTION_TIMEOUT.toLong()
private var mGzip = false

override fun send(packet: Packet): Boolean {
override fun send(packet: Packet): Exception? {
var urlConnection: HttpURLConnection? = null
try {
urlConnection = URL(packet.targetURL).openConnection() as HttpURLConnection
Expand Down Expand Up @@ -114,10 +114,11 @@ class DefaultPacketSender : PacketSender {
Timber.tag(TAG).w("Transmission failed (code=%d, reason=%s)", statusCode, errorReason.toString())
}

return successful
return null
} catch (e: Exception) {
Timber.tag(TAG).e(e, "Transmission failed unexpectedly.")
return false

return e
} finally {
urlConnection?.disconnect()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ interface PacketSender {
/**
* @return true if successful
*/
fun send(packet: Packet): Boolean
fun send(packet: Packet): Exception?

/**
* @param timeout in milliseconds
Expand Down
132 changes: 0 additions & 132 deletions tracker/src/test/java/org/matomo/sdk/MatomoTest.java

This file was deleted.

119 changes: 119 additions & 0 deletions tracker/src/test/java/org/matomo/sdk/MatomoTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Android SDK for Matomo
*
* @link https://github.com/matomo-org/matomo-android-sdk
* @license https://github.com/matomo-org/matomo-sdk-android/blob/master/LICENSE BSD-3 Clause
*/
package org.matomo.sdk

import android.annotation.SuppressLint
import android.app.Application
import androidx.test.core.app.ApplicationProvider
import org.hamcrest.MatcherAssert
import org.hamcrest.Matchers
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.matomo.sdk.Matomo.Companion.getInstance
import org.matomo.sdk.dispatcher.DefaultDispatcher
import org.matomo.sdk.dispatcher.DefaultDispatcherFactory
import org.matomo.sdk.dispatcher.Dispatcher
import org.matomo.sdk.dispatcher.DispatcherFactory
import org.matomo.sdk.dispatcher.EventCache
import org.matomo.sdk.dispatcher.EventDiskCache
import org.matomo.sdk.dispatcher.Packet
import org.matomo.sdk.dispatcher.PacketFactory
import org.matomo.sdk.dispatcher.PacketSender
import org.matomo.sdk.extra.TrackHelper
import org.matomo.sdk.tools.Connectivity
import org.mockito.ArgumentMatchers
import org.mockito.Mockito
import org.robolectric.annotation.Config
import testhelpers.BaseTest
import testhelpers.FullEnvTestRunner
import testhelpers.MatomoTestApplication

@Config(sdk = [28], manifest = Config.NONE, application = MatomoTestApplication::class)
@RunWith(
FullEnvTestRunner::class
)
class MatomoTest : BaseTest() {
@Test
fun testNewTracker() {
val app = ApplicationProvider.getApplicationContext<MatomoTestApplication>()
val tracker = app.onCreateTrackerConfig().build(getInstance(ApplicationProvider.getApplicationContext()))
Assert.assertNotNull(tracker)
Assert.assertEquals(app.onCreateTrackerConfig().apiUrl, tracker.apiUrl)
Assert.assertEquals(app.onCreateTrackerConfig().siteId.toLong(), tracker.siteId.toLong())
}

@Test
fun testNormalTracker() {
val matomo = getInstance(ApplicationProvider.getApplicationContext())
val tracker = TrackerBuilder("http://test/matomo.php", 1, "Default Tracker").build(matomo)
Assert.assertEquals("http://test/matomo.php", tracker.apiUrl)
Assert.assertEquals(1, tracker.siteId.toLong())
}

@Test
fun testTrackerNaming() {
// TODO can we somehow detect naming collisions on tracker creation?
// Would probably requiring us to track created trackers
}

@SuppressLint("InlinedApi")
@Test
fun testLowMemoryDispatch() {
val app = ApplicationProvider.getApplicationContext<MatomoTestApplication>()
val packetSender = Mockito.mock(PacketSender::class.java)
app.matomo.dispatcherFactory = object : DefaultDispatcherFactory() {
override fun build(tracker: Tracker): Dispatcher {
return DefaultDispatcher(
EventCache(EventDiskCache(tracker)),
Connectivity(tracker.matomo.context),
PacketFactory(tracker.apiUrl),
packetSender
)
}
}
val tracker = app.tracker
Assert.assertNotNull(tracker)
tracker.setDispatchInterval(-1)

tracker.track(TrackHelper.track().screen("test").build())
tracker.dispatch()
Mockito.verify(packetSender, Mockito.timeout(500).times(1)).send(ArgumentMatchers.any(Packet::class.java))

tracker.track(TrackHelper.track().screen("test").build())
Mockito.verify(packetSender, Mockito.timeout(500).times(1)).send(ArgumentMatchers.any(Packet::class.java))

app.onTrimMemory(Application.TRIM_MEMORY_UI_HIDDEN)
Mockito.verify(packetSender, Mockito.timeout(500).atLeast(2)).send(ArgumentMatchers.any(Packet::class.java))
}

@Test
fun testGetSettings() {
val tracker1 = Mockito.mock(Tracker::class.java)
Mockito.`when`(tracker1.name).thenReturn("1")
val tracker2 = Mockito.mock(Tracker::class.java)
Mockito.`when`(tracker2.name).thenReturn("2")
val tracker3 = Mockito.mock(Tracker::class.java)
Mockito.`when`(tracker3.name).thenReturn("1")

val matomo = getInstance(ApplicationProvider.getApplicationContext())
Assert.assertEquals(matomo!!.getTrackerPreferences(tracker1), matomo.getTrackerPreferences(tracker1))
Assert.assertNotEquals(matomo.getTrackerPreferences(tracker1), matomo.getTrackerPreferences(tracker2))
Assert.assertEquals(matomo.getTrackerPreferences(tracker1), matomo.getTrackerPreferences(tracker3))
}

@Test
fun testSetDispatcherFactory() {
val matomo = getInstance(ApplicationProvider.getApplicationContext())
val dispatcher = Mockito.mock(Dispatcher::class.java)
val factory = Mockito.mock(DispatcherFactory::class.java)
Mockito.`when`(factory.build(ArgumentMatchers.any(Tracker::class.java))).thenReturn(dispatcher)
MatcherAssert.assertThat(matomo!!.dispatcherFactory, Matchers.`is`(Matchers.not(Matchers.nullValue())))
matomo.dispatcherFactory = factory
MatcherAssert.assertThat(matomo.dispatcherFactory, Matchers.`is`(factory))
}
}
Loading
Loading