diff --git a/src/test/java/org/caosdb/server/database/DatabaseAccessManagerTest.java b/src/test/java/org/caosdb/server/database/DatabaseAccessManagerTest.java index a53b1825113aa7d757033b2f6e0b0a241dafad86..a65c4caca6ea3fecac412edf4255fae119da4596 100644 --- a/src/test/java/org/caosdb/server/database/DatabaseAccessManagerTest.java +++ b/src/test/java/org/caosdb/server/database/DatabaseAccessManagerTest.java @@ -43,41 +43,41 @@ public class DatabaseAccessManagerTest { @Test public void test1() throws InterruptedException { // first invoke a read thread - final ReadThread rt1 = new ReadThread(); + final ReadThread rt1 = new ReadThread("rt1"); rt1.start(); // paused, has acquired read access now // invoke a write thread - final WriteThread wt1 = new WriteThread(); + final WriteThread wt1 = new WriteThread("wt1"); wt1.start(); // paused, has reserved write access now synchronized (this) { - this.wait(1000); + this.wait(500); } // waiting means any processing Assert.assertEquals(wt1.getState(), Thread.State.WAITING); - final ReadThread rt2 = new ReadThread(); + final ReadThread rt2 = new ReadThread("rt2"); rt2.start(); // paused, has acquired a second read access now synchronized (this) { - this.wait(1000); + this.wait(500); } synchronized (rt2) { rt2.notify(); } synchronized (this) { - this.wait(1000); + this.wait(500); } // rt2 was processed while wt1 has reserved but not yet acquired write // access. rt2 terminated after releasing its read access. Assert.assertEquals(rt2.getState(), Thread.State.TERMINATED); - final WriteThread wt2 = new WriteThread(); + final WriteThread wt2 = new WriteThread("wt2"); wt2.start(); // wt2 immediatelly reserves write access and is block // since wt1 already yields it. synchronized (this) { - this.wait(1000); + this.wait(500); } - Assert.assertEquals(wt2.getState(), Thread.State.BLOCKED); + Assert.assertEquals(wt2.getState(), Thread.State.WAITING); // wt1 request write access. synchronized (wt1) { @@ -92,7 +92,7 @@ public class DatabaseAccessManagerTest { rt1.notify(); } synchronized (this) { - this.wait(1000); + this.wait(500); } // rt1 was notified an terminated, releasing the read acccess. // so wt1 acquires write access and pauses the second time. @@ -102,7 +102,7 @@ public class DatabaseAccessManagerTest { wt1.notify(); } synchronized (this) { - this.wait(1000); + this.wait(500); } // wt2 reserves write access as wt1 released it now. Assert.assertEquals(wt1.getState(), Thread.State.TERMINATED); @@ -110,10 +110,10 @@ public class DatabaseAccessManagerTest { // while wt2 has not yet acquired write access, rt3 acquires read // access - final ReadThread rt3 = new ReadThread(); + final ReadThread rt3 = new ReadThread("rt3"); rt3.start(); synchronized (this) { - this.wait(1000); + this.wait(500); } Assert.assertEquals(rt3.getState(), Thread.State.WAITING); @@ -121,24 +121,23 @@ public class DatabaseAccessManagerTest { wt2.notify(); } synchronized (this) { - this.wait(1000); + this.wait(500); } - Assert.assertEquals(wt2.getState(), Thread.State.BLOCKED); + Assert.assertEquals(wt2.getState(), Thread.State.WAITING); synchronized (rt3) { rt3.notify(); } synchronized (this) { - this.wait(1000); + this.wait(500); } Assert.assertEquals(rt3.getState(), Thread.State.TERMINATED); - Assert.assertEquals(wt2.getState(), Thread.State.WAITING); synchronized (wt2) { wt2.notify(); } synchronized (this) { - this.wait(1000); + this.wait(500); } Assert.assertEquals(wt2.getState(), Thread.State.TERMINATED); } @@ -147,14 +146,14 @@ public class DatabaseAccessManagerTest { public void test2() throws InterruptedException { // start a write-thread - final WriteThread wt1 = new WriteThread(); + final WriteThread wt1 = new WriteThread("wt1"); wt1.start(); // start another write-thread. It is blocked until wt1 releases the // write access. - final WriteThread wt2 = new WriteThread(); + final WriteThread wt2 = new WriteThread("wt2"); wt2.start(); synchronized (this) { - this.wait(1000); + this.wait(500); } // and interrupt wt1 after allocating, but before acquiring the write @@ -162,19 +161,19 @@ public class DatabaseAccessManagerTest { wt1.interrupt(); synchronized (this) { - this.wait(1000); + this.wait(500); } synchronized (wt2) { wt2.notify(); } // read access should still be blocked. - final ReadThread rt1 = new ReadThread(); + final ReadThread rt1 = new ReadThread("rt1"); rt1.start(); Assert.assertEquals(rt1.getState(), Thread.State.BLOCKED); synchronized (this) { - this.wait(1000); + this.wait(500); } synchronized (wt2) { wt2.notify(); @@ -185,23 +184,29 @@ public class DatabaseAccessManagerTest { @Override public void run() { try { - System.out.println("T" + currentThread().getId() + " request allocation sa"); + System.out.println(currentThread().getName() + " request to reserve write access"); writeAccess.reserve(); - System.out.println("T" + currentThread().getId() + " reserves sa"); - pause(); + System.out.println(currentThread().getName() + " has reserved write access "); + process("reserved write access"); writeAccess.lockInterruptibly(); - System.out.println("T" + currentThread().getId() + " acquires sa"); - pause(); + System.out.println(currentThread().getName() + " acquires write access"); + process("acquired write access"); writeAccess.unlock(); - System.out.println("T" + currentThread().getId() + " releases sa"); + System.out.println(currentThread().getName() + " releases write access"); } catch (final InterruptedException e) { - System.out.println("T" + currentThread().getId() + " was interrupted"); + System.out.println(currentThread().getName() + " was interrupted"); writeAccess.unlock(); } } - private synchronized void pause() throws InterruptedException { + private synchronized void process(String access) throws InterruptedException { + System.out.println(currentThread().getName() + " processes with " + access); this.wait(); + System.out.println(currentThread().getName() + " is ready with " + access); + } + + public WriteThread(String name) { + super(name); } } @@ -209,19 +214,25 @@ public class DatabaseAccessManagerTest { @Override public void run() { try { - System.out.println("T" + currentThread().getId() + " requests wa"); + System.out.println(currentThread().getName() + " requests read access"); readAccess.acquire(); - System.out.println("T" + currentThread().getId() + " acquires wa"); + System.out.println(currentThread().getName() + " acquires read access"); pause(); readAccess.release(); - System.out.println("T" + currentThread().getId() + " releases wa"); + System.out.println(currentThread().getName() + " releases read access"); } catch (final InterruptedException e) { e.printStackTrace(); } } private synchronized void pause() throws InterruptedException { + System.out.println(currentThread().getName() + " waits "); this.wait(); + System.out.println(currentThread().getName() + " goes on"); + } + + public ReadThread(String name) { + super(name); } } }