-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(callback): support configurable heartbeat interval (#360)
Update protocol to support configurable heartbeat interval. In order to keep backwards compatibility with older version of protocol, if no heartbeat value is provided, we default to 5s. Updated protocol to also support both `camelCase` and `snake_case` extension values.
- Loading branch information
1 parent
42ae752
commit 9cc070c
Showing
5 changed files
with
216 additions
and
24 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
15 changes: 11 additions & 4 deletions
15
...subscription-callback/src/test/java/com/apollographql/subscription/CallbackTestUtils.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 |
---|---|---|
@@ -1,19 +1,26 @@ | ||
package com.apollographql.subscription; | ||
|
||
import static com.apollographql.subscription.callback.SubscriptionCallback.CALLBACK_URL; | ||
import static com.apollographql.subscription.callback.SubscriptionCallback.HEARTBEAT_INTERVAL_MS; | ||
import static com.apollographql.subscription.callback.SubscriptionCallback.SUBSCRIPTION_EXTENSION; | ||
import static com.apollographql.subscription.callback.SubscriptionCallback.SUBSCRIPTION_ID; | ||
import static com.apollographql.subscription.callback.SubscriptionCallback.VERIFIER; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class CallbackTestUtils { | ||
public static Map<String, Object> createMockGraphQLRequest( | ||
String subscriptionId, String callbackUrl) { | ||
var subscriptionExtension = new HashMap<String, Object>(); | ||
subscriptionExtension.put("callback_url", callbackUrl); | ||
subscriptionExtension.put("subscription_id", subscriptionId); | ||
subscriptionExtension.put("verifier", "junit"); | ||
subscriptionExtension.put(CALLBACK_URL, callbackUrl); | ||
subscriptionExtension.put(SUBSCRIPTION_ID, subscriptionId); | ||
subscriptionExtension.put(VERIFIER, "junit"); | ||
subscriptionExtension.put(HEARTBEAT_INTERVAL_MS, 5000); | ||
return Map.of( | ||
"query", | ||
"subscription { counter }", | ||
"extensions", | ||
Map.of("subscription", subscriptionExtension)); | ||
Map.of(SUBSCRIPTION_EXTENSION, subscriptionExtension)); | ||
} | ||
} |
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
113 changes: 113 additions & 0 deletions
113
...lback/src/test/java/com/apollographql/subscription/callback/SubscriptionCallbackTest.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,113 @@ | ||
package com.apollographql.subscription.callback; | ||
|
||
import static com.apollographql.subscription.callback.SubscriptionCallback.CALLBACK_URL; | ||
import static com.apollographql.subscription.callback.SubscriptionCallback.HEARTBEAT_INTERVAL_MS; | ||
import static com.apollographql.subscription.callback.SubscriptionCallback.SUBSCRIPTION_EXTENSION; | ||
import static com.apollographql.subscription.callback.SubscriptionCallback.SUBSCRIPTION_ID; | ||
import static com.apollographql.subscription.callback.SubscriptionCallback.VERIFIER; | ||
|
||
import com.apollographql.subscription.exception.CallbackExtensionNotSpecifiedException; | ||
import com.apollographql.subscription.exception.InvalidCallbackExtensionException; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.Test; | ||
import reactor.test.StepVerifier; | ||
|
||
public class SubscriptionCallbackTest { | ||
|
||
@Test | ||
public void callback_valid() { | ||
var expected = new SubscriptionCallback("foo.com", "1234567890", "junit", 1000); | ||
Map<String, Object> extension = | ||
Map.of( | ||
SUBSCRIPTION_EXTENSION, | ||
Map.of( | ||
CALLBACK_URL, expected.callback_url(), | ||
SUBSCRIPTION_ID, expected.subscription_id(), | ||
VERIFIER, expected.verifier(), | ||
HEARTBEAT_INTERVAL_MS, expected.heartbeatIntervalMs())); | ||
var callback = SubscriptionCallback.parseSubscriptionCallbackExtension(extension); | ||
StepVerifier.create(callback).expectNext(expected).verifyComplete(); | ||
} | ||
|
||
@Test | ||
public void callback_missingExtension_returnsError() { | ||
var callback = SubscriptionCallback.parseSubscriptionCallbackExtension(Map.of()); | ||
StepVerifier.create(callback) | ||
.expectError(CallbackExtensionNotSpecifiedException.class) | ||
.verify(); | ||
} | ||
|
||
@Test | ||
public void callback_missingCallbackUrl_returnsError() { | ||
var callback = | ||
SubscriptionCallback.parseSubscriptionCallbackExtension( | ||
Map.of( | ||
SUBSCRIPTION_EXTENSION, | ||
Map.of( | ||
SUBSCRIPTION_ID, "123", | ||
VERIFIER, "junit", | ||
HEARTBEAT_INTERVAL_MS, 1000))); | ||
StepVerifier.create(callback).expectError(InvalidCallbackExtensionException.class).verify(); | ||
} | ||
|
||
@Test | ||
public void callback_missingHeartbeat_defaults5s() { | ||
var expected = new SubscriptionCallback("foo.com", "1234567890", "junit", 5000); | ||
Map<String, Object> extension = | ||
Map.of( | ||
SUBSCRIPTION_EXTENSION, | ||
Map.of( | ||
CALLBACK_URL, expected.callback_url(), | ||
SUBSCRIPTION_ID, expected.subscription_id(), | ||
VERIFIER, expected.verifier())); | ||
var callback = SubscriptionCallback.parseSubscriptionCallbackExtension(extension); | ||
StepVerifier.create(callback).expectNext(expected).verifyComplete(); | ||
} | ||
|
||
@Test | ||
public void callback_nonIntegerHeartbeat_returnsError() { | ||
var callback = | ||
SubscriptionCallback.parseSubscriptionCallbackExtension( | ||
Map.of( | ||
SUBSCRIPTION_EXTENSION, | ||
Map.of( | ||
CALLBACK_URL, "foo.com", | ||
SUBSCRIPTION_ID, "123", | ||
VERIFIER, "junit", | ||
HEARTBEAT_INTERVAL_MS, "100"))); | ||
StepVerifier.create(callback).expectError(InvalidCallbackExtensionException.class).verify(); | ||
} | ||
|
||
@Test | ||
public void callback_negativeHeartbeat_returnsError() { | ||
var callback = | ||
SubscriptionCallback.parseSubscriptionCallbackExtension( | ||
Map.of( | ||
SUBSCRIPTION_EXTENSION, | ||
Map.of( | ||
CALLBACK_URL, "foo.com", | ||
SUBSCRIPTION_ID, "123", | ||
VERIFIER, "junit", | ||
HEARTBEAT_INTERVAL_MS, -100))); | ||
StepVerifier.create(callback).expectError(InvalidCallbackExtensionException.class).verify(); | ||
} | ||
|
||
@Test | ||
public void callback_usingSnakeCase_valid() { | ||
var expected = new SubscriptionCallback("foo.com", "1234567890", "junit", 1000); | ||
Map<String, Object> extension = | ||
Map.of( | ||
SUBSCRIPTION_EXTENSION, | ||
Map.of( | ||
"callback_url", | ||
expected.callback_url(), | ||
"subscription_id", | ||
expected.subscription_id(), | ||
VERIFIER, | ||
expected.verifier(), | ||
"heartbeat_interval_ms", | ||
expected.heartbeatIntervalMs())); | ||
var callback = SubscriptionCallback.parseSubscriptionCallbackExtension(extension); | ||
StepVerifier.create(callback).expectNext(expected).verifyComplete(); | ||
} | ||
} |