-
-
Notifications
You must be signed in to change notification settings - Fork 756
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix race condition in DefaultBroadcasterFactory
Fix race condition in DefaultBroadcasterFactory
- Loading branch information
Jason Burgess
committed
Dec 5, 2011
1 parent
4dc491b
commit 592ed26
Showing
3 changed files
with
144 additions
and
39 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
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
118 changes: 118 additions & 0 deletions
118
modules/cpr/src/test/java/org/atmosphere/cpr/DefaultBroadcasterFactoryTest.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,118 @@ | ||
/* | ||
* To change this template, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
package org.atmosphere.cpr; | ||
|
||
import org.atmosphere.cpr.AtmosphereServlet.AtmosphereConfig; | ||
|
||
import org.atmosphere.util.SimpleBroadcaster; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
import static org.mockito.Mockito.*; | ||
|
||
/** | ||
* | ||
* @author jburgess | ||
*/ | ||
public class DefaultBroadcasterFactoryTest { | ||
|
||
private AtmosphereConfig config; | ||
private DefaultBroadcasterFactory factory; | ||
|
||
@BeforeMethod | ||
public void setUp() throws Exception { | ||
config = mock(AtmosphereConfig.class); | ||
factory = new DefaultBroadcasterFactory(DefaultBroadcaster.class, "NEVER", config); | ||
} | ||
|
||
@Test | ||
public void testGet_0args() { | ||
Broadcaster result = factory.get(); | ||
assert result != null; | ||
assert result instanceof DefaultBroadcaster; | ||
} | ||
|
||
@Test | ||
public void testGet_Object() { | ||
String id = "id"; | ||
Broadcaster result = factory.get(id); | ||
assert result != null; | ||
assert result instanceof DefaultBroadcaster; | ||
assert id.equals(result.getID()); | ||
} | ||
|
||
@Test(expectedExceptions = IllegalStateException.class) | ||
public void testGet_Object_Twice() { | ||
String id = "id"; | ||
factory.get(id); | ||
factory.get(id); | ||
} | ||
|
||
@Test | ||
public void testAdd() { | ||
String id = "id"; | ||
String id2 = "foo"; | ||
Broadcaster b = factory.get(id); | ||
assert factory.add(b, id) == false; | ||
assert factory.lookup(id) != null; | ||
assert factory.add(b, id2) == true; | ||
assert factory.lookup(id2) != null; | ||
} | ||
|
||
@Test | ||
public void testRemove() { | ||
String id = "id"; | ||
String id2 = "foo"; | ||
Broadcaster b = factory.get(id); | ||
Broadcaster b2 = factory.get(id2); | ||
assert factory.remove(b, id2) == false; | ||
assert factory.remove(b2, id) == false; | ||
assert factory.remove(b, id) == true; | ||
assert factory.lookup(id) == null; | ||
} | ||
|
||
@Test | ||
public void testLookup_Class_Object() { | ||
String id = "id"; | ||
String id2 = "foo"; | ||
Broadcaster b = factory.get(id); | ||
assert factory.lookup(DefaultBroadcaster.class, id) != null; | ||
assert factory.lookup(DefaultBroadcaster.class, id2) == null; | ||
} | ||
|
||
@Test(expectedExceptions = IllegalStateException.class) | ||
public void testLookup_Class_Object_BadClass() { | ||
String id = "id"; | ||
factory.get(id); | ||
factory.lookup(SimpleBroadcaster.class, id); | ||
} | ||
|
||
@Test | ||
public void testLookup_Object() { | ||
String id = "id"; | ||
String id2 = "foo"; | ||
factory.get(id); | ||
assert factory.lookup(id) != null; | ||
assert factory.lookup(id2) == null; | ||
} | ||
|
||
@Test | ||
public void testLookup_Object_boolean() { | ||
String id = "id"; | ||
assert factory.lookup(id, false) == null; | ||
Broadcaster b = factory.lookup(id, true); | ||
assert b != null; | ||
assert id.equals(b.getID()); | ||
} | ||
|
||
@Test | ||
public void testLookup_Class_Object_boolean() { | ||
String id = "id"; | ||
assert factory.lookup(DefaultBroadcaster.class, id, false) == null; | ||
Broadcaster b = factory.lookup(DefaultBroadcaster.class, id, true); | ||
assert b != null; | ||
assert b instanceof DefaultBroadcaster; | ||
assert id.equals(b.getID()); | ||
} | ||
} |