Skip to content

Commit a95d8bb

Browse files
refactor(audience): extend Identity with Get + ClearCache, relocate tests to Core/
Adds two methods to Identity that support the SDK's GDPR and lifecycle needs: Get returns the existing anonymousId without minting a new one (so GDPR DeleteData doesn't register an id just to delete it), and ClearCache drops the in-memory cache without touching disk (so re-Init with a different persistentDataPath reads the new file instead of returning the previous session's id). Relocates the test fixture from Tests/Runtime/IdentityTests.cs to Tests/Runtime/Core/IdentityTests.cs to mirror Runtime/Core/Identity.cs. The relocation expands coverage with two new tests for Get (Get_NoExistingFile_ReturnsNull_AndDoesNotCreate, Get_ExistingFile_ReturnsPersistedId) and swaps hardcoded Path.Combine(_testDir, "imtbl_audience", "identity") calls for the AudiencePaths.IdentityFile helper. The original five tests (NewDirectory_*, ExistingFile_*, SecondCall_*, Reset_*, ConsentNone_*) are preserved byte-equivalent modulo the path helper swap.
1 parent cb8d7b8 commit a95d8bb

2 files changed

Lines changed: 66 additions & 4 deletions

File tree

src/Packages/Audience/Runtime/Core/Identity.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,47 @@ internal sealed class Identity
1515
private static volatile string _cachedId;
1616
private static readonly object _sync = new object();
1717

18+
// Returns the existing anonymous ID, or null if none exists. Never
19+
// creates one — use for call sites that must not implicitly register
20+
// a new ID (e.g. GDPR deletion).
21+
internal static string Get(string persistentDataPath)
22+
{
23+
if (_cachedId != null) return _cachedId;
24+
25+
lock (_sync)
26+
{
27+
if (_cachedId != null) return _cachedId;
28+
29+
try
30+
{
31+
var filePath = AudiencePaths.IdentityFile(persistentDataPath);
32+
if (!File.Exists(filePath)) return null;
33+
34+
_cachedId = File.ReadAllText(filePath).Trim();
35+
return _cachedId;
36+
}
37+
catch (IOException)
38+
{
39+
return null;
40+
}
41+
catch (UnauthorizedAccessException)
42+
{
43+
return null;
44+
}
45+
}
46+
}
47+
48+
// Drops the in-memory cache without touching disk. Called on
49+
// Shutdown/ResetState so a subsequent Init with a different
50+
// persistentDataPath re-reads the file from the new location.
51+
internal static void ClearCache()
52+
{
53+
lock (_sync)
54+
{
55+
_cachedId = null;
56+
}
57+
}
58+
1859
// Returns the anonymous ID, generating and persisting it on first call.
1960
// Returns null without touching disk when consent is None.
2061
// Safe to call from any thread after ImmutableAudience.Init() has run on the main thread.

src/Packages/Audience/Tests/Runtime/IdentityTests.cs renamed to src/Packages/Audience/Tests/Runtime/Core/IdentityTests.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void NewDirectory_GeneratesNonEmptyId_AndWritesFile()
3131
Assert.IsNotNull(id);
3232
Assert.IsNotEmpty(id);
3333

34-
var filePath = Path.Combine(_testDir, "imtbl_audience", "identity");
34+
var filePath = AudiencePaths.IdentityFile(_testDir);
3535
Assert.IsTrue(File.Exists(filePath), "identity file should exist on disk");
3636
}
3737

@@ -40,9 +40,9 @@ public void ExistingFile_ReturnsPreviousId_WithoutGeneratingNew()
4040
{
4141
// Simulate a returning player by pre-writing an identity file (as a previous launch would have done).
4242
var expectedId = "pre-existing-id-from-last-launch";
43-
var dir = Path.Combine(_testDir, "imtbl_audience");
43+
var dir = AudiencePaths.AudienceDir(_testDir);
4444
Directory.CreateDirectory(dir);
45-
File.WriteAllText(Path.Combine(dir, "identity"), expectedId);
45+
File.WriteAllText(AudiencePaths.IdentityFile(_testDir), expectedId);
4646

4747
var result = Identity.GetOrCreate(_testDir, ConsentLevel.Anonymous);
4848

@@ -76,8 +76,29 @@ public void ConsentNone_ReturnsNull_AndNoFileWritten()
7676

7777
Assert.IsNull(id);
7878

79-
var filePath = Path.Combine(_testDir, "imtbl_audience", "identity");
79+
var filePath = AudiencePaths.IdentityFile(_testDir);
8080
Assert.IsFalse(File.Exists(filePath), "identity file must not be written when consent is None");
8181
}
82+
83+
[Test]
84+
public void Get_NoExistingFile_ReturnsNull_AndDoesNotCreate()
85+
{
86+
var id = Identity.Get(_testDir);
87+
88+
Assert.IsNull(id);
89+
var filePath = AudiencePaths.IdentityFile(_testDir);
90+
Assert.IsFalse(File.Exists(filePath), "Get must not create the identity file");
91+
}
92+
93+
[Test]
94+
public void Get_ExistingFile_ReturnsPersistedId()
95+
{
96+
var expectedId = "pre-existing-id";
97+
var dir = AudiencePaths.AudienceDir(_testDir);
98+
Directory.CreateDirectory(dir);
99+
File.WriteAllText(AudiencePaths.IdentityFile(_testDir), expectedId);
100+
101+
Assert.AreEqual(expectedId, Identity.Get(_testDir));
102+
}
82103
}
83104
}

0 commit comments

Comments
 (0)