|
| 1 | +package io.sentry.dsproxy |
| 2 | + |
| 3 | +import com.nhaarman.mockitokotlin2.mock |
| 4 | +import com.nhaarman.mockitokotlin2.whenever |
| 5 | +import io.sentry.IHub |
| 6 | +import io.sentry.SentryTracer |
| 7 | +import io.sentry.SpanStatus |
| 8 | +import io.sentry.TransactionContext |
| 9 | +import javax.sql.DataSource |
| 10 | +import kotlin.test.AfterTest |
| 11 | +import kotlin.test.Test |
| 12 | +import kotlin.test.assertEquals |
| 13 | +import kotlin.test.assertTrue |
| 14 | +import net.ttddyy.dsproxy.support.ProxyDataSourceBuilder |
| 15 | +import org.hsqldb.jdbc.JDBCDataSource |
| 16 | + |
| 17 | +class SentryQueryExecutionListenerTest { |
| 18 | + |
| 19 | + class Fixture { |
| 20 | + private val hub = mock<IHub>() |
| 21 | + val tx = SentryTracer(TransactionContext("name", "op"), hub) |
| 22 | + val actualDataSource = JDBCDataSource() |
| 23 | + |
| 24 | + fun getSut(withRunningTransaction: Boolean = true): DataSource { |
| 25 | + if (withRunningTransaction) { |
| 26 | + whenever(hub.span).thenReturn(tx) |
| 27 | + } |
| 28 | + actualDataSource.setURL("jdbc:hsqldb:mem:testdb") |
| 29 | + |
| 30 | + actualDataSource.connection.use { |
| 31 | + it.prepareStatement("CREATE TABLE foo (id int)").execute() |
| 32 | + } |
| 33 | + |
| 34 | + val sentryQueryExecutionListener = SentryQueryExecutionListener(hub) |
| 35 | + return ProxyDataSourceBuilder.create(actualDataSource) |
| 36 | + .listener(sentryQueryExecutionListener) |
| 37 | + .build() |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private val fixture = Fixture() |
| 42 | + |
| 43 | + @AfterTest |
| 44 | + fun clean() { |
| 45 | + fixture.actualDataSource.connection.use { |
| 46 | + it.prepareStatement("drop table foo").execute() |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + fun `creates spans for successful calls`() { |
| 52 | + val sut = fixture.getSut() |
| 53 | + |
| 54 | + sut.connection.use { |
| 55 | + it.prepareStatement("INSERT INTO foo VALUES (1)").executeUpdate() |
| 56 | + it.prepareStatement("INSERT INTO foo VALUES (2)").executeUpdate() |
| 57 | + } |
| 58 | + |
| 59 | + assertEquals(2, fixture.tx.children.size) |
| 60 | + fixture.tx.children.forEach { |
| 61 | + assertEquals(SpanStatus.OK, it.status) |
| 62 | + assertEquals("db", it.operation) |
| 63 | + } |
| 64 | + assertEquals("INSERT INTO foo VALUES (1)", fixture.tx.children[0].description) |
| 65 | + assertEquals("INSERT INTO foo VALUES (2)", fixture.tx.children[1].description) |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + fun `creates spans for calls resulting in error`() { |
| 70 | + val sut = fixture.getSut() |
| 71 | + |
| 72 | + try { |
| 73 | + sut.connection.use { |
| 74 | + it.prepareStatement("INSERT INTO foo VALUES ('x')").executeUpdate() |
| 75 | + } |
| 76 | + } catch (e: Exception) { |
| 77 | + } |
| 78 | + |
| 79 | + assertEquals(1, fixture.tx.children.size) |
| 80 | + assertEquals("INSERT INTO foo VALUES ('x')", fixture.tx.children[0].description) |
| 81 | + assertEquals("db", fixture.tx.children[0].operation) |
| 82 | + assertEquals(SpanStatus.INTERNAL_ERROR, fixture.tx.children[0].status) |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + fun `does not create spans when there is no running transactions`() { |
| 87 | + val sut = fixture.getSut(withRunningTransaction = false) |
| 88 | + |
| 89 | + sut.connection.use { |
| 90 | + it.prepareStatement("INSERT INTO foo VALUES (1)").executeUpdate() |
| 91 | + it.prepareStatement("INSERT INTO foo VALUES (2)").executeUpdate() |
| 92 | + } |
| 93 | + |
| 94 | + assertTrue(fixture.tx.children.isEmpty()) |
| 95 | + } |
| 96 | +} |
0 commit comments