Merge pull request #2644 from aspnet/release/2.2

Remove handlers from HubConnection (#2628)
This commit is contained in:
Mikael Mengistu 2018-07-16 16:33:07 -07:00 committed by GitHub
commit 9576c4cd7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 4 deletions

View File

@ -17,12 +17,16 @@ public class CallbackMap {
handlers.computeIfAbsent(target, (ac) -> new ArrayList<>(Arrays.asList(action)));
}
public Boolean containsKey(String key){
public Boolean containsKey(String key) {
return handlers.containsKey(key);
}
public List<ActionBase> get(String key){
public List<ActionBase> get(String key) {
return handlers.get(key);
}
public void remove(String key) {
handlers.remove(key);
}
}

View File

@ -17,7 +17,7 @@ public class HubConnection {
private Gson gson = new Gson();
private HubConnectionState connectionState = HubConnectionState.DISCONNECTED;
public HubConnection(String url, Transport transport){
public HubConnection(String url, Transport transport) {
this.url = url;
this.protocol = new JsonHubProtocol();
this.callback = (payload) -> {
@ -126,4 +126,8 @@ public class HubConnection {
};
handlers.put(target, action);
}
public void remove(String name) {
handlers.remove(name);
}
}

View File

@ -39,6 +39,74 @@ public class HubConnectionTest {
assertEquals(2, value.get(), 0);
}
@Test
public void RemoveHandlerByName() throws Exception {
AtomicReference<Double> value = new AtomicReference<>(0.0);
Transport mockTransport = new MockEchoTransport();
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
Action action = () -> value.getAndUpdate((val) -> val + 1);
hubConnection.on("inc", action);
assertEquals(0.0, value.get(), 0);
hubConnection.start();
hubConnection.send("inc");
// Confirming that our handler was called and that the counter property was incremented.
assertEquals(1, value.get(), 0);
hubConnection.remove("inc");
hubConnection.send("inc");
assertEquals(1, value.get(), 0);
}
@Test
public void AddAndRemoveHandlerImmediately() throws Exception {
AtomicReference<Double> value = new AtomicReference<>(0.0);
Transport mockTransport = new MockEchoTransport();
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
Action action = () -> value.getAndUpdate((val) -> val + 1);
hubConnection.on("inc", action);
hubConnection.remove("inc");
assertEquals(0.0, value.get(), 0);
hubConnection.start();
hubConnection.send("inc");
// Confirming that the handler was removed.
assertEquals(0, value.get(), 0);
}
@Test
public void RemovingMultipleHandlersWithOneCallToRemove() throws Exception {
AtomicReference<Double> value = new AtomicReference<>(0.0);
Transport mockTransport = new MockEchoTransport();
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
Action action = () -> value.getAndUpdate((val) -> val + 1);
Action secondAction = () -> value.getAndUpdate((val) -> val + 2);
hubConnection.on("inc", action);
hubConnection.on("inc", secondAction);
assertEquals(0.0, value.get(), 0);
hubConnection.start();
hubConnection.send("inc");
assertEquals(3, value.get(), 0);
hubConnection.remove("inc");
hubConnection.send("inc");
// Confirm that another invocation doesn't change anything because the handlers have been removed.
assertEquals(3, value.get(), 0);
}
@Test
public void RegisteringMultipleHandlersThatTakeParamsAndBothGetTriggered() throws Exception {
AtomicReference<Double> value = new AtomicReference<>(0.0);
@ -58,7 +126,6 @@ public class HubConnectionTest {
assertEquals(24, value.get(), 0);
}
// We're using AtomicReference<Double> in the send tests instead of int here because Gson has trouble deserializing to Integer
@Test
public void SendWithNoParamsTriggersOnHandler() throws Exception {