github.com/emate/nomad@v0.8.2-wo-binpacking/ui/tests/integration/allocation-row-test.js (about)

     1  import { getOwner } from '@ember/application';
     2  import { test, moduleForComponent } from 'ember-qunit';
     3  import wait from 'ember-test-helpers/wait';
     4  import hbs from 'htmlbars-inline-precompile';
     5  import generateResources from '../../mirage/data/generate-resources';
     6  import { startMirage } from 'nomad-ui/initializers/ember-cli-mirage';
     7  import Response from 'ember-cli-mirage/response';
     8  
     9  moduleForComponent('allocation-row', 'Integration | Component | allocation row', {
    10    integration: true,
    11    beforeEach() {
    12      this.store = getOwner(this).lookup('service:store');
    13      this.server = startMirage();
    14      this.server.create('namespace');
    15      this.server.create('node');
    16      this.server.create('job', { createAllocations: false });
    17    },
    18    afterEach() {
    19      this.server.shutdown();
    20    },
    21  });
    22  
    23  test('Allocation row polls for stats, even when it errors or has an invalid response', function(assert) {
    24    const component = this;
    25  
    26    let currentFrame = 0;
    27    let frames = [
    28      JSON.stringify({ ResourceUsage: generateResources() }),
    29      JSON.stringify({ ResourceUsage: generateResources() }),
    30      null,
    31      '<Not>Valid JSON</Not>',
    32      JSON.stringify({ ResourceUsage: generateResources() }),
    33    ];
    34    const backoffSequence = [50];
    35  
    36    this.server.get('/client/allocation/:id/stats', function() {
    37      const response = frames[++currentFrame];
    38  
    39      // Disable polling to stop the EC task in the component
    40      if (currentFrame >= frames.length) {
    41        component.set('enablePolling', false);
    42      }
    43  
    44      if (response) {
    45        return response;
    46      }
    47      return new Response(500, {}, '');
    48    });
    49  
    50    this.server.create('allocation');
    51    this.store.findAll('allocation');
    52  
    53    let allocation;
    54  
    55    return wait()
    56      .then(() => {
    57        allocation = this.store.peekAll('allocation').get('firstObject');
    58  
    59        this.setProperties({
    60          allocation,
    61          backoffSequence,
    62          context: 'job',
    63          enablePolling: true,
    64        });
    65  
    66        this.render(hbs`
    67          {{allocation-row
    68            allocation=allocation
    69            context=context
    70            backoffSequence=backoffSequence
    71            enablePolling=enablePolling}}
    72        `);
    73        return wait();
    74      })
    75      .then(() => {
    76        assert.equal(
    77          this.server.pretender.handledRequests.filterBy(
    78            'url',
    79            `/v1/client/allocation/${allocation.get('id')}/stats`
    80          ).length,
    81          frames.length,
    82          'Requests continue to be made after malformed responses and server errors'
    83        );
    84      });
    85  });