:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(flags): let feature-flags tests take database and Redis URLs from the environment (#109415)
Anderse35f949bedbd
diff --git a/rust/feature-flags/README.md b/rust/feature-flags/README.mdindex 35aa51d6efb7..104af08a0d0e 100644--- a/rust/feature-flags/README.md+++ b/rust/feature-flags/README.md@@ -21,6 +21,9 @@ Then, run the tests:cargo test --package feature-flags```+To run against other databases, for example from a second checkout, set `TEST_DATABASE_URL`, `TEST_PERSONS_DATABASE_URL` and `TEST_REDIS_URL`; unset, they default to `test_posthog`, `posthog_persons` and Redis database 0 on localhost.+Keep `TEST_REDIS_URL` off Redis database 1, which the dedicated flags cache tests use.+## To watch changes```shdiff --git a/rust/feature-flags/src/config.rs b/rust/feature-flags/src/config.rsindex 2549b8f795af..faed79a74065 100644--- a/rust/feature-flags/src/config.rs+++ b/rust/feature-flags/src/config.rs@@ -1096,10 +1096,27 @@ impl Config {/// `test_flag_definitions_billing_counter` — their negative-case sleeps/// must cover at least one full flush window.pub fn default_test_config() -> Self {+ Self::test_config_with_env(|name| std::env::var(name).ok())+ }++ fn test_config_with_env(env: impl Fn(&str) -> Option<String>) -> Self {+ let url = |name: &str, default: &str| {+ env(name)+ .filter(|value| !value.is_empty())+ .unwrap_or_else(|| default.to_string())+ };+ let database_url = url(+ "TEST_DATABASE_URL",+ "postgres://posthog:posthog@localhost:5432/test_posthog",+ );+ let persons_database_url = url(+ "TEST_PERSONS_DATABASE_URL",+ "postgres://posthog:posthog@localhost:5432/posthog_persons",+ );Self {continuous_profiling: ContinuousProfilingConfig::default(),address: SocketAddr::from_str("127.0.0.1:0").unwrap(),- redis_url: "redis://localhost:6379/".to_string(),+ redis_url: url("TEST_REDIS_URL", "redis://localhost:6379/"),redis_reader_url: "".to_string(),flags_redis_url: "".to_string(),flags_redis_reader_url: "".to_string(),@@ -1108,15 +1125,11 @@ impl Config {flag_definitions_dedicated_redis_enabled: FlexBool(false),redis_response_timeout_ms: 100,redis_connection_timeout_ms: 5000,- write_database_url: "postgres://posthog:posthog@localhost:5432/test_posthog"- .to_string(),- read_database_url: "postgres://posthog:posthog@localhost:5432/test_posthog".to_string(),- persons_write_database_url: "postgres://posthog:posthog@localhost:5432/posthog_persons"- .to_string(),- persons_read_database_url: "postgres://posthog:posthog@localhost:5432/posthog_persons"- .to_string(),- behavioral_cohorts_read_database_url:- "postgres://posthog:posthog@localhost:5432/test_posthog".to_string(),+ write_database_url: database_url.clone(),+ read_database_url: database_url.clone(),+ persons_write_database_url: persons_database_url.clone(),+ persons_read_database_url: persons_database_url,+ behavioral_cohorts_read_database_url: database_url,flags_secret_keys: String::new(),secret_key: "test-secret-key-at-least-32-bytes-long".to_string(),realtime_cohort_evaluation_team_ids: TeamIdCollection::None,@@ -1373,7 +1386,7 @@ mod tests {#[test]fn test_default_test_config() {- let config = Config::default_test_config();+ let config = Config::test_config_with_env(|_| None);assert_eq!(config.address, SocketAddr::from_str("127.0.0.1:0").unwrap());assert_eq!(config.write_database_url,@@ -1401,25 +1414,39 @@ mod tests {);}+ #[test]+ fn test_default_test_config_env_overrides() {+ let config = Config::test_config_with_env(|name| match name {+ "TEST_DATABASE_URL" => Some("postgres://db/main".to_string()),+ "TEST_PERSONS_DATABASE_URL" => Some("postgres://db/persons".to_string()),+ "TEST_REDIS_URL" => Some("redis://cache/2".to_string()),+ _ => None,+ });+ assert_eq!(config.write_database_url, "postgres://db/main");+ assert_eq!(config.read_database_url, "postgres://db/main");+ assert_eq!(+ config.behavioral_cohorts_read_database_url,+ "postgres://db/main"+ );+ assert_eq!(config.persons_write_database_url, "postgres://db/persons");+ assert_eq!(config.persons_read_database_url, "postgres://db/persons");+ assert_eq!(config.redis_url, "redis://cache/2");+ }+#[test]fn test_default_test_config_static() {let config = &*DEFAULT_TEST_CONFIG;+ let expected = Config::default_test_config();assert_eq!(config.address, SocketAddr::from_str("127.0.0.1:0").unwrap());- assert_eq!(- config.write_database_url,- "postgres://posthog:posthog@localhost:5432/test_posthog"- );- assert_eq!(- config.read_database_url,- "postgres://posthog:posthog@localhost:5432/test_posthog"- );+ assert_eq!(config.write_database_url, expected.write_database_url);+ assert_eq!(config.read_database_url, expected.read_database_url);assert_eq!(config.max_concurrency, 1000);assert_eq!(config.max_pg_connections, 10);assert_eq!(config.min_non_persons_reader_connections, 0);assert_eq!(config.min_non_persons_writer_connections, 0);assert_eq!(config.min_persons_reader_connections, 0);assert_eq!(config.min_persons_writer_connections, 0);- assert_eq!(config.redis_url, "redis://localhost:6379/");+ assert_eq!(config.redis_url, expected.redis_url);assert_eq!(config.team_ids_to_track, TeamIdCollection::All);assert_eq!(config.new_analytics_capture_excluded_team_ids,diff --git a/rust/feature-flags/src/database_pools.rs b/rust/feature-flags/src/database_pools.rsindex 47533bab4a7c..57aabfb6feb1 100644--- a/rust/feature-flags/src/database_pools.rs+++ b/rust/feature-flags/src/database_pools.rs@@ -483,11 +483,11 @@ mod tests {assert!(config.is_persons_db_routing_enabled());assert_eq!(config.get_persons_read_database_url(),- "postgres://posthog:posthog@localhost:5432/posthog_persons"+ config.persons_read_database_url);assert_eq!(config.get_persons_write_database_url(),- "postgres://posthog:posthog@localhost:5432/posthog_persons"+ config.persons_write_database_url);}diff --git a/rust/feature-flags/src/flags/flag_operations.rs b/rust/feature-flags/src/flags/flag_operations.rsindex b3acacb63504..d3b171e23719 100644--- a/rust/feature-flags/src/flags/flag_operations.rs+++ b/rust/feature-flags/src/flags/flag_operations.rs@@ -759,7 +759,7 @@ mod tests {#[tokio::test]async fn test_error_handling() {- let redis_client = setup_redis_client(Some("redis://localhost:6379/".to_string())).await;+ let redis_client = setup_redis_client(None).await;let context = TestContext::new(None).await;// Test malformed JSON in Redis (using Django-compatible hypercache key format)diff --git a/rust/feature-flags/src/utils/test_utils.rs b/rust/feature-flags/src/utils/test_utils.rsindex 2825a10ba75a..4331f284bd03 100644--- a/rust/feature-flags/src/utils/test_utils.rs+++ b/rust/feature-flags/src/utils/test_utils.rs@@ -199,10 +199,7 @@ pub async fn write_flags_wire_json_to_redis(}pub async fn setup_redis_client(url: Option<String>) -> Arc<dyn RedisClientTrait + Send + Sync> {- let redis_url = match url {- Some(value) => value,- None => "redis://localhost:6379/".to_string(),- };+ let redis_url = url.unwrap_or_else(|| DEFAULT_TEST_CONFIG.redis_url.clone());// Use reasonable test timeout defaultsconst TEST_RESPONSE_TIMEOUT_MS: u64 = 1000; // 1s for tests - longer than production to avoid flaky testsconst TEST_CONNECTION_TIMEOUT_MS: u64 = 5000; // 5s connection timeout