|
| 1 | +package io.sentry.test |
| 2 | + |
| 3 | +import com.sun.net.httpserver.HttpExchange |
| 4 | +import com.sun.net.httpserver.HttpServer |
| 5 | +import io.sentry.JsonSerializer |
| 6 | +import io.sentry.SentryEnvelope |
| 7 | +import io.sentry.SentryOptions |
| 8 | +import io.sentry.time.Deadline |
| 9 | +import io.sentry.time.JavaMonotonicTicker |
| 10 | +import java.io.Closeable |
| 11 | +import java.net.InetAddress |
| 12 | +import java.net.InetSocketAddress |
| 13 | +import java.util.concurrent.CopyOnWriteArrayList |
| 14 | +import java.util.concurrent.TimeUnit |
| 15 | +import java.util.zip.GZIPInputStream |
| 16 | + |
| 17 | +/** |
| 18 | + * A stand-in for Sentry's ingestion endpoint that integration tests point the SDK at through [dsn]. |
| 19 | + * |
| 20 | + * Envelopes travel through the real HTTP transport, so serialization, compression and the DSN are |
| 21 | + * covered too. It binds a free port so that test tasks running in parallel don't clash, and it |
| 22 | + * works for an app in a child process as well as in the test's own JVM. |
| 23 | + */ |
| 24 | +class MockSentryServer : Closeable { |
| 25 | + private val serializer = JsonSerializer(SentryOptions.empty()) |
| 26 | + private val received = CopyOnWriteArrayList<SentryEnvelope>() |
| 27 | + private val server = |
| 28 | + HttpServer.create(InetSocketAddress(InetAddress.getByName(HOST), 0), 0).apply { |
| 29 | + createContext("/api/") { exchange -> receive(exchange) } |
| 30 | + start() |
| 31 | + } |
| 32 | + |
| 33 | + val dsn: String = "http://key@$HOST:${server.address.port}/0" |
| 34 | + |
| 35 | + val envelopes: List<SentryEnvelope> |
| 36 | + get() = received.toList() |
| 37 | + |
| 38 | + /** Returns the first received envelope matching [predicate], waiting up to [timeoutMillis]. */ |
| 39 | + fun awaitEnvelope( |
| 40 | + timeoutMillis: Long = DEFAULT_TIMEOUT_MILLIS, |
| 41 | + predicate: (SentryEnvelope) -> Boolean, |
| 42 | + ): SentryEnvelope { |
| 43 | + val deadline = |
| 44 | + Deadline.after(JavaMonotonicTicker.getInstance(), timeoutMillis, TimeUnit.MILLISECONDS) |
| 45 | + while (true) { |
| 46 | + received.firstOrNull(predicate)?.let { |
| 47 | + return it |
| 48 | + } |
| 49 | + if (deadline.hasPassed()) { |
| 50 | + throw AssertionError( |
| 51 | + "No matching envelope within ${timeoutMillis}ms, received ${received.size} envelopes" |
| 52 | + ) |
| 53 | + } |
| 54 | + Thread.sleep(POLL_INTERVAL_MILLIS) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + fun reset() { |
| 59 | + received.clear() |
| 60 | + } |
| 61 | + |
| 62 | + override fun close() { |
| 63 | + server.stop(0) |
| 64 | + } |
| 65 | + |
| 66 | + private fun receive(exchange: HttpExchange) { |
| 67 | + try { |
| 68 | + val body = |
| 69 | + if (exchange.requestHeaders.getFirst("Content-Encoding") == "gzip") { |
| 70 | + GZIPInputStream(exchange.requestBody) |
| 71 | + } else { |
| 72 | + exchange.requestBody |
| 73 | + } |
| 74 | + serializer.deserializeEnvelope(body)?.let { received.add(it) } |
| 75 | + exchange.sendResponseHeaders(200, -1) |
| 76 | + } finally { |
| 77 | + exchange.close() |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private companion object { |
| 82 | + // An IP rather than "localhost", which can resolve to IPv4 for the server and IPv6 for the SDK. |
| 83 | + const val HOST = "127.0.0.1" |
| 84 | + const val DEFAULT_TIMEOUT_MILLIS = 10_000L |
| 85 | + const val POLL_INTERVAL_MILLIS = 50L |
| 86 | + } |
| 87 | +} |
0 commit comments