Add Comprehensive ZIOApp Behavior Test Suite

🎯 Overview

This PR addresses GitHub issue #9909 by implementing a comprehensive test suite for ZIOApp behavior validation. The test suite ensures that ZIO applications behave correctly during normal completion, failure scenarios, and external signal interruption across all supported platforms.

πŸ“‹ Implementation Summary

Core Test Suite

  • ZIOAppBehaviorSpec - Main test suite for JVM/Native platforms with 9 comprehensive test cases
  • ZIOAppBehaviorJSSpec - JavaScript-specific tests adapted for browser environment limitations
  • ZIOAppBehaviorNativeSpec - Scala Native-specific tests with platform-appropriate behavior validation

Test Infrastructure Components

  • TestableZIOApp - Base class extending ZIOAppDefault with testing capabilities
  • TestResult - Comprehensive result capture without process termination
  • Resource synchronization - Proper coordination between resource acquisition and interruption
  • Platform-aware testing - Different test suites for different platform capabilities

Test Scenarios Covered

  • βœ… App Completion: Success, failure, and exception scenarios with correct exit codes
  • βœ… Finalizer Execution: Normal completion, failure, and multiple finalizer coordination
  • βœ… External Signal Interruption: SIGINT/SIGTERM behavior with finalizer execution
  • βœ… Platform Compatibility: Graceful degradation when signal handlers unavailable
  • βœ… Issue-Specific Scenarios: Targeted tests for related issues #9901, #9807, #9240

πŸ§ͺ Testing Coverage

Test Results: 9/9 Passing βœ…

  1. Successful completion returns ExitCode.success βœ…
  2. Failed completion returns ExitCode.failure βœ…
  3. Exception completion returns ExitCode.failure βœ…
  4. Finalizers run on normal completion βœ…
  5. Finalizers run on failure βœ…
  6. Shutdown handles multiple finalizers βœ…
  7. Graceful degradation when signal handlers unavailable βœ…
  8. SIGINT interruption runs finalizers βœ…
  9. Service finalizers are properly executed βœ…

Issue-Specific Validation

  • #9901: Finalizer execution during Ctrl-C termination βœ…
  • #9807: Clean shutdown behavior regardless of hook timing βœ…
  • #9240: Signal handler availability and graceful degradation βœ…

Platform Compatibility

  • JVM/Native: Full functionality with signal handlers and shutdown hooks βœ…
  • JavaScript: Adapted for browser limitations (no shutdown hooks) βœ…
  • Scala Native: Adapted for native constraints (signal handlers no-op) βœ…

Integration Testing

  • Existing ZIO tests: All existing ZIOAppSpec tests continue to pass βœ…
  • Non-intrusive: Test suite doesn’t interfere with actual ZIO runtime βœ…
  • Code quality: All code formatted according to ZIO standards with scalafmt βœ…

πŸ”§ Technical Details

Files Created

core-tests/jvm-native/src/test/scala/zio/ZIOAppBehaviorSpec.scala (245 lines)
core-tests/js/src/test/scala/zio/ZIOAppBehaviorJSSpec.scala (180 lines)
core-tests/native/src/test/scala/zio/ZIOAppBehaviorNativeSpec.scala (165 lines)
docs/ZIOAppBehaviorTests.md (223 lines)

Key Architectural Decisions

  1. Non-Intrusive Testing: Tests extend ZIOAppDefault for realistic behavior without affecting actual runtime
  2. Resource Synchronization: Uses AtomicBoolean flags and ZIO.whileLoop to ensure proper timing between resource acquisition and interruption
  3. Platform Abstraction: Separate test suites handle platform-specific behavior differences
  4. Comprehensive Result Capture: TestResult captures all relevant execution information without process termination

Solution Architecture

abstract class TestableZIOApp extends ZIOAppDefault {
val finalizerRan = new AtomicBoolean(false)
val resourceAcquired = new AtomicBoolean(false)
def runTest(): UIO[TestResult]
def runTestWithInterruption(): UIO[TestResult] // Waits for resource acquisition
}

🎯 How This Addresses Issue #9909

The original issue requested comprehensive tests for ZIOApp behavior. This implementation provides:

  1. Complete Coverage: Tests all major ZIOApp lifecycle scenarios
  2. Issue Resolution: Addresses specific problems mentioned in related issues
  3. Platform Support: Works across all ZIO-supported platforms
  4. Future-Proof: Extensible infrastructure for additional test scenarios
  5. Production Ready: Robust test suite that validates real-world application behavior

πŸš€ Running the Tests

# Run all ZIOApp behavior tests
sbt "coreTestsJVM/testOnly zio.ZIOAppBehaviorSpec"
# Run platform-specific tests
sbt "coreTestsJS/testOnly zio.ZIOAppBehaviorJSSpec"
sbt "coreTestsNative/testOnly zio.ZIOAppBehaviorNativeSpec"
# Run specific test suites
sbt "coreTestsJVM/testOnly zio.ZIOAppBehaviorSpec -- -t \"App Completion\""

πŸ”„ Recent Updates

Latest Changes (v2)

  • βœ… Fixed finalizer execution timing: Resolved synchronization issues between resource acquisition and interruption
  • βœ… All tests now passing: 9/9 tests pass including previously failing finalizer tests
  • βœ… Applied code formatting: Used scalafmt to ensure code follows ZIO project standards
  • βœ… Verified integration: Confirmed existing ZIO tests continue to pass

Closes #9909
Related: #9901, #9807, #9240

/claim #9909

Claim

Total prize pool $400
Total paid $0
Status Pending
Submitted September 01, 2025
Last updated September 01, 2025

Contributors

VI

Vishwanath Martur

@vishwamartur

100%

Sponsors

ZI

ZIO

@ZIO

$400