|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import * as Rx from 'rxjs'; |
| 21 | +import { REPO_ROOT } from '@kbn/dev-utils'; |
| 22 | + |
| 23 | +import { Update } from '../common'; |
| 24 | + |
| 25 | +import { OptimizerState } from './optimizer_reducer'; |
| 26 | +import { OptimizerConfig } from './optimizer_config'; |
| 27 | +import { handleOptimizerCompletion } from './handle_optimizer_completion'; |
| 28 | +import { toArray } from 'rxjs/operators'; |
| 29 | + |
| 30 | +const createUpdate$ = (phase: OptimizerState['phase']) => |
| 31 | + Rx.of<Update<any, OptimizerState>>({ |
| 32 | + state: { |
| 33 | + phase, |
| 34 | + compilerStates: [], |
| 35 | + durSec: 0, |
| 36 | + offlineBundles: [], |
| 37 | + onlineBundles: [], |
| 38 | + startTime: Date.now(), |
| 39 | + }, |
| 40 | + }); |
| 41 | + |
| 42 | +const config = (watch?: boolean) => |
| 43 | + OptimizerConfig.create({ |
| 44 | + repoRoot: REPO_ROOT, |
| 45 | + watch, |
| 46 | + }); |
| 47 | +const collect = <T>(stream: Rx.Observable<T>): Promise<T[]> => stream.pipe(toArray()).toPromise(); |
| 48 | + |
| 49 | +it('errors if the optimizer completes when in watch mode', async () => { |
| 50 | + const update$ = createUpdate$('success'); |
| 51 | + |
| 52 | + await expect( |
| 53 | + collect(update$.pipe(handleOptimizerCompletion(config(true)))) |
| 54 | + ).rejects.toThrowErrorMatchingInlineSnapshot( |
| 55 | + `"optimizer unexpectedly completed when in watch mode"` |
| 56 | + ); |
| 57 | +}); |
| 58 | + |
| 59 | +it('errors if the optimizer completes in phase "issue"', async () => { |
| 60 | + const update$ = createUpdate$('issue'); |
| 61 | + |
| 62 | + await expect( |
| 63 | + collect(update$.pipe(handleOptimizerCompletion(config()))) |
| 64 | + ).rejects.toThrowErrorMatchingInlineSnapshot(`"webpack issue"`); |
| 65 | +}); |
| 66 | + |
| 67 | +it('errors if the optimizer completes in phase "initializing"', async () => { |
| 68 | + const update$ = createUpdate$('initializing'); |
| 69 | + |
| 70 | + await expect( |
| 71 | + collect(update$.pipe(handleOptimizerCompletion(config()))) |
| 72 | + ).rejects.toThrowErrorMatchingInlineSnapshot( |
| 73 | + `"optimizer unexpectedly exit in phase \\"initializing\\""` |
| 74 | + ); |
| 75 | +}); |
| 76 | + |
| 77 | +it('errors if the optimizer completes in phase "reallocating"', async () => { |
| 78 | + const update$ = createUpdate$('reallocating'); |
| 79 | + |
| 80 | + await expect( |
| 81 | + collect(update$.pipe(handleOptimizerCompletion(config()))) |
| 82 | + ).rejects.toThrowErrorMatchingInlineSnapshot( |
| 83 | + `"optimizer unexpectedly exit in phase \\"reallocating\\""` |
| 84 | + ); |
| 85 | +}); |
| 86 | + |
| 87 | +it('errors if the optimizer completes in phase "running"', async () => { |
| 88 | + const update$ = createUpdate$('running'); |
| 89 | + |
| 90 | + await expect( |
| 91 | + collect(update$.pipe(handleOptimizerCompletion(config()))) |
| 92 | + ).rejects.toThrowErrorMatchingInlineSnapshot( |
| 93 | + `"optimizer unexpectedly exit in phase \\"running\\""` |
| 94 | + ); |
| 95 | +}); |
| 96 | + |
| 97 | +it('passes through errors on the source stream', async () => { |
| 98 | + const error = new Error('foo'); |
| 99 | + const update$ = Rx.throwError(error); |
| 100 | + |
| 101 | + await expect(collect(update$.pipe(handleOptimizerCompletion(config())))).rejects.toThrowError( |
| 102 | + error |
| 103 | + ); |
| 104 | +}); |
0 commit comments