| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550 |
- import { liveOption } from '@grafana/ui/src/components/RefreshPicker/RefreshPicker';
- import { DataSourceApi, DataQuery } from '@grafana/ui/src/types/datasource';
- import { ExploreId, ExploreState } from 'app/types';
- import { actionCreatorFactory } from 'app/core/redux/actionCreatorFactory';
- import {
- startSubscriptionsEpic,
- startSubscriptionsAction,
- SubscriptionDataReceivedPayload,
- startSubscriptionAction,
- startSubscriptionEpic,
- limitMessageRatePayloadAction,
- } from './epics';
- import { makeExploreItemState } from './reducers';
- import { epicTester } from 'test/core/redux/epicTester';
- import {
- resetExploreAction,
- updateDatasourceInstanceAction,
- changeRefreshIntervalAction,
- clearQueriesAction,
- } from './actionTypes';
- const setup = (options: any = {}) => {
- const url = '/api/datasources/proxy/20/api/prom/tail?query=%7Bfilename%3D%22%2Fvar%2Flog%2Fdocker.log%22%7D';
- const webSocketUrl = 'ws://localhost' + url;
- const refId = options.refId || 'A';
- const exploreId = ExploreId.left;
- const datasourceInstance: DataSourceApi = options.datasourceInstance || {
- id: 1337,
- query: jest.fn(),
- name: 'test',
- testDatasource: jest.fn(),
- convertToStreamTargets: () => [
- {
- url,
- refId,
- },
- ],
- resultToSeriesData: data => [data],
- };
- const itemState = makeExploreItemState();
- const explore: Partial<ExploreState> = {
- [exploreId]: {
- ...itemState,
- datasourceInstance,
- refreshInterval: options.refreshInterval || liveOption.value,
- queries: [{} as DataQuery],
- },
- };
- const state: any = {
- explore,
- };
- return { url, state, refId, webSocketUrl, exploreId };
- };
- const dataReceivedActionCreator = actionCreatorFactory<SubscriptionDataReceivedPayload>('test').create();
- describe('startSubscriptionsEpic', () => {
- describe('when startSubscriptionsAction is dispatched', () => {
- describe('and datasource supports convertToStreamTargets', () => {
- describe('and explore is Live', () => {
- it('then correct actions should be dispatched', () => {
- const { state, refId, webSocketUrl, exploreId } = setup();
- epicTester(startSubscriptionsEpic, state)
- .whenActionIsDispatched(startSubscriptionsAction({ exploreId, dataReceivedActionCreator }))
- .thenResultingActionsEqual(
- startSubscriptionAction({
- exploreId,
- refId,
- url: webSocketUrl,
- dataReceivedActionCreator,
- })
- );
- });
- });
- describe('and explore is not Live', () => {
- it('then no actions should be dispatched', () => {
- const { state, exploreId } = setup({ refreshInterval: '10s' });
- epicTester(startSubscriptionsEpic, state)
- .whenActionIsDispatched(startSubscriptionsAction({ exploreId, dataReceivedActionCreator }))
- .thenNoActionsWhereDispatched();
- });
- });
- });
- describe('and datasource does not support streaming', () => {
- it('then no actions should be dispatched', () => {
- const { state, exploreId } = setup({ datasourceInstance: {} });
- epicTester(startSubscriptionsEpic, state)
- .whenActionIsDispatched(startSubscriptionsAction({ exploreId, dataReceivedActionCreator }))
- .thenNoActionsWhereDispatched();
- });
- });
- });
- });
- describe('startSubscriptionEpic', () => {
- describe('when startSubscriptionAction is dispatched', () => {
- describe('and datasource supports resultToSeriesData', () => {
- it('then correct actions should be dispatched', () => {
- const { state, webSocketUrl, refId, exploreId } = setup();
- epicTester(startSubscriptionEpic, state)
- .whenActionIsDispatched(
- startSubscriptionAction({ url: webSocketUrl, refId, exploreId, dataReceivedActionCreator })
- )
- .thenNoActionsWhereDispatched()
- .whenWebSocketReceivesData({ data: [1, 2, 3] })
- .thenResultingActionsEqual(
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [1, 2, 3] } as any,
- dataReceivedActionCreator,
- })
- )
- .whenWebSocketReceivesData({ data: [4, 5, 6] })
- .thenResultingActionsEqual(
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [1, 2, 3] } as any,
- dataReceivedActionCreator,
- }),
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [4, 5, 6] } as any,
- dataReceivedActionCreator,
- })
- );
- });
- });
- describe('and datasource does not support resultToSeriesData', () => {
- it('then no actions should be dispatched', () => {
- const { state, webSocketUrl, refId, exploreId } = setup({ datasourceInstance: {} });
- epicTester(startSubscriptionEpic, state)
- .whenActionIsDispatched(
- startSubscriptionAction({ url: webSocketUrl, refId, exploreId, dataReceivedActionCreator })
- )
- .thenNoActionsWhereDispatched()
- .whenWebSocketReceivesData({ data: [1, 2, 3] })
- .thenNoActionsWhereDispatched();
- });
- });
- });
- describe('when an subscription is active', () => {
- describe('and resetExploreAction is dispatched', () => {
- it('then subscription should be unsubscribed', () => {
- const { state, webSocketUrl, refId, exploreId } = setup();
- epicTester(startSubscriptionEpic, state)
- .whenActionIsDispatched(
- startSubscriptionAction({ url: webSocketUrl, refId, exploreId, dataReceivedActionCreator })
- )
- .thenNoActionsWhereDispatched()
- .whenWebSocketReceivesData({ data: [1, 2, 3] })
- .thenResultingActionsEqual(
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [1, 2, 3] } as any,
- dataReceivedActionCreator,
- })
- )
- .whenActionIsDispatched(resetExploreAction())
- .whenWebSocketReceivesData({ data: [4, 5, 6] })
- .thenResultingActionsEqual(
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [1, 2, 3] } as any,
- dataReceivedActionCreator,
- })
- );
- });
- });
- describe('and updateDatasourceInstanceAction is dispatched', () => {
- describe('and exploreId matches the websockets', () => {
- it('then subscription should be unsubscribed', () => {
- const { state, webSocketUrl, refId, exploreId } = setup();
- epicTester(startSubscriptionEpic, state)
- .whenActionIsDispatched(
- startSubscriptionAction({
- url: webSocketUrl,
- refId,
- exploreId,
- dataReceivedActionCreator,
- })
- )
- .thenNoActionsWhereDispatched()
- .whenWebSocketReceivesData({ data: [1, 2, 3] })
- .thenResultingActionsEqual(
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [1, 2, 3] } as any,
- dataReceivedActionCreator,
- })
- )
- .whenActionIsDispatched(updateDatasourceInstanceAction({ exploreId, datasourceInstance: null }))
- .whenWebSocketReceivesData({ data: [4, 5, 6] })
- .thenResultingActionsEqual(
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [1, 2, 3] } as any,
- dataReceivedActionCreator,
- })
- );
- });
- });
- describe('and exploreId does not match the websockets', () => {
- it('then subscription should not be unsubscribed', () => {
- const { state, webSocketUrl, refId, exploreId } = setup();
- epicTester(startSubscriptionEpic, state)
- .whenActionIsDispatched(
- startSubscriptionAction({
- url: webSocketUrl,
- refId,
- exploreId,
- dataReceivedActionCreator,
- })
- )
- .thenNoActionsWhereDispatched()
- .whenWebSocketReceivesData({ data: [1, 2, 3] })
- .thenResultingActionsEqual(
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [1, 2, 3] } as any,
- dataReceivedActionCreator,
- })
- )
- .whenActionIsDispatched(
- updateDatasourceInstanceAction({ exploreId: ExploreId.right, datasourceInstance: null })
- )
- .whenWebSocketReceivesData({ data: [4, 5, 6] })
- .thenResultingActionsEqual(
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [1, 2, 3] } as any,
- dataReceivedActionCreator,
- }),
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [4, 5, 6] } as any,
- dataReceivedActionCreator,
- })
- );
- });
- });
- });
- describe('and changeRefreshIntervalAction is dispatched', () => {
- describe('and exploreId matches the websockets', () => {
- describe('and refreshinterval is not "Live"', () => {
- it('then subscription should be unsubscribed', () => {
- const { state, webSocketUrl, refId, exploreId } = setup();
- epicTester(startSubscriptionEpic, state)
- .whenActionIsDispatched(
- startSubscriptionAction({
- url: webSocketUrl,
- refId,
- exploreId,
- dataReceivedActionCreator,
- })
- )
- .thenNoActionsWhereDispatched()
- .whenWebSocketReceivesData({ data: [1, 2, 3] })
- .thenResultingActionsEqual(
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [1, 2, 3] } as any,
- dataReceivedActionCreator,
- })
- )
- .whenActionIsDispatched(changeRefreshIntervalAction({ exploreId, refreshInterval: '10s' }))
- .whenWebSocketReceivesData({ data: [4, 5, 6] })
- .thenResultingActionsEqual(
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [1, 2, 3] } as any,
- dataReceivedActionCreator,
- })
- );
- });
- });
- describe('and refreshinterval is "Live"', () => {
- it('then subscription should not be unsubscribed', () => {
- const { state, webSocketUrl, refId, exploreId } = setup();
- epicTester(startSubscriptionEpic, state)
- .whenActionIsDispatched(
- startSubscriptionAction({
- url: webSocketUrl,
- refId,
- exploreId,
- dataReceivedActionCreator,
- })
- )
- .thenNoActionsWhereDispatched()
- .whenWebSocketReceivesData({ data: [1, 2, 3] })
- .thenResultingActionsEqual(
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [1, 2, 3] } as any,
- dataReceivedActionCreator,
- })
- )
- .whenActionIsDispatched(changeRefreshIntervalAction({ exploreId, refreshInterval: liveOption.value }))
- .whenWebSocketReceivesData({ data: [4, 5, 6] })
- .thenResultingActionsEqual(
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [1, 2, 3] } as any,
- dataReceivedActionCreator,
- }),
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [4, 5, 6] } as any,
- dataReceivedActionCreator,
- })
- );
- });
- });
- });
- describe('and exploreId does not match the websockets', () => {
- it('then subscription should not be unsubscribed', () => {
- const { state, webSocketUrl, refId, exploreId } = setup();
- epicTester(startSubscriptionEpic, state)
- .whenActionIsDispatched(
- startSubscriptionAction({
- url: webSocketUrl,
- refId,
- exploreId,
- dataReceivedActionCreator,
- })
- )
- .thenNoActionsWhereDispatched()
- .whenWebSocketReceivesData({ data: [1, 2, 3] })
- .thenResultingActionsEqual(
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [1, 2, 3] } as any,
- dataReceivedActionCreator,
- })
- )
- .whenActionIsDispatched(changeRefreshIntervalAction({ exploreId: ExploreId.right, refreshInterval: '10s' }))
- .whenWebSocketReceivesData({ data: [4, 5, 6] })
- .thenResultingActionsEqual(
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [1, 2, 3] } as any,
- dataReceivedActionCreator,
- }),
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [4, 5, 6] } as any,
- dataReceivedActionCreator,
- })
- );
- });
- });
- });
- describe('and clearQueriesAction is dispatched', () => {
- describe('and exploreId matches the websockets', () => {
- it('then subscription should be unsubscribed', () => {
- const { state, webSocketUrl, refId, exploreId } = setup();
- epicTester(startSubscriptionEpic, state)
- .whenActionIsDispatched(
- startSubscriptionAction({
- url: webSocketUrl,
- refId,
- exploreId,
- dataReceivedActionCreator,
- })
- )
- .thenNoActionsWhereDispatched()
- .whenWebSocketReceivesData({ data: [1, 2, 3] })
- .thenResultingActionsEqual(
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [1, 2, 3] } as any,
- dataReceivedActionCreator,
- })
- )
- .whenActionIsDispatched(clearQueriesAction({ exploreId }))
- .whenWebSocketReceivesData({ data: [4, 5, 6] })
- .thenResultingActionsEqual(
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [1, 2, 3] } as any,
- dataReceivedActionCreator,
- })
- );
- });
- });
- describe('and exploreId does not match the websockets', () => {
- it('then subscription should not be unsubscribed', () => {
- const { state, webSocketUrl, refId, exploreId } = setup();
- epicTester(startSubscriptionEpic, state)
- .whenActionIsDispatched(
- startSubscriptionAction({
- url: webSocketUrl,
- refId,
- exploreId,
- dataReceivedActionCreator,
- })
- )
- .thenNoActionsWhereDispatched()
- .whenWebSocketReceivesData({ data: [1, 2, 3] })
- .thenResultingActionsEqual(
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [1, 2, 3] } as any,
- dataReceivedActionCreator,
- })
- )
- .whenActionIsDispatched(clearQueriesAction({ exploreId: ExploreId.right }))
- .whenWebSocketReceivesData({ data: [4, 5, 6] })
- .thenResultingActionsEqual(
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [1, 2, 3] } as any,
- dataReceivedActionCreator,
- }),
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [4, 5, 6] } as any,
- dataReceivedActionCreator,
- })
- );
- });
- });
- });
- describe('and startSubscriptionAction is dispatched', () => {
- describe('and exploreId and refId matches the websockets', () => {
- it('then subscription should be unsubscribed', () => {
- const { state, webSocketUrl, refId, exploreId } = setup();
- epicTester(startSubscriptionEpic, state)
- .whenActionIsDispatched(
- startSubscriptionAction({
- url: webSocketUrl,
- refId,
- exploreId,
- dataReceivedActionCreator,
- })
- )
- .thenNoActionsWhereDispatched()
- .whenWebSocketReceivesData({ data: [1, 2, 3] })
- .thenResultingActionsEqual(
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [1, 2, 3] } as any,
- dataReceivedActionCreator,
- })
- )
- .whenActionIsDispatched(
- startSubscriptionAction({
- url: webSocketUrl,
- refId,
- exploreId,
- dataReceivedActionCreator,
- })
- )
- .whenWebSocketReceivesData({ data: [4, 5, 6] })
- .thenResultingActionsEqual(
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [1, 2, 3] } as any,
- dataReceivedActionCreator,
- }),
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [4, 5, 6] } as any,
- dataReceivedActionCreator,
- })
- // This looks like we haven't stopped the subscription but we actually started the same again
- );
- });
- describe('and exploreId or refId does not match the websockets', () => {
- it('then subscription should not be unsubscribed and another websocket is started', () => {
- const { state, webSocketUrl, refId, exploreId } = setup();
- epicTester(startSubscriptionEpic, state)
- .whenActionIsDispatched(
- startSubscriptionAction({
- url: webSocketUrl,
- refId,
- exploreId,
- dataReceivedActionCreator,
- })
- )
- .thenNoActionsWhereDispatched()
- .whenWebSocketReceivesData({ data: [1, 2, 3] })
- .thenResultingActionsEqual(
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [1, 2, 3] } as any,
- dataReceivedActionCreator,
- })
- )
- .whenActionIsDispatched(
- startSubscriptionAction({
- url: webSocketUrl,
- refId: 'B',
- exploreId,
- dataReceivedActionCreator,
- })
- )
- .whenWebSocketReceivesData({ data: [4, 5, 6] })
- .thenResultingActionsEqual(
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [1, 2, 3] } as any,
- dataReceivedActionCreator,
- }),
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [4, 5, 6] } as any,
- dataReceivedActionCreator,
- }),
- limitMessageRatePayloadAction({
- exploreId,
- data: { data: [4, 5, 6] } as any,
- dataReceivedActionCreator,
- })
- );
- });
- });
- });
- });
- });
- });
|