Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing #7

Closed
AndreaAdvanon opened this issue Nov 1, 2018 · 8 comments
Closed

Testing #7

AndreaAdvanon opened this issue Nov 1, 2018 · 8 comments

Comments

@AndreaAdvanon
Copy link

Is there an easy way to test annotated methods in SpringBootTest?

I'd like to disable distributed-lock entirely, I tried with:

@Profile("!test")
@Configuration
@EnableJdbcDistributedLock
class DistributedLockConfiguration

But this fails at runtime

@AndreaAdvanon
Copy link
Author

@RunWith(SpringRunner::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Sql(
    statements = [
    """
    CREATE TABLE IF NOT EXISTS lock (
        id        BIGINT AUTO_INCREMENT PRIMARY KEY,
        lock_key  VARCHAR(255) UNIQUE ,
        token     VARCHAR(255),
        expireAt  TIMESTAMP
    );
    """
])

this works, could the SQL be avoided?

@alturkovic
Copy link
Owner

What exactly are you trying to test? Have you considered mocks? The library itself has tests you can take a look at. If you want to write additional Jdbc tests, you will have to create the table (there is another way of creating it, take a look at the Jdbc tests).

@AndreaAdvanon
Copy link
Author

You mean using a script? Yes this is what I ended up doing.
I easily mocked the manual lock but how could I do it for the @JdbcLock annotated methods?
Would be nice to be able to just disable the lock during tests imo.

@alturkovic
Copy link
Owner

If you don't add @EnableDistributedLock or any of its aliases, LockBeanPostProcessor should not be registered and the annotation on itself should not do anything. The first example you wrote should work. Have you used @ActiveProfile in your test or some other mechanism to set the active profile to "test"?

@AndreaAdvanon
Copy link
Author

AndreaAdvanon commented Nov 2, 2018

Ah, I know what the problem is:

@Service
class DistributedLockService(@Qualifier("simpleJdbcLock") private val distributedLock: Lock) {

    // This value is the default table name for simpleJdbcLock
    private val STORE_ID = "lock"

    fun <T> runLocked(key: Any, expiration: Long = Duration.ofSeconds(1).toMillis(), block: () -> T): T {
        val token = acquireLock(key, expiration)
        val result = block()
        releaseLock(key, token)
        return result
    }

    fun acquireLock(key: Any, expiration: Long): String =
        distributedLock.acquire(mutableListOf(key.toString()), STORE_ID, expiration)

    fun releaseLock(key: Any, token: String) {
        distributedLock.release(mutableListOf(key.toString()), token, STORE_ID)
    }
}

This causes:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.github.alturkovic.lock.Lock' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=simpleJdbcLock)}

I guess I cannot have both the annotation and the manual locks at the same time if I want to disable it. Or I should provide another DistributedLockService for tests.

@alturkovic
Copy link
Owner

Oh yeah, disabling LockBeanPostProcessor will not help for manual invocations ofcourse :)

And only now I have noticed that I should have rearranged the parameters of the acquire and release methods :/

@alturkovic
Copy link
Owner

I guess I cannot have both the annotation and the manual locks at the same time if I want to disable it. Or I should provide another DistributedLockService for tests.

Exactly, you should write a different DistributedLockService OR mock the distributedLock if you are using the locks manually.

@AndreaAdvanon
Copy link
Author

AndreaAdvanon commented Nov 2, 2018

Yes, I mocked distributedLock in unit test and created the actual tables on integration tests.
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants