live.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import mockConsole, { RestoreConsole } from 'jest-mock-console';
  2. import { Subject } from 'rxjs';
  3. import { DataQueryResponse, FieldType, LiveChannelScope } from '@grafana/data';
  4. import { StreamingDataQueryResponse } from './centrifuge/service';
  5. import { StreamingDataFrame } from './data/StreamingDataFrame';
  6. import { StreamingResponseDataType } from './data/utils';
  7. import { GrafanaLiveService } from './live';
  8. describe('GrafanaLiveService', () => {
  9. let restoreConsole: RestoreConsole | undefined;
  10. const deps = {
  11. backendSrv: {},
  12. centrifugeSrv: {
  13. getDataStream: jest.fn(),
  14. },
  15. };
  16. const liveService = new GrafanaLiveService(deps as any);
  17. const liveDataStreamOptions = {
  18. addr: {
  19. scope: LiveChannelScope.Grafana,
  20. namespace: ' abc',
  21. path: 'abc',
  22. },
  23. };
  24. beforeEach(() => {
  25. jest.clearAllMocks();
  26. restoreConsole = mockConsole();
  27. });
  28. afterEach(() => {
  29. restoreConsole?.();
  30. });
  31. it('should map response from Centrifuge Service to a streaming data frame', async () => {
  32. const dummySubject = new Subject<StreamingDataQueryResponse>();
  33. deps.centrifugeSrv.getDataStream.mockReturnValueOnce(dummySubject);
  34. let response: DataQueryResponse | undefined;
  35. liveService.getDataStream(liveDataStreamOptions).subscribe((next) => {
  36. response = next;
  37. });
  38. dummySubject.next({
  39. data: [
  40. {
  41. type: StreamingResponseDataType.FullFrame,
  42. frame: StreamingDataFrame.empty().serialize(),
  43. },
  44. ],
  45. });
  46. expect(response).not.toBeUndefined();
  47. expect(response?.data[0]).toBeInstanceOf(StreamingDataFrame);
  48. });
  49. it('should add partial streaming data to the buffer', async () => {
  50. const dummySubject = new Subject<StreamingDataQueryResponse>();
  51. deps.centrifugeSrv.getDataStream.mockReturnValueOnce(dummySubject);
  52. let response: DataQueryResponse | undefined;
  53. liveService.getDataStream(liveDataStreamOptions).subscribe((next) => {
  54. response = next;
  55. });
  56. dummySubject.next({
  57. data: [
  58. {
  59. type: StreamingResponseDataType.FullFrame,
  60. frame: StreamingDataFrame.fromDataFrameJSON({
  61. schema: {
  62. fields: [
  63. { name: 'time', type: FieldType.time },
  64. { name: 'a', type: FieldType.string },
  65. { name: 'b', type: FieldType.number },
  66. ],
  67. },
  68. }).serialize(),
  69. },
  70. ],
  71. });
  72. dummySubject.next({
  73. data: [
  74. {
  75. type: StreamingResponseDataType.NewValuesSameSchema,
  76. values: [
  77. [100, 101],
  78. ['a', 'b'],
  79. [1, 2],
  80. ],
  81. },
  82. ],
  83. });
  84. expect(response).not.toBeUndefined();
  85. const frame: StreamingDataFrame = response?.data[0];
  86. expect(frame).toBeInstanceOf(StreamingDataFrame);
  87. expect(frame.fields).toEqual([
  88. {
  89. config: {},
  90. name: 'time',
  91. type: FieldType.time,
  92. values: {
  93. buffer: [100, 101],
  94. },
  95. },
  96. {
  97. config: {},
  98. name: 'a',
  99. type: FieldType.string,
  100. values: {
  101. buffer: ['a', 'b'],
  102. },
  103. },
  104. {
  105. config: {},
  106. name: 'b',
  107. type: FieldType.number,
  108. values: {
  109. buffer: [1, 2],
  110. },
  111. },
  112. ]);
  113. });
  114. it('should return an empty frame if first message was not a full frame', async () => {
  115. const dummySubject = new Subject<StreamingDataQueryResponse>();
  116. deps.centrifugeSrv.getDataStream.mockReturnValueOnce(dummySubject);
  117. let response: DataQueryResponse | undefined;
  118. liveService.getDataStream(liveDataStreamOptions).subscribe((next) => {
  119. response = next;
  120. });
  121. dummySubject.next({
  122. data: [
  123. {
  124. type: StreamingResponseDataType.NewValuesSameSchema,
  125. values: [
  126. [100, 101],
  127. ['a', 'b'],
  128. [1, 2],
  129. ],
  130. },
  131. ],
  132. });
  133. expect(response).not.toBeUndefined();
  134. const frame: StreamingDataFrame = response?.data[0];
  135. expect(frame).toBeInstanceOf(StreamingDataFrame);
  136. expect(frame.fields).toEqual([]);
  137. });
  138. });