xref: /aosp_15_r20/external/perfetto/ui/src/base/disposable_unittest.ts (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1// Copyright (C) 2023 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import {AsyncDisposableStack, DisposableStack} from './disposable_stack';
16
17test('DisposableStack', () => {
18  const order: number[] = [];
19  const trash = new DisposableStack();
20  trash.use({[Symbol.dispose]: () => order.push(3)});
21  trash.use({[Symbol.dispose]: () => order.push(2)});
22  trash.defer(() => order.push(1));
23  expect(order).toEqual([]);
24  trash[Symbol.dispose]();
25  expect(order).toEqual([1, 2, 3]);
26});
27
28test('AsyncDisposableStack', async () => {
29  const order: number[] = [];
30  const trash = new AsyncDisposableStack();
31  trash.use({
32    [Symbol.asyncDispose]: async () => {
33      order.push(3);
34    },
35  });
36  trash.use({
37    [Symbol.asyncDispose]: async () => {
38      order.push(2);
39    },
40  });
41  trash.defer(async () => {
42    order.push(1);
43  });
44  expect(order).toEqual([]);
45  await trash[Symbol.asyncDispose]();
46  expect(order).toEqual([1, 2, 3]);
47});
48
49test('AsyncDisposableStackWithDisposable', async () => {
50  const trash = new AsyncDisposableStack();
51  trash.use({
52    [Symbol.dispose]: () => {
53      console.log('Disposing...');
54    },
55  });
56  await trash[Symbol.asyncDispose]();
57});
58