Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
148 additions
and
18 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
91 changes: 91 additions & 0 deletions
91
symmetric/src/test/java/org/jumpmind/symmetric/web/NodeConcurrencyFilterTest.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,91 @@ | ||
package org.jumpmind.symmetric.web; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.ServletException; | ||
|
||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
public class NodeConcurrencyFilterTest { | ||
|
||
@Test(groups = "continuous") | ||
public void testFilter() throws Exception { | ||
NodeConcurrencyFilter filter = new NodeConcurrencyFilter(); | ||
filter.maxNumberOfConcurrentWorkers = 2; | ||
filter.waitTimeBetweenRetriesInMs = 0; | ||
|
||
MockWorker one = new MockWorker(filter,"push"); | ||
MockWorker two = new MockWorker(filter,"push"); | ||
MockWorker three = new MockWorker(filter,"push"); | ||
MockWorker other = new MockWorker(filter,"pull"); | ||
|
||
one.start(); | ||
Thread.sleep(500); | ||
|
||
two.start(); | ||
Thread.sleep(500); | ||
|
||
three.start(); | ||
Thread.sleep(500); | ||
|
||
other.start(); | ||
Thread.sleep(500); | ||
|
||
|
||
one.hold = false; | ||
three.hold = false; | ||
other.hold = false; | ||
|
||
Thread.sleep(500); | ||
|
||
Assert.assertEquals(one.success, true); | ||
Assert.assertEquals(three.success, false); | ||
Assert.assertEquals(other.success, true); | ||
|
||
MockWorker four = new MockWorker(filter,"push"); | ||
four.start(); | ||
Thread.sleep(500); | ||
|
||
two.hold = false; | ||
four.hold = false; | ||
|
||
Thread.sleep(500); | ||
|
||
Assert.assertEquals(two.success, true); | ||
Assert.assertEquals(four.success, true); | ||
|
||
} | ||
|
||
class MockWorker extends Thread { | ||
|
||
String servletPath; | ||
boolean hold = true; | ||
|
||
NodeConcurrencyFilter filter; | ||
|
||
boolean success; | ||
|
||
MockWorker(NodeConcurrencyFilter filter, String path) { | ||
this.filter = filter; | ||
this.servletPath = path; | ||
} | ||
|
||
public void run() { | ||
try { | ||
success = filter.doWork(servletPath, new NodeConcurrencyFilter.IWorker() { | ||
public void work() throws ServletException, IOException { | ||
while (hold) { | ||
try { | ||
Thread.sleep(100); | ||
} catch (InterruptedException e) { | ||
} | ||
} | ||
} | ||
}); | ||
} catch (Exception ex) { | ||
throw new RuntimeException(); | ||
} | ||
} | ||
} | ||
} |