-
Notifications
You must be signed in to change notification settings - Fork 13
/
op-test-harness.ts
71 lines (62 loc) · 2.03 KB
/
op-test-harness.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import OpHarness from '@terascope/teraslice-op-test-harness';
import { TestClientConfig, CoreOperation } from '@terascope/job-components';
import { OpTestHarnessOptions } from './interfaces';
/**
* A simple test harness for running an single operation
* with minimal customizations. Based of the older
* teraslice-op-test-harness package.
*
* This is useful for testing Data in -> out on a Fetcher,
* Reader, or Processor.
*/
export default class OpTestHarness {
harness: OpHarness.TestHarness;
opTester: OpHarness.OperationTester|undefined;
constructor(op: OpHarness.OpTestHarnessInput, options?: OpTestHarnessOptions) {
this.harness = OpHarness(op);
if (options) {
this.harness.setClients(options.clients);
}
}
/**
* Set the Terafoundation Clients on both
* the Slicer and Worker contexts
*/
setClients(clients: TestClientConfig[]): void {
this.harness.setClients(clients);
}
/**
* Get the Operation from the op test harness
*/
get operation(): CoreOperation|null {
if (this.opTester == null) return null;
return this.opTester.operation;
}
/**
* Initialize the Operations on the ExecutionContext
*/
async initialize(options?: OpHarness.InitOptions): Promise<void> {
const initOptions: OpHarness.InitOptions = options != null ? options : {
opConfig: {
_op: 'test'
},
type: 'reader'
};
this.opTester = await this.harness.init(initOptions);
}
async run(input: OpHarness.RunInput): Promise<OpHarness.RunResult> {
if (this.opTester == null) {
throw new Error('Initialize must be called before run');
}
return this.opTester.run(input);
}
/**
* Shutdown the Operations on the ExecutionContext
*/
async shutdown(): Promise<void> {
if (this.opTester != null) {
await this.opTester.operation.shutdown();
}
this.harness.events.removeAllListeners();
}
}