Comments (8)
Yes, I mocked distributedLock
in unit test and created the actual tables on integration tests.
Thank you!
from distributed-lock.
@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.
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.
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.
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.
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.
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.
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)
- Allow re-entrant code within same Thread? HOT 9
- Need a help HOT 13
- `scriptExecutor` of `RedisTempate` is null HOT 1
- JDBC lock is not working with MySql due to wrong SQL syntax
- JDBC lock not working with PSQL due to SQL syntax HOT 9
- MongoLocked: Don't retry on "fail to acquire" HOT 2
- Cant create a table with reserved name 'lock' for jdbclock as HOT 1
- Need a hand :) ! HOT 3
- Null lock token versus LockNotAvailableException HOT 4
- "RetriableLock should return null if lock is not acquired after the last retry" - ok.. but why? HOT 2
- RedisLock throws RedisReadOnlyException when using RedisStaticMasterReplicaConfiguration and ReadFrom.REPLICA_PREFERRED HOT 2
- Importing the whole library causes auto wiring issues in Spring Boot HOT 5
- Does it Support Spring Boot 3 ? HOT 7
- Getting Started HOT 2
- Option not to throw exception on lock fail HOT 1
- JDBC Manual Controlled Lock HOT 4
- Testing Annotated Locks HOT 8
- What is the reason behind only using JitPack? HOT 1
- About throwing option added in recent PR HOT 1
- Wrong documentation HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from distributed-lock.