Skip to content

Commit e89fe42

Browse files
authored
fix(analytics): Set start time before generating session ID (#2747)
1 parent 6413dbc commit e89fe42

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

aws-pinpoint-core/build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ dependencies {
3939
testImplementation(libs.test.robolectric)
4040
testImplementation(libs.test.androidx.core)
4141
testImplementation(libs.test.kotlin.coroutines)
42+
testImplementation(libs.test.kotest.assertions)
4243

4344
androidTestImplementation(libs.test.androidx.core)
4445
androidTestImplementation(libs.test.androidx.runner)

aws-pinpoint-core/src/main/java/com/amplifyframework/pinpoint/core/AnalyticsClient.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class AnalyticsClient(
9696
eventTimestamp: Long = System.currentTimeMillis(),
9797
eventId: String = UUID.randomUUID().toString()
9898
): PinpointEvent {
99-
val session = sessionClient?.session ?: Session(context, uniqueId)
99+
val session = sessionClient?.session ?: Session(uniqueId)
100100
return createEvent(
101101
eventType,
102102
session.sessionId,

aws-pinpoint-core/src/main/java/com/amplifyframework/pinpoint/core/Session.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515
package com.amplifyframework.pinpoint.core
1616

17-
import android.content.Context
1817
import com.amplifyframework.annotations.InternalAmplifyApi
1918
import java.text.SimpleDateFormat
2019
import java.util.Locale
@@ -53,11 +52,10 @@ class Session {
5352
}
5453

5554
constructor(
56-
context: Context,
5755
uniqueId: String
5856
) {
59-
this@Session.sessionId = generateSessionId(uniqueId)
6057
this@Session.startTime = System.currentTimeMillis()
58+
this@Session.sessionId = generateSessionId(uniqueId)
6159
this@Session.stopTime = null
6260
}
6361

aws-pinpoint-core/src/main/java/com/amplifyframework/pinpoint/core/SessionClient.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class SessionClient(
2323
private val context: Context,
2424
val targetingClient: TargetingClient,
2525
private val uniqueId: String,
26-
var analyticsClient: AnalyticsClient? = null,
26+
var analyticsClient: AnalyticsClient? = null
2727
) {
2828

2929
var session: Session? = null
@@ -66,7 +66,7 @@ class SessionClient(
6666

6767
private fun executeStart() {
6868
targetingClient.updateEndpointProfile()
69-
val newSession = Session(context, uniqueId)
69+
val newSession = Session(uniqueId)
7070
session = newSession
7171
analyticsClient?.let {
7272
val pinpointEvent = it.createEvent(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package com.amplifyframework.pinpoint.core
17+
18+
import io.kotest.matchers.equals.shouldNotBeEqual
19+
import kotlinx.coroutines.delay
20+
import kotlinx.coroutines.test.runTest
21+
import org.junit.Test
22+
23+
class SessionTest {
24+
@Test
25+
fun `sessions created at different times have different session IDs`() = runTest {
26+
val uniqueId = "id"
27+
val session = Session(uniqueId)
28+
delay(10)
29+
val session2 = Session(uniqueId)
30+
31+
session.sessionId shouldNotBeEqual session2.sessionId
32+
}
33+
}

0 commit comments

Comments
 (0)