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

fix(analytics): Set start time before generating session ID #2747

Merged
merged 2 commits into from
Mar 28, 2024
Merged
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 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
) {
[email protected] = generateSessionId(uniqueId)
[email protected] = System.currentTimeMillis()
[email protected] = generateSessionId(uniqueId)
[email protected] = 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
}
}
Loading