"payload history stores and retrieves data correctly"
# Small delay to ensure different timestamps
"payload history respects retention policies"
"payload_history filters by time correctly"
"payload history memory management works under stress"
"payload history handles concurrent access simulation"

payload_history filters by time correctly

`char_history` has length 0, not length 2.

Error in `eval(code, test_env)`: object 'shinypayload' not found

payload history memory management works under stress

payload history handles concurrent access simulation

duration < 3 is not TRUE

`actual`:   FALSE
`expected`: TRUE

test_that("payload storage handles high volume correctly", {
payload_history_config(max_items = 1000, max_age_hours = 1)
# Store large number of payloads rapidly
data = paste("payload", i, "with some data"),
cat("Stored", payload_count, "payloads in", round(duration, 2), "seconds\n")
cat("Rate:", round(payload_count / duration, 1), "payloads/second\n")
expect_true(length(history) <= 1000)  # Should respect max_items
timestamps <- sapply(history, function(x) x$timestamp)
expect_true(all(diff(as.numeric(timestamps)) <= 0))  # Descending order
# Performance should be reasonable (less than 1 second per 100 payloads)
expect_true(duration < (payload_count / 100))
test_that("payload history retrieval is efficient with large datasets", {
payload_history_config(max_items = 2000, max_age_hours = 2)
endpoints <- c("/api/sensors", "/api/logs", "/api/events", "/api/metrics")
timestamp = Sys.time() - (payloads_per_endpoint - i) * 60  # Spread over time
limited_history <- payload_history("/api/sensors", limit = 50)
recent_history <- payload_history("/api/sensors", since = Sys.time() - 1800)  # Last 30 min
retrieval_duration <- as.numeric(difftime(end_time, start_time, units = "secs"))
cat("Retrieved history in", round(retrieval_duration, 3), "seconds\n")
# Performance check - retrieval should be fast
expect_true(retrieval_duration < 1.0)  # Should complete in under 1 second
stats_duration <- as.numeric(difftime(stats_end, stats_start, units = "secs"))
expect_true(stats_duration < 0.5)  # Stats should be very fast
expect_true(overall_stats$total_entries >= length(endpoints) * payloads_per_endpoint * 0.8)  # Allow for retention
test_that("rate limiting performs well under load", {
# Simulate multiple IP addresses making requests
# Simulate concurrent load from multiple IPs
allowed_requests <- allowed_requests + 1
cat("Processed", total_requests, "rate limit checks in", round(duration, 2), "seconds\n")
cat("Rate:", round(total_requests / duration, 1), "checks/second\n")
cat("Allowed:", allowed_requests, "out of", total_requests, "requests\n")
expect_true(duration < 5.0)  # Should complete in reasonable time
expect_true(total_requests / duration > 200)  # Should process at least 200 checks/second
expect_true(allowed_requests <= length(ips) * 100)  # Should respect rate limits
expect_true(allowed_requests >= length(ips) * 90)   # Most IPs should get close to limit
test_that("logging system handles high volume efficiently", {
payload_debug_config(debug_mode = FALSE, log_level = "INFO", max_log_entries = 5000)
level <- sample(c("INFO", "WARN", "ERROR"), 1)
message <- paste("Log message", i, "with some additional context data")
REQUEST_METHOD = sample(c("GET", "POST", "PUT", "DELETE"), 1),
shinypayload:::.log_message(level, message, paste0("/api/endpoint", i %% 10), req)
cat("Generated", log_count, "log entries in", round(duration, 2), "seconds\n")
cat("Rate:", round(log_count / duration, 1), "logs/second\n")
error_logs <- payload_logs(level = "ERROR")
recent_logs <- payload_logs(limit = 100)
retrieval_duration <- as.numeric(difftime(retrieval_end, retrieval_start, units = "secs"))
cat("Retrieved logs in", round(retrieval_duration, 3), "seconds\n")
expect_true(duration < 3.0)  # Logging should be fast
expect_true(retrieval_duration < 0.5)  # Retrieval should be very fast
expect_true(log_count / duration > 500)  # Should handle at least 500 logs/second
expect_true(length(all_logs) <= 5000)  # Should respect max_log_entries
expect_true(length(recent_logs) <= 100)  # Should respect limit
test_that("data processing handles large payloads efficiently", {
cat("Testing payload size:", size, "bytes\n")
data = paste(rep("x", size / 10), collapse = ""),  # Approximate size
json_string <- jsonlite::toJSON(large_data, auto_unbox = TRUE)
cat("Actual payload size:", actual_size, "bytes\n")
req <- list(HTTP_CONTENT_TYPE = "application/json")
cat("Parsed in", round(duration, 3), "seconds\n")
# Performance expectations (should be sub-second for reasonable sizes)
transform_hook <- function(data, content_type, req) {
if (is.list(data) && !is.null(data$metadata)) {
hook_duration <- as.numeric(difftime(hook_end, hook_start, units = "secs"))
cat("Parsed with hooks in", round(hook_duration, 3), "seconds\n")
# Hook processing should not add significant overhead
expect_true(hook_duration < duration + 0.1)
test_that("memory usage remains reasonable under load", {
cat("Initial memory usage:", initial_memory, "MB\n")
data = paste(rep("data", 50), collapse = " "),  # ~200 bytes
properties = as.list(setNames(1:10, paste0("prop", 1:10)))
shinypayload:::.log_message("INFO", paste("Load test log", i))
req <- list(REMOTE_ADDR = paste0("192.168.1.", i))
cat("Memory after load:", loaded_memory, "MB\n")
cat("Memory increase:", round(loaded_memory - initial_memory, 2), "MB\n")
memory_per_payload <- (loaded_memory - initial_memory) / (length(endpoints) * payloads_per_endpoint)
cat("Memory per payload:", round(memory_per_payload * 1024, 1), "KB\n")
expect_true(loaded_memory < initial_memory + 50)  # Should not use more than 50MB extra
expect_true(memory_per_payload < 0.05)  # Should be less than 50KB per payload on average
cleanup_duration <- as.numeric(difftime(cleanup_end, cleanup_start, units = "secs"))
cat("Cleanup completed in", round(cleanup_duration, 3), "seconds\n")
cat("Memory after cleanup:", cleaned_memory, "MB\n")
# Memory should be significantly reduced
expect_true(cleaned_memory < loaded_memory * 0.5)
test_that("concurrent access simulation performs well", {
# This test simulates concurrent access patterns
payload_debug_config(log_level = "WARN")  # Reduce log noise
# Simulate concurrent writers to different endpoints
thread_id = which(endpoints == endpoint),
data = paste("iteration", i, "endpoint", endpoint)
history <- payload_history(endpoint, limit = 5)
shinypayload:::.log_message("INFO", paste("Concurrent operation", i, endpoint))
total_operations <- iterations * length(endpoints)
cat("Completed", total_operations, "operations in", round(duration, 2), "seconds\n")
cat("Rate:", round(total_operations / duration, 1), "operations/second\n")
# Verify data integrity after concurrent operations
# Check that the latest entry has the highest iteration number
expect_true(total_operations / duration > 200)  # Should maintain good throughput