12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049 |
- import mockConsole, { RestoreConsole } from 'jest-mock-console';
- import { mapValues } from 'lodash';
- import { Observable, Subject, Subscription, Unsubscribable } from 'rxjs';
- import {
- DataFrameJSON,
- dataFrameToJSON,
- DataQueryResponse,
- FieldType,
- LiveChannelAddress,
- LiveChannelConnectionState,
- LiveChannelEvent,
- LiveChannelEventType,
- LiveChannelLeaveEvent,
- LiveChannelScope,
- LoadingState,
- } from '@grafana/data';
- import { StreamingFrameAction } from '@grafana/runtime';
- import { StreamingDataFrame } from '../data/StreamingDataFrame';
- import { isStreamingResponseData, StreamingResponseData, StreamingResponseDataType } from '../data/utils';
- import { DataStreamHandlerDeps, LiveDataStream } from './LiveDataStream';
- type SubjectsInsteadOfObservables<T> = {
- [key in keyof T]: T[key] extends Observable<infer U> ? Subject<U> : T[key];
- };
- type DepsWithSubjectsInsteadOfObservables<T = any> = SubjectsInsteadOfObservables<DataStreamHandlerDeps<T>>;
- const createDeps = <T = any>(
- overrides?: Partial<DepsWithSubjectsInsteadOfObservables<T>>
- ): DepsWithSubjectsInsteadOfObservables<T> => {
- return {
- channelId: 'channel-1',
- liveEventsObservable: new Subject(),
- onShutdown: jest.fn(),
- subscriberReadiness: new Subject(),
- defaultStreamingFrameOptions: { maxLength: 100, maxDelta: Infinity, action: StreamingFrameAction.Append },
- shutdownDelayInMs: 1000,
- ...(overrides ?? {}),
- };
- };
- class ValuesCollection<T> implements Unsubscribable {
- values: T[] = [];
- errors: any[] = [];
- receivedComplete = false;
- subscription: Subscription | undefined;
- valuesCount = () => this.values.length;
- subscribeTo = (obs: Observable<T>) => {
- if (this.subscription) {
- throw new Error(`can't subscribe twice!`);
- }
- this.subscription = obs.subscribe({
- next: (n) => {
- this.values.push(n);
- },
- error: (err) => {
- this.errors.push(err);
- },
- complete: () => {
- this.receivedComplete = true;
- },
- });
- };
- get complete() {
- return this.receivedComplete || this.subscription?.closed;
- }
- unsubscribe = () => {
- this.subscription?.unsubscribe();
- };
- lastValue = () => {
- if (!this.values.length) {
- throw new Error(`no values available in ${JSON.stringify(this)}`);
- }
- return this.values[this.values.length - 1];
- };
- lastError = () => {
- if (!this.errors.length) {
- throw new Error(`no errors available in ${JSON.stringify(this)}`);
- }
- return this.errors[this.errors.length - 1];
- };
- }
- const liveChannelMessageEvent = <T extends DataFrameJSON>(message: T): LiveChannelEvent<T> => ({
- type: LiveChannelEventType.Message,
- message,
- });
- const liveChannelLeaveEvent = (): LiveChannelLeaveEvent => ({
- type: LiveChannelEventType.Leave,
- user: '',
- });
- const liveChannelStatusEvent = (state: LiveChannelConnectionState, error?: Error): LiveChannelEvent => ({
- type: LiveChannelEventType.Status,
- state,
- error,
- id: '',
- timestamp: 1,
- });
- const fieldsOf = (data: StreamingResponseData<StreamingResponseDataType.FullFrame>) => {
- return data.frame.fields.map((f) => ({
- name: f.name,
- values: f.values,
- }));
- };
- const dummyErrorMessage = 'dummy-error';
- describe('LiveDataStream', () => {
- jest.useFakeTimers();
- let restoreConsole: RestoreConsole | undefined;
- beforeEach(() => {
- restoreConsole = mockConsole();
- });
- afterEach(() => {
- restoreConsole?.();
- });
- const expectValueCollectionState = <T>(
- valuesCollection: ValuesCollection<T>,
- state: { errors: number; values: number; complete: boolean }
- ) => {
- expect(valuesCollection.values).toHaveLength(state.values);
- expect(valuesCollection.errors).toHaveLength(state.errors);
- expect(valuesCollection.complete).toEqual(state.complete);
- };
- const expectResponse =
- <T extends StreamingResponseDataType>(state: LoadingState) =>
- (res: DataQueryResponse, streamingDataType: T) => {
- expect(res.state).toEqual(state);
- expect(res.data).toHaveLength(1);
- const firstData = res.data[0];
- expect(isStreamingResponseData(firstData, streamingDataType)).toEqual(true);
- };
- const expectStreamingResponse = expectResponse(LoadingState.Streaming);
- const expectErrorResponse = expectResponse(LoadingState.Error);
- const dummyLiveChannelAddress: LiveChannelAddress = {
- scope: LiveChannelScope.Grafana,
- namespace: 'stream',
- path: 'abc',
- };
- const subscriptionKey = 'subKey';
- const liveDataStreamOptions = {
- withTimeBFilter: {
- addr: dummyLiveChannelAddress,
- buffer: {
- maxLength: 2,
- maxDelta: 10,
- action: StreamingFrameAction.Append,
- },
- filter: {
- fields: ['time', 'b'],
- },
- },
- withTimeAFilter: {
- addr: dummyLiveChannelAddress,
- buffer: {
- maxLength: 3,
- maxDelta: 10,
- action: StreamingFrameAction.Append,
- },
- filter: {
- fields: ['time', 'a'],
- },
- },
- withoutFilter: {
- addr: dummyLiveChannelAddress,
- buffer: {
- maxLength: 4,
- maxDelta: 10,
- action: StreamingFrameAction.Append,
- },
- },
- withReplaceMode: {
- addr: dummyLiveChannelAddress,
- buffer: {
- maxLength: 5,
- maxDelta: 10,
- action: StreamingFrameAction.Replace,
- },
- filter: {
- fields: ['time', 'b'],
- },
- },
- };
- const dataFrameJsons = {
- schema1: () => ({
- schema: {
- fields: [
- { name: 'time', type: FieldType.time },
- { name: 'a', type: FieldType.string },
- { name: 'b', type: FieldType.number },
- ],
- },
- data: {
- values: [
- [100, 101],
- ['a', 'b'],
- [1, 2],
- ],
- },
- }),
- schema1newValues: () => ({
- data: {
- values: [[102], ['c'], [3]],
- },
- }),
- schema1newValues2: () => ({
- data: {
- values: [[103], ['d'], [4]],
- },
- }),
- schema2: () => ({
- schema: {
- fields: [
- { name: 'time', type: FieldType.time },
- { name: 'a', type: FieldType.string },
- { name: 'b', type: FieldType.string },
- ],
- },
- data: {
- values: [[103], ['x'], ['y']],
- },
- }),
- schema2newValues: () => ({
- data: {
- values: [[104], ['w'], ['o']],
- },
- }),
- };
- describe('happy path with a single subscriber in `append` mode', () => {
- let deps: ReturnType<typeof createDeps>;
- let liveDataStream: LiveDataStream<any>;
- const valuesCollection = new ValuesCollection<DataQueryResponse>();
- beforeAll(() => {
- deps = createDeps();
- expect(deps.liveEventsObservable.observed).toBeFalsy();
- expect(deps.subscriberReadiness.observed).toBeFalsy();
- liveDataStream = new LiveDataStream(deps);
- });
- it('should subscribe to live events observable immediately after creation', async () => {
- expect(deps.liveEventsObservable.observed).toBeTruthy();
- });
- it('should not subscribe to subscriberReadiness observable until first subscription', async () => {
- expect(deps.subscriberReadiness.observed).toBeFalsy();
- });
- it('should subscribe to subscriberReadiness observable on first subscription and return observable without any values', async () => {
- const observable = liveDataStream.get(liveDataStreamOptions.withTimeBFilter, subscriptionKey);
- valuesCollection.subscribeTo(observable);
- //then
- expect(deps.subscriberReadiness.observed).toBeTruthy();
- expectValueCollectionState(valuesCollection, { errors: 0, values: 0, complete: false });
- });
- it('should emit the first live channel message event as a serialized streamingDataFrame', async () => {
- const valuesCount = valuesCollection.valuesCount();
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema1()));
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount + 1, complete: false });
- const response = valuesCollection.lastValue();
- expectStreamingResponse(response, StreamingResponseDataType.FullFrame);
- const data = response.data[0] as StreamingResponseData<StreamingResponseDataType.FullFrame>;
- expect(data.frame.options).toEqual(liveDataStreamOptions.withTimeBFilter.buffer);
- const deserializedFrame = StreamingDataFrame.deserialize(data.frame);
- expect(deserializedFrame.fields).toEqual([
- {
- config: {},
- name: 'time',
- type: 'time',
- values: {
- buffer: [100, 101],
- },
- },
- {
- config: {},
- name: 'b',
- type: 'number',
- values: {
- buffer: [1, 2],
- },
- },
- ]);
- expect(deserializedFrame.length).toEqual(dataFrameJsons.schema1().data.values[0].length);
- });
- it('should emit subsequent messages as deltas if the schema stays the same', async () => {
- const valuesCount = valuesCollection.valuesCount();
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema1newValues()));
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount + 1, complete: false });
- const response = valuesCollection.lastValue();
- expectStreamingResponse(response, StreamingResponseDataType.NewValuesSameSchema);
- const data = response.data[0] as StreamingResponseData<StreamingResponseDataType.NewValuesSameSchema>;
- expect(data.values).toEqual([[102], [3]]);
- });
- it('should emit a full frame if schema changes', async () => {
- const valuesCount = valuesCollection.valuesCount();
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema2()));
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount + 1, complete: false });
- const response = valuesCollection.lastValue();
- expectStreamingResponse(response, StreamingResponseDataType.FullFrame);
- const data = response.data[0] as StreamingResponseData<StreamingResponseDataType.FullFrame>;
- expect(fieldsOf(data)).toEqual([
- {
- name: 'time',
- values: [102, 103],
- },
- {
- name: 'b',
- values: [undefined, 'y'], // bug in streamingDataFrame - fix!
- },
- ]);
- });
- it('should emit a full frame if received a status live channel event with error', async () => {
- const valuesCount = valuesCollection.valuesCount();
- const error = new Error(`oh no!`);
- deps.liveEventsObservable.next(liveChannelStatusEvent(LiveChannelConnectionState.Connected, error));
- expectValueCollectionState(valuesCollection, {
- errors: 0,
- values: valuesCount + 1,
- complete: false,
- });
- const response = valuesCollection.lastValue();
- expectErrorResponse(response, StreamingResponseDataType.FullFrame);
- });
- it('should buffer new values until subscriber is ready', async () => {
- const valuesCount = valuesCollection.valuesCount();
- deps.subscriberReadiness.next(false);
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema2newValues()));
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount, complete: false });
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema2newValues()));
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount, complete: false });
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema2newValues()));
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount, complete: false });
- deps.subscriberReadiness.next(true);
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount + 1, complete: false });
- const response = valuesCollection.lastValue();
- expectStreamingResponse(response, StreamingResponseDataType.NewValuesSameSchema);
- const data = response.data[0] as StreamingResponseData<StreamingResponseDataType.NewValuesSameSchema>;
- expect(data.values).toEqual([
- [104, 104, 104],
- ['o', 'o', 'o'],
- ]);
- });
- it(`should reduce buffer to a full frame if schema changed at any point during subscriber's unavailability`, async () => {
- const valuesCount = valuesCollection.valuesCount();
- deps.subscriberReadiness.next(false);
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema2newValues()));
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount, complete: false });
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema2newValues()));
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount, complete: false });
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema1()));
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount, complete: false });
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema1newValues()));
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount, complete: false });
- deps.subscriberReadiness.next(true);
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount + 1, complete: false });
- const response = valuesCollection.lastValue();
- expectStreamingResponse(response, StreamingResponseDataType.FullFrame);
- expect(fieldsOf(response.data[0])).toEqual([
- {
- name: 'time',
- values: [101, 102],
- },
- {
- name: 'b',
- values: [2, 3],
- },
- ]);
- });
- it(`should reduce buffer to a full frame with last error if one or more errors occur during subscriber's unavailability`, async () => {
- const firstError = new Error('first error');
- const secondError = new Error(dummyErrorMessage);
- const valuesCount = valuesCollection.valuesCount();
- deps.subscriberReadiness.next(false);
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema1newValues()));
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema1newValues()));
- deps.liveEventsObservable.next(liveChannelStatusEvent(LiveChannelConnectionState.Connected, firstError));
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema1newValues()));
- deps.liveEventsObservable.next(liveChannelStatusEvent(LiveChannelConnectionState.Connected, secondError));
- deps.subscriberReadiness.next(true);
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount + 1, complete: false });
- const response = valuesCollection.lastValue();
- expectErrorResponse(response, StreamingResponseDataType.FullFrame);
- const errorMessage = response?.error?.message;
- expect(errorMessage?.includes(dummyErrorMessage)).toBeTruthy();
- expect(fieldsOf(response.data[0])).toEqual([
- {
- name: 'time',
- values: [102, 102],
- },
- {
- name: 'b',
- values: [3, 3],
- },
- ]);
- });
- it('should ignore messages without payload', async () => {
- const valuesCount = valuesCollection.valuesCount();
- deps.liveEventsObservable.next(liveChannelStatusEvent(LiveChannelConnectionState.Connected));
- deps.liveEventsObservable.next(liveChannelStatusEvent(LiveChannelConnectionState.Pending));
- deps.liveEventsObservable.next(liveChannelStatusEvent(LiveChannelConnectionState.Pending));
- deps.liveEventsObservable.next(liveChannelLeaveEvent());
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount, complete: false });
- });
- it(`should shutdown when source observable completes`, async () => {
- expect(deps.onShutdown).not.toHaveBeenCalled();
- expect(deps.subscriberReadiness.observed).toBeTruthy();
- expect(deps.liveEventsObservable.observed).toBeTruthy();
- deps.liveEventsObservable.complete();
- expectValueCollectionState(valuesCollection, {
- errors: 0,
- values: valuesCollection.valuesCount(),
- complete: true,
- });
- expect(deps.subscriberReadiness.observed).toBeFalsy();
- expect(deps.liveEventsObservable.observed).toBeFalsy();
- expect(deps.onShutdown).toHaveBeenCalled();
- });
- });
- describe('happy path with a single subscriber in `replace` mode', () => {
- let deps: ReturnType<typeof createDeps>;
- let liveDataStream: LiveDataStream<any>;
- const valuesCollection = new ValuesCollection<DataQueryResponse>();
- beforeAll(() => {
- deps = createDeps();
- expect(deps.liveEventsObservable.observed).toBeFalsy();
- expect(deps.subscriberReadiness.observed).toBeFalsy();
- liveDataStream = new LiveDataStream(deps);
- valuesCollection.subscribeTo(liveDataStream.get(liveDataStreamOptions.withReplaceMode, subscriptionKey));
- });
- it('should emit the first live channel message event as a serialized streamingDataFrame', async () => {
- const valuesCount = valuesCollection.valuesCount();
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema1()));
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount + 1, complete: false });
- const response = valuesCollection.lastValue();
- expectStreamingResponse(response, StreamingResponseDataType.FullFrame);
- const data = response.data[0] as StreamingResponseData<StreamingResponseDataType.FullFrame>;
- expect(data.frame.options).toEqual(liveDataStreamOptions.withReplaceMode.buffer);
- const deserializedFrame = StreamingDataFrame.deserialize(data.frame);
- expect(deserializedFrame.fields).toEqual([
- {
- config: {},
- name: 'time',
- type: 'time',
- values: {
- buffer: [100, 101],
- },
- },
- {
- config: {},
- name: 'b',
- type: 'number',
- values: {
- buffer: [1, 2],
- },
- },
- ]);
- expect(deserializedFrame.length).toEqual(dataFrameJsons.schema1().data.values[0].length);
- });
- it('should emit subsequent messages as deltas if the schema stays the same', async () => {
- const valuesCount = valuesCollection.valuesCount();
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema1newValues()));
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount + 1, complete: false });
- const response = valuesCollection.lastValue();
- expectStreamingResponse(response, StreamingResponseDataType.NewValuesSameSchema);
- const data = response.data[0] as StreamingResponseData<StreamingResponseDataType.NewValuesSameSchema>;
- expect(data.values).toEqual([[102], [3]]);
- });
- it('should emit a full frame if schema changes', async () => {
- const valuesCount = valuesCollection.valuesCount();
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema2()));
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount + 1, complete: false });
- const response = valuesCollection.lastValue();
- expectStreamingResponse(response, StreamingResponseDataType.FullFrame);
- const data = response.data[0] as StreamingResponseData<StreamingResponseDataType.FullFrame>;
- expect(fieldsOf(data)).toEqual([
- {
- name: 'time',
- values: [103],
- },
- {
- name: 'b',
- values: ['y'],
- },
- ]);
- });
- it('should emit a full frame if received a status live channel event with error', async () => {
- const valuesCount = valuesCollection.valuesCount();
- const error = new Error(`oh no!`);
- deps.liveEventsObservable.next(liveChannelStatusEvent(LiveChannelConnectionState.Connected, error));
- expectValueCollectionState(valuesCollection, {
- errors: 0,
- values: valuesCount + 1,
- complete: false,
- });
- const response = valuesCollection.lastValue();
- expectErrorResponse(response, StreamingResponseDataType.FullFrame);
- });
- it('should buffer new values until subscriber is ready', async () => {
- const valuesCount = valuesCollection.valuesCount();
- deps.subscriberReadiness.next(false);
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema2newValues()));
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount, complete: false });
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema2newValues()));
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount, complete: false });
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema2newValues()));
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount, complete: false });
- deps.subscriberReadiness.next(true);
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount + 1, complete: false });
- const response = valuesCollection.lastValue();
- expectStreamingResponse(response, StreamingResponseDataType.NewValuesSameSchema);
- const data = response.data[0] as StreamingResponseData<StreamingResponseDataType.NewValuesSameSchema>;
- expect(data.values).toEqual([[104], ['o']]);
- });
- it(`should reduce buffer to a full frame if schema changed at any point during subscriber's unavailability`, async () => {
- const valuesCount = valuesCollection.valuesCount();
- deps.subscriberReadiness.next(false);
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema2newValues()));
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount, complete: false });
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema2newValues()));
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount, complete: false });
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema1()));
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount, complete: false });
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema1newValues()));
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount, complete: false });
- deps.subscriberReadiness.next(true);
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount + 1, complete: false });
- const response = valuesCollection.lastValue();
- expectStreamingResponse(response, StreamingResponseDataType.FullFrame);
- expect(fieldsOf(response.data[0])).toEqual([
- {
- name: 'time',
- values: [102],
- },
- {
- name: 'b',
- values: [3],
- },
- ]);
- });
- it(`should reduce buffer to an empty full frame with last error if one or more errors occur during subscriber's unavailability`, async () => {
- const firstError = new Error('first error');
- const secondError = new Error(dummyErrorMessage);
- const valuesCount = valuesCollection.valuesCount();
- deps.subscriberReadiness.next(false);
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema1newValues()));
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema1newValues()));
- deps.liveEventsObservable.next(liveChannelStatusEvent(LiveChannelConnectionState.Connected, firstError));
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema1newValues()));
- deps.liveEventsObservable.next(liveChannelStatusEvent(LiveChannelConnectionState.Connected, secondError));
- deps.subscriberReadiness.next(true);
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount + 1, complete: false });
- const response = valuesCollection.lastValue();
- expectErrorResponse(response, StreamingResponseDataType.FullFrame);
- const errorMessage = response?.error?.message;
- expect(errorMessage?.includes(dummyErrorMessage)).toBeTruthy();
- expect(fieldsOf(response.data[0])).toEqual([
- {
- name: 'time',
- values: [],
- },
- {
- name: 'b',
- values: [],
- },
- ]);
- });
- it('should ignore messages without payload', async () => {
- const valuesCount = valuesCollection.valuesCount();
- deps.liveEventsObservable.next(liveChannelStatusEvent(LiveChannelConnectionState.Connected));
- deps.liveEventsObservable.next(liveChannelStatusEvent(LiveChannelConnectionState.Pending));
- deps.liveEventsObservable.next(liveChannelStatusEvent(LiveChannelConnectionState.Pending));
- deps.liveEventsObservable.next(liveChannelLeaveEvent());
- expectValueCollectionState(valuesCollection, { errors: 0, values: valuesCount, complete: false });
- });
- it(`should shutdown when source observable completes`, async () => {
- expect(deps.onShutdown).not.toHaveBeenCalled();
- expect(deps.subscriberReadiness.observed).toBeTruthy();
- expect(deps.liveEventsObservable.observed).toBeTruthy();
- deps.liveEventsObservable.complete();
- expectValueCollectionState(valuesCollection, {
- errors: 0,
- values: valuesCollection.valuesCount(),
- complete: true,
- });
- expect(deps.subscriberReadiness.observed).toBeFalsy();
- expect(deps.liveEventsObservable.observed).toBeFalsy();
- expect(deps.onShutdown).toHaveBeenCalled();
- });
- });
- describe('single subscriber with initial frame', () => {
- it('should emit the initial frame right after subscribe', async () => {
- const deps = createDeps();
- const liveDataStream = new LiveDataStream(deps);
- const valuesCollection = new ValuesCollection<DataQueryResponse>();
- const initialFrame = dataFrameJsons.schema2();
- const observable = liveDataStream.get(
- { ...liveDataStreamOptions.withTimeBFilter, frame: initialFrame },
- subscriptionKey
- );
- valuesCollection.subscribeTo(observable);
- //then
- expect(deps.subscriberReadiness.observed).toBeTruthy();
- expectValueCollectionState(valuesCollection, { errors: 0, values: 1, complete: false });
- const response = valuesCollection.lastValue();
- expectStreamingResponse(response, StreamingResponseDataType.FullFrame);
- const data = response.data[0] as StreamingResponseData<StreamingResponseDataType.FullFrame>;
- expect(fieldsOf(data)).toEqual([
- {
- name: 'time',
- values: [103],
- },
- {
- name: 'b',
- values: ['y'], // bug in streamingDataFrame - fix!
- },
- ]);
- });
- });
- describe('two subscribers with initial frames', () => {
- it('should ignore initial frame from second subscriber', async () => {
- const deps = createDeps();
- const liveDataStream = new LiveDataStream(deps);
- const valuesCollection = new ValuesCollection<DataQueryResponse>();
- const valuesCollection2 = new ValuesCollection<DataQueryResponse>();
- valuesCollection.subscribeTo(
- liveDataStream.get(
- {
- ...liveDataStreamOptions.withTimeBFilter,
- frame: dataFrameToJSON(StreamingDataFrame.fromDataFrameJSON(dataFrameJsons.schema1())),
- },
- subscriptionKey
- )
- );
- expectValueCollectionState(valuesCollection, { errors: 0, values: 1, complete: false });
- valuesCollection2.subscribeTo(
- liveDataStream.get(
- {
- ...liveDataStreamOptions.withTimeBFilter,
- frame: dataFrameJsons.schema2(),
- },
- subscriptionKey
- )
- );
- // no extra emits for initial subscriber
- expectValueCollectionState(valuesCollection, { errors: 0, values: 1, complete: false });
- expectValueCollectionState(valuesCollection2, { errors: 0, values: 1, complete: false });
- const frame1 = fieldsOf(valuesCollection.lastValue().data[0]);
- const frame2 = fieldsOf(valuesCollection2.lastValue().data[0]);
- expect(frame1).toEqual(frame2);
- });
- });
- describe('source observable emits completed event', () => {
- it('should shutdown', async () => {
- const deps = createDeps();
- const liveDataStream = new LiveDataStream(deps);
- const valuesCollection = new ValuesCollection<DataQueryResponse>();
- const observable = liveDataStream.get(liveDataStreamOptions.withTimeAFilter, subscriptionKey);
- valuesCollection.subscribeTo(observable);
- expect(deps.subscriberReadiness.observed).toBeTruthy();
- expect(deps.liveEventsObservable.observed).toBeTruthy();
- expect(deps.onShutdown).not.toHaveBeenCalled();
- deps.liveEventsObservable.complete();
- expectValueCollectionState(valuesCollection, {
- errors: 0,
- values: 0,
- complete: true,
- });
- expect(deps.subscriberReadiness.observed).toBeFalsy();
- expect(deps.liveEventsObservable.observed).toBeFalsy();
- expect(deps.onShutdown).toHaveBeenCalled();
- });
- });
- describe('source observable emits error event', () => {
- it('should shutdown', async () => {
- const deps = createDeps();
- const liveDataStream = new LiveDataStream(deps);
- const valuesCollection = new ValuesCollection<DataQueryResponse>();
- const observable = liveDataStream.get(liveDataStreamOptions.withTimeAFilter, subscriptionKey);
- valuesCollection.subscribeTo(observable);
- expect(deps.subscriberReadiness.observed).toBeTruthy();
- expect(deps.liveEventsObservable.observed).toBeTruthy();
- expect(deps.onShutdown).not.toHaveBeenCalled();
- deps.liveEventsObservable.error(new Error(dummyErrorMessage));
- expectValueCollectionState(valuesCollection, {
- errors: 0,
- values: 1,
- complete: true,
- });
- const response = valuesCollection.lastValue();
- expectErrorResponse(response, StreamingResponseDataType.FullFrame);
- expect(response?.error?.message?.includes(dummyErrorMessage)).toBeTruthy();
- expect(deps.subscriberReadiness.observed).toBeFalsy();
- expect(deps.liveEventsObservable.observed).toBeFalsy();
- expect(deps.onShutdown).toHaveBeenCalled();
- });
- });
- describe('happy path with multiple subscribers', () => {
- let deps: ReturnType<typeof createDeps>;
- let liveDataStream: LiveDataStream<any>;
- const valuesCollections = {
- withTimeBFilter: new ValuesCollection<DataQueryResponse>(),
- withTimeAFilter: new ValuesCollection<DataQueryResponse>(),
- withoutFilter: new ValuesCollection<DataQueryResponse>(),
- withReplaceMode: new ValuesCollection<DataQueryResponse>(),
- };
- beforeAll(() => {
- deps = createDeps();
- liveDataStream = new LiveDataStream(deps);
- });
- it('should emit the last value as full frame to new subscribers', async () => {
- valuesCollections.withTimeAFilter.subscribeTo(
- liveDataStream.get(liveDataStreamOptions.withTimeAFilter, subscriptionKey)
- );
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema1()));
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema1newValues()));
- expectValueCollectionState(valuesCollections.withTimeAFilter, { errors: 0, values: 2, complete: false });
- valuesCollections.withTimeBFilter.subscribeTo(
- liveDataStream.get(liveDataStreamOptions.withTimeBFilter, subscriptionKey)
- );
- valuesCollections.withoutFilter.subscribeTo(
- liveDataStream.get(liveDataStreamOptions.withoutFilter, subscriptionKey)
- );
- valuesCollections.withReplaceMode.subscribeTo(
- liveDataStream.get(liveDataStreamOptions.withReplaceMode, subscriptionKey)
- );
- expectValueCollectionState(valuesCollections.withTimeAFilter, { errors: 0, values: 2, complete: false });
- expectValueCollectionState(valuesCollections.withTimeBFilter, { errors: 0, values: 1, complete: false });
- expectValueCollectionState(valuesCollections.withoutFilter, { errors: 0, values: 1, complete: false });
- expectValueCollectionState(valuesCollections.withReplaceMode, { errors: 0, values: 1, complete: false });
- });
- it('should emit filtered data to each subscriber', async () => {
- deps.liveEventsObservable.next(liveChannelMessageEvent(dataFrameJsons.schema1newValues2()));
- expect(
- mapValues(valuesCollections, (collection) =>
- collection.values.map((response) => {
- const data = response.data[0];
- return isStreamingResponseData(data, StreamingResponseDataType.FullFrame)
- ? fieldsOf(data)
- : isStreamingResponseData(data, StreamingResponseDataType.NewValuesSameSchema)
- ? data.values
- : response;
- })
- )
- ).toEqual({
- withTimeAFilter: [
- [
- {
- name: 'time',
- values: [100, 101],
- },
- {
- name: 'a',
- values: ['a', 'b'],
- },
- ],
- [[102], ['c']],
- [[103], ['d']],
- ],
- withTimeBFilter: [
- [
- {
- name: 'time',
- values: [101, 102],
- },
- {
- name: 'b',
- values: [2, 3],
- },
- ],
- [[103], [4]],
- ],
- withoutFilter: [
- [
- {
- name: 'time',
- values: [100, 101, 102],
- },
- {
- name: 'a',
- values: ['a', 'b', 'c'],
- },
- {
- name: 'b',
- values: [1, 2, 3],
- },
- ],
- [[103], ['d'], [4]],
- ],
- withReplaceMode: [
- // only last packet
- [
- {
- name: 'time',
- values: [102],
- },
- {
- name: 'b',
- values: [3],
- },
- ],
- [[103], [4]],
- ],
- });
- });
- it('should not unsubscribe the source observable unless all subscribers unsubscribe', async () => {
- valuesCollections.withTimeAFilter.unsubscribe();
- jest.advanceTimersByTime(deps.shutdownDelayInMs + 1);
- expect(mapValues(valuesCollections, (coll) => coll.complete)).toEqual({
- withTimeAFilter: true,
- withTimeBFilter: false,
- withoutFilter: false,
- withReplaceMode: false,
- });
- expect(deps.subscriberReadiness.observed).toBeTruthy();
- expect(deps.liveEventsObservable.observed).toBeTruthy();
- expect(deps.onShutdown).not.toHaveBeenCalled();
- });
- it('should emit complete event to all subscribers during shutdown', async () => {
- deps.liveEventsObservable.complete();
- expect(mapValues(valuesCollections, (coll) => coll.complete)).toEqual({
- withTimeAFilter: true,
- withTimeBFilter: true,
- withoutFilter: true,
- withReplaceMode: true,
- });
- expect(deps.subscriberReadiness.observed).toBeFalsy();
- expect(deps.liveEventsObservable.observed).toBeFalsy();
- expect(deps.onShutdown).toHaveBeenCalled();
- });
- });
- describe('shutdown after unsubscribe', () => {
- it('should shutdown if no other subscriber subscribed during shutdown delay', async () => {
- const deps = createDeps();
- const liveDataStream = new LiveDataStream(deps);
- const valuesCollection = new ValuesCollection<DataQueryResponse>();
- valuesCollection.subscribeTo(liveDataStream.get(liveDataStreamOptions.withTimeAFilter, subscriptionKey));
- expect(deps.subscriberReadiness.observed).toBeTruthy();
- expect(deps.liveEventsObservable.observed).toBeTruthy();
- expect(deps.onShutdown).not.toHaveBeenCalled();
- valuesCollection.unsubscribe();
- jest.advanceTimersByTime(deps.shutdownDelayInMs - 1);
- // delay not finished - should still be subscribed
- expect(deps.subscriberReadiness.observed).toBeFalsy();
- expect(deps.liveEventsObservable.observed).toBeTruthy();
- expect(deps.onShutdown).not.toHaveBeenCalled();
- jest.advanceTimersByTime(2);
- // delay not finished - shut still be subscribed
- expect(deps.subscriberReadiness.observed).toBeFalsy();
- expect(deps.liveEventsObservable.observed).toBeFalsy();
- expect(deps.onShutdown).toHaveBeenCalled();
- });
- it('should not shutdown after unsubscribe if another subscriber subscribes during shutdown delay', async () => {
- const deps = createDeps();
- const liveDataStream = new LiveDataStream(deps);
- const valuesCollection1 = new ValuesCollection<DataQueryResponse>();
- const valuesCollection2 = new ValuesCollection<DataQueryResponse>();
- valuesCollection1.subscribeTo(liveDataStream.get(liveDataStreamOptions.withTimeAFilter, subscriptionKey));
- expect(deps.subscriberReadiness.observed).toBeTruthy();
- expect(deps.liveEventsObservable.observed).toBeTruthy();
- expect(deps.onShutdown).not.toHaveBeenCalled();
- valuesCollection1.unsubscribe();
- jest.advanceTimersByTime(deps.shutdownDelayInMs - 1);
- valuesCollection2.subscribeTo(liveDataStream.get(liveDataStreamOptions.withTimeAFilter, subscriptionKey));
- jest.advanceTimersByTime(deps.shutdownDelayInMs);
- expect(deps.subscriberReadiness.observed).toBeTruthy();
- expect(deps.liveEventsObservable.observed).toBeTruthy();
- expect(deps.onShutdown).not.toHaveBeenCalled();
- });
- });
- });
|