Skip to content

Commit

Permalink
🐛 FIX Scheduler forgets to pass the metadata when calling report (#625)
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzzz authored Jun 13, 2020
1 parent cb29836 commit eee2bf8
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/check/arbitrary/AsyncSchedulerArbitrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class SchedulerImplem<TMetaData> implements Scheduler<TMetaData> {
schedulingType: 'promise' | 'function' | 'sequence',
taskId: number,
label: string,
metadata: TMetaData | undefined,
status: 'resolved' | 'rejected',
data: unknown
) {
Expand All @@ -164,6 +165,7 @@ class SchedulerImplem<TMetaData> implements Scheduler<TMetaData> {
schedulingType,
taskId,
label,
metadata,
outputValue: data !== undefined ? stringify(data) : undefined,
});
}
Expand All @@ -181,11 +183,11 @@ class SchedulerImplem<TMetaData> implements Scheduler<TMetaData> {
trigger = () => {
(thenTaskToBeAwaited ? task.then(() => thenTaskToBeAwaited()) : task).then(
(data) => {
this.log(schedulingType, taskId, label, 'resolved', data);
this.log(schedulingType, taskId, label, metadata, 'resolved', data);
return resolve(data);
},
(err) => {
this.log(schedulingType, taskId, label, 'rejected', err);
this.log(schedulingType, taskId, label, metadata, 'rejected', err);
return reject(err);
}
);
Expand All @@ -198,6 +200,7 @@ class SchedulerImplem<TMetaData> implements Scheduler<TMetaData> {
schedulingType,
taskId,
label,
metadata,
});
return scheduledPromise;
}
Expand Down Expand Up @@ -303,6 +306,7 @@ class SchedulerImplem<TMetaData> implements Scheduler<TMetaData> {
schedulingType: t.schedulingType,
taskId: t.taskId,
label: t.label,
metadata: t.metadata,
})
),
];
Expand Down
76 changes: 76 additions & 0 deletions test/unit/check/arbitrary/AsyncSchedulerArbitrary.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,39 @@ describe('AsyncSchedulerArbitrary', () => {
// Assert
expect(then1Function.mock.calls).toEqual(then2Function.mock.calls);
});

it('Should attach passed metadata into the report', async () => {
// Arrange
const expectedMetadata = Symbol('123');

// Act
const mrng = stubRng.mutable.counter(42);
const s = scheduler().generate(mrng).value;
s.schedule(Promise.resolve(), 'label', expectedMetadata);

// Assert
await s.waitAll();
const report = s.report();
expect(report).toHaveLength(1);
expect(report[0].status).toBe('resolved');
expect(report[0].metadata).toBe(expectedMetadata);
});

it('Should attach passed metadata into the report even if not executed', async () => {
// Arrange
const expectedMetadata = Symbol('123');

// Act
const mrng = stubRng.mutable.counter(42);
const s = scheduler().generate(mrng).value;
s.schedule(Promise.resolve(), 'label', expectedMetadata);

// Assert
const report = s.report();
expect(report).toHaveLength(1);
expect(report[0].status).toBe('pending');
expect(report[0].metadata).toBe(expectedMetadata);
});
});

describe('scheduleFunction', () => {
Expand Down Expand Up @@ -787,6 +820,49 @@ describe('AsyncSchedulerArbitrary', () => {
expect(taskResolvedValue).toEqual({ done: false, faulty: true });
});
});

it('Should attach passed metadata into the report', async () => {
// Arrange
const expectedMetadataFirst = Symbol('123');
const expectedMetadataSecond = Symbol('1234');

// Act
const mrng = stubRng.mutable.counter(42);
const s = scheduler().generate(mrng).value;
s.scheduleSequence([
{ builder: () => Promise.resolve(42), label: 'firstStep', metadata: expectedMetadataFirst },
{ builder: () => Promise.reject(8), label: 'secondStep', metadata: expectedMetadataSecond },
]);

// Assert
await s.waitAll();
const report = s.report();
expect(report).toHaveLength(2);
expect(report[0].status).toBe('resolved');
expect(report[0].metadata).toBe(expectedMetadataFirst);
expect(report[1].status).toBe('rejected');
expect(report[1].metadata).toBe(expectedMetadataSecond);
});

it('Should attach passed metadata into the report even if not executed', async () => {
// Arrange
const expectedMetadataFirst = Symbol('123');
const expectedMetadataSecond = Symbol('1234');

// Act
const mrng = stubRng.mutable.counter(42);
const s = scheduler().generate(mrng).value;
s.scheduleSequence([
{ builder: () => Promise.resolve(42), label: 'firstStep', metadata: expectedMetadataFirst },
{ builder: () => Promise.reject(8), label: 'secondStep', metadata: expectedMetadataSecond },
]);

// Assert
const report = s.report();
expect(report).toHaveLength(1); // second task cannot be scheduled as first one is still pending
expect(report[0].status).toBe('pending');
expect(report[0].metadata).toBe(expectedMetadataFirst);
});
});
describe('schedulerFor', () => {
it('Should execute tasks in the required order when using template string from error logs', async () => {
Expand Down

0 comments on commit eee2bf8

Please sign in to comment.