-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reconnect): handle reconnect message type in java websocket sdk (#…
…83) Co-authored-by: bwilloughby <[email protected]>
- Loading branch information
1 parent
ab46d27
commit 6d1255b
Showing
7 changed files
with
116 additions
and
2 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,5 +10,6 @@ public enum MessageTypeEnum { | |
ohlcv, | ||
volume, | ||
hearbeat, | ||
error | ||
error, | ||
reconnect | ||
} |
23 changes: 23 additions & 0 deletions
23
data-api/java-websocket/src/main/java/io/coinapi/websocket/model/Reconnect.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,23 @@ | ||
package io.coinapi.websocket.model; | ||
|
||
import com.dslplatform.json.CompiledJson; | ||
import com.dslplatform.json.JsonAttribute; | ||
|
||
import java.time.OffsetDateTime; | ||
|
||
@CompiledJson | ||
public class Reconnect extends MessageBase { | ||
|
||
private Integer withinSeconds; | ||
private OffsetDateTime beforeTime; | ||
|
||
public Integer getWithinSeconds() { return withinSeconds; } | ||
|
||
@JsonAttribute(name = "within_seconds") | ||
public void setWithinSeconds(Integer withinSeconds) { this.withinSeconds = withinSeconds; } | ||
|
||
public OffsetDateTime getBeforeTime() { return beforeTime; } | ||
|
||
@JsonAttribute(name = "before_time") | ||
public void setBeforeTime(OffsetDateTime beforeTime) { this.beforeTime = beforeTime; } | ||
} |
39 changes: 39 additions & 0 deletions
39
data-api/java-websocket/src/test/java/MessageInjectableCoinAPISDKTest.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,39 @@ | ||
import io.coinapi.websocket.CoinAPIWebSocketImpl; | ||
import io.coinapi.websocket.model.Hello; | ||
import org.junit.Before; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Field; | ||
import java.util.Queue; | ||
import java.util.concurrent.LinkedBlockingDeque; | ||
|
||
public abstract class MessageInjectableCoinAPISDKTest { | ||
protected String apiKey; | ||
protected CoinAPIWebSocketImpl coinAPIWebSocket; | ||
protected Queue messagesQueue; | ||
|
||
@Before | ||
public void configuration() throws IOException, NoSuchFieldException, IllegalAccessException { | ||
Config config = new Config(); | ||
apiKey = config.getPropValues("coinapi_key"); | ||
|
||
//make private messagesQueue variable accessible for injecting messages during testing | ||
Field messagesQueueField = CoinAPIWebSocketImpl.class.getDeclaredField("messagesQueue"); | ||
messagesQueueField.setAccessible(true); | ||
|
||
coinAPIWebSocket = new CoinAPIWebSocketImpl(true); | ||
|
||
//inject this public queue into the class | ||
Queue messagesQueueToSet = new LinkedBlockingDeque(); | ||
messagesQueueField.set(coinAPIWebSocket, messagesQueueToSet); | ||
|
||
messagesQueue = messagesQueueToSet; | ||
} | ||
|
||
public Hello createHello(String type) { | ||
Hello hello = new Hello(); | ||
hello.setApiKey(apiKey); | ||
hello.setSubscribeDataType(new String[]{type}); | ||
return hello; | ||
} | ||
} |
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,39 @@ | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
public class ReconnectTest extends MessageInjectableCoinAPISDKTest { | ||
|
||
@Test | ||
public void getReconnectMessages() throws IOException, InterruptedException { | ||
|
||
AtomicReference<Integer> msgCount = new AtomicReference<>(0); | ||
|
||
coinAPIWebSocket.setOHLCVInvoke(message -> { | ||
//stub invoke to allow connection | ||
}); | ||
|
||
coinAPIWebSocket.setReconnectInvoke(message -> { | ||
msgCount.getAndSet(msgCount.get() + 1); | ||
}); | ||
|
||
coinAPIWebSocket.sendHelloMessage(createHello("ohlcv")); | ||
|
||
String message = "{" + | ||
"\"type\": \"reconnect\"," + | ||
"\"within_seconds\": 10," + | ||
"\"before_time\": \"2020-08-06T19:19:09.7035429Z\"" + | ||
"}"; | ||
|
||
//inject reconnect message into exposed queue | ||
messagesQueue.add(message); | ||
|
||
Thread.sleep(10000); | ||
System.out.println("processing " + msgCount.get() + " reconnect messages"); | ||
|
||
coinAPIWebSocket.closeConnect(); | ||
Assert.assertEquals(1, msgCount.get().intValue()); | ||
} | ||
} |