-
-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves #268: Calling a value is not thread safe (return another value)
The bug was introduced by commit 1acb314 which was supposed to resolve a thread contention issue #254, which now I think, it was not an issue since it was required to have some synchronization mechanism to avoid problems like the one happened here.
- Loading branch information
Luigi R. Viggiano
committed
Jun 6, 2020
1 parent
00096df
commit 1f89055
Showing
2 changed files
with
98 additions
and
15 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
90 changes: 90 additions & 0 deletions
90
owner/src/test/java/org/aeonbits/owner/issues/Issue268.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,90 @@ | ||
package org.aeonbits.owner.issues; | ||
|
||
import org.aeonbits.owner.Config; | ||
import org.aeonbits.owner.ConfigFactory; | ||
import org.junit.Test; | ||
|
||
import static java.lang.Thread.sleep; | ||
import static org.junit.Assert.assertArrayEquals; | ||
import static org.junit.Assert.assertFalse; | ||
|
||
// Issue #268 | ||
public class Issue268 { | ||
interface MyConfig extends Config { | ||
@DefaultValue("Pasha") | ||
String firstName(); | ||
@DefaultValue("Bairov") | ||
String lastName(); | ||
} | ||
|
||
@Test | ||
public void testConcurrentAccess() throws InterruptedException { | ||
final MyConfig cfg = ConfigFactory.create(MyConfig.class); | ||
|
||
final Object semaphore = new Object(); | ||
|
||
final boolean[] exit = {false}; | ||
final boolean[] nameMismatch = { false }; | ||
final boolean[] lastNameMismatch = { false }; | ||
final boolean[] interrupted = {false, false}; | ||
|
||
Thread t1 = new Thread() { | ||
@Override | ||
public void run() { | ||
try { | ||
synchronized (semaphore) { | ||
semaphore.wait(); | ||
} | ||
for (int i = 0; i < 1000 && ! exit[0]; i++) { | ||
String name = cfg.firstName(); | ||
|
||
if (!name.equals("Pasha")) { | ||
System.out.println("name " + name); | ||
nameMismatch[0] = true; | ||
exit[0] = true; | ||
} | ||
} | ||
} catch (InterruptedException e) { | ||
interrupted[0] = true; | ||
throw new IllegalStateException(); | ||
} | ||
} | ||
}; | ||
|
||
Thread t2 = new Thread() { | ||
@Override | ||
public void run() { | ||
try { | ||
synchronized (semaphore) { | ||
semaphore.wait(); | ||
} | ||
for (int i = 0; i < 1000 &&! exit[0] ; i++) { | ||
String lastName = cfg.lastName(); | ||
if (!lastName.equals("Bairov")) { | ||
System.out.println("lastName " + lastName); | ||
lastNameMismatch[0] = true; | ||
exit[0] = true; | ||
} | ||
} | ||
} catch (InterruptedException e) { | ||
interrupted[1] = true; | ||
throw new IllegalStateException(); | ||
} | ||
} | ||
}; | ||
t1.start(); | ||
t2.start(); | ||
|
||
sleep(300); | ||
|
||
synchronized (semaphore) { | ||
semaphore.notifyAll(); | ||
} | ||
|
||
t1.join(); | ||
t2.join(); | ||
assertFalse("mismatch on name", nameMismatch[0]); | ||
assertFalse("mismatch on lastName", lastNameMismatch[0]); | ||
assertArrayEquals(new boolean[] {false, false}, interrupted); | ||
} | ||
} |