|
| 1 | +/* |
| 2 | + * !++ |
| 3 | + * QDS - Quick Data Signalling Library |
| 4 | + * !- |
| 5 | + * Copyright (C) 2002 - 2020 Devexperts LLC |
| 6 | + * !- |
| 7 | + * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 8 | + * If a copy of the MPL was not distributed with this file, You can obtain one at |
| 9 | + * http://mozilla.org/MPL/2.0/. |
| 10 | + * !__ |
| 11 | + */ |
| 12 | +package com.dxfeed.api.test; |
| 13 | + |
| 14 | +import com.devexperts.qd.QDContract; |
| 15 | +import com.devexperts.qd.QDDistributor; |
| 16 | +import com.devexperts.qd.QDFactory; |
| 17 | +import com.devexperts.qd.ng.RecordBuffer; |
| 18 | +import com.devexperts.qd.ng.RecordMode; |
| 19 | +import com.devexperts.qd.qtp.QDEndpoint; |
| 20 | +import com.dxfeed.api.DXEndpoint; |
| 21 | +import com.dxfeed.api.DXFeedSubscription; |
| 22 | +import com.dxfeed.api.impl.DXEndpointImpl; |
| 23 | +import com.dxfeed.api.osub.ObservableSubscription; |
| 24 | +import com.dxfeed.api.osub.ObservableSubscriptionChangeListener; |
| 25 | +import com.dxfeed.api.osub.TimeSeriesSubscriptionSymbol; |
| 26 | +import com.dxfeed.event.candle.Candle; |
| 27 | +import com.dxfeed.event.candle.CandlePeriod; |
| 28 | +import com.dxfeed.event.candle.CandleSymbol; |
| 29 | +import com.dxfeed.event.candle.CandleType; |
| 30 | +import com.dxfeed.promise.Promise; |
| 31 | +import junit.framework.TestCase; |
| 32 | + |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.Collections; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Set; |
| 37 | + |
| 38 | +public class HistorySubscriptionTest extends TestCase { |
| 39 | + private static final CandleSymbol CANDLE_SYMBOL = |
| 40 | + CandleSymbol.valueOf("TEST", CandlePeriod.valueOf(1, CandleType.MINUTE)); |
| 41 | + private static final TimeSeriesSubscriptionSymbol<CandleSymbol> SUB_SYMBOL = |
| 42 | + new TimeSeriesSubscriptionSymbol<>(CANDLE_SYMBOL, 0); |
| 43 | + |
| 44 | + public void testEmptySubscription() { |
| 45 | + // tests [QD-1230] - absence of phantom subscription if not actually subscribed |
| 46 | + QDFactory.getDefaultScheme(); |
| 47 | + System.out.println(); |
| 48 | + int result = 0; |
| 49 | + for (int mask = 0; mask < 16; mask++) |
| 50 | + result += checkEmptySub((mask & 8) != 0, (mask & 4) != 0, (mask & 2) != 0, (mask & 1) != 0) ? 0 : 1; |
| 51 | + assertEquals("tests failed: " + result, 0, result); |
| 52 | + } |
| 53 | + |
| 54 | + private boolean checkEmptySub(boolean storeEverything, boolean getPromise, boolean subscribe, boolean publish) { |
| 55 | + System.out.println("testing storeEverything " + storeEverything + |
| 56 | + ", getPromise " + getPromise + ", subscribe " + subscribe + ", publish " + publish); |
| 57 | + |
| 58 | + DXEndpoint endpoint = DXEndpoint.newBuilder() |
| 59 | + .withRole(DXEndpoint.Role.LOCAL_HUB) |
| 60 | + .withProperty(DXEndpoint.DXENDPOINT_STORE_EVERYTHING_PROPERTY, String.valueOf(storeEverything)) |
| 61 | + .build(); |
| 62 | + endpoint.executor(Runnable::run); |
| 63 | + |
| 64 | + Promise<List<Candle>> promise = getPromise && publish ? |
| 65 | + endpoint.getFeed().getTimeSeriesPromise(Candle.class, CANDLE_SYMBOL, 0, 1000) : |
| 66 | + Promise.completed(Collections.singletonList(new Candle(CANDLE_SYMBOL))); |
| 67 | + ArrayList<Candle> events = new ArrayList<>(); |
| 68 | + DXFeedSubscription<Candle> sub = new DXFeedSubscription<>(Candle.class); |
| 69 | + sub.addEventListener(events::addAll); |
| 70 | + sub.setSymbols(SUB_SYMBOL); |
| 71 | + if (subscribe) |
| 72 | + sub.attach(endpoint.getFeed()); |
| 73 | + assertEquals("garbage events received", 0, events.size()); |
| 74 | + if (publish) |
| 75 | + endpoint.getPublisher().publishEvents(Collections.singletonList(new Candle(CANDLE_SYMBOL))); |
| 76 | + assertEquals("events received", subscribe && publish ? 1 : 0, events.size()); |
| 77 | + sub.setSymbols(); |
| 78 | + sub.close(); |
| 79 | + assertEquals("events received", subscribe && publish ? 1 : 0, events.size()); |
| 80 | + assertEquals("promise received", 1, promise.getResult().size()); |
| 81 | + |
| 82 | + boolean result = assertEmptySub(endpoint); |
| 83 | + endpoint.close(); |
| 84 | + System.out.println("test " + (result ? "passed" : "FAILED")); |
| 85 | + System.out.println(); |
| 86 | + return result; |
| 87 | + } |
| 88 | + |
| 89 | + private boolean assertEmptySub(DXEndpoint endpoint) { |
| 90 | + ArrayList<String> messages = new ArrayList<>(); |
| 91 | + |
| 92 | + QDEndpoint qdEndpoint = ((DXEndpointImpl) endpoint).getQDEndpoint(); |
| 93 | + RecordBuffer buffer = RecordBuffer.getInstance(RecordMode.HISTORY_SUBSCRIPTION); |
| 94 | + qdEndpoint.getCollector(QDContract.HISTORY).examineSubscription(buffer); |
| 95 | + if (!buffer.isEmpty()) |
| 96 | + messages.add("QDHistory subscription [" + buffer.size() + "] " + buffer.next()); |
| 97 | + buffer.clear(); |
| 98 | + QDDistributor distributor = qdEndpoint.getCollector(QDContract.HISTORY).distributorBuilder().build(); |
| 99 | + //noinspection StatementWithEmptyBody |
| 100 | + while (distributor.getAddedRecordProvider().retrieve(buffer)) { |
| 101 | + // do nothing |
| 102 | + } |
| 103 | + if (!buffer.isEmpty()) |
| 104 | + messages.add("QDDistributor added [" + buffer.size() + "] " + buffer.next()); |
| 105 | + buffer.clear(); |
| 106 | + distributor.close(); |
| 107 | + |
| 108 | + ObservableSubscription<Candle> subscription = endpoint.getPublisher().getSubscription(Candle.class); |
| 109 | + ObservableSubscriptionChangeListener listener = new ObservableSubscriptionChangeListener() { |
| 110 | + @Override |
| 111 | + public void symbolsAdded(Set<?> symbols) { |
| 112 | + messages.add("dxFeed added " + symbols); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void symbolsRemoved(Set<?> symbols) { |
| 117 | + messages.add("dxFeed removed " + symbols); |
| 118 | + } |
| 119 | + }; |
| 120 | + subscription.addChangeListener(listener); |
| 121 | + subscription.removeChangeListener(listener); |
| 122 | + |
| 123 | + messages.forEach(System.out::println); |
| 124 | + return messages.isEmpty(); |
| 125 | + } |
| 126 | +} |
0 commit comments