|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as assert from 'assert'; |
| 18 | +import * as sinon from 'sinon'; |
| 19 | +import { SpanExporter } from '../../../src'; |
| 20 | +import { BatchSpanProcessor } from '../../../src/platform/browser/export/BatchSpanProcessor'; |
| 21 | +import { TestTracingSpanExporter } from '../../common/export/TestTracingSpanExporter'; |
| 22 | + |
| 23 | +describe('BatchSpanProcessor - web', () => { |
| 24 | + let visibilityState: VisibilityState = 'visible'; |
| 25 | + let exporter: SpanExporter |
| 26 | + let processor: BatchSpanProcessor; |
| 27 | + let forceFlushSpy: sinon.SinonStub; |
| 28 | + let visibilityChangeEvent: Event; |
| 29 | + let pageHideEvent: Event |
| 30 | + |
| 31 | + beforeEach(() => { |
| 32 | + sinon.replaceGetter(document, 'visibilityState', () => visibilityState); |
| 33 | + visibilityState = 'visible'; |
| 34 | + exporter = new TestTracingSpanExporter(); |
| 35 | + processor = new BatchSpanProcessor(exporter, {}); |
| 36 | + forceFlushSpy = sinon.stub(processor, 'forceFlush'); |
| 37 | + visibilityChangeEvent = new Event('visibilitychange'); |
| 38 | + pageHideEvent = new Event('pagehide'); |
| 39 | + }); |
| 40 | + |
| 41 | + afterEach(async () => { |
| 42 | + sinon.restore(); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('when document becomes hidden', () => { |
| 46 | + const testDocumentHide = (hideDocument: () => void) => { |
| 47 | + it('should force flush spans', () => { |
| 48 | + assert.strictEqual(forceFlushSpy.callCount, 0); |
| 49 | + hideDocument() |
| 50 | + assert.strictEqual(forceFlushSpy.callCount, 1); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('AND shutdown has been called', () => { |
| 54 | + it('should NOT force flush spans', async () => { |
| 55 | + assert.strictEqual(forceFlushSpy.callCount, 0); |
| 56 | + await processor.shutdown(); |
| 57 | + hideDocument() |
| 58 | + assert.strictEqual(forceFlushSpy.callCount, 0); |
| 59 | + }); |
| 60 | + }) |
| 61 | + |
| 62 | + describe('AND disableAutoFlushOnDocumentHide configuration option', () => { |
| 63 | + it('set to false should force flush spans', () => { |
| 64 | + processor = new BatchSpanProcessor(exporter, { disableAutoFlushOnDocumentHide: false }); |
| 65 | + forceFlushSpy = sinon.stub(processor, 'forceFlush'); |
| 66 | + assert.strictEqual(forceFlushSpy.callCount, 0); |
| 67 | + hideDocument() |
| 68 | + assert.strictEqual(forceFlushSpy.callCount, 1); |
| 69 | + }) |
| 70 | + |
| 71 | + it('set to true should NOT force flush spans', () => { |
| 72 | + processor = new BatchSpanProcessor(exporter, { disableAutoFlushOnDocumentHide: true }); |
| 73 | + forceFlushSpy = sinon.stub(processor, 'forceFlush'); |
| 74 | + assert.strictEqual(forceFlushSpy.callCount, 0); |
| 75 | + hideDocument() |
| 76 | + assert.strictEqual(forceFlushSpy.callCount, 0); |
| 77 | + }) |
| 78 | + }) |
| 79 | + } |
| 80 | + |
| 81 | + describe('by the visibilitychange event', () => { |
| 82 | + testDocumentHide(() => { |
| 83 | + visibilityState = 'hidden'; |
| 84 | + document.dispatchEvent(visibilityChangeEvent); |
| 85 | + }) |
| 86 | + }) |
| 87 | + |
| 88 | + describe('by the pagehide event', () => { |
| 89 | + testDocumentHide(() => { |
| 90 | + document.dispatchEvent(pageHideEvent); |
| 91 | + }) |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + describe('when document becomes visible', () => { |
| 96 | + it('should NOT force flush spans', () => { |
| 97 | + assert.strictEqual(forceFlushSpy.callCount, 0); |
| 98 | + document.dispatchEvent(visibilityChangeEvent); |
| 99 | + assert.strictEqual(forceFlushSpy.callCount, 0); |
| 100 | + }); |
| 101 | + }) |
| 102 | +}); |
0 commit comments