package space.nstart.pcp.pcp_satellites_service.service

import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import kotlin.test.assertNotNull
import kotlin.test.assertNull

@SpringBootTest
class ComplexPlanExecutionLockServiceTest {

    @Autowired
    private lateinit var complexPlanExecutionLockService: ComplexPlanExecutionLockService

    @Test
    fun `same satellite cannot acquire execution lock twice before release`() {
        val firstLock = complexPlanExecutionLockService.tryAcquire(listOf(22L))
        assertNotNull(firstLock)

        try {
            val secondLock = complexPlanExecutionLockService.tryAcquire(listOf(22L))
            assertNull(secondLock)
        } finally {
            firstLock.close()
        }

        val thirdLock = complexPlanExecutionLockService.tryAcquire(listOf(22L))
        assertNotNull(thirdLock)
        thirdLock.close()
    }

    @Test
    fun `different satellites can acquire startup locks independently`() {
        val firstLock = complexPlanExecutionLockService.tryAcquire(listOf(22L))
        val secondLock = complexPlanExecutionLockService.tryAcquire(listOf(23L))

        assertNotNull(firstLock)
        assertNotNull(secondLock)

        firstLock.close()
        secondLock.close()
    }
}
