-
Notifications
You must be signed in to change notification settings - Fork 987
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* fix:deadlock when reentrant exclusive lock #2905 * confirm won't blocking other thread * apply suggestions Co-authored-by: Andy(Jingzhang)Chen <iRoiocam@gmail.com>
- Loading branch information
Showing
2 changed files
with
68 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/test/java/io/lettuce/core/protocol/SharedLockTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.lettuce.core.protocol; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class SharedLockTest { | ||
|
||
@Test | ||
public void safety_on_reentrant_lock_exclusive_on_writers() throws InterruptedException { | ||
final SharedLock sharedLock = new SharedLock(); | ||
CountDownLatch cnt = new CountDownLatch(1); | ||
try { | ||
sharedLock.incrementWriters(); | ||
|
||
String result = sharedLock.doExclusive(() -> { | ||
return sharedLock.doExclusive(() -> { | ||
return "ok"; | ||
}); | ||
}); | ||
if ("ok".equals(result)) { | ||
cnt.countDown(); | ||
} | ||
} finally { | ||
sharedLock.decrementWriters(); | ||
} | ||
|
||
boolean await = cnt.await(1, TimeUnit.SECONDS); | ||
Assertions.assertTrue(await); | ||
|
||
// verify writers won't be negative after finally decrementWriters | ||
String result = sharedLock.doExclusive(() -> { | ||
return sharedLock.doExclusive(() -> { | ||
return "ok"; | ||
}); | ||
}); | ||
|
||
Assertions.assertEquals("ok", result); | ||
|
||
// and other writers should be passed after exclusive lock released | ||
CountDownLatch cntOtherThread = new CountDownLatch(1); | ||
new Thread(() -> { | ||
try { | ||
sharedLock.incrementWriters(); | ||
cntOtherThread.countDown(); | ||
} finally { | ||
sharedLock.decrementWriters(); | ||
} | ||
}).start(); | ||
|
||
await = cntOtherThread.await(1, TimeUnit.SECONDS); | ||
Assertions.assertTrue(await); | ||
} | ||
|
||
} |