Giter Site home page Giter Site logo

Testing about distributed-lock HOT 8 CLOSED

alturkovic avatar alturkovic commented on June 19, 2024
Testing

from distributed-lock.

Comments (8)

AndreaAdvanon avatar AndreaAdvanon commented on June 19, 2024 1

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

from distributed-lock.

AndreaAdvanon avatar AndreaAdvanon commented on June 19, 2024
@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?

from distributed-lock.

alturkovic avatar alturkovic commented on June 19, 2024

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).

from distributed-lock.

AndreaAdvanon avatar AndreaAdvanon commented on June 19, 2024

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.

from distributed-lock.

alturkovic avatar alturkovic commented on June 19, 2024

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"?

from distributed-lock.

AndreaAdvanon avatar AndreaAdvanon commented on June 19, 2024

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.

from distributed-lock.

alturkovic avatar alturkovic commented on June 19, 2024

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 :/

from distributed-lock.

alturkovic avatar alturkovic commented on June 19, 2024

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.

from distributed-lock.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.