Skip to content

Commit

Permalink
fix(analytics): Set start time before generating session ID (#2747)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattcreaser authored Mar 28, 2024
1 parent 6413dbc commit e89fe42
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
1 change: 1 addition & 0 deletions aws-pinpoint-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies {
testImplementation(libs.test.robolectric)
testImplementation(libs.test.androidx.core)
testImplementation(libs.test.kotlin.coroutines)
testImplementation(libs.test.kotest.assertions)

androidTestImplementation(libs.test.androidx.core)
androidTestImplementation(libs.test.androidx.runner)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class AnalyticsClient(
eventTimestamp: Long = System.currentTimeMillis(),
eventId: String = UUID.randomUUID().toString()
): PinpointEvent {
val session = sessionClient?.session ?: Session(context, uniqueId)
val session = sessionClient?.session ?: Session(uniqueId)
return createEvent(
eventType,
session.sessionId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
package com.amplifyframework.pinpoint.core

import android.content.Context
import com.amplifyframework.annotations.InternalAmplifyApi
import java.text.SimpleDateFormat
import java.util.Locale
Expand Down Expand Up @@ -53,11 +52,10 @@ class Session {
}

constructor(
context: Context,
uniqueId: String
) {
this@Session.sessionId = generateSessionId(uniqueId)
this@Session.startTime = System.currentTimeMillis()
this@Session.sessionId = generateSessionId(uniqueId)
this@Session.stopTime = null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SessionClient(
private val context: Context,
val targetingClient: TargetingClient,
private val uniqueId: String,
var analyticsClient: AnalyticsClient? = null,
var analyticsClient: AnalyticsClient? = null
) {

var session: Session? = null
Expand Down Expand Up @@ -66,7 +66,7 @@ class SessionClient(

private fun executeStart() {
targetingClient.updateEndpointProfile()
val newSession = Session(context, uniqueId)
val newSession = Session(uniqueId)
session = newSession
analyticsClient?.let {
val pinpointEvent = it.createEvent(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package com.amplifyframework.pinpoint.core

import io.kotest.matchers.equals.shouldNotBeEqual
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.runTest
import org.junit.Test

class SessionTest {
@Test
fun `sessions created at different times have different session IDs`() = runTest {
val uniqueId = "id"
val session = Session(uniqueId)
delay(10)
val session2 = Session(uniqueId)

session.sessionId shouldNotBeEqual session2.sessionId
}
}

0 comments on commit e89fe42

Please sign in to comment.