github.com/hernad/nomad@v1.6.112/ui/tests/unit/utils/stream-logger-test.js (about)

     1  /**
     2   * Copyright (c) HashiCorp, Inc.
     3   * SPDX-License-Identifier: MPL-2.0
     4   */
     5  
     6  import { module, test } from 'qunit';
     7  import { Promise } from 'rsvp';
     8  import sinon from 'sinon';
     9  import StreamLogger from 'nomad-ui/utils/classes/stream-logger';
    10  
    11  module('Unit | Util | StreamLogger', function () {
    12    test('when a StreamLogger is stopped before the poll request responds, the request is immediately canceled upon completion', async function (assert) {
    13      const fetchMock = new FetchMock();
    14      const fetch = fetchMock.request();
    15  
    16      const logger = StreamLogger.create({
    17        logFetch: () => fetch,
    18      });
    19  
    20      logger.start();
    21      await logger.stop();
    22  
    23      assert.notOk(logger.poll.isRunning);
    24      assert.equal(fetchMock.reader.cancel.callCount, 0);
    25  
    26      fetchMock.closeRequest();
    27      await fetch;
    28  
    29      assert.equal(fetchMock.reader.cancel.callCount, 1);
    30    });
    31  
    32    test('when the streaming request sends the done flag, the poll task completes', async function (assert) {
    33      const fetchMock = new FetchMock();
    34      const fetch = fetchMock.request();
    35  
    36      const logger = StreamLogger.create({
    37        logFetch: () => fetch,
    38      });
    39  
    40      logger.start();
    41  
    42      assert.ok(logger.poll.isRunning);
    43      assert.equal(fetchMock.reader.readSpy.callCount, 0);
    44  
    45      fetchMock.closeRequest();
    46      await fetch;
    47  
    48      assert.notOk(logger.poll.isRunning);
    49      assert.equal(fetchMock.reader.readSpy.callCount, 1);
    50    });
    51  
    52    test('disable streaming if not supported', async function (assert) {
    53      window.ReadableStream = null;
    54      assert.false(StreamLogger.isSupported);
    55    });
    56  });
    57  
    58  class FetchMock {
    59    constructor() {
    60      this._closeRequest = null;
    61      this.reader = new ReadableStreamMock();
    62      this.response = new FetchResponseMock(this.reader);
    63    }
    64  
    65    request() {
    66      if (this._closeRequest) {
    67        throw new Error('Can only call FetchMock.request once');
    68      }
    69      return new Promise((resolve) => {
    70        this._closeRequest = resolve;
    71      });
    72    }
    73  
    74    closeRequest() {
    75      if (this._closeRequest) {
    76        this._closeRequest(this.response);
    77      } else {
    78        throw new Error(
    79          'Must call FetchMock.request() before FetchMock.closeRequest'
    80        );
    81      }
    82    }
    83  }
    84  
    85  class FetchResponseMock {
    86    constructor(reader) {
    87      this.reader = reader;
    88      this.body = {
    89        getReader() {
    90          return reader;
    91        },
    92      };
    93    }
    94  }
    95  
    96  class ReadableStreamMock {
    97    constructor() {
    98      this.cancel = sinon.spy();
    99      this.readSpy = sinon.spy();
   100    }
   101  
   102    read() {
   103      this.readSpy();
   104      return new Promise((resolve) => {
   105        resolve({ value: new ArrayBuffer(0), done: true });
   106      });
   107    }
   108  }