|
| 1 | +package io.sentry |
| 2 | + |
| 3 | +import io.sentry.cache.EnvelopeCache |
| 4 | +import io.sentry.cache.IEnvelopeCache |
| 5 | +import io.sentry.transport.NoOpEnvelopeCache |
| 6 | +import java.nio.file.Files |
| 7 | +import java.nio.file.Path |
| 8 | +import kotlin.test.AfterTest |
| 9 | +import kotlin.test.BeforeTest |
| 10 | +import kotlin.test.Test |
| 11 | +import kotlin.test.assertFalse |
| 12 | +import kotlin.test.assertTrue |
| 13 | +import org.mockito.kotlin.any |
| 14 | +import org.mockito.kotlin.mock |
| 15 | +import org.mockito.kotlin.never |
| 16 | +import org.mockito.kotlin.verify |
| 17 | + |
| 18 | +class MovePreviousSessionTest { |
| 19 | + |
| 20 | + private class Fixture { |
| 21 | + val tempDir: Path = Files.createTempDirectory("sentry-move-session-test") |
| 22 | + val options = |
| 23 | + SentryOptions().apply { |
| 24 | + isDebug = true |
| 25 | + setLogger(SystemOutLogger()) |
| 26 | + } |
| 27 | + val cache = mock<EnvelopeCache>() |
| 28 | + |
| 29 | + fun getSUT( |
| 30 | + cacheDirPath: String? = tempDir.toAbsolutePath().toFile().absolutePath, |
| 31 | + isEnableSessionTracking: Boolean = true, |
| 32 | + envelopeCache: IEnvelopeCache? = null, |
| 33 | + ): MovePreviousSession { |
| 34 | + options.cacheDirPath = cacheDirPath |
| 35 | + options.isEnableAutoSessionTracking = isEnableSessionTracking |
| 36 | + options.setEnvelopeDiskCache(envelopeCache ?: EnvelopeCache.create(options)) |
| 37 | + return MovePreviousSession(options) |
| 38 | + } |
| 39 | + |
| 40 | + fun cleanup() { |
| 41 | + tempDir.toFile().deleteRecursively() |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private lateinit var fixture: Fixture |
| 46 | + |
| 47 | + @BeforeTest |
| 48 | + fun setup() { |
| 49 | + fixture = Fixture() |
| 50 | + } |
| 51 | + |
| 52 | + @AfterTest |
| 53 | + fun teardown() { |
| 54 | + fixture.cleanup() |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + fun `when cache dir is null, logs and returns early`() { |
| 59 | + val sut = fixture.getSUT(cacheDirPath = null, envelopeCache = fixture.cache) |
| 60 | + |
| 61 | + sut.run() |
| 62 | + |
| 63 | + verify(fixture.cache, never()).movePreviousSession(any(), any()) |
| 64 | + verify(fixture.cache, never()).flushPreviousSession() |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + fun `when session tracking is disabled, logs and returns early`() { |
| 69 | + val sut = fixture.getSUT(isEnableSessionTracking = false, envelopeCache = fixture.cache) |
| 70 | + |
| 71 | + sut.run() |
| 72 | + |
| 73 | + verify(fixture.cache, never()).movePreviousSession(any(), any()) |
| 74 | + verify(fixture.cache, never()).flushPreviousSession() |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + fun `when envelope cache is not EnvelopeCache instance, does nothing`() { |
| 79 | + val sut = fixture.getSUT(envelopeCache = NoOpEnvelopeCache.getInstance()) |
| 80 | + |
| 81 | + sut.run() |
| 82 | + |
| 83 | + verify(fixture.cache, never()).movePreviousSession(any(), any()) |
| 84 | + verify(fixture.cache, never()).flushPreviousSession() |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + fun `integration test with real EnvelopeCache`() { |
| 89 | + val sut = fixture.getSUT() |
| 90 | + |
| 91 | + // Create a current session file |
| 92 | + val currentSessionFile = EnvelopeCache.getCurrentSessionFile(fixture.options.cacheDirPath!!) |
| 93 | + val previousSessionFile = EnvelopeCache.getPreviousSessionFile(fixture.options.cacheDirPath!!) |
| 94 | + |
| 95 | + currentSessionFile.createNewFile() |
| 96 | + currentSessionFile.writeText("session content") |
| 97 | + |
| 98 | + assertTrue(currentSessionFile.exists()) |
| 99 | + assertFalse(previousSessionFile.exists()) |
| 100 | + |
| 101 | + sut.run() |
| 102 | + |
| 103 | + // Wait for flush to complete |
| 104 | + (fixture.options.envelopeDiskCache as EnvelopeCache).waitPreviousSessionFlush() |
| 105 | + |
| 106 | + // Current session file should have been moved to previous |
| 107 | + assertFalse(currentSessionFile.exists()) |
| 108 | + assertTrue(previousSessionFile.exists()) |
| 109 | + assert(previousSessionFile.readText() == "session content") |
| 110 | + |
| 111 | + fixture.cleanup() |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + fun `integration test when current session file does not exist`() { |
| 116 | + val sut = fixture.getSUT() |
| 117 | + |
| 118 | + val currentSessionFile = EnvelopeCache.getCurrentSessionFile(fixture.options.cacheDirPath!!) |
| 119 | + val previousSessionFile = EnvelopeCache.getPreviousSessionFile(fixture.options.cacheDirPath!!) |
| 120 | + |
| 121 | + assertFalse(currentSessionFile.exists()) |
| 122 | + assertFalse(previousSessionFile.exists()) |
| 123 | + |
| 124 | + sut.run() |
| 125 | + |
| 126 | + (fixture.options.envelopeDiskCache as EnvelopeCache).waitPreviousSessionFlush() |
| 127 | + |
| 128 | + assertFalse(currentSessionFile.exists()) |
| 129 | + assertFalse(previousSessionFile.exists()) |
| 130 | + |
| 131 | + fixture.cleanup() |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + fun `integration test when previous session file already exists`() { |
| 136 | + val sut = fixture.getSUT() |
| 137 | + |
| 138 | + val currentSessionFile = EnvelopeCache.getCurrentSessionFile(fixture.options.cacheDirPath!!) |
| 139 | + val previousSessionFile = EnvelopeCache.getPreviousSessionFile(fixture.options.cacheDirPath!!) |
| 140 | + |
| 141 | + currentSessionFile.createNewFile() |
| 142 | + currentSessionFile.writeText("current session") |
| 143 | + previousSessionFile.createNewFile() |
| 144 | + previousSessionFile.writeText("previous session") |
| 145 | + |
| 146 | + assertTrue(currentSessionFile.exists()) |
| 147 | + assertTrue(previousSessionFile.exists()) |
| 148 | + |
| 149 | + sut.run() |
| 150 | + |
| 151 | + (fixture.options.envelopeDiskCache as EnvelopeCache).waitPreviousSessionFlush() |
| 152 | + |
| 153 | + // Current session file should have been moved to previous |
| 154 | + assertFalse(currentSessionFile.exists()) |
| 155 | + assertTrue(previousSessionFile.exists()) |
| 156 | + assert(previousSessionFile.readText() == "current session") |
| 157 | + |
| 158 | + fixture.cleanup() |
| 159 | + } |
| 160 | +} |
0 commit comments